blob: db9bd0698a364166dc07c546cf593bebf5e07b24 [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 Moolenaar05159a02005-02-26 23:04:13 +0000397
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398/*
399 * Declare cmdnames[].
400 */
401#define DO_DECLARE_EXCMD
402#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200403#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405static char_u dollar_command[2] = {'$', 0};
406
407
408#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000409/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410typedef struct
411{
412 char_u *line; /* command line */
413 linenr_T lnum; /* sourcing_lnum of the line */
414} wcmd_T;
415
416/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000417 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000419 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000421struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422{
423 garray_T *lines_gap; /* growarray with line info */
424 int current_line; /* last read line from growarray */
425 int repeating; /* TRUE when looping a second time */
426 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100427 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 void *cookie;
429};
430
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100431static char_u *get_loop_line(int c, void *cookie, int indent);
432static int store_loop_line(garray_T *gap, char_u *line);
433static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000434
435/* Struct to save a few things while debugging. Used in do_cmdline() only. */
436struct dbg_stuff
437{
438 int trylevel;
439 int force_abort;
440 except_T *caught_stack;
441 char_u *vv_exception;
442 char_u *vv_throwpoint;
443 int did_emsg;
444 int got_int;
445 int did_throw;
446 int need_rethrow;
447 int check_cstack;
448 except_T *current_exception;
449};
450
Bram Moolenaared203462004-06-16 11:19:22 +0000451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100452save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000453{
454 dsp->trylevel = trylevel; trylevel = 0;
455 dsp->force_abort = force_abort; force_abort = FALSE;
456 dsp->caught_stack = caught_stack; caught_stack = NULL;
457 dsp->vv_exception = v_exception(NULL);
458 dsp->vv_throwpoint = v_throwpoint(NULL);
459
460 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
461 dsp->did_emsg = did_emsg; did_emsg = FALSE;
462 dsp->got_int = got_int; got_int = FALSE;
463 dsp->did_throw = did_throw; did_throw = FALSE;
464 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
465 dsp->check_cstack = check_cstack; check_cstack = FALSE;
466 dsp->current_exception = current_exception; current_exception = NULL;
467}
468
469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100470restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000471{
472 suppress_errthrow = FALSE;
473 trylevel = dsp->trylevel;
474 force_abort = dsp->force_abort;
475 caught_stack = dsp->caught_stack;
476 (void)v_exception(dsp->vv_exception);
477 (void)v_throwpoint(dsp->vv_throwpoint);
478 did_emsg = dsp->did_emsg;
479 got_int = dsp->got_int;
480 did_throw = dsp->did_throw;
481 need_rethrow = dsp->need_rethrow;
482 check_cstack = dsp->check_cstack;
483 current_exception = dsp->current_exception;
484}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485#endif
486
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487/*
488 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
489 * command is given.
490 */
491 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100492do_exmode(
493 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 int save_msg_scroll;
496 int prev_msg_row;
497 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100498 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000499
500 if (improved)
501 exmode_active = EXMODE_VIM;
502 else
503 exmode_active = EXMODE_NORMAL;
504 State = NORMAL;
505
506 /* When using ":global /pat/ visual" and then "Q" we return to continue
507 * the :global command. */
508 if (global_busy)
509 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510
511 save_msg_scroll = msg_scroll;
512 ++RedrawingDisabled; /* don't redisplay the window */
513 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514#ifdef FEAT_GUI
515 /* Ignore scrollbar and mouse events in Ex mode */
516 ++hold_gui_events;
517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518
Bram Moolenaar32526b32019-01-19 17:43:09 +0100519 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 while (exmode_active)
521 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000522 /* Check for a ":normal" command and no more characters left. */
523 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
524 {
525 exmode_active = FALSE;
526 break;
527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 msg_scroll = TRUE;
529 need_wait_return = FALSE;
530 ex_pressedreturn = FALSE;
531 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100532 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 prev_msg_row = msg_row;
534 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (improved)
536 {
537 cmdline_row = msg_row;
538 do_cmdline(NULL, getexline, NULL, 0);
539 }
540 else
541 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
542 lines_left = Rows - 1;
543
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100545 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 if (ex_pressedreturn)
552 {
553 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000554 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000555 msg_row = prev_msg_row;
556 if (prev_msg_row == Rows - 1)
557 msg_row--;
558 }
559 msg_col = 0;
560 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
561 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000564 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
565 {
566 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100567 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000568 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100569 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 }
572
573#ifdef FEAT_GUI
574 --hold_gui_events;
575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 --RedrawingDisabled;
577 --no_wait_return;
578 update_screen(CLEAR);
579 need_wait_return = FALSE;
580 msg_scroll = save_msg_scroll;
581}
582
583/*
584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
615 char_u *(*fgetline)(int, void *, int),
616 void *cookie, /* argument for fgetline() */
617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
619 char_u *next_cmdline; /* next cmd to execute */
620 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100621 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 static int recursive = 0; /* recursive depth */
623 int msg_didout_before_start = 0;
624 int count = 0; /* line number count */
625 int did_inc = FALSE; /* incremented RedrawingDisabled */
626 int retval = OK;
627#ifdef FEAT_EVAL
628 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000629 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 int current_line = 0; /* active line in lines_ga */
631 char_u *fname = NULL; /* function or script name */
632 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
633 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000634 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 int initial_trylevel;
636 struct msglist **saved_msg_list = NULL;
637 struct msglist *private_msg_list;
638
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100639 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100640 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000642 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000644 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100646# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647# define cmd_cookie cookie
648#endif
649 static int call_depth = 0; /* recursiveness */
650
651#ifdef FEAT_EVAL
652 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
653 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000654 * This ensures that the do_errthrow() call in do_one_cmd() does not
655 * combine the messages stored by an earlier invocation of do_one_cmd()
656 * with the command name of the later one. This would happen when
657 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 saved_msg_list = msg_list;
659 msg_list = &private_msg_list;
660 private_msg_list = NULL;
661#endif
662
663 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100664 * here. The value of 200 allows nested function calls, ":source", etc.
665 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100666 if (call_depth >= 200
667#ifdef FEAT_EVAL
668 && call_depth >= p_mfd
669#endif
670 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100672 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673#ifdef FEAT_EVAL
674 /* When converting to an exception, we do not include the command name
675 * since this is not an error of the specific command. */
676 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
677 msg_list = saved_msg_list;
678#endif
679 return FAIL;
680 }
681 ++call_depth;
682
683#ifdef FEAT_EVAL
684 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000685 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 cstack.cs_trylevel = 0;
687 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000688 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
690
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000695 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 ++ex_nesting_level;
697
698 /* Get the function or script name and the address where the next breakpoint
699 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 {
702 fname = func_name(real_cookie);
703 breakpoint = func_breakpoint(real_cookie);
704 dbg_tick = func_dbg_tick(real_cookie);
705 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100706 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 fname = sourcing_name;
709 breakpoint = source_breakpoint(real_cookie);
710 dbg_tick = source_dbg_tick(real_cookie);
711 }
712
713 /*
714 * Initialize "force_abort" and "suppress_errthrow" at the top level.
715 */
716 if (!recursive)
717 {
718 force_abort = FALSE;
719 suppress_errthrow = FALSE;
720 }
721
722 /*
723 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000724 * exception handling (used when debugging). Otherwise clear it to avoid
725 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000727 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000728 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000729 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200730 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
732 initial_trylevel = trylevel;
733
734 /*
735 * "did_throw" will be set to TRUE when an exception is being thrown.
736 */
737 did_throw = FALSE;
738#endif
739 /*
740 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000741 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 * If force_abort is set, we cancel everything.
743 */
744 did_emsg = FALSE;
745
746 /*
747 * KeyTyped is only set when calling vgetc(). Reset it here when not
748 * calling vgetc() (sourced command lines).
749 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100750 if (!(flags & DOCMD_KEYTYPED)
751 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 KeyTyped = FALSE;
753
754 /*
755 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000756 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 * - for multiple commands on one line, separated with '|'
758 * - when repeating until there are no more lines (for ":source")
759 */
760 next_cmdline = cmdline;
761 do
762 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000763#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100764 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765#endif
766
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000767 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (next_cmdline == NULL
769#ifdef FEAT_EVAL
770 && !force_abort
771 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#endif
774 )
775 did_emsg = FALSE;
776
777 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000778 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100779 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 * 3. If a line is given: Make a copy, so we can mess with it.
781 */
782
783#ifdef FEAT_EVAL
784 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
787 /* Each '|' separated command is stored separately in lines_ga, to
788 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100789 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
791 /* Check if a function has returned or, unless it has an unclosed
792 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000793 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000796 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 func_line_end(real_cookie);
798# endif
799 if (func_has_ended(real_cookie))
800 {
801 retval = FAIL;
802 break;
803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000806 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100807 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808 script_line_end();
809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 {
814 retval = FAIL;
815 break;
816 }
817
818 /* If breakpoints have been added/deleted need to check for it. */
819 if (breakpoint != NULL && dbg_tick != NULL
820 && *dbg_tick != debug_tick)
821 {
822 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100823 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 fname, sourcing_lnum);
825 *dbg_tick = debug_tick;
826 }
827
828 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
829 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
830
831 /* Did we encounter a breakpoint? */
832 if (breakpoint != NULL && *breakpoint != 0
833 && *breakpoint <= sourcing_lnum)
834 {
835 dbg_breakpoint(fname, sourcing_lnum);
836 /* Find next breakpoint. */
837 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100838 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 fname, sourcing_lnum);
840 *dbg_tick = debug_tick;
841 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000843 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844 {
845 if (getline_is_func)
846 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100847 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848 script_line_start();
849 }
850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000853 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000855 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100856 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 * below, so that it stores lines in or reads them from
858 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000859 * while/for loop. */
860 cmd_getline = get_loop_line;
861 cmd_cookie = (void *)&cmd_loop_cookie;
862 cmd_loop_cookie.lines_gap = &lines_ga;
863 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100864 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000865 cmd_loop_cookie.cookie = cookie;
866 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 }
868 else
869 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100870 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 cmd_cookie = cookie;
872 }
873#endif
874
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 if (next_cmdline == NULL)
877 {
878 /*
879 * Need to set msg_didout for the first line after an ":if",
880 * otherwise the ":if" will be overwritten.
881 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885#ifdef FEAT_EVAL
886 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
887#else
888 0
889#endif
890 )) == NULL)
891 {
892 /* Don't call wait_return for aborted command line. The NULL
893 * returned for the end of a sourced file or executed function
894 * doesn't do this. */
895 if (KeyTyped && !(flags & DOCMD_REPEAT))
896 need_wait_return = FALSE;
897 retval = FAIL;
898 break;
899 }
900 used_getline = TRUE;
901
902 /*
903 * Keep the first typed line. Clear it when more lines are typed.
904 */
905 if (flags & DOCMD_KEEPLINE)
906 {
907 vim_free(repeat_cmdline);
908 if (count == 0)
909 repeat_cmdline = vim_strsave(next_cmdline);
910 else
911 repeat_cmdline = NULL;
912 }
913 }
914
915 /* 3. Make a copy of the command so we can mess with it. */
916 else if (cmdline_copy == NULL)
917 {
918 next_cmdline = vim_strsave(next_cmdline);
919 if (next_cmdline == NULL)
920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100921 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 retval = FAIL;
923 break;
924 }
925 }
926 cmdline_copy = next_cmdline;
927
928#ifdef FEAT_EVAL
929 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000930 * Save the current line when inside a ":while" or ":for", and when
931 * the command looks like a ":while" or ":for", because we may need it
932 * later. When there is a '|' and another command, it is stored
933 * separately, because we need to be able to jump back to it from an
934 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 if (current_line == lines_ga.ga_len
937 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000939 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
941 retval = FAIL;
942 break;
943 }
944 }
945 did_endif = FALSE;
946#endif
947
948 if (count++ == 0)
949 {
950 /*
951 * All output from the commands is put below each other, without
952 * waiting for a return. Don't do this when executing commands
953 * from a script or when being called recursive (e.g. for ":e
954 * +command file").
955 */
956 if (!(flags & DOCMD_NOWAIT) && !recursive)
957 {
958 msg_didout_before_start = msg_didout;
959 msg_didany = FALSE; /* no output yet */
960 msg_start();
961 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200962 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 ++RedrawingDisabled;
964 did_inc = TRUE;
965 }
966 }
967
968 if (p_verbose >= 15 && sourcing_name != NULL)
969 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000971 verbose_enter_scroll();
972
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100973 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000975 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100976 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000977
978 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 --no_wait_return;
980 }
981
982 /*
983 * 2. Execute one '|' separated command.
984 * do_one_cmd() will return NULL if there is no trailing '|'.
985 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
986 */
987 ++recursive;
988 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
989#ifdef FEAT_EVAL
990 &cstack,
991#endif
992 cmd_getline, cmd_cookie);
993 --recursive;
994
995#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000996 if (cmd_cookie == (void *)&cmd_loop_cookie)
997 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000#endif
1001
1002 if (next_cmdline == NULL)
1003 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001004 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#ifdef FEAT_CMDHIST
1006 /*
1007 * If the command was typed, remember it for the ':' register.
1008 * Do this AFTER executing the command to make :@: work.
1009 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001010 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 && new_last_cmdline != NULL)
1012 {
1013 vim_free(last_cmdline);
1014 last_cmdline = new_last_cmdline;
1015 new_last_cmdline = NULL;
1016 }
1017#endif
1018 }
1019 else
1020 {
1021 /* need to copy the command after the '|' to cmdline_copy, for the
1022 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001023 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 next_cmdline = cmdline_copy;
1025 }
1026
1027
1028#ifdef FEAT_EVAL
1029 /* reset did_emsg for a function that is not aborted by an error */
1030 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001031 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 && !func_has_abort(real_cookie))
1033 did_emsg = FALSE;
1034
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001035 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 {
1037 ++current_line;
1038
1039 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 * An ":endwhile", ":endfor" and ":continue" is handled here.
1041 * If we were executing commands, jump back to the ":while" or
1042 * ":for".
1043 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 /* Jump back to the matching ":while" or ":for". Be careful
1050 * not to use a cs_line[] from an entry that isn't a ":while"
1051 * or ":for": It would make "current_line" invalid and can
1052 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if (!did_emsg && !got_int && !did_throw
1054 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 && (cstack.cs_flags[cstack.cs_idx]
1056 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 && cstack.cs_line[cstack.cs_idx] >= 0
1058 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1059 {
1060 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 /* remember we jumped there */
1062 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 line_breakcheck(); /* check if CTRL-C typed */
1064
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 /* Check for the next breakpoint at or after the ":while"
1066 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (breakpoint != NULL)
1068 {
1069 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001070 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 fname,
1072 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1073 *dbg_tick = debug_tick;
1074 }
1075 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001080 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1081 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083 }
1084
1085 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001086 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001088 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1092 }
1093 }
1094
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001095 /* Check for the next breakpoint after a watchexpression */
1096 if (breakpoint != NULL && has_watchexpr())
1097 {
1098 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1099 *dbg_tick = debug_tick;
1100 }
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 /*
1103 * When not inside any ":while" loop, clear remembered lines.
1104 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 {
1107 if (lines_ga.ga_len > 0)
1108 {
1109 sourcing_lnum =
1110 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1111 free_cmdlines(&lines_ga);
1112 }
1113 current_line = 0;
1114 }
1115
1116 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1118 * being restored at the ":endtry". Reset them here and set the
1119 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1120 * This includes the case where a missing ":endif", ":endwhile" or
1121 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001125 cstack.cs_lflags &= ~CSL_HAD_FINA;
1126 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1127 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 did_throw ? (void *)current_exception : NULL);
1129 did_emsg = got_int = did_throw = FALSE;
1130 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1131 }
1132
1133 /* Update global "trylevel" for recursive calls to do_cmdline() from
1134 * within this loop. */
1135 trylevel = initial_trylevel + cstack.cs_trylevel;
1136
1137 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001138 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 * files) is aborted because of an error, an interrupt, or an uncaught
1140 * exception, cancel everything. If it is left normally, reset
1141 * force_abort to get the non-EH compatible abortion behavior for
1142 * the rest of the script.
1143 */
1144 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1145 force_abort = FALSE;
1146
1147 /* Convert an interrupt to an exception if appropriate. */
1148 (void)do_intthrow(&cstack);
1149#endif /* FEAT_EVAL */
1150
1151 }
1152 /*
1153 * Continue executing command lines when:
1154 * - no CTRL-C typed, no aborting error, no exception thrown or try
1155 * conditionals need to be checked for executing finally clauses or
1156 * catching an interrupt exception
1157 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001158 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 * looping for ":source" command or function call.
1160 */
1161 while (!((got_int
1162#ifdef FEAT_EVAL
1163 || (did_emsg && force_abort) || did_throw
1164#endif
1165 )
1166#ifdef FEAT_EVAL
1167 && cstack.cs_trylevel == 0
1168#endif
1169 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001170 && !(did_emsg
1171#ifdef FEAT_EVAL
1172 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001173 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001174 * the :endtry to be missed. */
1175 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1176#endif
1177 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001178 && (getline_equal(fgetline, cookie, getexmodeline)
1179 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 && (next_cmdline != NULL
1181#ifdef FEAT_EVAL
1182 || cstack.cs_idx >= 0
1183#endif
1184 || (flags & DOCMD_REPEAT)));
1185
1186 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001187 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188#ifdef FEAT_EVAL
1189 free_cmdlines(&lines_ga);
1190 ga_clear(&lines_ga);
1191
1192 if (cstack.cs_idx >= 0)
1193 {
1194 /*
1195 * If a sourced file or executed function ran to its end, report the
1196 * unclosed conditional.
1197 */
1198 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001199 && ((getline_equal(fgetline, cookie, getsourceline)
1200 && !source_finished(fgetline, cookie))
1201 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 && !func_has_ended(real_cookie))))
1203 {
1204 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001207 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001208 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001209 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213
1214 /*
1215 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1216 * ":endtry" in a sourced file or executed function. If the try
1217 * conditional is in its finally clause, ignore anything pending.
1218 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
1221 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001222 {
1223 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1224
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001225 if (idx >= 0)
1226 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001227 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1228 &cstack.cs_looplevel);
1229 }
1230 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 trylevel = initial_trylevel;
1232 }
1233
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001234 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1235 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001237 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 ? (char_u *)"endfunction" : (char_u *)NULL);
1239
1240 if (trylevel == 0)
1241 {
1242 /*
1243 * When an exception is being thrown out of the outermost try
1244 * conditional, discard the uncaught exception, disable the conversion
1245 * of interrupts or errors to exceptions, and ensure that no more
1246 * commands are executed.
1247 */
1248 if (did_throw)
1249 {
1250 void *p = NULL;
1251 char_u *saved_sourcing_name;
1252 int saved_sourcing_lnum;
1253 struct msglist *messages = NULL, *next;
1254
1255 /*
1256 * If the uncaught exception is a user exception, report it as an
1257 * error. If it is an error exception, display the saved error
1258 * message now. For an interrupt exception, do nothing; the
1259 * interrupt message is given elsewhere.
1260 */
1261 switch (current_exception->type)
1262 {
1263 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001264 vim_snprintf((char *)IObuff, IOSIZE,
1265 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 current_exception->value);
1267 p = vim_strsave(IObuff);
1268 break;
1269 case ET_ERROR:
1270 messages = current_exception->messages;
1271 current_exception->messages = NULL;
1272 break;
1273 case ET_INTERRUPT:
1274 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 }
1276
1277 saved_sourcing_name = sourcing_name;
1278 saved_sourcing_lnum = sourcing_lnum;
1279 sourcing_name = current_exception->throw_name;
1280 sourcing_lnum = current_exception->throw_lnum;
1281 current_exception->throw_name = NULL;
1282
1283 discard_current_exception(); /* uses IObuff if 'verbose' */
1284 suppress_errthrow = TRUE;
1285 force_abort = TRUE;
1286
1287 if (messages != NULL)
1288 {
1289 do
1290 {
1291 next = messages->next;
1292 emsg(messages->msg);
1293 vim_free(messages->msg);
1294 vim_free(messages);
1295 messages = next;
1296 }
1297 while (messages != NULL);
1298 }
1299 else if (p != NULL)
1300 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001301 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 vim_free(p);
1303 }
1304 vim_free(sourcing_name);
1305 sourcing_name = saved_sourcing_name;
1306 sourcing_lnum = saved_sourcing_lnum;
1307 }
1308
1309 /*
1310 * On an interrupt or an aborting error not converted to an exception,
1311 * disable the conversion of errors to exceptions. (Interrupts are not
1312 * converted any more, here.) This enables also the interrupt message
1313 * when force_abort is set and did_emsg unset in case of an interrupt
1314 * from a finally clause after an error.
1315 */
1316 else if (got_int || (did_emsg && force_abort))
1317 suppress_errthrow = TRUE;
1318 }
1319
1320 /*
1321 * The current cstack will be freed when do_cmdline() returns. An uncaught
1322 * exception will have to be rethrown in the previous cstack. If a function
1323 * has just returned or a script file was just finished and the previous
1324 * cstack belongs to the same function or, respectively, script file, it
1325 * will have to be checked for finally clauses to be executed due to the
1326 * ":return" or ":finish". This is done in do_one_cmd().
1327 */
1328 if (did_throw)
1329 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 && ex_nesting_level > func_level(real_cookie) + 1))
1334 {
1335 if (!did_throw)
1336 check_cstack = TRUE;
1337 }
1338 else
1339 {
1340 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001341 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 --ex_nesting_level;
1343 /*
1344 * Go to debug mode when returning from a function in which we are
1345 * single-stepping.
1346 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001347 if ((getline_equal(fgetline, cookie, getsourceline)
1348 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001350 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 ? (char_u *)_("End of sourced file")
1352 : (char_u *)_("End of function"));
1353 }
1354
1355 /*
1356 * Restore the exception environment (done after returning from the
1357 * debugger).
1358 */
1359 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001360 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
1362 msg_list = saved_msg_list;
1363#endif /* FEAT_EVAL */
1364
1365 /*
1366 * If there was too much output to fit on the command line, ask the user to
1367 * hit return before redrawing the screen. With the ":global" command we do
1368 * this only once after the command is finished.
1369 */
1370 if (did_inc)
1371 {
1372 --RedrawingDisabled;
1373 --no_wait_return;
1374 msg_scroll = FALSE;
1375
1376 /*
1377 * When just finished an ":if"-":else" which was typed, no need to
1378 * wait for hit-return. Also for an error situation.
1379 */
1380 if (retval == FAIL
1381#ifdef FEAT_EVAL
1382 || (did_endif && KeyTyped && !did_emsg)
1383#endif
1384 )
1385 {
1386 need_wait_return = FALSE;
1387 msg_didany = FALSE; /* don't wait when restarting edit */
1388 }
1389 else if (need_wait_return)
1390 {
1391 /*
1392 * The msg_start() above clears msg_didout. The wait_return we do
1393 * here should not overwrite the command that may be shown before
1394 * doing that.
1395 */
1396 msg_didout |= msg_didout_before_start;
1397 wait_return(FALSE);
1398 }
1399 }
1400
Bram Moolenaar2a942252012-11-28 23:03:07 +01001401#ifdef FEAT_EVAL
1402 did_endif = FALSE; /* in case do_cmdline used recursively */
1403#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /*
1405 * Reset if_level, in case a sourced script file contains more ":if" than
1406 * ":endif" (could be ":if x | foo | endif").
1407 */
1408 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001409#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 --call_depth;
1412 return retval;
1413}
1414
1415#ifdef FEAT_EVAL
1416/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001417 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
1419 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001420get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001422 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 wcmd_T *wp;
1424 char_u *line;
1425
1426 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1427 {
1428 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001429 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001431 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (cp->getline == NULL)
1433 line = getcmdline(c, 0L, indent);
1434 else
1435 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001436 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 ++cp->current_line;
1438
1439 return line;
1440 }
1441
1442 KeyTyped = FALSE;
1443 ++cp->current_line;
1444 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1445 sourcing_lnum = wp->lnum;
1446 return vim_strsave(wp->line);
1447}
1448
1449/*
1450 * Store a line in "gap" so that a ":while" loop can execute it again.
1451 */
1452 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001453store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 if (ga_grow(gap, 1) == FAIL)
1456 return FAIL;
1457 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1458 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1459 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 return OK;
1461}
1462
1463/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001464 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 */
1466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001467free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 while (gap->ga_len > 0)
1470 {
1471 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1472 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474}
1475#endif
1476
1477/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001478 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1479 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001482getline_equal(
1483 char_u *(*fgetline)(int, void *, int),
1484 void *cookie UNUSED, /* argument for fgetline() */
1485 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001488 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001489 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar89d40322006-08-29 15:30:07 +00001491 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001492 * function that's originally used to obtain the lines. This may be
1493 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 cp = (struct loop_cookie *)cookie;
1496 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 gp = cp->getline;
1499 cp = cp->cookie;
1500 }
1501 return gp == func;
1502#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001503 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
1505}
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001508 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 * getline function. Otherwise return "cookie".
1510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512getline_cookie(
1513 char_u *(*fgetline)(int, void *, int) UNUSED,
1514 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar13505972019-01-24 15:04:48 +01001516#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001517 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar89d40322006-08-29 15:30:07 +00001520 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001521 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001523 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 cp = (struct loop_cookie *)cookie;
1525 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
1527 gp = cp->getline;
1528 cp = cp->cookie;
1529 }
1530 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001531#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001536
1537/*
1538 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1539 * ":bwipeout", etc.
1540 * Returns the buffer number.
1541 */
1542 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001543compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001544{
1545 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001546 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001547 int count = offset;
1548
1549 buf = firstbuf;
1550 while (buf->b_next != NULL && buf->b_fnum < lnum)
1551 buf = buf->b_next;
1552 while (count != 0)
1553 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001554 count += (offset < 0) ? 1 : -1;
1555 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1556 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001557 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001558 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001559 if (addr_type == ADDR_LOADED_BUFFERS)
1560 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001561 while (buf->b_ml.ml_mfp == NULL)
1562 {
1563 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1564 if (nextbuf == NULL)
1565 break;
1566 buf = nextbuf;
1567 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001568 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001569 /* we might have gone too far, last buffer is not loadedd */
1570 if (addr_type == ADDR_LOADED_BUFFERS)
1571 while (buf->b_ml.ml_mfp == NULL)
1572 {
1573 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1574 if (nextbuf == NULL)
1575 break;
1576 buf = nextbuf;
1577 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001578 return buf->b_fnum;
1579}
1580
Bram Moolenaarb7316892019-05-01 18:08:42 +02001581/*
1582 * Return the window number of "win".
1583 * When "win" is NULL return the number of windows.
1584 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001586current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001587{
1588 win_T *wp;
1589 int nr = 0;
1590
Bram Moolenaar29323592016-07-24 22:04:11 +02001591 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001592 {
1593 ++nr;
1594 if (wp == win)
1595 break;
1596 }
1597 return nr;
1598}
1599
1600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001601current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001602{
1603 tabpage_T *tp;
1604 int nr = 0;
1605
Bram Moolenaar29323592016-07-24 22:04:11 +02001606 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001607 {
1608 ++nr;
1609 if (tp == tab)
1610 break;
1611 }
1612 return nr;
1613}
1614
1615# define CURRENT_WIN_NR current_win_nr(curwin)
1616# define LAST_WIN_NR current_win_nr(NULL)
1617# define CURRENT_TAB_NR current_tab_nr(curtab)
1618# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
1621 * Execute one Ex command.
1622 *
1623 * If 'sourcing' is TRUE, the command will be included in the error message.
1624 *
1625 * 1. skip comment lines and leading space
1626 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001627 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001629 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630 * 6. parse arguments
1631 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001633 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 *
1635 * This function may be called recursively!
1636 */
1637#if (_MSC_VER == 1200)
1638/*
Bram Moolenaared203462004-06-16 11:19:22 +00001639 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001641 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001644do_one_cmd(
1645 char_u **cmdlinep,
1646 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001648 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001650 char_u *(*fgetline)(int, void *, int),
1651 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
1653 char_u *p;
1654 linenr_T lnum;
1655 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001656 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001657 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001659 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001661 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001663 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 vim_memset(&ea, 0, sizeof(ea));
1666 ea.line1 = 1;
1667 ea.line2 = 1;
1668#ifdef FEAT_EVAL
1669 ++ex_nesting_level;
1670#endif
1671
Bram Moolenaar21691f82012-12-05 19:13:18 +01001672 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 if (quitmore
1674#ifdef FEAT_EVAL
1675 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001676 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001677#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001678 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001679 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 --quitmore;
1681
1682 /*
1683 * Reset browse, confirm, etc.. They are restored when returning, for
1684 * recursive calls.
1685 */
1686 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001688 /* "#!anything" is handled like a comment. */
1689 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1690 goto doend;
1691
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001692/*
1693 * 1. Skip comment lines and leading white space and colons.
1694 * 2. Handle command modifiers.
1695 */
1696 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001698 ea.cmdlinep = cmdlinep;
1699 ea.getline = fgetline;
1700 ea.cookie = cookie;
1701#ifdef FEAT_EVAL
1702 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001704 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001705 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001707 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708
1709#ifdef FEAT_EVAL
1710 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1711 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1712#else
1713 ea.skip = (if_level > 0);
1714#endif
1715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001717 * 3. Skip over the range to find the command. Let "p" point to after it.
1718 *
1719 * We need the command to know what kind of range it uses.
1720 */
1721 cmd = ea.cmd;
1722 ea.cmd = skip_range(ea.cmd, NULL);
1723 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1724 ea.cmd = skipwhite(ea.cmd + 1);
1725 p = find_command(&ea, NULL);
1726
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001727#ifdef FEAT_EVAL
1728# ifdef FEAT_PROFILE
1729 // Count this line for profiling if skip is TRUE.
1730 if (do_profiling == PROF_YES
1731 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1732 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1733 {
1734 int skip = did_emsg || got_int || did_throw;
1735
1736 if (ea.cmdidx == CMD_catch)
1737 skip = !skip && !(cstack->cs_idx >= 0
1738 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1739 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1740 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1741 skip = skip || !(cstack->cs_idx >= 0
1742 && !(cstack->cs_flags[cstack->cs_idx]
1743 & (CSF_ACTIVE | CSF_TRUE)));
1744 else if (ea.cmdidx == CMD_finally)
1745 skip = FALSE;
1746 else if (ea.cmdidx != CMD_endif
1747 && ea.cmdidx != CMD_endfor
1748 && ea.cmdidx != CMD_endtry
1749 && ea.cmdidx != CMD_endwhile)
1750 skip = ea.skip;
1751
1752 if (!skip)
1753 {
1754 if (getline_equal(fgetline, cookie, get_func_line))
1755 func_line_exec(getline_cookie(fgetline, cookie));
1756 else if (getline_equal(fgetline, cookie, getsourceline))
1757 script_line_exec();
1758 }
1759 }
1760# endif
1761
1762 /* May go to debug mode. If this happens and the ">quit" debug command is
1763 * used, throw an interrupt exception and skip the next command. */
1764 dbg_check_breakpoint(&ea);
1765 if (!ea.skip && got_int)
1766 {
1767 ea.skip = TRUE;
1768 (void)do_intthrow(cstack);
1769 }
1770#endif
1771
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001772/*
1773 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 *
1775 * where 'addr' is:
1776 *
1777 * % (entire file)
1778 * $ [+-NUM]
1779 * 'x [+-NUM] (where x denotes a currently defined mark)
1780 * . [+-NUM]
1781 * [+-NUM]..
1782 * NUM
1783 *
1784 * The ea.cmd pointer is updated to point to the first character following the
1785 * range spec. If an initial address is found, but no second, the upper bound
1786 * is equal to the lower.
1787 */
1788
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001789 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001790 if (!IS_USER_CMDIDX(ea.cmdidx))
1791 {
1792 if (ea.cmdidx != CMD_SIZE)
1793 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1794 else
1795 ea.addr_type = ADDR_LINES;
1796
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001797 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001798 if (ea.cmdidx == CMD_wincmd && p != NULL)
1799 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001800 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001802 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001803 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001804 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001807 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 */
1809
1810 /*
1811 * Skip ':' and any white space
1812 */
1813 ea.cmd = skipwhite(ea.cmd);
1814 while (*ea.cmd == ':')
1815 ea.cmd = skipwhite(ea.cmd + 1);
1816
1817 /*
1818 * If we got a line, but no command, then go to the line.
1819 * If we find a '|' or '\n' we set ea.nextcmd.
1820 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001821 if (*ea.cmd == NUL || *ea.cmd == '"'
1822 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 /*
1825 * strange vi behaviour:
1826 * ":3" jumps to line 3
1827 * ":3|..." prints line 3
1828 * ":|" prints current line
1829 */
1830 if (ea.skip) /* skip this if inside :if */
1831 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001832 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
1834 ea.cmdidx = CMD_print;
1835 ea.argt = RANGE+COUNT+TRLBAR;
1836 if ((errormsg = invalid_range(&ea)) == NULL)
1837 {
1838 correct_range(&ea);
1839 ex_print(&ea);
1840 }
1841 }
1842 else if (ea.addr_count != 0)
1843 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001844 if (ea.line2 > curbuf->b_ml.ml_line_count)
1845 {
1846 /* With '-' in 'cpoptions' a line number past the file is an
1847 * error, otherwise put it at the end of the file. */
1848 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1849 ea.line2 = -1;
1850 else
1851 ea.line2 = curbuf->b_ml.ml_line_count;
1852 }
1853
1854 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001855 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857 {
1858 if (ea.line2 == 0)
1859 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 else
1861 curwin->w_cursor.lnum = ea.line2;
1862 beginline(BL_SOL | BL_FIX);
1863 }
1864 }
1865 goto doend;
1866 }
1867
Bram Moolenaard5005162014-08-22 23:05:54 +02001868 /* If this looks like an undefined user command and there are CmdUndefined
1869 * autocommands defined, trigger the matching autocommands. */
1870 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1871 && ASCII_ISUPPER(*ea.cmd)
1872 && has_cmdundefined())
1873 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001874 int ret;
1875
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001876 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001877 while (ASCII_ISALNUM(*p))
1878 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001879 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001880 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1881 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001882 /* If the autocommands did something and didn't cause an error, try
1883 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001884 p = (ret
1885#ifdef FEAT_EVAL
1886 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001887#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001888 ) ? find_command(&ea, NULL) : ea.cmd;
1889 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (p == NULL)
1892 {
1893 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001894 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 goto doend;
1896 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001897 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001898 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1899 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 errormsg = uc_fun_cmd();
1902 goto doend;
1903 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (ea.cmdidx == CMD_SIZE)
1906 {
1907 if (!ea.skip)
1908 {
1909 STRCPY(IObuff, _("E492: Not an editor command"));
1910 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001911 {
1912 /* If the modifier was parsed OK the error must be in the
1913 * following command */
1914 if (after_modifier != NULL)
1915 append_command(after_modifier);
1916 else
1917 append_command(*cmdlinep);
1918 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001919 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001920 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
1922 goto doend;
1923 }
1924
Bram Moolenaar958636c2014-10-21 20:01:58 +02001925 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1926 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001927#ifdef HAVE_EX_SCRIPT_NI
1928 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1929#endif
1930 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
1932#ifndef FEAT_EVAL
1933 /*
1934 * When the expression evaluation is disabled, recognize the ":if" and
1935 * ":endif" commands and ignore everything in between it.
1936 */
1937 if (ea.cmdidx == CMD_if)
1938 ++if_level;
1939 if (if_level)
1940 {
1941 if (ea.cmdidx == CMD_endif)
1942 --if_level;
1943 goto doend;
1944 }
1945
1946#endif
1947
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001948 /* forced commands */
1949 if (*p == '!' && ea.cmdidx != CMD_substitute
1950 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952 ++p;
1953 ea.forceit = TRUE;
1954 }
1955 else
1956 ea.forceit = FALSE;
1957
1958/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001959 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001961 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001962 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 if (!ea.skip)
1965 {
1966#ifdef HAVE_SANDBOX
1967 if (sandbox != 0 && !(ea.argt & SBOXOK))
1968 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001969 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001970 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 goto doend;
1972 }
1973#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001974 if (restricted != 0 && (ea.argt & RESTRICT))
1975 {
1976 errormsg = _("E981: Command not allowed in rvim");
1977 goto doend;
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
1980 {
1981 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 goto doend;
1984 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001985
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001986 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001987 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001989 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001990 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 goto doend;
1992 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001993
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001994 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001995 * Do allow ":checktime" (it is postponed).
1996 * Do allow ":edit" (check for an argument later).
1997 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001998 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001999 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002000 && ea.cmdidx != CMD_edit
2001 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002002 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002003 && curbuf_locked())
2004 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
2006 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2007 {
2008 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002009 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 goto doend;
2011 }
2012 }
2013
2014 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
2019
2020 /*
2021 * Don't complain about the range if it is not used
2022 * (could happen if line_count is accidentally set to 0).
2023 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002024 if (!ea.skip && !ni && (ea.argt & RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 /*
2027 * If the range is backwards, ask for confirmation and, if given, swap
2028 * ea.line1 & ea.line2 so it's forwards again.
2029 * When global command is busy, don't ask, will fail below.
2030 */
2031 if (!global_busy && ea.line1 > ea.line2)
2032 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002033 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002035 if (sourcing || exmode_active)
2036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002037 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002038 goto doend;
2039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (ask_yesno((char_u *)
2041 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002042 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044 lnum = ea.line1;
2045 ea.line1 = ea.line2;
2046 ea.line2 = lnum;
2047 }
2048 if ((errormsg = invalid_range(&ea)) != NULL)
2049 goto doend;
2050 }
2051
Bram Moolenaarb7316892019-05-01 18:08:42 +02002052 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2053 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 ea.line2 = 1;
2055
2056 correct_range(&ea);
2057
2058#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002059 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2060 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
2062 /* Put the first line at the start of a closed fold, put the last line
2063 * at the end of a closed fold. */
2064 (void)hasFolding(ea.line1, &ea.line1, NULL);
2065 (void)hasFolding(ea.line2, NULL, &ea.line2);
2066 }
2067#endif
2068
2069#ifdef FEAT_QUICKFIX
2070 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002071 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 * option here, so things like % get expanded.
2073 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002074 p = replace_makeprg(&ea, p, cmdlinep);
2075 if (p == NULL)
2076 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#endif
2078
2079 /*
2080 * Skip to start of argument.
2081 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2082 */
2083 if (ea.cmdidx == CMD_bang)
2084 ea.arg = p;
2085 else
2086 ea.arg = skipwhite(p);
2087
Bram Moolenaar379fb762018-08-30 15:58:28 +02002088 // ":file" cannot be run with an argument when "curbuf_lock" is set
2089 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2090 goto doend;
2091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 /*
2093 * Check for "++opt=val" argument.
2094 * Must be first, allow ":w ++enc=utf8 !cmd"
2095 */
2096 if (ea.argt & ARGOPT)
2097 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2098 if (getargopt(&ea) == FAIL && !ni)
2099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 goto doend;
2102 }
2103
2104 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2105 {
2106 if (*ea.arg == '>') /* append */
2107 {
2108 if (*++ea.arg != '>') /* typed wrong */
2109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 goto doend;
2112 }
2113 ea.arg = skipwhite(ea.arg + 1);
2114 ea.append = TRUE;
2115 }
2116 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2117 {
2118 ++ea.arg;
2119 ea.usefilter = TRUE;
2120 }
2121 }
2122
2123 if (ea.cmdidx == CMD_read)
2124 {
2125 if (ea.forceit)
2126 {
2127 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2128 ea.forceit = FALSE;
2129 }
2130 else if (*ea.arg == '!') /* :r !filter */
2131 {
2132 ++ea.arg;
2133 ea.usefilter = TRUE;
2134 }
2135 }
2136
2137 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2138 {
2139 ea.amount = 1;
2140 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2141 {
2142 ++ea.arg;
2143 ++ea.amount;
2144 }
2145 ea.arg = skipwhite(ea.arg);
2146 }
2147
2148 /*
2149 * Check for "+command" argument, before checking for next command.
2150 * Don't do this for ":read !cmd" and ":write !cmd".
2151 */
2152 if ((ea.argt & EDITCMD) && !ea.usefilter)
2153 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2154
2155 /*
2156 * Check for '|' to separate commands and '"' to start comments.
2157 * Don't do this for ":read !cmd" and ":write !cmd".
2158 */
2159 if ((ea.argt & TRLBAR) && !ea.usefilter)
2160 separate_nextcmd(&ea);
2161
2162 /*
2163 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002164 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2165 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002167 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002168 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002169 || ea.cmdidx == CMD_global
2170 || ea.cmdidx == CMD_vglobal
2171 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
2173 for (p = ea.arg; *p; ++p)
2174 {
2175 /* Remove one backslash before a newline, so that it's possible to
2176 * pass a newline to the shell and also a newline that is preceded
2177 * with a backslash. This makes it impossible to end a shell
2178 * command in a backslash, but that doesn't appear useful.
2179 * Halving the number of backslashes is incompatible with previous
2180 * versions. */
2181 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002182 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 else if (*p == '\n')
2184 {
2185 ea.nextcmd = p + 1;
2186 *p = NUL;
2187 break;
2188 }
2189 }
2190 }
2191
2192 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2193 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002194 buf_T *buf;
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002197 switch (ea.addr_type)
2198 {
2199 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002200 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002201 ea.line2 = curbuf->b_ml.ml_line_count;
2202 break;
2203 case ADDR_LOADED_BUFFERS:
2204 buf = firstbuf;
2205 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2206 buf = buf->b_next;
2207 ea.line1 = buf->b_fnum;
2208 buf = lastbuf;
2209 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2210 buf = buf->b_prev;
2211 ea.line2 = buf->b_fnum;
2212 break;
2213 case ADDR_BUFFERS:
2214 ea.line1 = firstbuf->b_fnum;
2215 ea.line2 = lastbuf->b_fnum;
2216 break;
2217 case ADDR_WINDOWS:
2218 ea.line2 = LAST_WIN_NR;
2219 break;
2220 case ADDR_TABS:
2221 ea.line2 = LAST_TAB_NR;
2222 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002223 case ADDR_TABS_RELATIVE:
2224 ea.line2 = 1;
2225 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002226 case ADDR_ARGUMENTS:
2227 if (ARGCOUNT == 0)
2228 ea.line1 = ea.line2 = 0;
2229 else
2230 ea.line2 = ARGCOUNT;
2231 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002232 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002233#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002234 ea.line2 = qf_get_size(&ea);
2235 if (ea.line2 == 0)
2236 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002237#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002238 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002239 case ADDR_NONE:
2240 iemsg(_("INTERNAL: Cannot use DFLALL with ADDR_NONE"));
2241 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
2244
2245 /* accept numbered register only when no count allowed (:put) */
2246 if ( (ea.argt & REGSTR)
2247 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002248 /* Do not allow register = for user commands */
2249 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2251 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002252#ifndef FEAT_CLIPBOARD
2253 /* check these explicitly for a more specific error message */
2254 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002256 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002257 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
2259#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002260 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2261 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002262 {
2263 ea.regname = *ea.arg++;
2264#ifdef FEAT_EVAL
2265 /* for '=' register: accept the rest of the line as an expression */
2266 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2267 {
2268 set_expr_line(vim_strsave(ea.arg));
2269 ea.arg += STRLEN(ea.arg);
2270 }
2271#endif
2272 ea.arg = skipwhite(ea.arg);
2273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275
2276 /*
2277 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2278 * count, it's a buffer name.
2279 */
2280 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2281 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002282 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
2284 n = getdigits(&ea.arg);
2285 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002286 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002288 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 goto doend;
2290 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002291 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
2293 ea.line2 = n;
2294 if (ea.addr_count == 0)
2295 ea.addr_count = 1;
2296 }
2297 else
2298 {
2299 ea.line1 = ea.line2;
2300 ea.line2 += n - 1;
2301 ++ea.addr_count;
2302 /*
2303 * Be vi compatible: no error message for out of range.
2304 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002305 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 ea.line2 = curbuf->b_ml.ml_line_count;
2307 }
2308 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002309
2310 /*
2311 * Check for flags: 'l', 'p' and '#'.
2312 */
2313 if (ea.argt & EXFLAGS)
2314 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002316 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002317 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002319 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 goto doend;
2321 }
2322
2323 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002325 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 goto doend;
2327 }
2328
2329#ifdef FEAT_EVAL
2330 /*
2331 * Skip the command when it's not going to be executed.
2332 * The commands like :if, :endif, etc. always need to be executed.
2333 * Also make an exception for commands that handle a trailing command
2334 * themselves.
2335 */
2336 if (ea.skip)
2337 {
2338 switch (ea.cmdidx)
2339 {
2340 /* commands that need evaluation */
2341 case CMD_while:
2342 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002343 case CMD_for:
2344 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 case CMD_if:
2346 case CMD_elseif:
2347 case CMD_else:
2348 case CMD_endif:
2349 case CMD_try:
2350 case CMD_catch:
2351 case CMD_finally:
2352 case CMD_endtry:
2353 case CMD_function:
2354 break;
2355
2356 /* Commands that handle '|' themselves. Check: A command should
2357 * either have the TRLBAR flag, appear in this list or appear in
2358 * the list at ":help :bar". */
2359 case CMD_aboveleft:
2360 case CMD_and:
2361 case CMD_belowright:
2362 case CMD_botright:
2363 case CMD_browse:
2364 case CMD_call:
2365 case CMD_confirm:
2366 case CMD_delfunction:
2367 case CMD_djump:
2368 case CMD_dlist:
2369 case CMD_dsearch:
2370 case CMD_dsplit:
2371 case CMD_echo:
2372 case CMD_echoerr:
2373 case CMD_echomsg:
2374 case CMD_echon:
2375 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002376 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 case CMD_help:
2378 case CMD_hide:
2379 case CMD_ijump:
2380 case CMD_ilist:
2381 case CMD_isearch:
2382 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002383 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 case CMD_keepjumps:
2385 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002386 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 case CMD_leftabove:
2388 case CMD_let:
2389 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002390 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002392 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002393 case CMD_noautocmd:
2394 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 case CMD_perl:
2396 case CMD_psearch:
2397 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002398 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002399 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_return:
2401 case CMD_rightbelow:
2402 case CMD_ruby:
2403 case CMD_silent:
2404 case CMD_smagic:
2405 case CMD_snomagic:
2406 case CMD_substitute:
2407 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002408 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 case CMD_tcl:
2410 case CMD_throw:
2411 case CMD_tilde:
2412 case CMD_topleft:
2413 case CMD_unlet:
2414 case CMD_verbose:
2415 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002416 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 break;
2418
2419 default: goto doend;
2420 }
2421 }
2422#endif
2423
2424 if (ea.argt & XFILE)
2425 {
2426 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2427 goto doend;
2428 }
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 /*
2431 * Accept buffer name. Cannot be used at the same time with a buffer
2432 * number. Don't do this for a user command.
2433 */
2434 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002435 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 /*
2438 * :bdelete, :bwipeout and :bunload take several arguments, separated
2439 * by spaces: find next space (skipping over escaped characters).
2440 * The others take one argument: ignore trailing spaces.
2441 */
2442 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2443 || ea.cmdidx == CMD_bunload)
2444 p = skiptowhite_esc(ea.arg);
2445 else
2446 {
2447 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002448 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 --p;
2450 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002451 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2452 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 if (ea.line2 < 0) /* failed */
2454 goto doend;
2455 ea.addr_count = 1;
2456 ea.arg = skipwhite(p);
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
Bram Moolenaar2be57332018-02-13 18:05:18 +01002459 /* The :try command saves the emsg_silent flag, reset it here when
2460 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002461 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002462 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002463 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002464 if (emsg_silent < 0)
2465 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002466 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002467 }
2468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002470 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
Bram Moolenaar958636c2014-10-21 20:01:58 +02002473 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 /*
2476 * Execute a user-defined command.
2477 */
2478 do_ucmd(&ea);
2479 }
2480 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
2482 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002483 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
2485 ea.errmsg = NULL;
2486 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2487 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002488 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 }
2490
2491#ifdef FEAT_EVAL
2492 /*
2493 * If the command just executed called do_cmdline(), any throw or ":return"
2494 * or ":finish" encountered there must also check the cstack of the still
2495 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2496 * exception, or reanimate a returned function or finished script file and
2497 * return or finish it again.
2498 */
2499 if (need_rethrow)
2500 do_throw(cstack);
2501 else if (check_cstack)
2502 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002503 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002505 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 && current_func_returned())
2507 do_return(&ea, TRUE, FALSE, NULL);
2508 }
2509 need_rethrow = check_cstack = FALSE;
2510#endif
2511
2512doend:
2513 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002516 curwin->w_cursor.col = 0;
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2520 {
2521 if (sourcing)
2522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002523 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 {
2525 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002526 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002528 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530 emsg(errormsg);
2531 }
2532#ifdef FEAT_EVAL
2533 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002534 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2535 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536#endif
2537
Bram Moolenaareffed932018-08-14 13:38:17 +02002538 if (ea.verbose_save >= 0)
2539 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002540
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002541 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002543 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
Bram Moolenaareffed932018-08-14 13:38:17 +02002545 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
2547 /* messages could be enabled for a serious error, need to check if the
2548 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002549 if (!did_emsg || msg_silent > ea.save_msg_silent)
2550 msg_silent = ea.save_msg_silent;
2551 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 if (emsg_silent < 0)
2553 emsg_silent = 0;
2554 /* Restore msg_scroll, it's set by file I/O commands, even when no
2555 * message is actually displayed. */
2556 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002557
2558 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2559 * somewhere in the line. Put it back in the first column. */
2560 if (redirecting())
2561 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002564#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002565 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002566 --sandbox;
2567#endif
2568
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2570 ea.nextcmd = NULL;
2571
2572#ifdef FEAT_EVAL
2573 --ex_nesting_level;
2574#endif
2575
2576 return ea.nextcmd;
2577}
2578#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002579 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580#endif
2581
2582/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002583 * Parse and skip over command modifiers:
2584 * - update eap->cmd
2585 * - store flags in "cmdmod".
2586 * - Set ex_pressedreturn for an empty command line.
2587 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002588 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002589 * - set p_verbose for ":verbose"
2590 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002591 * When "skip_only" is TRUE the global variables are not changed, except for
2592 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002593 * Return FAIL when the command is not to be executed.
2594 * May set "errormsg" to an error message.
2595 */
2596 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002597parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002598{
2599 char_u *p;
2600
2601 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2602 eap->verbose_save = -1;
2603 eap->save_msg_silent = -1;
2604
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002605 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002606 for (;;)
2607 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002608 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2609 ++eap->cmd;
2610
2611 /* in ex mode, an empty line works like :+ */
2612 if (*eap->cmd == NUL && exmode_active
2613 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2614 || getline_equal(eap->getline, eap->cookie, getexline))
2615 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2616 {
2617 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002618 if (!skip_only)
2619 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002620 }
2621
2622 /* ignore comment and empty lines */
2623 if (*eap->cmd == '"')
2624 return FAIL;
2625 if (*eap->cmd == NUL)
2626 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002627 if (!skip_only)
2628 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002629 return FAIL;
2630 }
2631
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 p = skip_range(eap->cmd, NULL);
2633 switch (*p)
2634 {
2635 /* When adding an entry, also modify cmd_exists(). */
2636 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2637 break;
2638 cmdmod.split |= WSP_ABOVE;
2639 continue;
2640
2641 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2642 {
2643 cmdmod.split |= WSP_BELOW;
2644 continue;
2645 }
2646 if (checkforcmd(&eap->cmd, "browse", 3))
2647 {
2648#ifdef FEAT_BROWSE_CMD
2649 cmdmod.browse = TRUE;
2650#endif
2651 continue;
2652 }
2653 if (!checkforcmd(&eap->cmd, "botright", 2))
2654 break;
2655 cmdmod.split |= WSP_BOT;
2656 continue;
2657
2658 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2659 break;
2660#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2661 cmdmod.confirm = TRUE;
2662#endif
2663 continue;
2664
2665 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2666 {
2667 cmdmod.keepmarks = TRUE;
2668 continue;
2669 }
2670 if (checkforcmd(&eap->cmd, "keepalt", 5))
2671 {
2672 cmdmod.keepalt = TRUE;
2673 continue;
2674 }
2675 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2676 {
2677 cmdmod.keeppatterns = TRUE;
2678 continue;
2679 }
2680 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2681 break;
2682 cmdmod.keepjumps = TRUE;
2683 continue;
2684
2685 case 'f': /* only accept ":filter {pat} cmd" */
2686 {
2687 char_u *reg_pat;
2688
2689 if (!checkforcmd(&p, "filter", 4)
2690 || *p == NUL || ends_excmd(*p))
2691 break;
2692 if (*p == '!')
2693 {
2694 cmdmod.filter_force = TRUE;
2695 p = skipwhite(p + 1);
2696 if (*p == NUL || ends_excmd(*p))
2697 break;
2698 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002699 if (skip_only)
2700 p = skip_vimgrep_pat(p, NULL, NULL);
2701 else
2702 // NOTE: This puts a NUL after the pattern.
2703 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002704 if (p == NULL || *p == NUL)
2705 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002706 if (!skip_only)
2707 {
2708 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002709 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002710 if (cmdmod.filter_regmatch.regprog == NULL)
2711 break;
2712 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002713 eap->cmd = p;
2714 continue;
2715 }
2716
2717 /* ":hide" and ":hide | cmd" are not modifiers */
2718 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2719 || *p == NUL || ends_excmd(*p))
2720 break;
2721 eap->cmd = p;
2722 cmdmod.hide = TRUE;
2723 continue;
2724
2725 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2726 {
2727 cmdmod.lockmarks = TRUE;
2728 continue;
2729 }
2730
2731 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2732 break;
2733 cmdmod.split |= WSP_ABOVE;
2734 continue;
2735
2736 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2737 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002738 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002739 {
2740 /* Set 'eventignore' to "all". Restore the
2741 * existing option value later. */
2742 cmdmod.save_ei = vim_strsave(p_ei);
2743 set_string_option_direct((char_u *)"ei", -1,
2744 (char_u *)"all", OPT_FREE, SID_NONE);
2745 }
2746 continue;
2747 }
2748 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2749 break;
2750 cmdmod.noswapfile = TRUE;
2751 continue;
2752
2753 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2754 break;
2755 cmdmod.split |= WSP_BELOW;
2756 continue;
2757
2758 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2759 {
2760#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002761 if (!skip_only)
2762 {
2763 if (!eap->did_sandbox)
2764 ++sandbox;
2765 eap->did_sandbox = TRUE;
2766 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002767#endif
2768 continue;
2769 }
2770 if (!checkforcmd(&eap->cmd, "silent", 3))
2771 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002772 if (!skip_only)
2773 {
2774 if (eap->save_msg_silent == -1)
2775 eap->save_msg_silent = msg_silent;
2776 ++msg_silent;
2777 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2779 {
2780 /* ":silent!", but not "silent !cmd" */
2781 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002782 if (!skip_only)
2783 {
2784 ++emsg_silent;
2785 ++eap->did_esilent;
2786 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002787 }
2788 continue;
2789
2790 case 't': if (checkforcmd(&p, "tab", 3))
2791 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002792 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002794 long tabnr = get_address(eap, &eap->cmd,
2795 ADDR_TABS, eap->skip,
2796 skip_only, FALSE, 1);
2797 if (tabnr == MAXLNUM)
2798 cmdmod.tab = tabpage_index(curtab) + 1;
2799 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002800 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002801 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2802 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002803 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002804 return FAIL;
2805 }
2806 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002808 }
2809 eap->cmd = p;
2810 continue;
2811 }
2812 if (!checkforcmd(&eap->cmd, "topleft", 2))
2813 break;
2814 cmdmod.split |= WSP_TOP;
2815 continue;
2816
2817 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2818 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002819 if (!skip_only)
2820 {
2821 if (eap->save_msg_silent == -1)
2822 eap->save_msg_silent = msg_silent;
2823 msg_silent = 0;
2824 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 continue;
2826
2827 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2828 {
2829 cmdmod.split |= WSP_VERT;
2830 continue;
2831 }
2832 if (!checkforcmd(&p, "verbose", 4))
2833 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002834 if (!skip_only)
2835 {
2836 if (eap->verbose_save < 0)
2837 eap->verbose_save = p_verbose;
2838 if (vim_isdigit(*eap->cmd))
2839 p_verbose = atoi((char *)eap->cmd);
2840 else
2841 p_verbose = 1;
2842 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002843 eap->cmd = p;
2844 continue;
2845 }
2846 break;
2847 }
2848
2849 return OK;
2850}
2851
2852/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002853 * Free contents of "cmdmod".
2854 */
2855 static void
2856free_cmdmod(void)
2857{
2858 if (cmdmod.save_ei != NULL)
2859 {
2860 /* Restore 'eventignore' to the value before ":noautocmd". */
2861 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2862 OPT_FREE, SID_NONE);
2863 free_string_option(cmdmod.save_ei);
2864 }
2865
2866 if (cmdmod.filter_regmatch.regprog != NULL)
2867 vim_regfree(cmdmod.filter_regmatch.regprog);
2868}
2869
2870/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002871 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002872 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002873 * Return FAIL and set "errormsg" or return OK.
2874 */
2875 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002876parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002877{
2878 int address_count = 1;
2879 linenr_T lnum;
2880
2881 // Repeat for all ',' or ';' separated addresses.
2882 for (;;)
2883 {
2884 eap->line1 = eap->line2;
2885 switch (eap->addr_type)
2886 {
2887 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002888 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002889 // default is current line number
2890 eap->line2 = curwin->w_cursor.lnum;
2891 break;
2892 case ADDR_WINDOWS:
2893 eap->line2 = CURRENT_WIN_NR;
2894 break;
2895 case ADDR_ARGUMENTS:
2896 eap->line2 = curwin->w_arg_idx + 1;
2897 if (eap->line2 > ARGCOUNT)
2898 eap->line2 = ARGCOUNT;
2899 break;
2900 case ADDR_LOADED_BUFFERS:
2901 case ADDR_BUFFERS:
2902 eap->line2 = curbuf->b_fnum;
2903 break;
2904 case ADDR_TABS:
2905 eap->line2 = CURRENT_TAB_NR;
2906 break;
2907 case ADDR_TABS_RELATIVE:
2908 eap->line2 = 1;
2909 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002910 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002911#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002912 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002913#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002914 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002915 case ADDR_NONE:
2916 // Will give an error later if a range is found.
2917 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002918 }
2919 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002920 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002921 eap->addr_count == 0, address_count++);
2922 if (eap->cmd == NULL) // error detected
2923 return FAIL;
2924 if (lnum == MAXLNUM)
2925 {
2926 if (*eap->cmd == '%') // '%' - all lines
2927 {
2928 ++eap->cmd;
2929 switch (eap->addr_type)
2930 {
2931 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002932 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002933 eap->line1 = 1;
2934 eap->line2 = curbuf->b_ml.ml_line_count;
2935 break;
2936 case ADDR_LOADED_BUFFERS:
2937 {
2938 buf_T *buf = firstbuf;
2939
2940 while (buf->b_next != NULL
2941 && buf->b_ml.ml_mfp == NULL)
2942 buf = buf->b_next;
2943 eap->line1 = buf->b_fnum;
2944 buf = lastbuf;
2945 while (buf->b_prev != NULL
2946 && buf->b_ml.ml_mfp == NULL)
2947 buf = buf->b_prev;
2948 eap->line2 = buf->b_fnum;
2949 break;
2950 }
2951 case ADDR_BUFFERS:
2952 eap->line1 = firstbuf->b_fnum;
2953 eap->line2 = lastbuf->b_fnum;
2954 break;
2955 case ADDR_WINDOWS:
2956 case ADDR_TABS:
2957 if (IS_USER_CMDIDX(eap->cmdidx))
2958 {
2959 eap->line1 = 1;
2960 eap->line2 = eap->addr_type == ADDR_WINDOWS
2961 ? LAST_WIN_NR : LAST_TAB_NR;
2962 }
2963 else
2964 {
2965 // there is no Vim command which uses '%' and
2966 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002967 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002968 return FAIL;
2969 }
2970 break;
2971 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002972 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002973 return FAIL;
2974 case ADDR_ARGUMENTS:
2975 if (ARGCOUNT == 0)
2976 eap->line1 = eap->line2 = 0;
2977 else
2978 {
2979 eap->line1 = 1;
2980 eap->line2 = ARGCOUNT;
2981 }
2982 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002983 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002984#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002985 eap->line1 = 1;
2986 eap->line2 = qf_get_size(eap);
2987 if (eap->line2 == 0)
2988 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002990 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002991 case ADDR_NONE:
2992 // Will give an error later if a range is found.
2993 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002994 }
2995 ++eap->addr_count;
2996 }
2997 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2998 {
2999 pos_T *fp;
3000
3001 // '*' - visual area
3002 if (eap->addr_type != ADDR_LINES)
3003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003004 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 return FAIL;
3006 }
3007
3008 ++eap->cmd;
3009 if (!eap->skip)
3010 {
3011 fp = getmark('<', FALSE);
3012 if (check_mark(fp) == FAIL)
3013 return FAIL;
3014 eap->line1 = fp->lnum;
3015 fp = getmark('>', FALSE);
3016 if (check_mark(fp) == FAIL)
3017 return FAIL;
3018 eap->line2 = fp->lnum;
3019 ++eap->addr_count;
3020 }
3021 }
3022 }
3023 else
3024 eap->line2 = lnum;
3025 eap->addr_count++;
3026
3027 if (*eap->cmd == ';')
3028 {
3029 if (!eap->skip)
3030 {
3031 curwin->w_cursor.lnum = eap->line2;
3032 // don't leave the cursor on an illegal line or column
3033 check_cursor();
3034 }
3035 }
3036 else if (*eap->cmd != ',')
3037 break;
3038 ++eap->cmd;
3039 }
3040
3041 // One address given: set start and end lines.
3042 if (eap->addr_count == 1)
3043 {
3044 eap->line1 = eap->line2;
3045 // ... but only implicit: really no address given
3046 if (lnum == MAXLNUM)
3047 eap->addr_count = 0;
3048 }
3049 return OK;
3050}
3051
3052/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003053 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 * If there is a match advance "pp" to the argument and return TRUE.
3055 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003056 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003057checkforcmd(
3058 char_u **pp, /* start of command */
3059 char *cmd, /* name of command */
3060 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 int i;
3063
3064 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003065 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 break;
3067 if (i >= len && !isalpha((*pp)[i]))
3068 {
3069 *pp = skipwhite(*pp + i);
3070 return TRUE;
3071 }
3072 return FALSE;
3073}
3074
3075/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003076 * Append "cmd" to the error message in IObuff.
3077 * Takes care of limiting the length and handling 0xa0, which would be
3078 * invisible otherwise.
3079 */
3080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003081append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003082{
3083 char_u *s = cmd;
3084 char_u *d;
3085
3086 STRCAT(IObuff, ": ");
3087 d = IObuff + STRLEN(IObuff);
3088 while (*s != NUL && d - IObuff < IOSIZE - 7)
3089 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003090 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003091 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003092 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003093 STRCPY(d, "<a0>");
3094 d += 4;
3095 }
3096 else
3097 MB_COPY_CHAR(s, d);
3098 }
3099 *d = NUL;
3100}
3101
3102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003104 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003106 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * Returns NULL for an ambiguous user command.
3108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 int len;
3113 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003114 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115
3116 /*
3117 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003118 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 * - the 'k' command can directly be followed by any character.
3120 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003121 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003123 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 */
3125 p = eap->cmd;
3126 if (*p == 'k')
3127 {
3128 eap->cmdidx = CMD_k;
3129 ++p;
3130 }
3131 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003132 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3133 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 || p[1] == 'g'
3135 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3136 || p[1] == 'I'
3137 || (p[1] == 'r' && p[2] != 'e')))
3138 {
3139 eap->cmdidx = CMD_substitute;
3140 ++p;
3141 }
3142 else
3143 {
3144 while (ASCII_ISALPHA(*p))
3145 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003146 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003147 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003148 while (ASCII_ISALNUM(*p))
3149 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 /* check for non-alpha command */
3152 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3153 ++p;
3154 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003155 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3156 {
3157 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3158 * :delete with the 'l' flag. Same for 'p'. */
3159 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003160 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003161 break;
3162 if (i == len - 1)
3163 {
3164 --len;
3165 if (p[-1] == 'l')
3166 eap->flags |= EXFLAG_LIST;
3167 else
3168 eap->flags |= EXFLAG_PRINT;
3169 }
3170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003172 if (ASCII_ISLOWER(eap->cmd[0]))
3173 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003174 int c1 = eap->cmd[0];
3175 int c2 = eap->cmd[1];
3176
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003177 if (command_count != (int)CMD_SIZE)
3178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003179 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003180 getout(1);
3181 }
3182
3183 /* Use a precomputed index for fast look-up in cmdnames[]
3184 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003185 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3186 if (ASCII_ISLOWER(c2))
3187 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003190 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3193 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3194 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3195 (size_t)len) == 0)
3196 {
3197#ifdef FEAT_EVAL
3198 if (full != NULL
3199 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3200 *full = TRUE;
3201#endif
3202 break;
3203 }
3204
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003205 // Look for a user defined command as a last resort. Let ":Print" be
3206 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003207 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3208 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003210 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 while (ASCII_ISALNUM(*p))
3212 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003215 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 eap->cmdidx = CMD_SIZE;
3217 }
3218
3219 return p;
3220}
3221
3222#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003223static struct cmdmod
3224{
3225 char *name;
3226 int minlen;
3227 int has_count; /* :123verbose :3tab */
3228} cmdmods[] = {
3229 {"aboveleft", 3, FALSE},
3230 {"belowright", 3, FALSE},
3231 {"botright", 2, FALSE},
3232 {"browse", 3, FALSE},
3233 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003234 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003235 {"hide", 3, FALSE},
3236 {"keepalt", 5, FALSE},
3237 {"keepjumps", 5, FALSE},
3238 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003239 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003240 {"leftabove", 5, FALSE},
3241 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003242 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003243 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003244 {"rightbelow", 6, FALSE},
3245 {"sandbox", 3, FALSE},
3246 {"silent", 3, FALSE},
3247 {"tab", 3, TRUE},
3248 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003249 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003250 {"verbose", 4, TRUE},
3251 {"vertical", 4, FALSE},
3252};
3253
3254/*
3255 * Return length of a command modifier (including optional count).
3256 * Return zero when it's not a modifier.
3257 */
3258 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003259modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003260{
3261 int i, j;
3262 char_u *p = cmd;
3263
3264 if (VIM_ISDIGIT(*cmd))
3265 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003266 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003267 {
3268 for (j = 0; p[j] != NUL; ++j)
3269 if (p[j] != cmdmods[i].name[j])
3270 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003271 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003272 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003273 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003274 }
3275 return 0;
3276}
3277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278/*
3279 * Return > 0 if an Ex command "name" exists.
3280 * Return 2 if there is an exact match.
3281 * Return 3 if there is an ambiguous match.
3282 */
3283 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003284cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
3286 exarg_T ea;
3287 int full = FALSE;
3288 int i;
3289 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003290 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291
3292 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003293 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
3295 for (j = 0; name[j] != NUL; ++j)
3296 if (name[j] != cmdmods[i].name[j])
3297 break;
3298 if (name[j] == NUL && j >= cmdmods[i].minlen)
3299 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3300 }
3301
Bram Moolenaara9587612006-05-04 21:47:50 +00003302 /* Check built-in commands and user defined commands.
3303 * For ":2match" and ":3match" we need to skip the number. */
3304 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003306 p = find_command(&ea, &full);
3307 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003309 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3310 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003311 if (*skipwhite(p) != NUL)
3312 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3314}
3315#endif
3316
3317/*
3318 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3319 * we don't need/want deleted. Maybe this could be done better if we didn't
3320 * repeat all this stuff. The only problem is that they may not stay
3321 * perfectly compatible with each other, but then the command line syntax
3322 * probably won't change that much -- webb.
3323 */
3324 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003325set_one_cmd_context(
3326 expand_T *xp,
3327 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 char_u *p;
3330 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003331 int len = 0;
3332 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003334 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 int delim;
3336#endif
3337 int forceit = FALSE;
3338 int usefilter = FALSE; /* filter instead of file name */
3339
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003340 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 xp->xp_pattern = buff;
3342 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003343 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344
3345/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003346 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 */
3348 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3349 ;
3350 xp->xp_pattern = cmd;
3351
3352 if (*cmd == NUL)
3353 return NULL;
3354 if (*cmd == '"') /* ignore comment lines */
3355 {
3356 xp->xp_context = EXPAND_NOTHING;
3357 return NULL;
3358 }
3359
3360/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003361 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 */
3363 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 xp->xp_pattern = cmd;
3365 if (*cmd == NUL)
3366 return NULL;
3367 if (*cmd == '"')
3368 {
3369 xp->xp_context = EXPAND_NOTHING;
3370 return NULL;
3371 }
3372
3373 if (*cmd == '|' || *cmd == '\n')
3374 return cmd + 1; /* There's another command */
3375
3376 /*
3377 * Isolate the command and search for it in the command table.
3378 * Exceptions:
3379 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003380 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3382 */
3383 if (*cmd == 'k' && cmd[1] != 'e')
3384 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003385 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 p = cmd + 1;
3387 }
3388 else
3389 {
3390 p = cmd;
3391 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3392 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003393 /* a user command may contain digits */
3394 if (ASCII_ISUPPER(cmd[0]))
3395 while (ASCII_ISALNUM(*p) || *p == '*')
3396 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003397 /* for python 3.x: ":py3*" commands completion */
3398 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003399 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003400 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003401 while (ASCII_ISALPHA(*p) || *p == '*')
3402 ++p;
3403 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003404 /* check for non-alpha command */
3405 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3406 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003407 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003409 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 {
3411 xp->xp_context = EXPAND_UNSUCCESSFUL;
3412 return NULL;
3413 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003414 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003415 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3416 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3417 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 break;
3419
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003421 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424
3425 /*
3426 * If the cursor is touching the command, and it ends in an alpha-numeric
3427 * character, complete the command name.
3428 */
3429 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3430 return NULL;
3431
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003432 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 {
3434 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3435 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003436 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 p = cmd + 1;
3438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3440 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003441 ea.cmd = cmd;
3442 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003443#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003444 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003445#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003446 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003447#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003448 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003449 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003450 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
3455 /* Not still touching the command and it was an illegal one */
3456 xp->xp_context = EXPAND_UNSUCCESSFUL;
3457 return NULL;
3458 }
3459
3460 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3461
3462 if (*p == '!') /* forced commands */
3463 {
3464 forceit = TRUE;
3465 ++p;
3466 }
3467
3468/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003469 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003471 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
3474 arg = skipwhite(p);
3475
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003476 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
3478 if (*arg == '>') /* append */
3479 {
3480 if (*++arg == '>')
3481 ++arg;
3482 arg = skipwhite(arg);
3483 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003484 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
3486 ++arg;
3487 usefilter = TRUE;
3488 }
3489 }
3490
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
3493 usefilter = forceit; /* :r! filter if forced */
3494 if (*arg == '!') /* :r !filter */
3495 {
3496 ++arg;
3497 usefilter = TRUE;
3498 }
3499 }
3500
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003501 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
3503 while (*arg == *cmd) /* allow any number of '>' or '<' */
3504 ++arg;
3505 arg = skipwhite(arg);
3506 }
3507
3508 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
3511 /* Check if we're in the +command */
3512 p = arg + 1;
3513 arg = skip_cmd_arg(arg, FALSE);
3514
3515 /* Still touching the command after '+'? */
3516 if (*arg == NUL)
3517 return p;
3518
3519 /* Skip space(s) after +command to get to the real argument */
3520 arg = skipwhite(arg);
3521 }
3522
3523 /*
3524 * Check for '|' to separate commands and '"' to start comments.
3525 * Don't do this for ":read !cmd" and ":write !cmd".
3526 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003527 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 p = arg;
3530 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003531 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 p += 2;
3533 while (*p)
3534 {
3535 if (*p == Ctrl_V)
3536 {
3537 if (p[1] != NUL)
3538 ++p;
3539 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003540 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 || *p == '|' || *p == '\n')
3542 {
3543 if (*(p - 1) != '\\')
3544 {
3545 if (*p == '|' || *p == '\n')
3546 return p + 1;
3547 return NULL; /* It's a comment */
3548 }
3549 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003550 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 }
3553
3554 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 vim_strchr((char_u *)"|\"", *arg) == NULL)
3557 return NULL;
3558
3559 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003560 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003562 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003563 while (*p && p < buff + len)
3564 {
3565 if (*p == ' ' || *p == TAB)
3566 {
3567 /* argument starts after a space */
3568 xp->xp_pattern = ++p;
3569 }
3570 else
3571 {
3572 if (*p == '\\' && *(p + 1) != NUL)
3573 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003574 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003575 }
3576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003578 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003580 int c;
3581 int in_quote = FALSE;
3582 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
3584 /*
3585 * Allow spaces within back-quotes to count as part of the argument
3586 * being expanded.
3587 */
3588 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003589 p = xp->xp_pattern;
3590 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003592 if (has_mbyte)
3593 c = mb_ptr2char(p);
3594 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003595 c = *p;
3596 if (c == '\\' && p[1] != NUL)
3597 ++p;
3598 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
3600 if (!in_quote)
3601 {
3602 xp->xp_pattern = p;
3603 bow = p + 1;
3604 }
3605 in_quote = !in_quote;
3606 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003607 /* An argument can contain just about everything, except
3608 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003609 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003610#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003611 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003612#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003613 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003614 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003615 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003616 while (*p != NUL)
3617 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003618 if (has_mbyte)
3619 c = mb_ptr2char(p);
3620 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003621 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003622 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003623 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003624 if (has_mbyte)
3625 len = (*mb_ptr2len)(p);
3626 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003627 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003628 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003629 }
3630 if (in_quote)
3631 bow = p;
3632 else
3633 xp->xp_pattern = p;
3634 p -= len;
3635 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003636 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638
3639 /*
3640 * If we are still inside the quotes, and we passed a space, just
3641 * expand from there.
3642 */
3643 if (bow != NULL && in_quote)
3644 xp->xp_pattern = bow;
3645 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003646
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003647 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003648 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003649 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003650#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003651 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003652#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003653 /* When still after the command name expand executables. */
3654 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003655 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
3658 /* Check for environment variable */
3659 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003660#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 || *xp->xp_pattern == '%'
3662#endif
3663 )
3664 {
3665 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3666 if (!vim_isIDc(*p))
3667 break;
3668 if (*p == NUL)
3669 {
3670 xp->xp_context = EXPAND_ENV_VARS;
3671 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003672#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003673 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003674 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003675 compl = EXPAND_ENV_VARS;
3676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003679#if defined(FEAT_CMDL_COMPL)
3680 /* Check for user names */
3681 if (*xp->xp_pattern == '~')
3682 {
3683 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3684 ;
3685 /* Complete ~user only if it partially matches a user name.
3686 * A full match ~user<Tab> will be replaced by user's home
3687 * directory i.e. something like ~user<Tab> -> /home/user/ */
3688 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003689 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003690 {
3691 xp->xp_context = EXPAND_USER;
3692 ++xp->xp_pattern;
3693 }
3694 }
3695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697
3698/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003699 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003701 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003703 case CMD_find:
3704 case CMD_sfind:
3705 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003706 if (xp->xp_context == EXPAND_FILES)
3707 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 case CMD_cd:
3710 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003711 case CMD_tcd:
3712 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 case CMD_lcd:
3714 case CMD_lchdir:
3715 if (xp->xp_context == EXPAND_FILES)
3716 xp->xp_context = EXPAND_DIRECTORIES;
3717 break;
3718 case CMD_help:
3719 xp->xp_context = EXPAND_HELP;
3720 xp->xp_pattern = arg;
3721 break;
3722
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003723 /* Command modifiers: return the argument.
3724 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003726 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 case CMD_belowright:
3728 case CMD_botright:
3729 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003730 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003731 case CMD_cdo:
3732 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003734 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 case CMD_folddoclosed:
3736 case CMD_folddoopen:
3737 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003738 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 case CMD_keepjumps:
3740 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003741 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003742 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003744 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003746 case CMD_noautocmd:
3747 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003749 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003751 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003752 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 case CMD_topleft:
3754 case CMD_verbose:
3755 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003756 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 return arg;
3758
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003759 case CMD_filter:
3760 if (*arg != NUL)
3761 arg = skip_vimgrep_pat(arg, NULL, NULL);
3762 if (arg == NULL || *arg == NUL)
3763 {
3764 xp->xp_context = EXPAND_NOTHING;
3765 return NULL;
3766 }
3767 return skipwhite(arg);
3768
Bram Moolenaar4f688582007-07-24 12:34:30 +00003769#ifdef FEAT_CMDL_COMPL
3770# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 case CMD_match:
3772 if (*arg == NUL || !ends_excmd(*arg))
3773 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003774 /* also complete "None" */
3775 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 arg = skipwhite(skiptowhite(arg));
3777 if (*arg != NUL)
3778 {
3779 xp->xp_context = EXPAND_NOTHING;
3780 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3781 }
3782 }
3783 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786/*
3787 * All completion for the +cmdline_compl feature goes here.
3788 */
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003791 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 case CMD_delcommand:
3794 xp->xp_context = EXPAND_USER_COMMANDS;
3795 xp->xp_pattern = arg;
3796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798 case CMD_global:
3799 case CMD_vglobal:
3800 delim = *arg; /* get the delimiter */
3801 if (delim)
3802 ++arg; /* skip delimiter if there is one */
3803
3804 while (arg[0] != NUL && arg[0] != delim)
3805 {
3806 if (arg[0] == '\\' && arg[1] != NUL)
3807 ++arg;
3808 ++arg;
3809 }
3810 if (arg[0] != NUL)
3811 return arg + 1;
3812 break;
3813 case CMD_and:
3814 case CMD_substitute:
3815 delim = *arg;
3816 if (delim)
3817 {
3818 /* skip "from" part */
3819 ++arg;
3820 arg = skip_regexp(arg, delim, p_magic, NULL);
3821 }
3822 /* skip "to" part */
3823 while (arg[0] != NUL && arg[0] != delim)
3824 {
3825 if (arg[0] == '\\' && arg[1] != NUL)
3826 ++arg;
3827 ++arg;
3828 }
3829 if (arg[0] != NUL) /* skip delimiter */
3830 ++arg;
3831 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3832 ++arg;
3833 if (arg[0] != NUL)
3834 return arg;
3835 break;
3836 case CMD_isearch:
3837 case CMD_dsearch:
3838 case CMD_ilist:
3839 case CMD_dlist:
3840 case CMD_ijump:
3841 case CMD_psearch:
3842 case CMD_djump:
3843 case CMD_isplit:
3844 case CMD_dsplit:
3845 arg = skipwhite(skipdigits(arg)); /* skip count */
3846 if (*arg == '/') /* Match regexp, not just whole words */
3847 {
3848 for (++arg; *arg && *arg != '/'; arg++)
3849 if (*arg == '\\' && arg[1] != NUL)
3850 arg++;
3851 if (*arg)
3852 {
3853 arg = skipwhite(arg + 1);
3854
3855 /* Check for trailing illegal characters */
3856 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3857 xp->xp_context = EXPAND_NOTHING;
3858 else
3859 return arg;
3860 }
3861 }
3862 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003863
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 case CMD_autocmd:
3865 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003867 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 case CMD_set:
3870 set_context_in_set_cmd(xp, arg, 0);
3871 break;
3872 case CMD_setglobal:
3873 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3874 break;
3875 case CMD_setlocal:
3876 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3877 break;
3878 case CMD_tag:
3879 case CMD_stag:
3880 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003881 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case CMD_tselect:
3883 case CMD_stselect:
3884 case CMD_ptselect:
3885 case CMD_tjump:
3886 case CMD_stjump:
3887 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003888 if (*p_wop != NUL)
3889 xp->xp_context = EXPAND_TAGS_LISTFILES;
3890 else
3891 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 xp->xp_pattern = arg;
3893 break;
3894 case CMD_augroup:
3895 xp->xp_context = EXPAND_AUGROUP;
3896 xp->xp_pattern = arg;
3897 break;
3898#ifdef FEAT_SYN_HL
3899 case CMD_syntax:
3900 set_context_in_syntax_cmd(xp, arg);
3901 break;
3902#endif
3903#ifdef FEAT_EVAL
3904 case CMD_let:
3905 case CMD_if:
3906 case CMD_elseif:
3907 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003908 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 case CMD_echo:
3910 case CMD_echon:
3911 case CMD_execute:
3912 case CMD_echomsg:
3913 case CMD_echoerr:
3914 case CMD_call:
3915 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003916 case CMD_cexpr:
3917 case CMD_caddexpr:
3918 case CMD_cgetexpr:
3919 case CMD_lexpr:
3920 case CMD_laddexpr:
3921 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003922 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 break;
3924
3925 case CMD_unlet:
3926 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3927 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003928
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 xp->xp_context = EXPAND_USER_VARS;
3930 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003931
3932 if (*xp->xp_pattern == '$')
3933 {
3934 xp->xp_context = EXPAND_ENV_VARS;
3935 ++xp->xp_pattern;
3936 }
3937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 break;
3939
3940 case CMD_function:
3941 case CMD_delfunction:
3942 xp->xp_context = EXPAND_USER_FUNC;
3943 xp->xp_pattern = arg;
3944 break;
3945
3946 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003947 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 break;
3949#endif
3950 case CMD_highlight:
3951 set_context_in_highlight_cmd(xp, arg);
3952 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003953#ifdef FEAT_CSCOPE
3954 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003955 case CMD_lcscope:
3956 case CMD_scscope:
3957 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003958 break;
3959#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003960#ifdef FEAT_SIGNS
3961 case CMD_sign:
3962 set_context_in_sign_cmd(xp, arg);
3963 break;
3964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 case CMD_bdelete:
3966 case CMD_bwipeout:
3967 case CMD_bunload:
3968 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3969 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003970 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 case CMD_buffer:
3972 case CMD_sbuffer:
3973 case CMD_checktime:
3974 xp->xp_context = EXPAND_BUFFERS;
3975 xp->xp_pattern = arg;
3976 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 case CMD_USER:
3979 case CMD_USER_BUF:
3980 if (compl != EXPAND_NOTHING)
3981 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003982 // XFILE: file names are handled above
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003983 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003985#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 if (compl == EXPAND_MENUS)
3987 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (compl == EXPAND_COMMANDS)
3990 return arg;
3991 if (compl == EXPAND_MAPPINGS)
3992 return set_context_in_map_cmd(xp, (char_u *)"map",
3993 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003994 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02003995 p = arg;
3996 while (*p)
3997 {
3998 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003999 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004000 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004001 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004002 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004003 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 xp->xp_pattern = arg;
4006 }
4007 xp->xp_context = compl;
4008 }
4009 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004010
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 case CMD_map: case CMD_noremap:
4012 case CMD_nmap: case CMD_nnoremap:
4013 case CMD_vmap: case CMD_vnoremap:
4014 case CMD_omap: case CMD_onoremap:
4015 case CMD_imap: case CMD_inoremap:
4016 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004017 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004018 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004019 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004020 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004022 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 case CMD_unmap:
4024 case CMD_nunmap:
4025 case CMD_vunmap:
4026 case CMD_ounmap:
4027 case CMD_iunmap:
4028 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004029 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004030 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004031 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004032 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004034 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004035 case CMD_mapclear:
4036 case CMD_nmapclear:
4037 case CMD_vmapclear:
4038 case CMD_omapclear:
4039 case CMD_imapclear:
4040 case CMD_cmapclear:
4041 case CMD_lmapclear:
4042 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004043 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004044 case CMD_xmapclear:
4045 xp->xp_context = EXPAND_MAPCLEAR;
4046 xp->xp_pattern = arg;
4047 break;
4048
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 case CMD_abbreviate: case CMD_noreabbrev:
4050 case CMD_cabbrev: case CMD_cnoreabbrev:
4051 case CMD_iabbrev: case CMD_inoreabbrev:
4052 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004053 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 case CMD_unabbreviate:
4055 case CMD_cunabbrev:
4056 case CMD_iunabbrev:
4057 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004058 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059#ifdef FEAT_MENU
4060 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4061 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4062 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4063 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4064 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4065 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4066 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004067 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 case CMD_tmenu: case CMD_tunmenu:
4069 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4070 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4071#endif
4072
4073 case CMD_colorscheme:
4074 xp->xp_context = EXPAND_COLORS;
4075 xp->xp_pattern = arg;
4076 break;
4077
4078 case CMD_compiler:
4079 xp->xp_context = EXPAND_COMPILER;
4080 xp->xp_pattern = arg;
4081 break;
4082
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004083 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004084 xp->xp_context = EXPAND_OWNSYNTAX;
4085 xp->xp_pattern = arg;
4086 break;
4087
4088 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004089 xp->xp_context = EXPAND_FILETYPE;
4090 xp->xp_pattern = arg;
4091 break;
4092
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004093 case CMD_packadd:
4094 xp->xp_context = EXPAND_PACKADD;
4095 xp->xp_pattern = arg;
4096 break;
4097
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004098#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004100 p = skiptowhite(arg);
4101 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
4103 xp->xp_context = EXPAND_LANGUAGE;
4104 xp->xp_pattern = arg;
4105 }
4106 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004107 {
4108 if ( STRNCMP(arg, "messages", p - arg) == 0
4109 || STRNCMP(arg, "ctype", p - arg) == 0
4110 || STRNCMP(arg, "time", p - arg) == 0)
4111 {
4112 xp->xp_context = EXPAND_LOCALES;
4113 xp->xp_pattern = skipwhite(p);
4114 }
4115 else
4116 xp->xp_context = EXPAND_NOTHING;
4117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 break;
4119#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004120#if defined(FEAT_PROFILE)
4121 case CMD_profile:
4122 set_context_in_profile_cmd(xp, arg);
4123 break;
4124#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004125 case CMD_behave:
4126 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004127 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004128 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004130 case CMD_messages:
4131 xp->xp_context = EXPAND_MESSAGES;
4132 xp->xp_pattern = arg;
4133 break;
4134
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004135#if defined(FEAT_CMDHIST)
4136 case CMD_history:
4137 xp->xp_context = EXPAND_HISTORY;
4138 xp->xp_pattern = arg;
4139 break;
4140#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004141#if defined(FEAT_PROFILE)
4142 case CMD_syntime:
4143 xp->xp_context = EXPAND_SYNTIME;
4144 xp->xp_pattern = arg;
4145 break;
4146#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004147
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004148 case CMD_argdelete:
4149 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4150 arg = xp->xp_pattern + 1;
4151 xp->xp_context = EXPAND_ARGLIST;
4152 xp->xp_pattern = arg;
4153 break;
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155#endif /* FEAT_CMDL_COMPL */
4156
4157 default:
4158 break;
4159 }
4160 return NULL;
4161}
4162
4163/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004164 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 *
4166 * Backslashed delimiters after / or ? will be skipped, and commands will
4167 * not be expanded between /'s and ?'s or after "'".
4168 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004169 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 * Returns the "cmd" pointer advanced to beyond the range.
4171 */
4172 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004173skip_range(
4174 char_u *cmd,
4175 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004177 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004179 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004181 if (*cmd == '\\')
4182 {
4183 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4184 ++cmd;
4185 else
4186 break;
4187 }
4188 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
4190 if (*++cmd == NUL && ctx != NULL)
4191 *ctx = EXPAND_NOTHING;
4192 }
4193 else if (*cmd == '/' || *cmd == '?')
4194 {
4195 delim = *cmd++;
4196 while (*cmd != NUL && *cmd != delim)
4197 if (*cmd++ == '\\' && *cmd != NUL)
4198 ++cmd;
4199 if (*cmd == NUL && ctx != NULL)
4200 *ctx = EXPAND_NOTHING;
4201 }
4202 if (*cmd != NUL)
4203 ++cmd;
4204 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004205
4206 /* Skip ":" and white space. */
4207 while (*cmd == ':')
4208 cmd = skipwhite(cmd + 1);
4209
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return cmd;
4211}
4212
4213/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004214 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 *
4216 * Set ptr to the next character after the part that was interpreted.
4217 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004218 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 *
4220 * Return MAXLNUM when no Ex address was found.
4221 */
4222 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004223get_address(
4224 exarg_T *eap UNUSED,
4225 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004226 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004227 int skip, // only skip the address, don't use it
4228 int silent, // no errors or side effects
4229 int to_other_file, // flag: may jump to other file
4230 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004232 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 int c;
4234 int i;
4235 long n;
4236 char_u *cmd;
4237 pos_T pos;
4238 pos_T *fp;
4239 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004240 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 cmd = skipwhite(*ptr);
4243 lnum = MAXLNUM;
4244 do
4245 {
4246 switch (*cmd)
4247 {
4248 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004249 ++cmd;
4250 switch (addr_type)
4251 {
4252 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004253 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 lnum = curwin->w_cursor.lnum;
4255 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004256 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004257 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004258 break;
4259 case ADDR_ARGUMENTS:
4260 lnum = curwin->w_arg_idx + 1;
4261 break;
4262 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004263 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004264 lnum = curbuf->b_fnum;
4265 break;
4266 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004267 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004268 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004269 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004270 case ADDR_NONE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004271 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004272 cmd = NULL;
4273 goto error;
4274 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004275 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004276#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004277 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004278#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004279 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004280 }
4281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004284 ++cmd;
4285 switch (addr_type)
4286 {
4287 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004288 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 lnum = curbuf->b_ml.ml_line_count;
4290 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004291 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004292 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004293 break;
4294 case ADDR_ARGUMENTS:
4295 lnum = ARGCOUNT;
4296 break;
4297 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004298 buf = lastbuf;
4299 while (buf->b_ml.ml_mfp == NULL)
4300 {
4301 if (buf->b_prev == NULL)
4302 break;
4303 buf = buf->b_prev;
4304 }
4305 lnum = buf->b_fnum;
4306 break;
4307 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004308 lnum = lastbuf->b_fnum;
4309 break;
4310 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004311 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004313 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004314 case ADDR_NONE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004315 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004316 cmd = NULL;
4317 goto error;
4318 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004319 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004320#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004321 lnum = qf_get_size(eap);
4322 if (lnum == 0)
4323 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004324#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004325 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004326 }
4327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004330 if (*++cmd == NUL)
4331 {
4332 cmd = NULL;
4333 goto error;
4334 }
4335 if (addr_type != ADDR_LINES)
4336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004337 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004338 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004339 goto error;
4340 }
4341 if (skip)
4342 ++cmd;
4343 else
4344 {
4345 /* Only accept a mark in another file when it is
4346 * used by itself: ":'M". */
4347 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4348 ++cmd;
4349 if (fp == (pos_T *)-1)
4350 /* Jumped to another file. */
4351 lnum = curwin->w_cursor.lnum;
4352 else
4353 {
4354 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
4356 cmd = NULL;
4357 goto error;
4358 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 lnum = fp->lnum;
4360 }
4361 }
4362 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 case '/':
4365 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 c = *cmd++;
4367 if (addr_type != ADDR_LINES)
4368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004369 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004370 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 goto error;
4372 }
4373 if (skip) /* skip "/pat/" */
4374 {
4375 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4376 if (*cmd == c)
4377 ++cmd;
4378 }
4379 else
4380 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004381 int flags;
4382
4383 pos = curwin->w_cursor; // save curwin->w_cursor
4384
4385 // When '/' or '?' follows another address, start from
4386 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 if (lnum != MAXLNUM)
4388 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004389
4390 // Start a forward search at the end of the line (unless
4391 // before the first line).
4392 // Start a backward search at the start of the line.
4393 // This makes sure we never match in the current
4394 // line, and can match anywhere in the
4395 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004396 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004397 curwin->w_cursor.col = MAXCOL;
4398 else
4399 curwin->w_cursor.col = 0;
4400 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004401 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4402 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 {
4404 curwin->w_cursor = pos;
4405 cmd = NULL;
4406 goto error;
4407 }
4408 lnum = curwin->w_cursor.lnum;
4409 curwin->w_cursor = pos;
4410 /* adjust command string pointer */
4411 cmd += searchcmdlen;
4412 }
4413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414
4415 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 ++cmd;
4417 if (addr_type != ADDR_LINES)
4418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004419 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004420 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 goto error;
4422 }
4423 if (*cmd == '&')
4424 i = RE_SUBST;
4425 else if (*cmd == '?' || *cmd == '/')
4426 i = RE_SEARCH;
4427 else
4428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004429 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 cmd = NULL;
4431 goto error;
4432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 if (!skip)
4435 {
4436 /*
4437 * When search follows another address, start from
4438 * there.
4439 */
4440 if (lnum != MAXLNUM)
4441 pos.lnum = lnum;
4442 else
4443 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 /*
4446 * Start the search just like for the above
4447 * do_search().
4448 */
4449 if (*cmd != '?')
4450 pos.col = MAXCOL;
4451 else
4452 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004453 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004454 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 *cmd == '?' ? BACKWARD : FORWARD,
4456 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004457 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004458 lnum = pos.lnum;
4459 else
4460 {
4461 cmd = NULL;
4462 goto error;
4463 }
4464 }
4465 ++cmd;
4466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
4468 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004469 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4470 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472
4473 for (;;)
4474 {
4475 cmd = skipwhite(cmd);
4476 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4477 break;
4478
4479 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 {
4481 switch (addr_type)
4482 {
4483 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004484 case ADDR_OTHER:
4485 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004486 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 break;
4488 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004489 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 break;
4491 case ADDR_ARGUMENTS:
4492 lnum = curwin->w_arg_idx + 1;
4493 break;
4494 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004495 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 lnum = curbuf->b_fnum;
4497 break;
4498 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004499 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004501 case ADDR_TABS_RELATIVE:
4502 lnum = 1;
4503 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004504 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004505#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004506 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004507#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004508 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004509 case ADDR_NONE:
4510 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 }
4512 }
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 if (VIM_ISDIGIT(*cmd))
4515 i = '+'; /* "number" is same as "+number" */
4516 else
4517 i = *cmd++;
4518 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4519 n = 1;
4520 else
4521 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004522
4523 if (addr_type == ADDR_TABS_RELATIVE)
4524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004525 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004526 cmd = NULL;
4527 goto error;
4528 }
4529 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004530 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004531 lnum = compute_buffer_local_count(
4532 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004534 {
4535#ifdef FEAT_FOLDING
4536 /* Relative line addressing, need to adjust for folded lines
4537 * now, but only do it after the first address. */
4538 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4539 && address_count >= 2)
4540 (void)hasFolding(lnum, NULL, &lnum);
4541#endif
4542 if (i == '-')
4543 lnum -= n;
4544 else
4545 lnum += n;
4546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 } while (*cmd == '/' || *cmd == '?');
4549
4550error:
4551 *ptr = cmd;
4552 return lnum;
4553}
4554
4555/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004556 * Get flags from an Ex command argument.
4557 */
4558 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004559get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004560{
4561 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4562 {
4563 if (*eap->arg == 'l')
4564 eap->flags |= EXFLAG_LIST;
4565 else if (*eap->arg == 'p')
4566 eap->flags |= EXFLAG_PRINT;
4567 else
4568 eap->flags |= EXFLAG_NR;
4569 eap->arg = skipwhite(eap->arg + 1);
4570 }
4571}
4572
4573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 * Function called for command which is Not Implemented. NI!
4575 */
4576 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004577ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
4579 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004580 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581}
4582
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004583#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584/*
4585 * Function called for script command which is Not Implemented. NI!
4586 * Skips over ":perl <<EOF" constructs.
4587 */
4588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004589ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590{
4591 if (!eap->skip)
4592 ex_ni(eap);
4593 else
4594 vim_free(script_get(eap, eap->arg));
4595}
4596#endif
4597
4598/*
4599 * Check range in Ex command for validity.
4600 * Return NULL when valid, error message when invalid.
4601 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004602 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004605 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if ( eap->line1 < 0
4607 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004608 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004609 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004610
4611 if (eap->argt & RANGE)
4612 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004613 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004614 {
4615 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004616 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004617#ifdef FEAT_DIFF
4618 + (eap->cmdidx == CMD_diffget)
4619#endif
4620 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004621 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004622 break;
4623 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004624 /* add 1 if ARGCOUNT is 0 */
4625 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004626 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004627 break;
4628 case ADDR_BUFFERS:
4629 if (eap->line1 < firstbuf->b_fnum
4630 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004631 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004632 break;
4633 case ADDR_LOADED_BUFFERS:
4634 buf = firstbuf;
4635 while (buf->b_ml.ml_mfp == NULL)
4636 {
4637 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004638 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004639 buf = buf->b_next;
4640 }
4641 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004642 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004643 buf = lastbuf;
4644 while (buf->b_ml.ml_mfp == NULL)
4645 {
4646 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004647 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004648 buf = buf->b_prev;
4649 }
4650 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004651 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004652 break;
4653 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004654 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004655 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004656 break;
4657 case ADDR_TABS:
4658 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004659 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004660 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004661 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004662 case ADDR_OTHER:
4663 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004664 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004665 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004666#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004667 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004668 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004669#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004670 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004671 case ADDR_NONE:
4672 // Will give an error elsewhere.
4673 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004674 }
4675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 return NULL;
4677}
4678
4679/*
4680 * Correct the range for zero line number, if required.
4681 */
4682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004683correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684{
4685 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4686 {
4687 if (eap->line1 == 0)
4688 eap->line1 = 1;
4689 if (eap->line2 == 0)
4690 eap->line2 = 1;
4691 }
4692}
4693
Bram Moolenaar748bf032005-02-02 23:04:36 +00004694#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004695/*
4696 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4697 * pattern. Otherwise return eap->arg.
4698 */
4699 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004700skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004701{
4702 char_u *p = eap->arg;
4703
Bram Moolenaara37420f2006-02-04 22:37:47 +00004704 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4705 || eap->cmdidx == CMD_vimgrepadd
4706 || eap->cmdidx == CMD_lvimgrepadd
4707 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004708 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004709 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004710 if (p == NULL)
4711 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004712 }
4713 return p;
4714}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004715
4716/*
4717 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4718 * in the command line, so that things like % get expanded.
4719 */
4720 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004721replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004722{
4723 char_u *new_cmdline;
4724 char_u *program;
4725 char_u *pos;
4726 char_u *ptr;
4727 int len;
4728 int i;
4729
4730 /*
4731 * Don't do it when ":vimgrep" is used for ":grep".
4732 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004733 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4734 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4735 || eap->cmdidx == CMD_grepadd
4736 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004737 && !grep_internal(eap->cmdidx))
4738 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004739 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4740 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004741 {
4742 if (*curbuf->b_p_gp == NUL)
4743 program = p_gp;
4744 else
4745 program = curbuf->b_p_gp;
4746 }
4747 else
4748 {
4749 if (*curbuf->b_p_mp == NUL)
4750 program = p_mp;
4751 else
4752 program = curbuf->b_p_mp;
4753 }
4754
4755 p = skipwhite(p);
4756
4757 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4758 {
4759 /* replace $* by given arguments */
4760 i = 1;
4761 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4762 ++i;
4763 len = (int)STRLEN(p);
4764 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4765 if (new_cmdline == NULL)
4766 return NULL; /* out of memory */
4767 ptr = new_cmdline;
4768 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4769 {
4770 i = (int)(pos - program);
4771 STRNCPY(ptr, program, i);
4772 STRCPY(ptr += i, p);
4773 ptr += len;
4774 program = pos + 2;
4775 }
4776 STRCPY(ptr, program);
4777 }
4778 else
4779 {
4780 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4781 if (new_cmdline == NULL)
4782 return NULL; /* out of memory */
4783 STRCPY(new_cmdline, program);
4784 STRCAT(new_cmdline, " ");
4785 STRCAT(new_cmdline, p);
4786 }
4787 msg_make(p);
4788
4789 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4790 vim_free(*cmdlinep);
4791 *cmdlinep = new_cmdline;
4792 p = new_cmdline;
4793 }
4794 return p;
4795}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004796#endif
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798/*
4799 * Expand file name in Ex command argument.
4800 * Return FAIL for failure, OK otherwise.
4801 */
4802 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004803expand_filename(
4804 exarg_T *eap,
4805 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004806 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807{
4808 int has_wildcards; /* need to expand wildcards */
4809 char_u *repl;
4810 int srclen;
4811 char_u *p;
4812 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004813 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814
Bram Moolenaar748bf032005-02-02 23:04:36 +00004815#ifdef FEAT_QUICKFIX
4816 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4817 p = skip_grep_pat(eap);
4818#else
4819 p = eap->arg;
4820#endif
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 /*
4823 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4824 * the file name contains a wildcard it should not cause expanding.
4825 * (it will be expanded anyway if there is a wildcard before replacing).
4826 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004827 has_wildcards = mch_has_wildcard(p);
4828 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004830#ifdef FEAT_EVAL
4831 /* Skip over `=expr`, wildcards in it are not expanded. */
4832 if (p[0] == '`' && p[1] == '=')
4833 {
4834 p += 2;
4835 (void)skip_expr(&p);
4836 if (*p == '`')
4837 ++p;
4838 continue;
4839 }
4840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 /*
4842 * Quick check if this cannot be the start of a special string.
4843 * Also removes backslash before '%', '#' and '<'.
4844 */
4845 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4846 {
4847 ++p;
4848 continue;
4849 }
4850
4851 /*
4852 * Try to find a match at this position.
4853 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004854 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4855 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 if (*errormsgp != NULL) /* error detected */
4857 return FAIL;
4858 if (repl == NULL) /* no match found */
4859 {
4860 p += srclen;
4861 continue;
4862 }
4863
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004864 /* Wildcards won't be expanded below, the replacement is taken
4865 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4866 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4867 {
4868 char_u *l = repl;
4869
4870 repl = expand_env_save(repl);
4871 vim_free(l);
4872 }
4873
Bram Moolenaar63b92542007-03-27 14:57:09 +00004874 /* Need to escape white space et al. with a backslash.
4875 * Don't do this for:
4876 * - replacement that already has been escaped: "##"
4877 * - shell commands (may have to use quotes instead).
4878 * - non-unix systems when there is a single argument (spaces don't
4879 * separate arguments then).
4880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004882 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 && eap->cmdidx != CMD_grep
4885 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004886 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004887 && eap->cmdidx != CMD_lgrep
4888 && eap->cmdidx != CMD_lgrepadd
4889 && eap->cmdidx != CMD_lmake
4890 && eap->cmdidx != CMD_make
4891 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892#ifndef UNIX
4893 && !(eap->argt & NOSPC)
4894#endif
4895 )
4896 {
4897 char_u *l;
4898#ifdef BACKSLASH_IN_FILENAME
4899 /* Don't escape a backslash here, because rem_backslash() doesn't
4900 * remove it later. */
4901 static char_u *nobslash = (char_u *)" \t\"|";
4902# define ESCAPE_CHARS nobslash
4903#else
4904# define ESCAPE_CHARS escape_chars
4905#endif
4906
4907 for (l = repl; *l; ++l)
4908 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4909 {
4910 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4911 if (l != NULL)
4912 {
4913 vim_free(repl);
4914 repl = l;
4915 }
4916 break;
4917 }
4918 }
4919
4920 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004921 if ((eap->usefilter || eap->cmdidx == CMD_bang
4922 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004923 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
4925 char_u *l;
4926
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004927 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (l != NULL)
4929 {
4930 vim_free(repl);
4931 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
4933 }
4934
4935 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4936 vim_free(repl);
4937 if (p == NULL)
4938 return FAIL;
4939 }
4940
4941 /*
4942 * One file argument: Expand wildcards.
4943 * Don't do this with ":r !command" or ":w !command".
4944 */
4945 if ((eap->argt & NOSPC) && !eap->usefilter)
4946 {
4947 /*
4948 * May do this twice:
4949 * 1. Replace environment variables.
4950 * 2. Replace any other wildcards, remove backslashes.
4951 */
4952 for (n = 1; n <= 2; ++n)
4953 {
4954 if (n == 2)
4955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 /*
4957 * Halve the number of backslashes (this is Vi compatible).
4958 * For Unix and OS/2, when wildcards are expanded, this is
4959 * done by ExpandOne() below.
4960 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004961#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 if (!has_wildcards)
4963#endif
4964 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966
4967 if (has_wildcards)
4968 {
4969 if (n == 1)
4970 {
4971 /*
4972 * First loop: May expand environment variables. This
4973 * can be done much faster with expand_env() than with
4974 * something else (e.g., calling a shell).
4975 * After expanding environment variables, check again
4976 * if there are still wildcards present.
4977 */
4978 if (vim_strchr(eap->arg, '$') != NULL
4979 || vim_strchr(eap->arg, '~') != NULL)
4980 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004981 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004982 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 has_wildcards = mch_has_wildcard(NameBuff);
4984 p = NameBuff;
4985 }
4986 else
4987 p = NULL;
4988 }
4989 else /* n == 2 */
4990 {
4991 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004992 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
4994 ExpandInit(&xpc);
4995 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004996 if (p_wic)
4997 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004999 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 if (p == NULL)
5001 return FAIL;
5002 }
5003 if (p != NULL)
5004 {
5005 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5006 p, cmdlinep);
5007 if (n == 2) /* p came from ExpandOne() */
5008 vim_free(p);
5009 }
5010 }
5011 }
5012 }
5013 return OK;
5014}
5015
5016/*
5017 * Replace part of the command line, keeping eap->cmd, eap->arg and
5018 * eap->nextcmd correct.
5019 * "src" points to the part that is to be replaced, of length "srclen".
5020 * "repl" is the replacement string.
5021 * Returns a pointer to the character after the replaced string.
5022 * Returns NULL for failure.
5023 */
5024 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005025repl_cmdline(
5026 exarg_T *eap,
5027 char_u *src,
5028 int srclen,
5029 char_u *repl,
5030 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031{
5032 int len;
5033 int i;
5034 char_u *new_cmdline;
5035
5036 /*
5037 * The new command line is build in new_cmdline[].
5038 * First allocate it.
5039 * Careful: a "+cmd" argument may have been NUL terminated.
5040 */
5041 len = (int)STRLEN(repl);
5042 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005043 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5045 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5046 return NULL; /* out of memory! */
5047
5048 /*
5049 * Copy the stuff before the expanded part.
5050 * Copy the expanded stuff.
5051 * Copy what came after the expanded part.
5052 * Copy the next commands, if there are any.
5053 */
5054 i = (int)(src - *cmdlinep); /* length of part before match */
5055 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 mch_memmove(new_cmdline + i, repl, (size_t)len);
5058 i += len; /* remember the end of the string */
5059 STRCPY(new_cmdline + i, src + srclen);
5060 src = new_cmdline + i; /* remember where to continue */
5061
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005062 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064 i = (int)STRLEN(new_cmdline) + 1;
5065 STRCPY(new_cmdline + i, eap->nextcmd);
5066 eap->nextcmd = new_cmdline + i;
5067 }
5068 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5069 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5070 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5071 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5072 vim_free(*cmdlinep);
5073 *cmdlinep = new_cmdline;
5074
5075 return src;
5076}
5077
5078/*
5079 * Check for '|' to separate commands and '"' to start comments.
5080 */
5081 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005082separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
5084 char_u *p;
5085
Bram Moolenaar86b68352004-12-27 21:59:20 +00005086#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005087 p = skip_grep_pat(eap);
5088#else
5089 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005090#endif
5091
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005092 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {
5094 if (*p == Ctrl_V)
5095 {
5096 if (eap->argt & (USECTRLV | XFILE))
5097 ++p; /* skip CTRL-V and next char */
5098 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005099 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005100 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (*p == NUL) /* stop at NUL after CTRL-V */
5102 break;
5103 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005104
5105#ifdef FEAT_EVAL
5106 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005107 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005108 {
5109 p += 2;
5110 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005111 }
5112#endif
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 /* Check for '"': start of comment or '|': next command */
5115 /* :@" and :*" do not start a comment!
5116 * :redir @" doesn't either. */
5117 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5118 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5119 || p != eap->arg)
5120 && (eap->cmdidx != CMD_redir
5121 || p != eap->arg + 1 || p[-1] != '@'))
5122 || *p == '|' || *p == '\n')
5123 {
5124 /*
5125 * We remove the '\' before the '|', unless USECTRLV is used
5126 * AND 'b' is present in 'cpoptions'.
5127 */
5128 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5129 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5130 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005131 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 --p;
5133 }
5134 else
5135 {
5136 eap->nextcmd = check_nextcmd(p);
5137 *p = NUL;
5138 break;
5139 }
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5144 del_trailing_spaces(eap->arg);
5145}
5146
5147/*
5148 * get + command from ex argument
5149 */
5150 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005151getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 char_u *arg = *argp;
5154 char_u *command = NULL;
5155
5156 if (*arg == '+') /* +[command] */
5157 {
5158 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005159 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 command = dollar_command;
5161 else
5162 {
5163 command = arg;
5164 arg = skip_cmd_arg(command, TRUE);
5165 if (*arg != NUL)
5166 *arg++ = NUL; /* terminate command with NUL */
5167 }
5168
5169 arg = skipwhite(arg); /* skip over spaces */
5170 *argp = arg;
5171 }
5172 return command;
5173}
5174
5175/*
5176 * Find end of "+command" argument. Skip over "\ " and "\\".
5177 */
5178 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005179skip_cmd_arg(
5180 char_u *p,
5181 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
5183 while (*p && !vim_isspace(*p))
5184 {
5185 if (*p == '\\' && p[1] != NUL)
5186 {
5187 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005188 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 else
5190 ++p;
5191 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005192 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 return p;
5195}
5196
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005197 int
5198get_bad_opt(char_u *p, exarg_T *eap)
5199{
5200 if (STRICMP(p, "keep") == 0)
5201 eap->bad_char = BAD_KEEP;
5202 else if (STRICMP(p, "drop") == 0)
5203 eap->bad_char = BAD_DROP;
5204 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5205 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005206 else
5207 return FAIL;
5208 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005209}
5210
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211/*
5212 * Get "++opt=arg" argument.
5213 * Return FAIL or OK.
5214 */
5215 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005216getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
5218 char_u *arg = eap->arg + 2;
5219 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005220 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222
5223 /* ":edit ++[no]bin[ary] file" */
5224 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5225 {
5226 if (*arg == 'n')
5227 {
5228 arg += 2;
5229 eap->force_bin = FORCE_NOBIN;
5230 }
5231 else
5232 eap->force_bin = FORCE_BIN;
5233 if (!checkforcmd(&arg, "binary", 3))
5234 return FAIL;
5235 eap->arg = skipwhite(arg);
5236 return OK;
5237 }
5238
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005239 /* ":read ++edit file" */
5240 if (STRNCMP(arg, "edit", 4) == 0)
5241 {
5242 eap->read_edit = TRUE;
5243 eap->arg = skipwhite(arg + 4);
5244 return OK;
5245 }
5246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 if (STRNCMP(arg, "ff", 2) == 0)
5248 {
5249 arg += 2;
5250 pp = &eap->force_ff;
5251 }
5252 else if (STRNCMP(arg, "fileformat", 10) == 0)
5253 {
5254 arg += 10;
5255 pp = &eap->force_ff;
5256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 else if (STRNCMP(arg, "enc", 3) == 0)
5258 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005259 if (STRNCMP(arg, "encoding", 8) == 0)
5260 arg += 8;
5261 else
5262 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 pp = &eap->force_enc;
5264 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005265 else if (STRNCMP(arg, "bad", 3) == 0)
5266 {
5267 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005268 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 if (pp == NULL || *arg != '=')
5272 return FAIL;
5273
5274 ++arg;
5275 *pp = (int)(arg - eap->cmd);
5276 arg = skip_cmd_arg(arg, FALSE);
5277 eap->arg = skipwhite(arg);
5278 *arg = NUL;
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (pp == &eap->force_ff)
5281 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5283 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005284 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005286 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 {
5288 /* Make 'fileencoding' lower case. */
5289 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5290 *p = TOLOWER_ASC(*p);
5291 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005292 else
5293 {
5294 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5295 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005296 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005297 return FAIL;
5298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
5300 return OK;
5301}
5302
5303/*
5304 * ":abbreviate" and friends.
5305 */
5306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005307ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
5309 do_exmap(eap, TRUE); /* almost the same as mapping */
5310}
5311
5312/*
5313 * ":map" and friends.
5314 */
5315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005316ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 /*
5319 * If we are sourcing .exrc or .vimrc in current directory we
5320 * print the mappings for security reasons.
5321 */
5322 if (secure)
5323 {
5324 secure = 2;
5325 msg_outtrans(eap->cmd);
5326 msg_putchar('\n');
5327 }
5328 do_exmap(eap, FALSE);
5329}
5330
5331/*
5332 * ":unmap" and friends.
5333 */
5334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005335ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337 do_exmap(eap, FALSE);
5338}
5339
5340/*
5341 * ":mapclear" and friends.
5342 */
5343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005344ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
5346 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5347}
5348
5349/*
5350 * ":abclear" and friends.
5351 */
5352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005353ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
5355 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5356}
5357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005362 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 * directory for security reasons.
5364 */
5365 if (secure)
5366 {
5367 secure = 2;
5368 eap->errmsg = e_curdir;
5369 }
5370 else if (eap->cmdidx == CMD_autocmd)
5371 do_autocmd(eap->arg, eap->forceit);
5372 else
5373 do_augroup(eap->arg, eap->forceit);
5374}
5375
5376/*
5377 * ":doautocmd": Apply the automatic commands to the current buffer.
5378 */
5379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005380ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005382 char_u *arg = eap->arg;
5383 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005384 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005385
Bram Moolenaar1610d052016-06-09 22:53:01 +02005386 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5387 /* Only when there is no <nomodeline>. */
5388 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005389 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392/*
5393 * :[N]bunload[!] [N] [bufname] unload buffer
5394 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5395 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5396 */
5397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005398ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399{
5400 eap->errmsg = do_bufdel(
5401 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5402 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5403 : DOBUF_UNLOAD, eap->arg,
5404 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5405}
5406
5407/*
5408 * :[N]buffer [N] to buffer N
5409 * :[N]sbuffer [N] to buffer N
5410 */
5411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005412ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413{
5414 if (*eap->arg)
5415 eap->errmsg = e_trailing;
5416 else
5417 {
5418 if (eap->addr_count == 0) /* default is current buffer */
5419 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5420 else
5421 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005422 if (eap->do_ecmd_cmd != NULL)
5423 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
5425}
5426
5427/*
5428 * :[N]bmodified [N] to next mod. buffer
5429 * :[N]sbmodified [N] to next mod. buffer
5430 */
5431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005432ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
5434 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005435 if (eap->do_ecmd_cmd != NULL)
5436 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437}
5438
5439/*
5440 * :[N]bnext [N] to next buffer
5441 * :[N]sbnext [N] split and to next buffer
5442 */
5443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005444ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
5446 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005447 if (eap->do_ecmd_cmd != NULL)
5448 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449}
5450
5451/*
5452 * :[N]bNext [N] to previous buffer
5453 * :[N]bprevious [N] to previous buffer
5454 * :[N]sbNext [N] split and to previous buffer
5455 * :[N]sbprevious [N] split and to previous buffer
5456 */
5457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005458ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459{
5460 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005461 if (eap->do_ecmd_cmd != NULL)
5462 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463}
5464
5465/*
5466 * :brewind to first buffer
5467 * :bfirst to first buffer
5468 * :sbrewind split and to first buffer
5469 * :sbfirst split and to first buffer
5470 */
5471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005472ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473{
5474 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005475 if (eap->do_ecmd_cmd != NULL)
5476 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477}
5478
5479/*
5480 * :blast to last buffer
5481 * :sblast split and to last buffer
5482 */
5483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005487 if (eap->do_ecmd_cmd != NULL)
5488 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
5491 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005492ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 return (c == NUL || c == '|' || c == '"' || c == '\n');
5495}
5496
5497#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5498 || defined(PROTO)
5499/*
5500 * Return the next command, after the first '|' or '\n'.
5501 * Return NULL if not found.
5502 */
5503 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005504find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505{
5506 while (*p != '|' && *p != '\n')
5507 {
5508 if (*p == NUL)
5509 return NULL;
5510 ++p;
5511 }
5512 return (p + 1);
5513}
5514#endif
5515
5516/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005517 * Check if *p is a separator between Ex commands, skipping over white space.
5518 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 */
5520 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005521check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005523 char_u *s = skipwhite(p);
5524
5525 if (*s == '|' || *s == '\n')
5526 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 else
5528 return NULL;
5529}
5530
5531/*
5532 * - if there are more files to edit
5533 * - and this is the last window
5534 * - and forceit not used
5535 * - and not repeated twice on a row
5536 * return FAIL and give error message if 'message' TRUE
5537 * return OK otherwise
5538 */
5539 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540check_more(
5541 int message, /* when FALSE check only, no messages */
5542 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5545
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005546 if (!forceit && only_one_window()
5547 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
5549 if (message)
5550 {
5551#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5552 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5553 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005554 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005556 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5557 NGETTEXT("%d more file to edit. Quit anyway?",
5558 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5560 return OK;
5561 return FAIL;
5562 }
5563#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005564 semsg(NGETTEXT("E173: %d more file to edit",
5565 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 quitmore = 2; /* next try to quit is allowed */
5567 }
5568 return FAIL;
5569 }
5570 return OK;
5571}
5572
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005573#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574/*
5575 * Function given to ExpandGeneric() to obtain the list of command names.
5576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
5580 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 return cmdnames[idx].cmd_name;
5583}
5584#endif
5585
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005587ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
Bram Moolenaare6850792010-05-14 15:28:44 +02005589 if (*eap->arg == NUL)
5590 {
5591#ifdef FEAT_EVAL
5592 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5593 char_u *p = NULL;
5594
5595 if (expr != NULL)
5596 {
5597 ++emsg_off;
5598 p = eval_to_string(expr, NULL, FALSE);
5599 --emsg_off;
5600 vim_free(expr);
5601 }
5602 if (p != NULL)
5603 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005604 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005605 vim_free(p);
5606 }
5607 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005608 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005609#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005610 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005611#endif
5612 }
5613 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005614 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005615
5616#ifdef FEAT_VTP
5617 else if (has_vtp_working())
5618 {
5619 // background color change requires clear + redraw
5620 update_screen(CLEAR);
5621 redrawcmd();
5622 }
5623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624}
5625
5626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005627ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005630 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 do_highlight(eap->arg, eap->forceit, FALSE);
5632}
5633
5634
5635/*
5636 * Call this function if we thought we were going to exit, but we won't
5637 * (because of an error). May need to restore the terminal mode.
5638 */
5639 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005640not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 exiting = FALSE;
5643 settmode(TMODE_RAW);
5644}
5645
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005646 static int
5647before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5648{
5649 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5650
5651 /* Bail out when autocommands closed the window.
5652 * Refuse to quit when the buffer in the last window is being closed (can
5653 * only happen in autocommands). */
5654 if (!win_valid(wp)
5655 || curbuf_locked()
5656 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5657 return TRUE;
5658
5659 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5660 {
5661 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5662 /* Refuse to quit when locked or when the buffer in the last window is
5663 * being closed (can only happen in autocommands). */
5664 if (curbuf_locked()
5665 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5666 return TRUE;
5667 }
5668
5669 return FALSE;
5670}
5671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005673 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005674 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 */
5676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005679 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#ifdef FEAT_CMDWIN
5682 if (cmdwin_type != 0)
5683 {
5684 cmdwin_result = Ctrl_C;
5685 return;
5686 }
5687#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005688 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005689 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005690 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005691 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005692 return;
5693 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005694 if (eap->addr_count > 0)
5695 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005696 int wnr = eap->line2;
5697
5698 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5699 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005700 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005701 }
5702 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005703 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005704
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005705 /* Refuse to quit when locked. */
5706 if (curbuf_locked())
5707 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005708
5709 /* Trigger QuitPre and maybe ExitPre */
5710 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005711 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
5713#ifdef FEAT_NETBEANS_INTG
5714 netbeansForcedQuit = eap->forceit;
5715#endif
5716
5717 /*
5718 * If there are more files or windows we won't exit.
5719 */
5720 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5721 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005722 if ((!buf_hide(wp->w_buffer)
5723 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005724 | (eap->forceit ? CCGD_FORCEIT : 0)
5725 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005727 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
5729 not_exiting();
5730 }
5731 else
5732 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005733 /* quit last window
5734 * Note: only_one_window() returns true, even so a help window is
5735 * still open. In that case only quit, if no address has been
5736 * specified. Example:
5737 * :h|wincmd w|1q - don't quit
5738 * :h|wincmd w|q - quit
5739 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005740 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005742 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005743#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005747 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749}
5750
5751/*
5752 * ":cquit".
5753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 getout(1); /* this does not always pass on the exit code to the Manx
5758 compiler. why? */
5759}
5760
5761/*
5762 * ":qall": try to quit all windows
5763 */
5764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005765ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767# ifdef FEAT_CMDWIN
5768 if (cmdwin_type != 0)
5769 {
5770 if (eap->forceit)
5771 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5772 else
5773 cmdwin_result = K_XF2;
5774 return;
5775 }
5776# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005777
5778 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005779 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005780 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005781 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005782 return;
5783 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005784
5785 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005786 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005789 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 getout(0);
5791 not_exiting();
5792}
5793
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794/*
5795 * ":close": close current window, unless it is the last one
5796 */
5797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005798ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005800 win_T *win;
5801 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005802#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005804 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005806#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005807 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005808 {
5809 if (eap->addr_count == 0)
5810 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005811 else
5812 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005813 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005814 {
5815 winnr++;
5816 if (winnr == eap->line2)
5817 break;
5818 }
5819 if (win == NULL)
5820 win = lastwin;
5821 ex_win_close(eap->forceit, win, NULL);
5822 }
5823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
5825
Bram Moolenaar4033c552017-09-16 20:54:51 +02005826#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005827/*
5828 * ":pclose": Close any preview window.
5829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005832{
5833 win_T *win;
5834
Bram Moolenaar29323592016-07-24 22:04:11 +02005835 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005836 if (win->w_p_pvw)
5837 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005838 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005839 break;
5840 }
5841}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005842#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005843
Bram Moolenaarf740b292006-02-16 22:11:02 +00005844/*
5845 * Close window "win" and take care of handling closing the last window for a
5846 * modified buffer.
5847 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005849ex_win_close(
5850 int forceit,
5851 win_T *win,
5852 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853{
5854 int need_hide;
5855 buf_T *buf = win->w_buffer;
5856
5857 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005858 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005860#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 if ((p_confirm || cmdmod.confirm) && p_write)
5862 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005863 bufref_T bufref;
5864
5865 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005867 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 return;
5869 need_hide = FALSE;
5870 }
5871 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005874 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 return;
5876 }
5877 }
5878
Bram Moolenaar4033c552017-09-16 20:54:51 +02005879#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005881#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005882
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005884 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005885 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005886 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005887 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888}
5889
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005891 * Handle the argument for a tabpage related ex command.
5892 * Returns a tabpage number.
5893 * When an error is encountered then eap->errmsg is set.
5894 */
5895 static int
5896get_tabpage_arg(exarg_T *eap)
5897{
5898 int tab_number;
5899 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5900
5901 if (eap->arg && *eap->arg != NUL)
5902 {
5903 char_u *p = eap->arg;
5904 char_u *p_save;
5905 int relative = 0; /* argument +N/-N means: go to N places to the
5906 * right/left relative to the current position. */
5907
5908 if (*p == '-')
5909 {
5910 relative = -1;
5911 p++;
5912 }
5913 else if (*p == '+')
5914 {
5915 relative = 1;
5916 p++;
5917 }
5918
5919 p_save = p;
5920 tab_number = getdigits(&p);
5921
5922 if (relative == 0)
5923 {
5924 if (STRCMP(p, "$") == 0)
5925 tab_number = LAST_TAB_NR;
5926 else if (p == p_save || *p_save == '-' || *p != NUL
5927 || tab_number > LAST_TAB_NR)
5928 {
5929 /* No numbers as argument. */
5930 eap->errmsg = e_invarg;
5931 goto theend;
5932 }
5933 }
5934 else
5935 {
5936 if (*p_save == NUL)
5937 tab_number = 1;
5938 else if (p == p_save || *p_save == '-' || *p != NUL
5939 || tab_number == 0)
5940 {
5941 /* No numbers as argument. */
5942 eap->errmsg = e_invarg;
5943 goto theend;
5944 }
5945 tab_number = tab_number * relative + tabpage_index(curtab);
5946 if (!unaccept_arg0 && relative == -1)
5947 --tab_number;
5948 }
5949 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5950 eap->errmsg = e_invarg;
5951 }
5952 else if (eap->addr_count > 0)
5953 {
5954 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005955 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005956 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005957 tab_number = 0;
5958 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005959 else
5960 {
5961 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005962 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005963 {
5964 --tab_number;
5965 if (tab_number < unaccept_arg0)
5966 eap->errmsg = e_invarg;
5967 }
5968 }
5969 }
5970 else
5971 {
5972 switch (eap->cmdidx)
5973 {
5974 case CMD_tabnext:
5975 tab_number = tabpage_index(curtab) + 1;
5976 if (tab_number > LAST_TAB_NR)
5977 tab_number = 1;
5978 break;
5979 case CMD_tabmove:
5980 tab_number = LAST_TAB_NR;
5981 break;
5982 default:
5983 tab_number = tabpage_index(curtab);
5984 }
5985 }
5986
5987theend:
5988 return tab_number;
5989}
5990
5991/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005992 * ":tabclose": close current tab page, unless it is the last one.
5993 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 */
5995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005996ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005998 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005999 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006000
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006001# ifdef FEAT_CMDWIN
6002 if (cmdwin_type != 0)
6003 cmdwin_result = K_IGNORE;
6004 else
6005# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006006 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006007 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006008 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006010 tab_number = get_tabpage_arg(eap);
6011 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006012 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006013 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006014 if (tp == NULL)
6015 {
6016 beep_flush();
6017 return;
6018 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006019 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006020 {
6021 tabpage_close_other(tp, eap->forceit);
6022 return;
6023 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006024 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006025 tabpage_close(eap->forceit);
6026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006028}
6029
6030/*
6031 * ":tabonly": close all tab pages except the current one
6032 */
6033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006034ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006035{
6036 tabpage_T *tp;
6037 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006038 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006039
6040# ifdef FEAT_CMDWIN
6041 if (cmdwin_type != 0)
6042 cmdwin_result = K_IGNORE;
6043 else
6044# endif
6045 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006046 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006047 else
6048 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006049 tab_number = get_tabpage_arg(eap);
6050 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006051 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006052 goto_tabpage(tab_number);
6053 /* Repeat this up to a 1000 times, because autocommands may
6054 * mess up the lists. */
6055 for (done = 0; done < 1000; ++done)
6056 {
6057 FOR_ALL_TABPAGES(tp)
6058 if (tp->tp_topframe != topframe)
6059 {
6060 tabpage_close_other(tp, eap->forceit);
6061 /* if we failed to close it quit */
6062 if (valid_tabpage(tp))
6063 done = 1000;
6064 /* start over, "tp" is now invalid */
6065 break;
6066 }
6067 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006068 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006069 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006070 }
6071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073
6074/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006075 * Close the current tab page.
6076 */
6077 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006078tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006079{
6080 /* First close all the windows but the current one. If that worked then
6081 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006082 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006083 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006084 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006085 ex_win_close(forceit, curwin, NULL);
6086# ifdef FEAT_GUI
6087 need_mouse_correct = TRUE;
6088# endif
6089}
6090
6091/*
6092 * Close tab page "tp", which is not the current tab page.
6093 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006094 * Also takes care of the tab pages line disappearing when closing the
6095 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006096 */
6097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006098tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006099{
6100 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006101 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006102 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006103
6104 /* Limit to 1000 windows, autocommands may add a window while we close
6105 * one. OK, so I'm paranoid... */
6106 while (++done < 1000)
6107 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006108 wp = tp->tp_firstwin;
6109 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006110
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006111 /* Autocommands may delete the tab page under our fingers and we may
6112 * fail to close a window with a modified buffer. */
6113 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006114 break;
6115 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006116
Bram Moolenaar29323592016-07-24 22:04:11 +02006117 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006118
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006119 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006120 if (h != tabline_height())
6121 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006122}
6123
6124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 * ":only".
6126 */
6127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006130 win_T *wp;
6131 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132# ifdef FEAT_GUI
6133 need_mouse_correct = TRUE;
6134# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006135 if (eap->addr_count > 0)
6136 {
6137 wnr = eap->line2;
6138 for (wp = firstwin; --wnr > 0; )
6139 {
6140 if (wp->w_next == NULL)
6141 break;
6142 else
6143 wp = wp->w_next;
6144 }
6145 win_goto(wp);
6146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 close_others(TRUE, eap->forceit);
6148}
6149
6150/*
6151 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006152 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006154 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006155ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
6157 if (eap->addr_count == 0)
6158 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006159 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006163ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006165 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006166 if (!eap->skip)
6167 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006168#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006169 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006170#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006171 if (eap->addr_count == 0)
6172 win_close(curwin, FALSE); /* don't free buffer */
6173 else
6174 {
6175 int winnr = 0;
6176 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006177
Bram Moolenaar2256c992016-11-15 21:17:07 +01006178 FOR_ALL_WINDOWS(win)
6179 {
6180 winnr++;
6181 if (winnr == eap->line2)
6182 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006183 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006184 if (win == NULL)
6185 win = lastwin;
6186 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 }
6189}
6190
6191/*
6192 * ":stop" and ":suspend": Suspend Vim.
6193 */
6194 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006195ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 /*
6198 * Disallow suspending for "rvim".
6199 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006200 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 {
6202 if (!eap->forceit)
6203 autowrite_all();
6204 windgoto((int)Rows - 1, 0);
6205 out_char('\n');
6206 out_flush();
6207 stoptermcap();
6208 out_flush(); /* needed for SUN to restore xterm buffer */
6209#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006210 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211#endif
6212 ui_suspend(); /* call machine specific function */
6213#ifdef FEAT_TITLE
6214 maketitle();
6215 resettitle(); /* force updating the title */
6216#endif
6217 starttermcap();
6218 scroll_start(); /* scroll screen before redrawing */
6219 redraw_later_clear();
6220 shell_resized(); /* may have resized window */
6221 }
6222}
6223
6224/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006225 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 */
6227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006228ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230#ifdef FEAT_CMDWIN
6231 if (cmdwin_type != 0)
6232 {
6233 cmdwin_result = Ctrl_C;
6234 return;
6235 }
6236#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006237 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006238 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006239 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006240 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006241 return;
6242 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006243
6244 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006245 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246
6247 /*
6248 * if more files or windows we won't exit
6249 */
6250 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6251 exiting = TRUE;
6252 if ( ((eap->cmdidx == CMD_wq
6253 || curbufIsChanged())
6254 && do_write(eap) == FAIL)
6255 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006256 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
6258 not_exiting();
6259 }
6260 else
6261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006264 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265# ifdef FEAT_GUI
6266 need_mouse_correct = TRUE;
6267# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006268 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006269 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271}
6272
6273/*
6274 * ":print", ":list", ":number".
6275 */
6276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006279 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006280 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006283 for ( ;!got_int; ui_breakcheck())
6284 {
6285 print_line(eap->line1,
6286 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6287 || (eap->flags & EXFLAG_NR)),
6288 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6289 if (++eap->line1 > eap->line2)
6290 break;
6291 out_flush(); /* show one line at a time */
6292 }
6293 setpcmark();
6294 /* put cursor at last line */
6295 curwin->w_cursor.lnum = eap->line2;
6296 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
6302#ifdef FEAT_BYTEOFF
6303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006304ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 goto_byte(eap->line2);
6307}
6308#endif
6309
6310/*
6311 * ":shell".
6312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006314ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
6316 do_shell(NULL, 0);
6317}
6318
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006319#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006321static int drop_busy = FALSE;
6322static int drop_filec;
6323static char_u **drop_filev = NULL;
6324static int drop_split;
6325static void (*drop_callback)(void *);
6326static void *drop_cookie;
6327
6328 static void
6329handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 exarg_T ea;
6332 int save_msg_scroll = msg_scroll;
6333
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006334 // Setting the argument list may cause screen updates and being called
6335 // recursively. Avoid that by setting drop_busy.
6336 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337
6338 /* Check whether the current buffer is changed. If so, we will need
6339 * to split the current window or data could be lost.
6340 * We don't need to check if the 'hidden' option is set, as in this
6341 * case the buffer won't be lost.
6342 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006343 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 {
6345 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006346 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 --emsg_off;
6348 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006349 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 if (win_split(0, 0) == FAIL)
6352 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006353 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354
6355 /* When splitting the window, create a new alist. Otherwise the
6356 * existing one is overwritten. */
6357 alist_unlink(curwin->w_alist);
6358 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360
6361 /*
6362 * Set up the new argument list.
6363 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006364 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366 /*
6367 * Move to the first file.
6368 */
6369 /* Fake up a minimal "next" command for do_argfile() */
6370 vim_memset(&ea, 0, sizeof(ea));
6371 ea.cmd = (char_u *)"next";
6372 do_argfile(&ea, 0);
6373
6374 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6375 * mode that is not needed here. */
6376 need_start_insertmode = FALSE;
6377
6378 /* Restore msg_scroll, otherwise a following command may cause scrolling
6379 * unexpectedly. The screen will be redrawn by the caller, thus
6380 * msg_scroll being set by displaying a message is irrelevant. */
6381 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006382
6383 if (drop_callback != NULL)
6384 drop_callback(drop_cookie);
6385
6386 drop_filev = NULL;
6387 drop_busy = FALSE;
6388}
6389
6390/*
6391 * Handle a file drop. The code is here because a drop is *nearly* like an
6392 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006393 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006394 * code is very similar to :args and hence needs access to a lot of the static
6395 * functions in this file.
6396 *
6397 * The "filev" list must have been allocated using alloc(), as should each item
6398 * in the list. This function takes over responsibility for freeing the "filev"
6399 * list.
6400 */
6401 void
6402handle_drop(
6403 int filec, // the number of files dropped
6404 char_u **filev, // the list of files dropped
6405 int split, // force splitting the window
6406 void (*callback)(void *), // to be called after setting the argument
6407 // list
6408 void *cookie) // argument for "callback" (allocated)
6409{
6410 // Cannot handle recursive drops, finish the pending one.
6411 if (drop_busy)
6412 {
6413 FreeWild(filec, filev);
6414 vim_free(cookie);
6415 return;
6416 }
6417
6418 // When calling handle_drop() more than once in a row we only use the last
6419 // one.
6420 if (drop_filev != NULL)
6421 {
6422 FreeWild(drop_filec, drop_filev);
6423 vim_free(drop_cookie);
6424 }
6425
6426 drop_filec = filec;
6427 drop_filev = filev;
6428 drop_split = split;
6429 drop_callback = callback;
6430 drop_cookie = cookie;
6431
6432 // Postpone this when:
6433 // - editing the command line
6434 // - not possible to change the current buffer
6435 // - updating the screen
6436 // As it may change buffers and window structures that are in use and cause
6437 // freed memory to be used.
6438 if (text_locked() || curbuf_locked() || updating_screen)
6439 return;
6440
6441 handle_drop_internal();
6442}
6443
6444/*
6445 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6446 * reset: Handle a postponed drop.
6447 */
6448 void
6449handle_any_postponed_drop(void)
6450{
6451 if (!drop_busy && drop_filev != NULL
6452 && !text_locked() && !curbuf_locked() && !updating_screen)
6453 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454}
6455#endif
6456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457/*
6458 * Clear an argument list: free all file names and reset it to zero entries.
6459 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006460 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006461alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
6463 while (--al->al_ga.ga_len >= 0)
6464 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6465 ga_clear(&al->al_ga);
6466}
6467
6468/*
6469 * Init an argument list.
6470 */
6471 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006472alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
6474 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6475}
6476
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477/*
6478 * Remove a reference from an argument list.
6479 * Ignored when the argument list is the global one.
6480 * If the argument list is no longer used by any window, free it.
6481 */
6482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006483alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
6485 if (al != &global_alist && --al->al_refcount <= 0)
6486 {
6487 alist_clear(al);
6488 vim_free(al);
6489 }
6490}
6491
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492/*
6493 * Create a new argument list and use it for the current window.
6494 */
6495 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006496alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497{
6498 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6499 if (curwin->w_alist == NULL)
6500 {
6501 curwin->w_alist = &global_alist;
6502 ++global_alist.al_refcount;
6503 }
6504 else
6505 {
6506 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006507 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 alist_init(curwin->w_alist);
6509 }
6510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006512#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513/*
6514 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006515 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6516 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 */
6518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006519alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520{
6521 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006522 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 char_u **new_arg_files;
6524 int new_arg_file_count;
6525 char_u *save_p_su = p_su;
6526 int i;
6527
6528 /* Don't use 'suffixes' here. This should work like the shell did the
6529 * expansion. Also, the vimrc file isn't read yet, thus the user
6530 * can't set the options. */
6531 p_su = empty_option;
6532 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6533 if (old_arg_files != NULL)
6534 {
6535 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006536 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6537 old_arg_count = GARGCOUNT;
6538 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006540 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 && new_arg_file_count > 0)
6542 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006543 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6544 TRUE, fnum_list, fnum_len);
6545 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
6548 p_su = save_p_su;
6549}
6550#endif
6551
6552/*
6553 * Set the argument list for the current window.
6554 * Takes over the allocated files[] and the allocated fnames in it.
6555 */
6556 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006557alist_set(
6558 alist_T *al,
6559 int count,
6560 char_u **files,
6561 int use_curbuf,
6562 int *fnum_list,
6563 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
6565 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006566 static int recursive = 0;
6567
6568 if (recursive)
6569 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006570 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006571 return;
6572 }
6573 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 alist_clear(al);
6576 if (ga_grow(&al->al_ga, count) == OK)
6577 {
6578 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006579 {
6580 if (got_int)
6581 {
6582 /* When adding many buffers this can take a long time. Allow
6583 * interrupting here. */
6584 while (i < count)
6585 vim_free(files[i++]);
6586 break;
6587 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006588
6589 /* May set buffer name of a buffer previously used for the
6590 * argument list, so that it's re-used by alist_add. */
6591 if (fnum_list != NULL && i < fnum_len)
6592 buf_set_name(fnum_list[i], files[i]);
6593
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006595 ui_breakcheck();
6596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 vim_free(files);
6598 }
6599 else
6600 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006603
6604 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605}
6606
6607/*
6608 * Add file "fname" to argument list "al".
6609 * "fname" must have been allocated and "al" must have been checked for room.
6610 */
6611 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006612alist_add(
6613 alist_T *al,
6614 char_u *fname,
6615 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 if (fname == NULL) /* don't add NULL file names */
6618 return;
6619#ifdef BACKSLASH_IN_FILENAME
6620 slash_adjust(fname);
6621#endif
6622 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6623 if (set_fnum > 0)
6624 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6625 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6626 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627}
6628
6629#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6630/*
6631 * Adjust slashes in file names. Called after 'shellslash' was set.
6632 */
6633 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006634alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006638 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639
6640 for (i = 0; i < GARGCOUNT; ++i)
6641 if (GARGLIST[i].ae_fname != NULL)
6642 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006643 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (wp->w_alist != &global_alist)
6645 for (i = 0; i < WARGCOUNT(wp); ++i)
6646 if (WARGLIST(wp)[i].ae_fname != NULL)
6647 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648}
6649#endif
6650
6651/*
6652 * ":preserve".
6653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006655ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006657 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 ml_preserve(curbuf, TRUE);
6659}
6660
6661/*
6662 * ":recover".
6663 */
6664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006665ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6668 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006669 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6670 | CCGD_MULTWIN
6671 | (eap->forceit ? CCGD_FORCEIT : 0)
6672 | CCGD_EXCMD)
6673
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 && (*eap->arg == NUL
6675 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6676 ml_recover();
6677 recoverymode = FALSE;
6678}
6679
6680/*
6681 * Command modifier used in a wrong way.
6682 */
6683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006684ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 eap->errmsg = e_invcmd;
6687}
6688
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689/*
6690 * :sview [+command] file split window with new file, read-only
6691 * :split [[+command] file] split window with current or new file
6692 * :vsplit [[+command] file] split window vertically with current or new file
6693 * :new [[+command] file] split window with no or new file
6694 * :vnew [[+command] file] split vertically window with no or new file
6695 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006696 *
6697 * :tabedit open new Tab page with empty window
6698 * :tabedit [+command] file open new Tab page and edit "file"
6699 * :tabnew [[+command] file] just like :tabedit
6700 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
6702 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006703ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006705 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006706#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006708#endif
6709#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006711#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006712 int use_tab = eap->cmdidx == CMD_tabedit
6713 || eap->cmdidx == CMD_tabfind
6714 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
Bram Moolenaar4033c552017-09-16 20:54:51 +02006716#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719
Bram Moolenaar4033c552017-09-16 20:54:51 +02006720#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006722 * quickfix windows. But it's OK when doing ":tab split". */
6723 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 {
6725 if (eap->cmdidx == CMD_split)
6726 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 if (eap->cmdidx == CMD_vsplit)
6728 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731
Bram Moolenaar4033c552017-09-16 20:54:51 +02006732#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006733 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {
6735 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6736 FNAME_MESS, TRUE, curbuf->b_ffname);
6737 if (fname == NULL)
6738 goto theend;
6739 eap->arg = fname;
6740 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006741# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006742 else
6743# endif
6744#endif
6745#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 && eap->cmdidx != CMD_new)
6749 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006750 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006751# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006752 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006753# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006754 au_has_group((char_u *)"FileExplorer"))
6755 {
6756 /* No browsing supported but we do have the file explorer:
6757 * Edit the directory. */
6758 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6759 eap->arg = (char_u *)".";
6760 }
6761 else
6762 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006763 fname = do_browse(0, (char_u *)(use_tab
6764 ? _("Edit File in new tab page")
6765 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006767 if (fname == NULL)
6768 goto theend;
6769 eap->arg = fname;
6770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
6772 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006775 /*
6776 * Either open new tab page or split the window.
6777 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006778 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006779 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006780 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6781 : eap->addr_count == 0 ? 0
6782 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006783 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006784 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006785
6786 /* set the alternate buffer for the window we came from */
6787 if (curwin != old_curwin
6788 && win_valid(old_curwin)
6789 && old_curwin->w_buffer != curbuf
6790 && !cmdmod.keepalt)
6791 old_curwin->w_alt_fnum = curbuf->b_fnum;
6792 }
6793 }
6794 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6796 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* Reset 'scrollbind' when editing another file, but keep it when
6798 * doing ":split" without arguments. */
6799 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006800# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006804 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 else
6806 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 do_exedit(eap, old_curwin);
6808 }
6809
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006810# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006814# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815theend:
6816 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006819
6820/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006821 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006822 */
6823 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006824tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006825{
6826 exarg_T ea;
6827
6828 vim_memset(&ea, 0, sizeof(ea));
6829 ea.cmdidx = CMD_tabnew;
6830 ea.cmd = (char_u *)"tabn";
6831 ea.arg = (char_u *)"";
6832 ex_splitview(&ea);
6833}
6834
6835/*
6836 * :tabnext command
6837 */
6838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006839ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006840{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006841 int tab_number;
6842
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006843 switch (eap->cmdidx)
6844 {
6845 case CMD_tabfirst:
6846 case CMD_tabrewind:
6847 goto_tabpage(1);
6848 break;
6849 case CMD_tablast:
6850 goto_tabpage(9999);
6851 break;
6852 case CMD_tabprevious:
6853 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006854 if (eap->arg && *eap->arg != NUL)
6855 {
6856 char_u *p = eap->arg;
6857 char_u *p_save = p;
6858
6859 tab_number = getdigits(&p);
6860 if (p == p_save || *p_save == '-' || *p != NUL
6861 || tab_number == 0)
6862 {
6863 /* No numbers as argument. */
6864 eap->errmsg = e_invarg;
6865 return;
6866 }
6867 }
6868 else
6869 {
6870 if (eap->addr_count == 0)
6871 tab_number = 1;
6872 else
6873 {
6874 tab_number = eap->line2;
6875 if (tab_number < 1)
6876 {
6877 eap->errmsg = e_invrange;
6878 return;
6879 }
6880 }
6881 }
6882 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006883 break;
6884 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006885 tab_number = get_tabpage_arg(eap);
6886 if (eap->errmsg == NULL)
6887 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006888 break;
6889 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006890}
6891
6892/*
6893 * :tabmove command
6894 */
6895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006896ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006897{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006898 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006899
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006900 tab_number = get_tabpage_arg(eap);
6901 if (eap->errmsg == NULL)
6902 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006903}
6904
6905/*
6906 * :tabs command: List tabs and their contents.
6907 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006909ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910{
6911 tabpage_T *tp;
6912 win_T *wp;
6913 int tabcount = 1;
6914
6915 msg_start();
6916 msg_scroll = TRUE;
6917 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6918 {
6919 msg_putchar('\n');
6920 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006921 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006922 out_flush(); /* output one line at a time */
6923 ui_breakcheck();
6924
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006925 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006926 wp = firstwin;
6927 else
6928 wp = tp->tp_firstwin;
6929 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6930 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006931 msg_putchar('\n');
6932 msg_putchar(wp == curwin ? '>' : ' ');
6933 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006934 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6935 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006936 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006937 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938 else
6939 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6940 IObuff, IOSIZE, TRUE);
6941 msg_outtrans(IObuff);
6942 out_flush(); /* output one line at a time */
6943 ui_breakcheck();
6944 }
6945 }
6946}
6947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948/*
6949 * ":mode": Set screen mode.
6950 * If no argument given, just get the screen size and redraw.
6951 */
6952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006953ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
6955 if (*eap->arg == NUL)
6956 shell_resized();
6957 else
6958 mch_screenmode(eap->arg);
6959}
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961/*
6962 * ":resize".
6963 * set, increment or decrement current window height
6964 */
6965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int n;
6969 win_T *wp = curwin;
6970
6971 if (eap->addr_count > 0)
6972 {
6973 n = eap->line2;
6974 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6975 ;
6976 }
6977
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006978# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (cmdmod.split & WSP_VERT)
6983 {
6984 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006985 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6987 n = 9999;
6988 win_setwidth_win((int)n, wp);
6989 }
6990 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
6992 if (*eap->arg == '-' || *eap->arg == '+')
6993 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006994 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 n = 9999;
6996 win_setheight_win((int)n, wp);
6997 }
6998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000/*
7001 * ":find [+command] <file>" command.
7002 */
7003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007004ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006#ifdef FEAT_SEARCHPATH
7007 char_u *fname;
7008 int count;
7009
7010 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7011 TRUE, curbuf->b_ffname);
7012 if (eap->addr_count > 0)
7013 {
7014 /* Repeat finding the file "count" times. This matters when it
7015 * appears several times in the path. */
7016 count = eap->line2;
7017 while (fname != NULL && --count > 0)
7018 {
7019 vim_free(fname);
7020 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7021 FALSE, curbuf->b_ffname);
7022 }
7023 }
7024
7025 if (fname != NULL)
7026 {
7027 eap->arg = fname;
7028#endif
7029 do_exedit(eap, NULL);
7030#ifdef FEAT_SEARCHPATH
7031 vim_free(fname);
7032 }
7033#endif
7034}
7035
7036/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007037 * ":open" simulation: for now just work like ":visual".
7038 */
7039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007040ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007041{
7042 regmatch_T regmatch;
7043 char_u *p;
7044
7045 curwin->w_cursor.lnum = eap->line2;
7046 beginline(BL_SOL | BL_FIX);
7047 if (*eap->arg == '/')
7048 {
7049 /* ":open /pattern/": put cursor in column found with pattern */
7050 ++eap->arg;
7051 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7052 *p = NUL;
7053 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7054 if (regmatch.regprog != NULL)
7055 {
7056 regmatch.rm_ic = p_ic;
7057 p = ml_get_curline();
7058 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007059 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007060 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007061 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007062 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007063 }
7064 /* Move to the NUL, ignore any other arguments. */
7065 eap->arg += STRLEN(eap->arg);
7066 }
7067 check_cursor();
7068
7069 eap->cmdidx = CMD_visual;
7070 do_exedit(eap, NULL);
7071}
7072
7073/*
7074 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 */
7076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007077ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 do_exedit(eap, NULL);
7080}
7081
7082/*
7083 * ":edit <file>" command and alikes.
7084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007086do_exedit(
7087 exarg_T *eap,
7088 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089{
7090 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007092 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
7094 /*
7095 * ":vi" command ends Ex mode.
7096 */
7097 if (exmode_active && (eap->cmdidx == CMD_visual
7098 || eap->cmdidx == CMD_view))
7099 {
7100 exmode_active = FALSE;
7101 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007102 {
7103 /* Special case: ":global/pat/visual\NLvi-commands" */
7104 if (global_busy)
7105 {
7106 int rd = RedrawingDisabled;
7107 int nwr = no_wait_return;
7108 int ms = msg_scroll;
7109#ifdef FEAT_GUI
7110 int he = hold_gui_events;
7111#endif
7112
7113 if (eap->nextcmd != NULL)
7114 {
7115 stuffReadbuff(eap->nextcmd);
7116 eap->nextcmd = NULL;
7117 }
7118
7119 if (exmode_was != EXMODE_VIM)
7120 settmode(TMODE_RAW);
7121 RedrawingDisabled = 0;
7122 no_wait_return = 0;
7123 need_wait_return = FALSE;
7124 msg_scroll = 0;
7125#ifdef FEAT_GUI
7126 hold_gui_events = 0;
7127#endif
7128 must_redraw = CLEAR;
7129
7130 main_loop(FALSE, TRUE);
7131
7132 RedrawingDisabled = rd;
7133 no_wait_return = nwr;
7134 msg_scroll = ms;
7135#ifdef FEAT_GUI
7136 hold_gui_events = he;
7137#endif
7138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
7142
7143 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007144 || eap->cmdidx == CMD_tabnew
7145 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007146 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007148 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 setpcmark();
7150 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007151 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7152 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007154 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 || *eap->arg != NUL
7156#ifdef FEAT_BROWSE
7157 || cmdmod.browse
7158#endif
7159 )
7160 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007161 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7162 * can bring us here, others are stopped earlier. */
7163 if (*eap->arg != NUL && curbuf_locked())
7164 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 n = readonlymode;
7167 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7168 readonlymode = TRUE;
7169 else if (eap->cmdidx == CMD_enew)
7170 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7171 empty buffer */
7172 setpcmark();
7173 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7174 NULL, eap,
7175 /* ":edit" goes to first line if Vi compatible */
7176 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7177 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7178 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007179 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007181 /* after a split we can use an existing buffer */
7182 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007184 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 {
7186 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 if (old_curwin != NULL)
7188 {
7189 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007190 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007192#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007193 cleanup_T cs;
7194
7195 /* Reset the error/interrupt/exception state here so that
7196 * aborting() returns FALSE when closing a window. */
7197 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007198#endif
7199#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007201#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007202 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007203
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007204#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007205 /* Restore the error/interrupt/exception state if not
7206 * discarded by a new aborting error, interrupt, or
7207 * uncaught exception. */
7208 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
7211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213 else if (readonlymode && curbuf->b_nwindows == 1)
7214 {
7215 /* When editing an already visited buffer, 'readonly' won't be set
7216 * but the previous value is kept. With ":view" and ":sview" we
7217 * want the file to be readonly, except when another window is
7218 * editing the same buffer. */
7219 curbuf->b_p_ro = TRUE;
7220 }
7221 readonlymode = n;
7222 }
7223 else
7224 {
7225 if (eap->do_ecmd_cmd != NULL)
7226 do_cmdline_cmd(eap->do_ecmd_cmd);
7227#ifdef FEAT_TITLE
7228 n = curwin->w_arg_idx_invalid;
7229#endif
7230 check_arg_idx(curwin);
7231#ifdef FEAT_TITLE
7232 if (n != curwin->w_arg_idx_invalid)
7233 maketitle();
7234#endif
7235 }
7236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 /*
7238 * if ":split file" worked, set alternate file name in old window to new
7239 * file
7240 */
7241 if (old_curwin != NULL
7242 && *eap->arg != NUL
7243 && curwin != old_curwin
7244 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007245 && old_curwin->w_buffer != curbuf
7246 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
7249 ex_no_reprint = TRUE;
7250}
7251
7252#ifndef FEAT_GUI
7253/*
7254 * ":gui" and ":gvim" when there is no GUI.
7255 */
7256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007257ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 eap->errmsg = e_nogvim;
7260}
7261#endif
7262
Bram Moolenaar4f974752019-02-17 17:44:42 +01007263#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007265ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266{
7267 gui_make_tearoff(eap->arg);
7268}
7269#endif
7270
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007271#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7272 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007274ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007276# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7277 if (gui.in_use)
7278 gui_make_popup(eap->arg, eap->forceit);
7279# ifdef FEAT_TERM_POPUP_MENU
7280 else
7281# endif
7282# endif
7283# ifdef FEAT_TERM_POPUP_MENU
7284 pum_make_popup(eap->arg, eap->forceit);
7285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286}
7287#endif
7288
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007290ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007293 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007295 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296}
7297
7298/*
7299 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7300 * offset.
7301 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007304ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007307 win_T *save_curwin = curwin;
7308 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 long topline;
7310 long y;
7311 linenr_T old_linenr = curwin->w_cursor.lnum;
7312
7313 setpcmark();
7314
7315 /*
7316 * determine max topline
7317 */
7318 if (curwin->w_p_scb)
7319 {
7320 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007321 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
7323 if (wp->w_p_scb && wp->w_buffer)
7324 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007325 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 if (topline > y)
7327 topline = y;
7328 }
7329 }
7330 if (topline < 1)
7331 topline = 1;
7332 }
7333 else
7334 {
7335 topline = 1;
7336 }
7337
7338
7339 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007340 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007342 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
7344 if (curwin->w_p_scb)
7345 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007346 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 y = topline - curwin->w_topline;
7348 if (y > 0)
7349 scrollup(y, TRUE);
7350 else
7351 scrolldown(-y, TRUE);
7352 curwin->w_scbind_pos = topline;
7353 redraw_later(VALID);
7354 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 }
7357 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007358 curwin = save_curwin;
7359 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 if (curwin->w_p_scb)
7361 {
7362 did_syncbind = TRUE;
7363 checkpcmark();
7364 if (old_linenr != curwin->w_cursor.lnum)
7365 {
7366 char_u ctrl_o[2];
7367
7368 ctrl_o[0] = Ctrl_O;
7369 ctrl_o[1] = 0;
7370 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7371 }
7372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373}
7374
7375
7376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007377ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007379 int i;
7380 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7381 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
7383 if (eap->usefilter) /* :r!cmd */
7384 do_bang(1, eap, FALSE, FALSE, TRUE);
7385 else
7386 {
7387 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7388 return;
7389
7390#ifdef FEAT_BROWSE
7391 if (cmdmod.browse)
7392 {
7393 char_u *browseFile;
7394
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007395 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 NULL, NULL, NULL, curbuf);
7397 if (browseFile != NULL)
7398 {
7399 i = readfile(browseFile, NULL,
7400 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7401 vim_free(browseFile);
7402 }
7403 else
7404 i = OK;
7405 }
7406 else
7407#endif
7408 if (*eap->arg == NUL)
7409 {
7410 if (check_fname() == FAIL) /* check for no file name */
7411 return;
7412 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7413 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7414 }
7415 else
7416 {
7417 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7418 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7419 i = readfile(eap->arg, NULL,
7420 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7421
7422 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007423 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007425#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 if (!aborting())
7427#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007428 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007431 {
7432 if (empty && exmode_active)
7433 {
7434 /* Delete the empty line that remains. Historically ex does
7435 * this but vi doesn't. */
7436 if (eap->line2 == 0)
7437 lnum = curbuf->b_ml.ml_line_count;
7438 else
7439 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007440 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007441 {
7442 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007443 if (curwin->w_cursor.lnum > 1
7444 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007445 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007446 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007447 }
7448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 }
7452}
7453
Bram Moolenaarea408852005-06-25 22:49:46 +00007454static char_u *prev_dir = NULL;
7455
7456#if defined(EXITFREE) || defined(PROTO)
7457 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007458free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007459{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007460 VIM_CLEAR(prev_dir);
7461 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007462}
7463#endif
7464
Bram Moolenaarf4258302013-06-02 18:20:17 +02007465/*
7466 * Deal with the side effects of changing the current directory.
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007467 * When "tablocal" is TRUE then this was after an ":tcd" command.
7468 * When "winlocal" is TRUE then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007469 */
7470 void
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007471post_chdir(int tablocal, int winlocal)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007472{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007473 if (!winlocal)
7474 // Clear tab local directory for both :cd and :tcd
7475 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007476 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007477 if (winlocal || tablocal)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007478 {
7479 /* If still in global directory, need to remember current
7480 * directory as global directory. */
7481 if (globaldir == NULL && prev_dir != NULL)
7482 globaldir = vim_strsave(prev_dir);
7483 /* Remember this local directory for the window. */
7484 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007485 {
7486 if (tablocal)
7487 curtab->tp_localdir = vim_strsave(NameBuff);
7488 else
7489 curwin->w_localdir = vim_strsave(NameBuff);
7490 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007491 }
7492 else
7493 {
7494 /* We are now in the global directory, no need to remember its
7495 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007496 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007497 }
7498
7499 shorten_fnames(TRUE);
7500}
7501
Bram Moolenaarea408852005-06-25 22:49:46 +00007502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007504 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007507ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007509 char_u *new_dir;
7510 char_u *tofree;
7511 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512
7513 new_dir = eap->arg;
7514#if !defined(UNIX) && !defined(VMS)
7515 /* for non-UNIX ":cd" means: print current directory */
7516 if (*new_dir == NUL)
7517 ex_pwd(NULL);
7518 else
7519#endif
7520 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007521 if (allbuf_locked())
7522 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007523 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7524 && !eap->forceit)
7525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007526 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007527 return;
7528 }
7529
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 /* ":cd -": Change to previous directory */
7531 if (STRCMP(new_dir, "-") == 0)
7532 {
7533 if (prev_dir == NULL)
7534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007535 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 return;
7537 }
7538 new_dir = prev_dir;
7539 }
7540
7541 /* Save current directory for next ":cd -" */
7542 tofree = prev_dir;
7543 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7544 prev_dir = vim_strsave(NameBuff);
7545 else
7546 prev_dir = NULL;
7547
7548#if defined(UNIX) || defined(VMS)
7549 /* for UNIX ":cd" means: go to home directory */
7550 if (*new_dir == NUL)
7551 {
7552 /* use NameBuff for home directory name */
7553# ifdef VMS
7554 char_u *p;
7555
7556 p = mch_getenv((char_u *)"SYS$LOGIN");
7557 if (p == NULL || *p == NUL) /* empty is the same as not set */
7558 NameBuff[0] = NUL;
7559 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007560 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561# else
7562 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7563# endif
7564 new_dir = NameBuff;
7565 }
7566#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007567 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01007568 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007569 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007570 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 else
7572 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007573 char_u *acmd_fname;
7574 int is_winlocal_chdir = eap->cmdidx == CMD_lcd
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007575 || eap->cmdidx == CMD_lchdir;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007576 int is_tablocal_chdir = eap->cmdidx == CMD_tcd
7577 || eap->cmdidx == CMD_tchdir;
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007578
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007579 post_chdir(is_tablocal_chdir, is_winlocal_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580
7581 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007582 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007584
7585 if (dir_differs)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007586 {
7587 if (is_winlocal_chdir)
7588 acmd_fname = (char_u *)"window";
7589 else if (is_tablocal_chdir)
7590 acmd_fname = (char_u *)"tabpage";
7591 else
7592 acmd_fname = (char_u *)"global";
7593 apply_autocmds(EVENT_DIRCHANGED, acmd_fname,
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007594 new_dir, FALSE, curbuf);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597 vim_free(tofree);
7598 }
7599}
7600
7601/*
7602 * ":pwd".
7603 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007605ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7608 {
7609#ifdef BACKSLASH_IN_FILENAME
7610 slash_adjust(NameBuff);
7611#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007612 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 }
7614 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007615 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616}
7617
7618/*
7619 * ":=".
7620 */
7621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007622ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007624 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007625 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626}
7627
7628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007629ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007631 int n;
7632 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
7634 if (cursor_valid())
7635 {
7636 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7637 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007638 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007640
7641 len = eap->line2;
7642 switch (*eap->arg)
7643 {
7644 case 'm': break;
7645 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007646 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007647 }
7648 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649}
7650
7651/*
7652 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7653 */
7654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007655do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
7657 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007658 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007661 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007662 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007664 wait_now = msec - done > 1000L ? 1000L : msec - done;
7665#ifdef FEAT_TIMERS
7666 {
7667 long due_time = check_due_timer();
7668
7669 if (due_time > 0 && due_time < wait_now)
7670 wait_now = due_time;
7671 }
7672#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007673#ifdef FEAT_JOB_CHANNEL
7674 if (has_any_channel() && wait_now > 100L)
7675 wait_now = 100L;
7676#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007677 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007678#ifdef FEAT_JOB_CHANNEL
7679 if (has_any_channel())
7680 ui_breakcheck_force(TRUE);
7681 else
7682#endif
7683 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007684#ifdef MESSAGE_QUEUE
7685 /* Process the netbeans and clientserver messages that may have been
7686 * received in the call to ui_breakcheck() when the GUI is in use. This
7687 * may occur when running a test case. */
7688 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007691
7692 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7693 // input buffer, otherwise a following call to input() fails.
7694 if (got_int)
7695 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696}
7697
7698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 int mode;
7702 char_u *cmdp;
7703
7704 cmdp = eap->cmd;
7705 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7706
7707 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7708 eap->arg, mode, isabbrev))
7709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007710 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007712 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 break;
7714 }
7715}
7716
7717/*
7718 * ":winsize" command (obsolete).
7719 */
7720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007721ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
7723 int w, h;
7724 char_u *arg = eap->arg;
7725 char_u *p;
7726
7727 w = getdigits(&arg);
7728 arg = skipwhite(arg);
7729 p = arg;
7730 h = getdigits(&arg);
7731 if (*p != NUL && *arg == NUL)
7732 set_shellsize(w, h, TRUE);
7733 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007734 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735}
7736
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007738ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 int xchar = NUL;
7741 char_u *p;
7742
7743 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7744 {
7745 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7746 if (eap->arg[1] == NUL)
7747 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007748 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 return;
7750 }
7751 xchar = eap->arg[1];
7752 p = eap->arg + 2;
7753 }
7754 else
7755 p = eap->arg + 1;
7756
7757 eap->nextcmd = check_nextcmd(p);
7758 p = skipwhite(p);
7759 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007760 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007761 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {
7763 /* Pass flags on for ":vertical wincmd ]". */
7764 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007765 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7767 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007768 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
7770}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771
Bram Moolenaar843ee412004-06-30 16:16:41 +00007772#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773/*
7774 * ":winpos".
7775 */
7776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007777ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778{
7779 int x, y;
7780 char_u *arg = eap->arg;
7781 char_u *p;
7782
7783 if (*arg == NUL)
7784 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007785# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007786# ifdef VIMDLL
7787 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7788 mch_get_winpos(&x, &y) != FAIL)
7789# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007791# else
7792 if (mch_get_winpos(&x, &y) != FAIL)
7793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {
7795 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007796 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 }
7798 else
7799# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007800 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 }
7802 else
7803 {
7804 x = getdigits(&arg);
7805 arg = skipwhite(arg);
7806 p = arg;
7807 y = getdigits(&arg);
7808 if (*p == NUL || *arg != NUL)
7809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007810 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 return;
7812 }
7813# ifdef FEAT_GUI
7814 if (gui.in_use)
7815 gui_mch_set_winpos(x, y);
7816 else if (gui.starting)
7817 {
7818 /* Remember the coordinates for when the window is opened. */
7819 gui_win_x = x;
7820 gui_win_y = y;
7821 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007822# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 else
7824# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007825# endif
7826# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007827 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828# endif
7829# ifdef HAVE_TGETENT
7830 if (*T_CWP)
7831 term_set_winpos(x, y);
7832# endif
7833 }
7834}
7835#endif
7836
7837/*
7838 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7839 */
7840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007841ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 oparg_T oa;
7844
7845 clear_oparg(&oa);
7846 oa.regname = eap->regname;
7847 oa.start.lnum = eap->line1;
7848 oa.end.lnum = eap->line2;
7849 oa.line_count = eap->line2 - eap->line1 + 1;
7850 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7853 {
7854 setpcmark();
7855 curwin->w_cursor.lnum = eap->line1;
7856 beginline(BL_SOL | BL_FIX);
7857 }
7858
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007859 if (VIsual_active)
7860 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007861
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 switch (eap->cmdidx)
7863 {
7864 case CMD_delete:
7865 oa.op_type = OP_DELETE;
7866 op_delete(&oa);
7867 break;
7868
7869 case CMD_yank:
7870 oa.op_type = OP_YANK;
7871 (void)op_yank(&oa, FALSE, TRUE);
7872 break;
7873
7874 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007875 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007877 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7878#else
7879 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007881 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 oa.op_type = OP_RSHIFT;
7883 else
7884 oa.op_type = OP_LSHIFT;
7885 op_shift(&oa, FALSE, eap->amount);
7886 break;
7887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007889 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890}
7891
7892/*
7893 * ":put".
7894 */
7895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007896ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 /* ":0put" works like ":1put!". */
7899 if (eap->line2 == 0)
7900 {
7901 eap->line2 = 1;
7902 eap->forceit = TRUE;
7903 }
7904 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007905 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7906 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907}
7908
7909/*
7910 * Handle ":copy" and ":move".
7911 */
7912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007913ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 long n;
7916
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007917 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 if (eap->arg == NULL) /* error detected */
7919 {
7920 eap->nextcmd = NULL;
7921 return;
7922 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007923 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924
7925 /*
7926 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7927 */
7928 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7929 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007930 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 return;
7932 }
7933
7934 if (eap->cmdidx == CMD_move)
7935 {
7936 if (do_move(eap->line1, eap->line2, n) == FAIL)
7937 return;
7938 }
7939 else
7940 ex_copy(eap->line1, eap->line2, n);
7941 u_clearline();
7942 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007943 ex_may_print(eap);
7944}
7945
7946/*
7947 * Print the current line if flags were given to the Ex command.
7948 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007949 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007950ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007951{
7952 if (eap->flags != 0)
7953 {
7954 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7955 (eap->flags & EXFLAG_LIST));
7956 ex_no_reprint = TRUE;
7957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958}
7959
7960/*
7961 * ":smagic" and ":snomagic".
7962 */
7963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007964ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 int magic_save = p_magic;
7967
7968 p_magic = (eap->cmdidx == CMD_smagic);
7969 do_sub(eap);
7970 p_magic = magic_save;
7971}
7972
7973/*
7974 * ":join".
7975 */
7976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007977ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
7979 curwin->w_cursor.lnum = eap->line1;
7980 if (eap->line1 == eap->line2)
7981 {
7982 if (eap->addr_count >= 2) /* :2,2join does nothing */
7983 return;
7984 if (eap->line2 == curbuf->b_ml.ml_line_count)
7985 {
7986 beep_flush();
7987 return;
7988 }
7989 ++eap->line2;
7990 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007991 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007993 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994}
7995
7996/*
7997 * ":[addr]@r" or ":[addr]*r": execute register
7998 */
7999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008000ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
8002 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008003 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004
8005 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008006 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007
8008#ifdef USE_ON_FLY_SCROLL
8009 dont_scroll = TRUE; /* disallow scrolling here */
8010#endif
8011
8012 /* get the register name. No name means to use the previous one */
8013 c = *eap->arg;
8014 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8015 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008016 /* Put the register in the typeahead buffer with the "silent" flag. */
8017 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8018 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 else
8023 {
8024 int save_efr = exec_from_reg;
8025
8026 exec_from_reg = TRUE;
8027
8028 /*
8029 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008030 * Continue until the stuff buffer is empty and all added characters
8031 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008033 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8035
8036 exec_from_reg = save_efr;
8037 }
8038}
8039
8040/*
8041 * ":!".
8042 */
8043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008044ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
8046 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8047}
8048
8049/*
8050 * ":undo".
8051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008053ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008055 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008056 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008057 else
8058 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059}
8060
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008061#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008063ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008064{
8065 char_u hash[UNDO_HASH_SIZE];
8066
8067 u_compute_hash(hash);
8068 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8069}
8070
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008072ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008073{
8074 char_u hash[UNDO_HASH_SIZE];
8075
8076 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008077 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008078}
8079#endif
8080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081/*
8082 * ":redo".
8083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008085ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086{
8087 u_redo(1);
8088}
8089
8090/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008091 * ":earlier" and ":later".
8092 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008094ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008095{
8096 long count = 0;
8097 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008098 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008099 char_u *p = eap->arg;
8100
8101 if (*p == NUL)
8102 count = 1;
8103 else if (isdigit(*p))
8104 {
8105 count = getdigits(&p);
8106 switch (*p)
8107 {
8108 case 's': ++p; sec = TRUE; break;
8109 case 'm': ++p; sec = TRUE; count *= 60; break;
8110 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008111 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8112 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008113 }
8114 }
8115
8116 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008117 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008118 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008119 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8120 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008121}
8122
8123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 * ":redir": start/stop redirection.
8125 */
8126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008127ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 char *mode;
8130 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008131 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132
Bram Moolenaarba768492016-07-08 15:32:54 +02008133#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008134 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008136 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008137 return;
8138 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008139#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008140
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 if (STRICMP(eap->arg, "END") == 0)
8142 close_redir();
8143 else
8144 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008145 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008147 ++arg;
8148 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008150 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 mode = "a";
8152 }
8153 else
8154 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008155 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156
8157 close_redir();
8158
8159 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008160 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 if (fname == NULL)
8162 return;
8163#ifdef FEAT_BROWSE
8164 if (cmdmod.browse)
8165 {
8166 char_u *browseFile;
8167
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008168 browseFile = do_browse(BROWSE_SAVE,
8169 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008170 fname, NULL, NULL,
8171 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 if (browseFile == NULL)
8173 return; /* operation cancelled */
8174 vim_free(fname);
8175 fname = browseFile;
8176 eap->forceit = TRUE; /* since dialog already asked */
8177 }
8178#endif
8179
8180 redir_fd = open_exfile(fname, eap->forceit, mode);
8181 vim_free(fname);
8182 }
8183#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008184 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {
8186 /* redirect to a register a-z (resp. A-Z for appending) */
8187 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008188 ++arg;
8189 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008191 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008192 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008194 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008196 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008197 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008198 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008201 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008202 if (*arg == '>')
8203 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008204 /* Make register empty when not using @A-@Z and the
8205 * command is valid. */
8206 if (*arg == NUL && !isupper(redir_reg))
8207 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008209 }
8210 if (*arg != NUL)
8211 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008212 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008213 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008214 }
8215 }
8216 else if (*arg == '=' && arg[1] == '>')
8217 {
8218 int append;
8219
8220 /* redirect to a variable */
8221 close_redir();
8222 arg += 2;
8223
8224 if (*arg == '>')
8225 {
8226 ++arg;
8227 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 }
8229 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008230 append = FALSE;
8231
8232 if (var_redir_start(skipwhite(arg), append) == OK)
8233 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
8235#endif
8236
8237 /* TODO: redirect to a buffer */
8238
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008240 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008242
8243 /* Make sure redirection is not off. Can happen for cmdline completion
8244 * that indirectly invokes a command to catch its output. */
8245 if (redir_fd != NULL
8246#ifdef FEAT_EVAL
8247 || redir_reg || redir_vname
8248#endif
8249 )
8250 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251}
8252
8253/*
8254 * ":redraw": force redraw
8255 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008256 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008257ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
8259 int r = RedrawingDisabled;
8260 int p = p_lz;
8261
8262 RedrawingDisabled = 0;
8263 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008264 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008266 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267#ifdef FEAT_TITLE
8268 if (need_maketitle)
8269 maketitle();
8270#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008271#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8272# ifdef VIMDLL
8273 if (!gui.in_use)
8274# endif
8275 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 RedrawingDisabled = r;
8278 p_lz = p;
8279
8280 /* Reset msg_didout, so that a message that's there is overwritten. */
8281 msg_didout = FALSE;
8282 msg_col = 0;
8283
Bram Moolenaar56667a52013-07-17 11:54:28 +02008284 /* No need to wait after an intentional redraw. */
8285 need_wait_return = FALSE;
8286
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 out_flush();
8288}
8289
8290/*
8291 * ":redrawstatus": force redraw of status line(s)
8292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008294ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 int r = RedrawingDisabled;
8297 int p = p_lz;
8298
8299 RedrawingDisabled = 0;
8300 p_lz = FALSE;
8301 if (eap->forceit)
8302 status_redraw_all();
8303 else
8304 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008305 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 RedrawingDisabled = r;
8307 p_lz = p;
8308 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309}
8310
Bram Moolenaare12bab32019-01-08 22:02:56 +01008311/*
8312 * ":redrawtabline": force redraw of the tabline
8313 */
8314 static void
8315ex_redrawtabline(exarg_T *eap UNUSED)
8316{
8317 int r = RedrawingDisabled;
8318 int p = p_lz;
8319
8320 RedrawingDisabled = 0;
8321 p_lz = FALSE;
8322
8323 draw_tabline();
8324
8325 RedrawingDisabled = r;
8326 p_lz = p;
8327 out_flush();
8328}
8329
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008331close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 if (redir_fd != NULL)
8334 {
8335 fclose(redir_fd);
8336 redir_fd = NULL;
8337 }
8338#ifdef FEAT_EVAL
8339 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008340 if (redir_vname)
8341 {
8342 var_redir_stop();
8343 redir_vname = 0;
8344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345#endif
8346}
8347
8348#if defined(FEAT_SESSION) && defined(USE_CRNL)
8349# define MKSESSION_NL
8350static int mksession_nl = FALSE; /* use NL only in put_eol() */
8351#endif
8352
8353/*
8354 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8355 */
8356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008357ex_mkrc(
8358 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359{
8360 FILE *fd;
8361 int failed = FALSE;
8362 char_u *fname;
8363#ifdef FEAT_BROWSE
8364 char_u *browseFile = NULL;
8365#endif
8366#ifdef FEAT_SESSION
8367 int view_session = FALSE;
8368 int using_vdir = FALSE; /* using 'viewdir'? */
8369 char_u *viewFile = NULL;
8370 unsigned *flagp;
8371#endif
8372
8373 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8374 {
8375#ifdef FEAT_SESSION
8376 view_session = TRUE;
8377#else
8378 ex_ni(eap);
8379 return;
8380#endif
8381 }
8382
8383#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008384 /* Use the short file name until ":lcd" is used. We also don't use the
8385 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008386 did_lcd = FALSE;
8387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8389 if (eap->cmdidx == CMD_mkview
8390 && (*eap->arg == NUL
8391 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8392 {
8393 eap->forceit = TRUE;
8394 fname = get_view_file(*eap->arg);
8395 if (fname == NULL)
8396 return;
8397 viewFile = fname;
8398 using_vdir = TRUE;
8399 }
8400 else
8401#endif
8402 if (*eap->arg != NUL)
8403 fname = eap->arg;
8404 else if (eap->cmdidx == CMD_mkvimrc)
8405 fname = (char_u *)VIMRC_FILE;
8406#ifdef FEAT_SESSION
8407 else if (eap->cmdidx == CMD_mksession)
8408 fname = (char_u *)SESSION_FILE;
8409#endif
8410 else
8411 fname = (char_u *)EXRC_FILE;
8412
8413#ifdef FEAT_BROWSE
8414 if (cmdmod.browse)
8415 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008416 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417# ifdef FEAT_SESSION
8418 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8419 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8420# endif
8421 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008422 fname, (char_u *)"vim", NULL,
8423 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 if (browseFile == NULL)
8425 goto theend;
8426 fname = browseFile;
8427 eap->forceit = TRUE; /* since dialog already asked */
8428 }
8429#endif
8430
8431#if defined(FEAT_SESSION) && defined(vim_mkdir)
8432 /* When using 'viewdir' may have to create the directory. */
8433 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008434 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435#endif
8436
8437 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8438 if (fd != NULL)
8439 {
8440#ifdef FEAT_SESSION
8441 if (eap->cmdidx == CMD_mkview)
8442 flagp = &vop_flags;
8443 else
8444 flagp = &ssop_flags;
8445#endif
8446
8447#ifdef MKSESSION_NL
8448 /* "unix" in 'sessionoptions': use NL line separator */
8449 if (view_session && (*flagp & SSOP_UNIX))
8450 mksession_nl = TRUE;
8451#endif
8452
8453 /* Write the version command for :mkvimrc */
8454 if (eap->cmdidx == CMD_mkvimrc)
8455 (void)put_line(fd, "version 6.0");
8456
8457#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008458 if (eap->cmdidx == CMD_mksession)
8459 {
8460 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8461 failed = TRUE;
8462 }
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 if (eap->cmdidx != CMD_mkview)
8465#endif
8466 {
8467 /* Write setting 'compatible' first, because it has side effects.
8468 * For that same reason only do it when needed. */
8469 if (p_cp)
8470 (void)put_line(fd, "if !&cp | set cp | endif");
8471 else
8472 (void)put_line(fd, "if &cp | set nocp | endif");
8473 }
8474
8475#ifdef FEAT_SESSION
8476 if (!view_session
8477 || (eap->cmdidx == CMD_mksession
8478 && (*flagp & SSOP_OPTIONS)))
8479#endif
8480 failed |= (makemap(fd, NULL) == FAIL
8481 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8482
8483#ifdef FEAT_SESSION
8484 if (!failed && view_session)
8485 {
8486 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8487 failed = TRUE;
8488 if (eap->cmdidx == CMD_mksession)
8489 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008490 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
Bram Moolenaard9462e32011-04-11 21:35:11 +02008492 dirnow = alloc(MAXPATHL);
8493 if (dirnow == NULL)
8494 failed = TRUE;
8495 else
8496 {
8497 /*
8498 * Change to session file's dir.
8499 */
8500 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008502 *dirnow = NUL;
8503 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8504 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008505 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008506 shorten_fnames(TRUE);
8507 }
8508 else if (*dirnow != NUL
8509 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8510 {
8511 if (mch_chdir((char *)globaldir) == 0)
8512 shorten_fnames(TRUE);
8513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514
Bram Moolenaard9462e32011-04-11 21:35:11 +02008515 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516
Bram Moolenaard9462e32011-04-11 21:35:11 +02008517 /* restore original dir */
8518 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008520 {
8521 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008522 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008523 shorten_fnames(TRUE);
8524 }
8525 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 }
8527 }
8528 else
8529 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008530 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8531 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 }
8533 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8534 == FAIL)
8535 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008536# ifdef FEAT_SEARCH_EXTRA
8537 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8538 failed = TRUE;
8539# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008540 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8541 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008542 if (eap->cmdidx == CMD_mksession)
8543 {
8544 if (put_line(fd, "unlet SessionLoad") == FAIL)
8545 failed = TRUE;
8546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 }
8548#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008549 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8550 failed = TRUE;
8551
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 failed |= fclose(fd);
8553
8554 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008555 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8557 else if (eap->cmdidx == CMD_mksession)
8558 {
8559 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008560 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561
Bram Moolenaard9462e32011-04-11 21:35:11 +02008562 tbuf = alloc(MAXPATHL);
8563 if (tbuf != NULL)
8564 {
8565 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8566 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8567 vim_free(tbuf);
8568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
8570#endif
8571#ifdef MKSESSION_NL
8572 mksession_nl = FALSE;
8573#endif
8574 }
8575
8576#ifdef FEAT_BROWSE
8577theend:
8578 vim_free(browseFile);
8579#endif
8580#ifdef FEAT_SESSION
8581 vim_free(viewFile);
8582#endif
8583}
8584
Bram Moolenaardf177f62005-02-22 08:39:57 +00008585#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8586 || defined(PROTO)
8587 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008588vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008589{
8590 if (vim_mkdir(name, prot) != 0)
8591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008592 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008593 return FAIL;
8594 }
8595 return OK;
8596}
8597#endif
8598
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599/*
8600 * Open a file for writing for an Ex command, with some checks.
8601 * Return file descriptor, or NULL on failure.
8602 */
8603 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008604open_exfile(
8605 char_u *fname,
8606 int forceit,
8607 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608{
8609 FILE *fd;
8610
8611#ifdef UNIX
8612 /* with Unix it is possible to open a directory */
8613 if (mch_isdir(fname))
8614 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008615 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 return NULL;
8617 }
8618#endif
8619 if (!forceit && *mode != 'a' && vim_fexists(fname))
8620 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008621 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 return NULL;
8623 }
8624
8625 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008626 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627
8628 return fd;
8629}
8630
8631/*
8632 * ":mark" and ":k".
8633 */
8634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008635ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636{
8637 pos_T pos;
8638
8639 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008640 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008642 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 else
8644 {
8645 pos = curwin->w_cursor; /* save curwin->w_cursor */
8646 curwin->w_cursor.lnum = eap->line2;
8647 beginline(BL_WHITE | BL_FIX);
8648 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008649 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8651 }
8652}
8653
8654/*
8655 * Update w_topline, w_leftcol and the cursor position.
8656 */
8657 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008658update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 check_cursor(); /* put cursor on valid line */
8661 update_topline();
8662 if (!curwin->w_p_wrap)
8663 validate_cursor();
8664 update_curswant();
8665}
8666
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008668 * Save the current State and go to Normal mode.
8669 * Return TRUE if the typeahead could be saved.
8670 */
8671 int
8672save_current_state(save_state_T *sst)
8673{
8674 sst->save_msg_scroll = msg_scroll;
8675 sst->save_restart_edit = restart_edit;
8676 sst->save_msg_didout = msg_didout;
8677 sst->save_State = State;
8678 sst->save_insertmode = p_im;
8679 sst->save_finish_op = finish_op;
8680 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008681 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008682
8683 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8684 restart_edit = 0; /* don't go to Insert mode */
8685 p_im = FALSE; /* don't use 'insertmode' */
8686
8687 /*
8688 * Save the current typeahead. This is required to allow using ":normal"
8689 * from an event handler and makes sure we don't hang when the argument
8690 * ends with half a command.
8691 */
8692 save_typeahead(&sst->tabuf);
8693 return sst->tabuf.typebuf_valid;
8694}
8695
8696 void
8697restore_current_state(save_state_T *sst)
8698{
8699 /* Restore the previous typeahead. */
8700 restore_typeahead(&sst->tabuf);
8701
8702 msg_scroll = sst->save_msg_scroll;
8703 restart_edit = sst->save_restart_edit;
8704 p_im = sst->save_insertmode;
8705 finish_op = sst->save_finish_op;
8706 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008707 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008708 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8709
8710 /* Restore the state (needed when called from a function executed for
8711 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8712 State = sst->save_State;
8713#ifdef CURSOR_SHAPE
8714 ui_cursor_shape(); /* may show different cursor shape */
8715#endif
8716}
8717
8718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 * ":normal[!] {commands}": Execute normal mode commands.
8720 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008721 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008722ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008724 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 char_u *arg = NULL;
8726 int l;
8727 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008729 if (ex_normal_lock > 0)
8730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008731 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008732 return;
8733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 if (ex_normal_busy >= p_mmd)
8735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008736 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 return;
8738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 /*
8741 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8742 * this for the K_SPECIAL leading byte, otherwise special keys will not
8743 * work.
8744 */
8745 if (has_mbyte)
8746 {
8747 int len = 0;
8748
8749 /* Count the number of characters to be escaped. */
8750 for (p = eap->arg; *p != NUL; ++p)
8751 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008752#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 if (*p == CSI) /* leadbyte CSI */
8754 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008755#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008756 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008758#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 )
8762 len += 2;
8763 }
8764 if (len > 0)
8765 {
8766 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8767 if (arg != NULL)
8768 {
8769 len = 0;
8770 for (p = eap->arg; *p != NUL; ++p)
8771 {
8772 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008773#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 if (*p == CSI)
8775 {
8776 arg[len++] = KS_EXTRA;
8777 arg[len++] = (int)KE_CSI;
8778 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008779#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008780 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {
8782 arg[len++] = *++p;
8783 if (*p == K_SPECIAL)
8784 {
8785 arg[len++] = KS_SPECIAL;
8786 arg[len++] = KE_FILLER;
8787 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008788#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 else if (*p == CSI)
8790 {
8791 arg[len++] = KS_EXTRA;
8792 arg[len++] = (int)KE_CSI;
8793 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 }
8796 arg[len] = NUL;
8797 }
8798 }
8799 }
8800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008802 ++ex_normal_busy;
8803 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {
8805 /*
8806 * Repeat the :normal command for each line in the range. When no
8807 * range given, execute it just once, without positioning the cursor
8808 * first.
8809 */
8810 do
8811 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 if (eap->addr_count != 0)
8813 {
8814 curwin->w_cursor.lnum = eap->line1++;
8815 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008816 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 }
8818
Bram Moolenaar13505972019-01-24 15:04:48 +01008819 exec_normal_cmd(arg != NULL
8820 ? arg
8821 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 }
8823 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8824 }
8825
8826 /* Might not return to the main loop when in an event handler. */
8827 update_topline_cursor();
8828
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008829 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008831#ifdef FEAT_MOUSE
8832 setmouse();
8833#endif
8834#ifdef CURSOR_SHAPE
8835 ui_cursor_shape(); /* may show different cursor shape */
8836#endif
8837
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
8841/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008842 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 */
8844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008845ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008847 if (eap->forceit)
8848 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008849 /* cursor line can be zero on startup */
8850 if (!curwin->w_cursor.lnum)
8851 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008852 coladvance((colnr_T)MAXCOL);
8853 curwin->w_curswant = MAXCOL;
8854 curwin->w_set_curswant = FALSE;
8855 }
8856
Bram Moolenaara40c5002005-01-09 21:16:21 +00008857 /* Ignore the command when already in Insert mode. Inserting an
8858 * expression register that invokes a function can do this. */
8859 if (State & INSERT)
8860 return;
8861
Bram Moolenaar64969662005-12-14 21:59:55 +00008862 if (eap->cmdidx == CMD_startinsert)
8863 restart_edit = 'a';
8864 else if (eap->cmdidx == CMD_startreplace)
8865 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008867 restart_edit = 'V';
8868
8869 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008871 if (eap->cmdidx == CMD_startinsert)
8872 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 curwin->w_curswant = 0; /* avoid MAXCOL */
8874 }
8875}
8876
8877/*
8878 * ":stopinsert"
8879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008881ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882{
8883 restart_edit = 0;
8884 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008885 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008888/*
8889 * Execute normal mode command "cmd".
8890 * "remap" can be REMAP_NONE or REMAP_YES.
8891 */
8892 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008893exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008894{
Bram Moolenaar25281632016-01-21 23:32:32 +01008895 /* Stuff the argument into the typeahead buffer. */
8896 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008897 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008898}
Bram Moolenaar25281632016-01-21 23:32:32 +01008899
Bram Moolenaar25281632016-01-21 23:32:32 +01008900/*
8901 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008902 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008903 */
8904 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008905exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008906{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008907 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008908 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008909
Bram Moolenaar905dd902019-04-07 14:21:47 +02008910 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8911 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008912 clear_oparg(&oa);
8913 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008914 while ((!stuff_empty()
8915 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008916 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008917 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008918 {
8919 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008920#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008921 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008922 && oa.op_type == OP_NOP && oa.regname == NUL
8923 && !VIsual_active)
8924 {
8925 /* If terminal_loop() returns OK we got a key that is handled
8926 * in Normal model. With FAIL we first need to position the
8927 * cursor and the screen needs to be redrawn. */
8928 if (terminal_loop(TRUE) == OK)
8929 normal_cmd(&oa, TRUE);
8930 }
8931 else
8932#endif
8933 /* execute a Normal mode cmd */
8934 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008935 }
8936}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938#ifdef FEAT_FIND_ID
8939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008940ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941{
8942 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8943 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8944 (linenr_T)1, (linenr_T)MAXLNUM);
8945}
8946
Bram Moolenaar4033c552017-09-16 20:54:51 +02008947#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948/*
8949 * ":psearch"
8950 */
8951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008952ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953{
8954 g_do_tagpreview = p_pvh;
8955 ex_findpat(eap);
8956 g_do_tagpreview = 0;
8957}
8958#endif
8959
8960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008961ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
8963 int whole = TRUE;
8964 long n;
8965 char_u *p;
8966 int action;
8967
8968 switch (cmdnames[eap->cmdidx].cmd_name[2])
8969 {
8970 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8971 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8972 action = ACTION_GOTO;
8973 else
8974 action = ACTION_SHOW;
8975 break;
8976 case 'i': /* ":ilist" and ":dlist" */
8977 action = ACTION_SHOW_ALL;
8978 break;
8979 case 'u': /* ":ijump" and ":djump" */
8980 action = ACTION_GOTO;
8981 break;
8982 default: /* ":isplit" and ":dsplit" */
8983 action = ACTION_SPLIT;
8984 break;
8985 }
8986
8987 n = 1;
8988 if (vim_isdigit(*eap->arg)) /* get count */
8989 {
8990 n = getdigits(&eap->arg);
8991 eap->arg = skipwhite(eap->arg);
8992 }
8993 if (*eap->arg == '/') /* Match regexp, not just whole words */
8994 {
8995 whole = FALSE;
8996 ++eap->arg;
8997 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8998 if (*p)
8999 {
9000 *p++ = NUL;
9001 p = skipwhite(p);
9002
9003 /* Check for trailing illegal characters */
9004 if (!ends_excmd(*p))
9005 eap->errmsg = e_trailing;
9006 else
9007 eap->nextcmd = check_nextcmd(p);
9008 }
9009 }
9010 if (!eap->skip)
9011 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9012 whole, !eap->forceit,
9013 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9014 n, action, eap->line1, eap->line2);
9015}
9016#endif
9017
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018
Bram Moolenaar4033c552017-09-16 20:54:51 +02009019#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020/*
9021 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9022 */
9023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009024ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009026 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9028}
9029
9030/*
9031 * ":pedit"
9032 */
9033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009034ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
9036 win_T *curwin_save = curwin;
9037
9038 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009039 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009040 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 do_exedit(eap, NULL);
9042 keep_help_flag = FALSE;
9043 if (curwin != curwin_save && win_valid(curwin_save))
9044 {
9045 /* Return cursor to where we were */
9046 validate_cursor();
9047 redraw_later(VALID);
9048 win_enter(curwin_save, TRUE);
9049 }
9050 g_do_tagpreview = 0;
9051}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054/*
9055 * ":stag", ":stselect" and ":stjump".
9056 */
9057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009058ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 postponed_split = -1;
9061 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009062 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9064 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009065 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
9068/*
9069 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9070 */
9071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009072ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073{
9074 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9075}
9076
9077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009078ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 int cmd;
9081
9082 switch (name[1])
9083 {
9084 case 'j': cmd = DT_JUMP; /* ":tjump" */
9085 break;
9086 case 's': cmd = DT_SELECT; /* ":tselect" */
9087 break;
9088 case 'p': cmd = DT_PREV; /* ":tprevious" */
9089 break;
9090 case 'N': cmd = DT_PREV; /* ":tNext" */
9091 break;
9092 case 'n': cmd = DT_NEXT; /* ":tnext" */
9093 break;
9094 case 'o': cmd = DT_POP; /* ":pop" */
9095 break;
9096 case 'f': /* ":tfirst" */
9097 case 'r': cmd = DT_FIRST; /* ":trewind" */
9098 break;
9099 case 'l': cmd = DT_LAST; /* ":tlast" */
9100 break;
9101 default: /* ":tag" */
9102#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009103 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009105 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 return;
9107 }
9108#endif
9109 cmd = DT_TAG;
9110 break;
9111 }
9112
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009113 if (name[0] == 'l')
9114 {
9115#ifndef FEAT_QUICKFIX
9116 ex_ni(eap);
9117 return;
9118#else
9119 cmd = DT_LTAG;
9120#endif
9121 }
9122
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9124 eap->forceit, TRUE);
9125}
9126
9127/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009128 * Check "str" for starting with a special cmdline variable.
9129 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9130 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9131 */
9132 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009133find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009134{
9135 int len;
9136 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009137 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009138 "%",
9139#define SPEC_PERC 0
9140 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009141#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009142 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009143#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009144 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009145#define SPEC_CCWORD (SPEC_CWORD + 1)
9146 "<cexpr>", /* expr under cursor */
9147#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009148 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009149#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009150 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009151#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009152 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009153#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009154 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009155#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009156 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009157#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009158 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009159#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009160 "<sflnum>", /* script file line number */
9161#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009162#ifdef FEAT_CLIENTSERVER
9163 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009164# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009165#endif
9166 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009167
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009168 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009169 {
9170 len = (int)STRLEN(spec_str[i]);
9171 if (STRNCMP(src, spec_str[i], len) == 0)
9172 {
9173 *usedlen = len;
9174 return i;
9175 }
9176 }
9177 return -1;
9178}
9179
9180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 * Evaluate cmdline variables.
9182 *
9183 * change '%' to curbuf->b_ffname
9184 * '#' to curwin->w_altfile
9185 * '<cword>' to word under the cursor
9186 * '<cWORD>' to WORD under the cursor
9187 * '<cfile>' to path name under the cursor
9188 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009189 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 * '<afile>' to file name for autocommand
9191 * '<abuf>' to buffer number for autocommand
9192 * '<amatch>' to matching name for autocommand
9193 *
9194 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9195 * "" for error without a message) and NULL is returned.
9196 * Returns an allocated string if a valid match was found.
9197 * Returns NULL if no match was found. "usedlen" then still contains the
9198 * number of characters to skip.
9199 */
9200 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009201eval_vars(
9202 char_u *src, /* pointer into commandline */
9203 char_u *srcstart, /* beginning of valid memory for src */
9204 int *usedlen, /* characters after src that are used */
9205 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009206 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009207 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009208 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
9210 int i;
9211 char_u *s;
9212 char_u *result;
9213 char_u *resultbuf = NULL;
9214 int resultlen;
9215 buf_T *buf;
9216 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9217 int spec_idx;
9218#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009219 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 int skip_mod = FALSE;
9221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223
9224 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009225 if (escaped != NULL)
9226 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227
9228 /*
9229 * Check if there is something to do.
9230 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009231 spec_idx = find_cmdline_var(src, usedlen);
9232 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233 {
9234 *usedlen = 1;
9235 return NULL;
9236 }
9237
9238 /*
9239 * Skip when preceded with a backslash "\%" and "\#".
9240 * Note: In "\\%" the % is also not recognized!
9241 */
9242 if (src > srcstart && src[-1] == '\\')
9243 {
9244 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009245 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 return NULL;
9247 }
9248
9249 /*
9250 * word or WORD under cursor
9251 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009252 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9253 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009255 resultlen = find_ident_under_cursor(&result,
9256 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9257 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9258 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 if (resultlen == 0)
9260 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009261 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 return NULL;
9263 }
9264 }
9265
9266 /*
9267 * '#': Alternate file name
9268 * '%': Current file name
9269 * File name under the cursor
9270 * File name for autocommand
9271 * and following modifiers
9272 */
9273 else
9274 {
9275 switch (spec_idx)
9276 {
9277 case SPEC_PERC: /* '%': current file */
9278 if (curbuf->b_fname == NULL)
9279 {
9280 result = (char_u *)"";
9281 valid = 0; /* Must have ":p:h" to be valid */
9282 }
9283 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009286#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009287 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009288#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 break;
9291
9292 case SPEC_HASH: /* '#' or "#99": alternate file */
9293 if (src[1] == '#') /* "##": the argument list */
9294 {
9295 result = arg_all();
9296 resultbuf = result;
9297 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009298 if (escaped != NULL)
9299 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300#ifdef FEAT_MODIFY_FNAME
9301 skip_mod = TRUE;
9302#endif
9303 break;
9304 }
9305 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009306 if (*s == '<') /* "#<99" uses v:oldfiles */
9307 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009309 if (s == src + 2 && src[1] == '-')
9310 /* just a minus sign, don't skip over it */
9311 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 *usedlen = (int)(s - src); /* length of what we expand */
9313
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009314 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009316 if (*usedlen < 2)
9317 {
9318 /* Should we give an error message for #<text? */
9319 *usedlen = 1;
9320 return NULL;
9321 }
9322#ifdef FEAT_EVAL
9323 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9324 (long)i);
9325 if (result == NULL)
9326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009327 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009328 return NULL;
9329 }
9330#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009331 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 }
9335 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009336 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009337 if (i == 0 && src[1] == '<' && *usedlen > 1)
9338 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009339 buf = buflist_findnr(i);
9340 if (buf == NULL)
9341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009342 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009343 return NULL;
9344 }
9345 if (lnump != NULL)
9346 *lnump = ECMD_LAST;
9347 if (buf->b_fname == NULL)
9348 {
9349 result = (char_u *)"";
9350 valid = 0; /* Must have ":p:h" to be valid */
9351 }
9352 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009353 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009354 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009355#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009356 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009357#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009358 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 break;
9361
9362#ifdef FEAT_SEARCHPATH
9363 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009364 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 if (result == NULL)
9366 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009367 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 return NULL;
9369 }
9370 resultbuf = result; /* remember allocated string */
9371 break;
9372#endif
9373
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374 case SPEC_AFILE: /* file name for autocommand */
9375 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009376 if (result != NULL && !autocmd_fname_full)
9377 {
9378 /* Still need to turn the fname into a full path. It is
9379 * postponed to avoid a delay when <afile> is not used. */
9380 autocmd_fname_full = TRUE;
9381 result = FullName_save(autocmd_fname, FALSE);
9382 vim_free(autocmd_fname);
9383 autocmd_fname = result;
9384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 if (result == NULL)
9386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009387 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 return NULL;
9389 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009390 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 break;
9392
9393 case SPEC_ABUF: /* buffer number for autocommand */
9394 if (autocmd_bufnr <= 0)
9395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009396 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 return NULL;
9398 }
9399 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9400 result = strbuf;
9401 break;
9402
9403 case SPEC_AMATCH: /* match name for autocommand */
9404 result = autocmd_match;
9405 if (result == NULL)
9406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009407 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 return NULL;
9409 }
9410 break;
9411
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 case SPEC_SFILE: /* file name for ":so" command */
9413 result = sourcing_name;
9414 if (result == NULL)
9415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009416 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 return NULL;
9418 }
9419 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009420
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009421 case SPEC_SLNUM: /* line in file for ":so" command */
9422 if (sourcing_name == NULL || sourcing_lnum == 0)
9423 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009424 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009425 return NULL;
9426 }
9427 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9428 result = strbuf;
9429 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009430
9431#ifdef FEAT_EVAL
9432 case SPEC_SFLNUM: /* line in script file */
9433 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9434 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009435 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009436 return NULL;
9437 }
9438 sprintf((char *)strbuf, "%ld",
9439 (long)(current_sctx.sc_lnum + sourcing_lnum));
9440 result = strbuf;
9441 break;
9442#endif
9443
9444#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009446 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9447 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 result = strbuf;
9449 break;
9450#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009451
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009452 default:
9453 result = (char_u *)""; /* avoid gcc warning */
9454 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 }
9456
9457 resultlen = (int)STRLEN(result); /* length of new string */
9458 if (src[*usedlen] == '<') /* remove the file name extension */
9459 {
9460 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009462 resultlen = (int)(s - result);
9463 }
9464#ifdef FEAT_MODIFY_FNAME
9465 else if (!skip_mod)
9466 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009467 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 &resultlen);
9469 if (result == NULL)
9470 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009471 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 return NULL;
9473 }
9474 }
9475#endif
9476 }
9477
9478 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9479 {
9480 if (valid != VALID_HEAD + VALID_PATH)
9481 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009482 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009484 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 result = NULL;
9486 }
9487 else
9488 result = vim_strnsave(result, resultlen);
9489 vim_free(resultbuf);
9490 return result;
9491}
9492
9493/*
9494 * Concatenate all files in the argument list, separated by spaces, and return
9495 * it in one allocated string.
9496 * Spaces and backslashes in the file names are escaped with a backslash.
9497 * Returns NULL when out of memory.
9498 */
9499 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009500arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502 int len;
9503 int idx;
9504 char_u *retval = NULL;
9505 char_u *p;
9506
9507 /*
9508 * Do this loop two times:
9509 * first time: compute the total length
9510 * second time: concatenate the names
9511 */
9512 for (;;)
9513 {
9514 len = 0;
9515 for (idx = 0; idx < ARGCOUNT; ++idx)
9516 {
9517 p = alist_name(&ARGLIST[idx]);
9518 if (p != NULL)
9519 {
9520 if (len > 0)
9521 {
9522 /* insert a space in between names */
9523 if (retval != NULL)
9524 retval[len] = ' ';
9525 ++len;
9526 }
9527 for ( ; *p != NUL; ++p)
9528 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009529 if (*p == ' '
9530#ifndef BACKSLASH_IN_FILENAME
9531 || *p == '\\'
9532#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009533 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 {
9535 /* insert a backslash */
9536 if (retval != NULL)
9537 retval[len] = '\\';
9538 ++len;
9539 }
9540 if (retval != NULL)
9541 retval[len] = *p;
9542 ++len;
9543 }
9544 }
9545 }
9546
9547 /* second time: break here */
9548 if (retval != NULL)
9549 {
9550 retval[len] = NUL;
9551 break;
9552 }
9553
9554 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009555 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 if (retval == NULL)
9557 break;
9558 }
9559
9560 return retval;
9561}
9562
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563/*
9564 * Expand the <sfile> string in "arg".
9565 *
9566 * Returns an allocated string, or NULL for any error.
9567 */
9568 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009569expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009571 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 int len;
9573 char_u *result;
9574 char_u *newres;
9575 char_u *repl;
9576 int srclen;
9577 char_u *p;
9578
9579 result = vim_strsave(arg);
9580 if (result == NULL)
9581 return NULL;
9582
9583 for (p = result; *p; )
9584 {
9585 if (STRNCMP(p, "<sfile>", 7) != 0)
9586 ++p;
9587 else
9588 {
9589 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009590 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 if (errormsg != NULL)
9592 {
9593 if (*errormsg)
9594 emsg(errormsg);
9595 vim_free(result);
9596 return NULL;
9597 }
9598 if (repl == NULL) /* no match (cannot happen) */
9599 {
9600 p += srclen;
9601 continue;
9602 }
9603 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9604 newres = alloc(len);
9605 if (newres == NULL)
9606 {
9607 vim_free(repl);
9608 vim_free(result);
9609 return NULL;
9610 }
9611 mch_memmove(newres, result, (size_t)(p - result));
9612 STRCPY(newres + (p - result), repl);
9613 len = (int)STRLEN(newres);
9614 STRCAT(newres, p + srclen);
9615 vim_free(repl);
9616 vim_free(result);
9617 result = newres;
9618 p = newres + len; /* continue after the match */
9619 }
9620 }
9621
9622 return result;
9623}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624
9625#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009626static int ses_winsizes(FILE *fd, int restore_size,
9627 win_T *tab_firstwin);
9628static int ses_win_rec(FILE *fd, frame_T *fr);
9629static frame_T *ses_skipframe(frame_T *fr);
9630static int ses_do_frame(frame_T *fr);
9631static int ses_do_win(win_T *wp);
9632static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9633static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009634static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635
9636/*
9637 * Write openfile commands for the current buffers to an .exrc file.
9638 * Return FAIL on error, OK otherwise.
9639 */
9640 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009641makeopens(
9642 FILE *fd,
9643 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
9645 buf_T *buf;
9646 int only_save_windows = TRUE;
9647 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 int restore_size = TRUE;
9649 win_T *wp;
9650 char_u *sname;
9651 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009652 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009653 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009654 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009655 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009656 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009657 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658
9659 if (ssop_flags & SSOP_BUFFERS)
9660 only_save_windows = FALSE; /* Save ALL buffers */
9661
9662 /*
9663 * Begin by setting the this_session variable, and then other
9664 * sessionable variables.
9665 */
9666#ifdef FEAT_EVAL
9667 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9668 return FAIL;
9669 if (ssop_flags & SSOP_GLOBALS)
9670 if (store_session_globals(fd) == FAIL)
9671 return FAIL;
9672#endif
9673
9674 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009675 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 */
9677 if (put_line(fd, "silent only") == FAIL)
9678 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009679 if ((ssop_flags & SSOP_TABPAGES)
9680 && put_line(fd, "silent tabonly") == FAIL)
9681 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682
9683 /*
9684 * Now a :cd command to the session directory or the current directory
9685 */
9686 if (ssop_flags & SSOP_SESDIR)
9687 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009688 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9689 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 return FAIL;
9691 }
9692 else if (ssop_flags & SSOP_CURDIR)
9693 {
9694 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9695 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009696 || fputs("cd ", fd) < 0
9697 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9698 || put_eol(fd) == FAIL)
9699 {
9700 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 vim_free(sname);
9704 }
9705
9706 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009707 * If there is an empty, unnamed buffer we will wipe it out later.
9708 * Remember the buffer number.
9709 */
9710 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9711 return FAIL;
9712 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9713 return FAIL;
9714 if (put_line(fd, "endif") == FAIL)
9715 return FAIL;
9716
9717 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 * Now save the current files, current buffer first.
9719 */
9720 if (put_line(fd, "set shortmess=aoO") == FAIL)
9721 return FAIL;
9722
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009724 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9726 return FAIL;
9727
9728 if (ssop_flags & SSOP_RESIZE)
9729 {
9730 /* Note: after the restore we still check it worked!*/
9731 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9732 || put_eol(fd) == FAIL)
9733 return FAIL;
9734 }
9735
9736#ifdef FEAT_GUI
9737 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9738 {
9739 int x, y;
9740
9741 if (gui_mch_get_winpos(&x, &y) == OK)
9742 {
9743 /* Note: after the restore we still check it worked!*/
9744 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9745 return FAIL;
9746 }
9747 }
9748#endif
9749
9750 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009751 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9752 * will be displayed when creating the next tab. That resizes the windows
9753 * in the first tab, which may cause problems. Set 'showtabline' to 2
9754 * temporarily to avoid that.
9755 */
9756 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9757 {
9758 if (put_line(fd, "set stal=2") == FAIL)
9759 return FAIL;
9760 restore_stal = TRUE;
9761 }
9762
9763 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009764 * May repeat putting Windows for each tab, when "tabpages" is in
9765 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009766 * Don't use goto_tabpage(), it may change directory and trigger
9767 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009769 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009770 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009771 if ((ssop_flags & SSOP_TABPAGES))
9772 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009773 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009774
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009775 // Similar to ses_win_rec() below, populate the tab pages first so
9776 // later local options won't be copied to the new tabs.
9777 FOR_ALL_TABPAGES(tp)
9778 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009779 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009780 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009781 return FAIL;
9782 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009783 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009785 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009786 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009787 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009788
Bram Moolenaar18144c82006-04-12 21:52:12 +00009789 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009791 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009792
9793 if (tp == NULL)
9794 break; /* done all tab pages */
9795 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009796 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009797 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009798 tab_topframe = topframe;
9799 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009800 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009801 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009802 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009803 tab_topframe = tp->tp_topframe;
9804 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009805 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009806 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
Bram Moolenaar18144c82006-04-12 21:52:12 +00009809 /*
9810 * Before creating the window layout, try loading one file. If this
9811 * is aborted we don't end up with a number of useless windows.
9812 * This may have side effects! (e.g., compressed or network file).
9813 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009814 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009815 {
9816 if (ses_do_win(wp)
9817 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009818 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009819#ifdef FEAT_QUICKFIX
9820 && !bt_nofile(wp->w_buffer)
9821#endif
9822 )
9823 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009824 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9825 return FAIL;
9826 need_tabnext = FALSE;
9827
9828 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009829 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9830 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009831 return FAIL;
9832 if (!wp->w_arg_idx_invalid)
9833 edited_win = wp;
9834 break;
9835 }
9836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009838 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009839 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009840 return FAIL;
9841
Bram Moolenaar18144c82006-04-12 21:52:12 +00009842 /*
9843 * Save current window layout.
9844 */
9845 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009847 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009849 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9850 return FAIL;
9851 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9852 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
Bram Moolenaar18144c82006-04-12 21:52:12 +00009854 /*
9855 * Check if window sizes can be restored (no windows omitted).
9856 * Remember the window number of the current window after restoring.
9857 */
9858 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009859 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009860 {
9861 if (ses_do_win(wp))
9862 ++nr;
9863 else
9864 restore_size = FALSE;
9865 if (curwin == wp)
9866 cnr = nr;
9867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
Bram Moolenaar18144c82006-04-12 21:52:12 +00009869 /* Go to the first window. */
9870 if (put_line(fd, "wincmd t") == FAIL)
9871 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009872
Bram Moolenaar18144c82006-04-12 21:52:12 +00009873 /*
9874 * If more than one window, see if sizes can be restored.
9875 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9876 * resized when moving between windows.
9877 * Do this before restoring the view, so that the topline and the
9878 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009879 * winminheight and winminwidth need to be set to avoid an error if the
9880 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009881 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009882 if (put_line(fd, "set winminheight=0") == FAIL
9883 || put_line(fd, "set winheight=1") == FAIL
9884 || put_line(fd, "set winminwidth=0") == FAIL
9885 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009886 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009887 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009888 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009890 // Restore the tab-local working directory if specified
9891 // Do this before the windows, so that the window-local directory can
9892 // override the tab-local directory.
9893 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9894 {
9895 if (fputs("tcd ", fd) < 0
9896 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9897 || put_eol(fd) == FAIL)
9898 return FAIL;
9899 did_lcd = TRUE;
9900 }
9901
Bram Moolenaar18144c82006-04-12 21:52:12 +00009902 /*
9903 * Restore the view of the window (options, file, cursor, etc.).
9904 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009905 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009906 {
9907 if (!ses_do_win(wp))
9908 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009909 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
9910 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009911 return FAIL;
9912 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9913 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009914 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009915 }
9916
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009917 /* The argument index in the first tab page is zero, need to set it in
9918 * each window. For further tab pages it's the window where we do
9919 * "tabedit". */
9920 cur_arg_idx = next_arg_idx;
9921
Bram Moolenaar18144c82006-04-12 21:52:12 +00009922 /*
9923 * Restore cursor to the current window if it's not the first one.
9924 */
9925 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
9926 || put_eol(fd) == FAIL))
9927 return FAIL;
9928
9929 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009930 * Restore window sizes again after jumping around in windows, because
9931 * the current window has a minimum size while others may not.
9932 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009933 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009934 return FAIL;
9935
Bram Moolenaar18144c82006-04-12 21:52:12 +00009936 /* Don't continue in another tab page when doing only the current one
9937 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009938 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009939 break;
9940 }
9941
9942 if (ssop_flags & SSOP_TABPAGES)
9943 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009944 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
9945 || put_eol(fd) == FAIL)
9946 return FAIL;
9947 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009948 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
9949 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009950
Bram Moolenaard39e2752019-01-26 20:07:38 +01009951 // Now put the remaining buffers into the buffer list.
9952 // This is near the end, so that when 'hidden' is set we don't create extra
9953 // buffers. If the buffer was already created with another command the
9954 // ":badd" will have no effect.
9955 FOR_ALL_BUFFERS(buf)
9956 {
9957 if (!(only_save_windows && buf->b_nwindows == 0)
9958 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9959#ifdef FEAT_TERMINAL
9960 // Skip terminal buffers: finished ones are not useful, others
9961 // will be resurrected and result in a new buffer.
9962 && !bt_terminal(buf)
9963#endif
9964 && buf->b_fname != NULL
9965 && buf->b_p_bl)
9966 {
9967 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9968 : buf->b_wininfo->wi_fpos.lnum) < 0
9969 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
9970 return FAIL;
9971 }
9972 }
9973
Bram Moolenaar9c102382006-05-03 21:26:49 +00009974 /*
9975 * Wipe out an empty unnamed buffer we started in.
9976 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009977 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01009978 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009979 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00009980 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009981 return FAIL;
9982 if (put_line(fd, "endif") == FAIL)
9983 return FAIL;
9984 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9985 return FAIL;
9986
9987 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9988 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9989 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9990 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009991 /* Re-apply 'winminheight' and 'winminwidth'. */
9992 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
9993 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
9994 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995
9996 /*
9997 * Lastly, execute the x.vim file if it exists.
9998 */
9999 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10000 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010001 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002 || put_line(fd, "endif") == FAIL)
10003 return FAIL;
10004
10005 return OK;
10006}
10007
10008 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010009ses_winsizes(
10010 FILE *fd,
10011 int restore_size,
10012 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013{
10014 int n = 0;
10015 win_T *wp;
10016
10017 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10018 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010019 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010020 {
10021 if (!ses_do_win(wp))
10022 continue;
10023 ++n;
10024
10025 /* restore height when not full height */
10026 if (wp->w_height + wp->w_status_height < topframe->fr_height
10027 && (fprintf(fd,
10028 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10029 n, (long)wp->w_height, Rows / 2, Rows) < 0
10030 || put_eol(fd) == FAIL))
10031 return FAIL;
10032
10033 /* restore width when not full width */
10034 if (wp->w_width < Columns && (fprintf(fd,
10035 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10036 n, (long)wp->w_width, Columns / 2, Columns) < 0
10037 || put_eol(fd) == FAIL))
10038 return FAIL;
10039 }
10040 }
10041 else
10042 {
10043 /* Just equalise window sizes */
10044 if (put_line(fd, "wincmd =") == FAIL)
10045 return FAIL;
10046 }
10047 return OK;
10048}
10049
10050/*
10051 * Write commands to "fd" to recursively create windows for frame "fr",
10052 * horizontally and vertically split.
10053 * After the commands the last window in the frame is the current window.
10054 * Returns FAIL when writing the commands to "fd" fails.
10055 */
10056 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010057ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 frame_T *frc;
10060 int count = 0;
10061
10062 if (fr->fr_layout != FR_LEAF)
10063 {
10064 /* Find first frame that's not skipped and then create a window for
10065 * each following one (first frame is already there). */
10066 frc = ses_skipframe(fr->fr_child);
10067 if (frc != NULL)
10068 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10069 {
10070 /* Make window as big as possible so that we have lots of room
10071 * to split. */
10072 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10073 || put_line(fd, fr->fr_layout == FR_COL
10074 ? "split" : "vsplit") == FAIL)
10075 return FAIL;
10076 ++count;
10077 }
10078
10079 /* Go back to the first window. */
10080 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10081 ? "%dwincmd k" : "%dwincmd h", count) < 0
10082 || put_eol(fd) == FAIL))
10083 return FAIL;
10084
10085 /* Recursively create frames/windows in each window of this column or
10086 * row. */
10087 frc = ses_skipframe(fr->fr_child);
10088 while (frc != NULL)
10089 {
10090 ses_win_rec(fd, frc);
10091 frc = ses_skipframe(frc->fr_next);
10092 /* Go to next window. */
10093 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10094 return FAIL;
10095 }
10096 }
10097 return OK;
10098}
10099
10100/*
10101 * Skip frames that don't contain windows we want to save in the Session.
10102 * Returns NULL when there none.
10103 */
10104 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010105ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107 frame_T *frc;
10108
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010109 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 if (ses_do_frame(frc))
10111 break;
10112 return frc;
10113}
10114
10115/*
10116 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10117 * the Session.
10118 */
10119 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010120ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121{
10122 frame_T *frc;
10123
10124 if (fr->fr_layout == FR_LEAF)
10125 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010126 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 if (ses_do_frame(frc))
10128 return TRUE;
10129 return FALSE;
10130}
10131
10132/*
10133 * Return non-zero if window "wp" is to be stored in the Session.
10134 */
10135 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010136ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010138#ifdef FEAT_TERMINAL
10139 if (bt_terminal(wp->w_buffer))
10140 return !term_is_finished(wp->w_buffer)
10141 && (ssop_flags & SSOP_TERMINAL)
10142 && term_should_restore(wp->w_buffer);
10143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 if (wp->w_buffer->b_fname == NULL
10145#ifdef FEAT_QUICKFIX
10146 /* When 'buftype' is "nofile" can't restore the window contents. */
10147 || bt_nofile(wp->w_buffer)
10148#endif
10149 )
10150 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010151 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 return (ssop_flags & SSOP_HELP);
10153 return TRUE;
10154}
10155
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010156 static int
10157put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10158{
10159 int r;
10160
10161 if (wp->w_curswant == MAXCOL)
10162 r = fprintf(fd, "%snormal! $", spaces);
10163 else
10164 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10165 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10166}
10167
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168/*
10169 * Write commands to "fd" to restore the view of a window.
10170 * Caller must make sure 'scrolloff' is zero.
10171 */
10172 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010173put_view(
10174 FILE *fd,
10175 win_T *wp,
10176 int add_edit, /* add ":edit" command to view */
10177 unsigned *flagp, /* vop_flags or ssop_flags */
10178 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010179 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180{
10181 win_T *save_curwin;
10182 int f;
10183 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010184 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185
10186 /* Always restore cursor position for ":mksession". For ":mkview" only
10187 * when 'viewoptions' contains "cursor". */
10188 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10189
10190 /*
10191 * Local argument list.
10192 */
10193 if (wp->w_alist == &global_alist)
10194 {
10195 if (put_line(fd, "argglobal") == FAIL)
10196 return FAIL;
10197 }
10198 else
10199 {
10200 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10201 flagp == &vop_flags
10202 || !(*flagp & SSOP_CURDIR)
10203 || wp->w_localdir != NULL, flagp) == FAIL)
10204 return FAIL;
10205 }
10206
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010207 /* Only when part of a session: restore the argument index. Some
10208 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010209 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010210 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010212 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 || put_eol(fd) == FAIL)
10214 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010215 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216 }
10217
10218 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010219 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010221# ifdef FEAT_TERMINAL
10222 if (bt_terminal(wp->w_buffer))
10223 {
10224 if (term_write_session(fd, wp) == FAIL)
10225 return FAIL;
10226 }
10227 else
10228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 /*
10230 * Load the file.
10231 */
10232 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010233# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010235# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 )
10237 {
10238 /*
10239 * Editing a file in this buffer: use ":edit file".
10240 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010241 *
10242 * Note, if a buffer for that file already exists, use :badd to
10243 * edit that buffer, to not lose folding information (:edit resets
10244 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010246 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010247 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010248 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010249 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10250 || fputs(" | else | edit ", fd) < 0
10251 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10252 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010253 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 return FAIL;
10255 }
10256 else
10257 {
10258 /* No file in this buffer, just make it empty. */
10259 if (put_line(fd, "enew") == FAIL)
10260 return FAIL;
10261#ifdef FEAT_QUICKFIX
10262 if (wp->w_buffer->b_ffname != NULL)
10263 {
10264 /* The buffer does have a name, but it's not a file name. */
10265 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010266 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 return FAIL;
10268 }
10269#endif
10270 do_cursor = FALSE;
10271 }
10272 }
10273
10274 /*
10275 * Local mappings and abbreviations.
10276 */
10277 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10278 && makemap(fd, wp->w_buffer) == FAIL)
10279 return FAIL;
10280
10281 /*
10282 * Local options. Need to go to the window temporarily.
10283 * Store only local values when using ":mkview" and when ":mksession" is
10284 * used and 'sessionoptions' doesn't include "options".
10285 * Some folding options are always stored when "folds" is included,
10286 * otherwise the folds would not be restored correctly.
10287 */
10288 save_curwin = curwin;
10289 curwin = wp;
10290 curbuf = curwin->w_buffer;
10291 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10292 f = makeset(fd, OPT_LOCAL,
10293 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10294#ifdef FEAT_FOLDING
10295 else if (*flagp & SSOP_FOLDS)
10296 f = makefoldset(fd);
10297#endif
10298 else
10299 f = OK;
10300 curwin = save_curwin;
10301 curbuf = curwin->w_buffer;
10302 if (f == FAIL)
10303 return FAIL;
10304
10305#ifdef FEAT_FOLDING
10306 /*
10307 * Save Folds when 'buftype' is empty and for help files.
10308 */
10309 if ((*flagp & SSOP_FOLDS)
10310 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010311 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 {
10313 if (put_folds(fd, wp) == FAIL)
10314 return FAIL;
10315 }
10316#endif
10317
10318 /*
10319 * Set the cursor after creating folds, since that moves the cursor.
10320 */
10321 if (do_cursor)
10322 {
10323
10324 /* Restore the cursor line in the file and relatively in the
10325 * window. Don't use "G", it changes the jumplist. */
10326 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10327 (long)wp->w_cursor.lnum,
10328 (long)(wp->w_cursor.lnum - wp->w_topline),
10329 (long)wp->w_height / 2, (long)wp->w_height) < 0
10330 || put_eol(fd) == FAIL
10331 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10332 || put_line(fd, "exe s:l") == FAIL
10333 || put_line(fd, "normal! zt") == FAIL
10334 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10335 || put_eol(fd) == FAIL)
10336 return FAIL;
10337 /* Restore the cursor column and left offset when not wrapping. */
10338 if (wp->w_cursor.col == 0)
10339 {
10340 if (put_line(fd, "normal! 0") == FAIL)
10341 return FAIL;
10342 }
10343 else
10344 {
10345 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10346 {
10347 if (fprintf(fd,
10348 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010349 (long)wp->w_virtcol + 1,
10350 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010351 (long)wp->w_width / 2, (long)wp->w_width) < 0
10352 || put_eol(fd) == FAIL
10353 || put_line(fd, "if s:c > 0") == FAIL
10354 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010355 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10356 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 || put_eol(fd) == FAIL
10358 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010359 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 || put_line(fd, "endif") == FAIL)
10361 return FAIL;
10362 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010363 else if (put_view_curpos(fd, wp, "") == FAIL)
10364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 }
10366 }
10367
10368 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010369 * Local directory, if the current flag is not view options or the "curdir"
10370 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010372 if (wp->w_localdir != NULL
10373 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 {
10375 if (fputs("lcd ", fd) < 0
10376 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10377 || put_eol(fd) == FAIL)
10378 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010379 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380 }
10381
10382 return OK;
10383}
10384
10385/*
10386 * Write an argument list to the session file.
10387 * Returns FAIL if writing fails.
10388 */
10389 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010390ses_arglist(
10391 FILE *fd,
10392 char *cmd,
10393 garray_T *gap,
10394 int fullname, /* TRUE: use full path name */
10395 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396{
10397 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010398 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399 char_u *s;
10400
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010401 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10402 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010403 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 return FAIL;
10405 for (i = 0; i < gap->ga_len; ++i)
10406 {
10407 /* NULL file names are skipped (only happens when out of memory). */
10408 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10409 if (s != NULL)
10410 {
10411 if (fullname)
10412 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010413 buf = alloc(MAXPATHL);
10414 if (buf != NULL)
10415 {
10416 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10417 s = buf;
10418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010420 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010421 || ses_put_fname(fd, s, flagp) == FAIL
10422 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010423 {
10424 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010426 }
10427 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 }
10429 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010430 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431}
10432
10433/*
10434 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010435 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 * Returns FAIL if writing fails.
10437 */
10438 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010439ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
10441 char_u *name;
10442
10443 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010444 * the session file will be sourced.
10445 * Don't do this for ":mkview", we don't know the current directory.
10446 * Don't do this after ":lcd", we don't keep track of what the current
10447 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 if (buf->b_sfname != NULL
10449 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010450 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010451#ifdef FEAT_AUTOCHDIR
10452 && !p_acd
10453#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010454 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 name = buf->b_sfname;
10456 else
10457 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010458 if (ses_put_fname(fd, name, flagp) == FAIL
10459 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 return FAIL;
10461 return OK;
10462}
10463
10464/*
10465 * Write a file name to the session file.
10466 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10467 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010468 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 */
10470 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010471ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010474 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476
10477 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010478 if (sname == NULL)
10479 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010481 if (*flagp & SSOP_SLASH)
10482 {
10483 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010484 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010485 if (*p == '\\')
10486 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010488
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010489 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010490 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010492 if (p == NULL)
10493 return FAIL;
10494
10495 /* write the result */
10496 if (fputs((char *)p, fd) < 0)
10497 retval = FAIL;
10498
10499 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 return retval;
10501}
10502
10503/*
10504 * ":loadview [nr]"
10505 */
10506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010507ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508{
10509 char_u *fname;
10510
10511 fname = get_view_file(*eap->arg);
10512 if (fname != NULL)
10513 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010514 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 vim_free(fname);
10516 }
10517}
10518
10519/*
10520 * Get the name of the view file for the current buffer.
10521 */
10522 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010523get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524{
10525 int len = 0;
10526 char_u *p, *s;
10527 char_u *retval;
10528 char_u *sname;
10529
10530 if (curbuf->b_ffname == NULL)
10531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010532 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 return NULL;
10534 }
10535 sname = home_replace_save(NULL, curbuf->b_ffname);
10536 if (sname == NULL)
10537 return NULL;
10538
10539 /*
10540 * We want a file name without separators, because we're not going to make
10541 * a directory.
10542 * "normal" path separator -> "=+"
10543 * "=" -> "=="
10544 * ":" path separator -> "=-"
10545 */
10546 for (p = sname; *p; ++p)
10547 if (*p == '=' || vim_ispathsep(*p))
10548 ++len;
10549 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10550 if (retval != NULL)
10551 {
10552 STRCPY(retval, p_vdir);
10553 add_pathsep(retval);
10554 s = retval + STRLEN(retval);
10555 for (p = sname; *p; ++p)
10556 {
10557 if (*p == '=')
10558 {
10559 *s++ = '=';
10560 *s++ = '=';
10561 }
10562 else if (vim_ispathsep(*p))
10563 {
10564 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010565#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 if (*p == ':')
10567 *s++ = '-';
10568 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010570 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 }
10572 else
10573 *s++ = *p;
10574 }
10575 *s++ = '=';
10576 *s++ = c;
10577 STRCPY(s, ".vim");
10578 }
10579
10580 vim_free(sname);
10581 return retval;
10582}
10583
10584#endif /* FEAT_SESSION */
10585
10586/*
10587 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10588 * Return FAIL for a write error.
10589 */
10590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010591put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592{
10593 if (
10594#ifdef USE_CRNL
10595 (
10596# ifdef MKSESSION_NL
10597 !mksession_nl &&
10598# endif
10599 (putc('\r', fd) < 0)) ||
10600#endif
10601 (putc('\n', fd) < 0))
10602 return FAIL;
10603 return OK;
10604}
10605
10606/*
10607 * Write a line to "fd".
10608 * Return FAIL for a write error.
10609 */
10610 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010611put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612{
10613 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10614 return FAIL;
10615 return OK;
10616}
10617
10618#ifdef FEAT_VIMINFO
10619/*
10620 * ":rviminfo" and ":wviminfo".
10621 */
10622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010623ex_viminfo(
10624 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625{
10626 char_u *save_viminfo;
10627
10628 save_viminfo = p_viminfo;
10629 if (*p_viminfo == NUL)
10630 p_viminfo = (char_u *)"'100";
10631 if (eap->cmdidx == CMD_rviminfo)
10632 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010633 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10634 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010635 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 }
10637 else
10638 write_viminfo(eap->arg, eap->forceit);
10639 p_viminfo = save_viminfo;
10640}
10641#endif
10642
10643#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010644/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010645 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010646 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010649dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 if (fname == NULL)
10652 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010653 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654}
10655#endif
10656
10657/*
10658 * ":behave {mswin,xterm}"
10659 */
10660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010661ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662{
10663 if (STRCMP(eap->arg, "mswin") == 0)
10664 {
10665 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10666 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10667 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10668 set_option_value((char_u *)"keymodel", 0L,
10669 (char_u *)"startsel,stopsel", 0);
10670 }
10671 else if (STRCMP(eap->arg, "xterm") == 0)
10672 {
10673 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10674 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10675 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10676 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10677 }
10678 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010679 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680}
10681
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010682#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10683/*
10684 * Function given to ExpandGeneric() to obtain the possible arguments of the
10685 * ":behave {mswin,xterm}" command.
10686 */
10687 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010688get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010689{
10690 if (idx == 0)
10691 return (char_u *)"mswin";
10692 if (idx == 1)
10693 return (char_u *)"xterm";
10694 return NULL;
10695}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010696
10697/*
10698 * Function given to ExpandGeneric() to obtain the possible arguments of the
10699 * ":messages {clear}" command.
10700 */
10701 char_u *
10702get_messages_arg(expand_T *xp UNUSED, int idx)
10703{
10704 if (idx == 0)
10705 return (char_u *)"clear";
10706 return NULL;
10707}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010708#endif
10709
Bram Moolenaar113e1072019-01-20 15:30:40 +010010710#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010711 char_u *
10712get_mapclear_arg(expand_T *xp UNUSED, int idx)
10713{
10714 if (idx == 0)
10715 return (char_u *)"<buffer>";
10716 return NULL;
10717}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010718#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010719
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720static int filetype_detect = FALSE;
10721static int filetype_plugin = FALSE;
10722static int filetype_indent = FALSE;
10723
10724/*
10725 * ":filetype [plugin] [indent] {on,off,detect}"
10726 * on: Load the filetype.vim file to install autocommands for file types.
10727 * off: Load the ftoff.vim file to remove all autocommands for file types.
10728 * plugin on: load filetype.vim and ftplugin.vim
10729 * plugin off: load ftplugof.vim
10730 * indent on: load filetype.vim and indent.vim
10731 * indent off: load indoff.vim
10732 */
10733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010734ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735{
10736 char_u *arg = eap->arg;
10737 int plugin = FALSE;
10738 int indent = FALSE;
10739
10740 if (*eap->arg == NUL)
10741 {
10742 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010743 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 filetype_detect ? "ON" : "OFF",
10745 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10746 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10747 return;
10748 }
10749
10750 /* Accept "plugin" and "indent" in any order. */
10751 for (;;)
10752 {
10753 if (STRNCMP(arg, "plugin", 6) == 0)
10754 {
10755 plugin = TRUE;
10756 arg = skipwhite(arg + 6);
10757 continue;
10758 }
10759 if (STRNCMP(arg, "indent", 6) == 0)
10760 {
10761 indent = TRUE;
10762 arg = skipwhite(arg + 6);
10763 continue;
10764 }
10765 break;
10766 }
10767 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10768 {
10769 if (*arg == 'o' || !filetype_detect)
10770 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010771 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 filetype_detect = TRUE;
10773 if (plugin)
10774 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010775 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 filetype_plugin = TRUE;
10777 }
10778 if (indent)
10779 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010780 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781 filetype_indent = TRUE;
10782 }
10783 }
10784 if (*arg == 'd')
10785 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010786 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010787 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788 }
10789 }
10790 else if (STRCMP(arg, "off") == 0)
10791 {
10792 if (plugin || indent)
10793 {
10794 if (plugin)
10795 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010796 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 filetype_plugin = FALSE;
10798 }
10799 if (indent)
10800 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010801 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 filetype_indent = FALSE;
10803 }
10804 }
10805 else
10806 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010807 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808 filetype_detect = FALSE;
10809 }
10810 }
10811 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010812 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813}
10814
10815/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010816 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 */
10818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010819ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010822 {
10823 char_u *arg = eap->arg;
10824
10825 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10826 arg += 9;
10827
10828 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10829 if (arg != eap->arg)
10830 did_filetype = FALSE;
10831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010835ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836{
10837#ifdef FEAT_DIGRAPHS
10838 if (*eap->arg != NUL)
10839 putdigraph(eap->arg);
10840 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010841 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010843 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844#endif
10845}
10846
10847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010848ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849{
10850 int flags = 0;
10851
10852 if (eap->cmdidx == CMD_setlocal)
10853 flags = OPT_LOCAL;
10854 else if (eap->cmdidx == CMD_setglobal)
10855 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010856#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 if (cmdmod.browse && flags == 0)
10858 ex_options(eap);
10859 else
10860#endif
10861 (void)do_set(eap->arg, flags);
10862}
10863
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010864#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10865 void
10866set_no_hlsearch(int flag)
10867{
10868 no_hlsearch = flag;
10869# ifdef FEAT_EVAL
10870 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10871# endif
10872}
10873
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874/*
10875 * ":nohlsearch"
10876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010878ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010880 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010881 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882}
10883
10884/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010885 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 * Sets nextcmd to the start of the next command, if any. Also called when
10887 * skipping commands to find the next command.
10888 */
10889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010890ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891{
10892 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010893 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 char_u *end;
10895 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010896 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010897
10898 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010899 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010900 else
10901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010902 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010903 return;
10904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905
10906 /* First clear any old pattern. */
10907 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010908 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909
10910 if (ends_excmd(*eap->arg))
10911 end = eap->arg;
10912 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010010913 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 end = eap->arg + 4;
10915 else
10916 {
10917 p = skiptowhite(eap->arg);
10918 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010919 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920 p = skipwhite(p);
10921 if (*p == NUL)
10922 {
10923 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010924 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010925 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 return;
10927 }
10928 end = skip_regexp(p + 1, *p, TRUE, NULL);
10929 if (!eap->skip)
10930 {
10931 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10932 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010933 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 eap->errmsg = e_trailing;
10935 return;
10936 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010937 if (*end != *p)
10938 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010939 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010940 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010941 return;
10942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943
10944 c = *end;
10945 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020010946 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010947 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010948 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 }
10950 }
10951 eap->nextcmd = find_nextcmd(end);
10952}
10953#endif
10954
10955#ifdef FEAT_CRYPT
10956/*
10957 * ":X": Get crypt key
10958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010960ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010962 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010963 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964}
10965#endif
10966
10967#ifdef FEAT_FOLDING
10968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010969ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970{
10971 if (foldManualAllowed(TRUE))
10972 foldCreate(eap->line1, eap->line2);
10973}
10974
10975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010976ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
10978 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10979 eap->forceit, FALSE);
10980}
10981
10982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010983ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984{
10985 linenr_T lnum;
10986
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010987#ifdef FEAT_CLIPBOARD
10988 start_global_changes();
10989#endif
10990
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 /* First set the marks for all lines closed/open. */
10992 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10993 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10994 ml_setmarked(lnum);
10995
10996 /* Execute the command on the marked lines. */
10997 global_exe(eap->arg);
10998 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010999#ifdef FEAT_CLIPBOARD
11000 end_global_changes();
11001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002}
11003#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011004
Bram Moolenaar39665952018-08-15 20:59:48 +020011005#ifdef FEAT_QUICKFIX
11006/*
11007 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11008 * instead of a quickfix command.
11009 */
11010 int
11011is_loclist_cmd(int cmdidx)
11012{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011013 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011014 return FALSE;
11015 return cmdnames[cmdidx].cmd_name[0] == 'l';
11016}
11017#endif
11018
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011019# if defined(FEAT_TIMERS) || defined(PROTO)
11020 int
11021get_pressedreturn(void)
11022{
11023 return ex_pressedreturn;
11024}
11025
11026 void
11027set_pressedreturn(int val)
11028{
11029 ex_pressedreturn = val;
11030}
11031#endif