blob: a2b302d92250b7cf4c662ab3f5e60a503730f4c9 [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 Moolenaar25190db2019-05-04 15:05:28 +02001789 // 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 Moolenaar25190db2019-05-04 15:05:28 +02001797 // :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 Moolenaar25190db2019-05-04 15:05:28 +02001800#ifdef FEAT_QUICKFIX
1801 // :.cc in quickfix window uses line number
1802 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1803 ea.addr_type = ADDR_OTHER;
1804#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001805 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001806
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001807 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001808 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001809 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001812 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 */
1814
1815 /*
1816 * Skip ':' and any white space
1817 */
1818 ea.cmd = skipwhite(ea.cmd);
1819 while (*ea.cmd == ':')
1820 ea.cmd = skipwhite(ea.cmd + 1);
1821
1822 /*
1823 * If we got a line, but no command, then go to the line.
1824 * If we find a '|' or '\n' we set ea.nextcmd.
1825 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001826 if (*ea.cmd == NUL || *ea.cmd == '"'
1827 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 /*
1830 * strange vi behaviour:
1831 * ":3" jumps to line 3
1832 * ":3|..." prints line 3
1833 * ":|" prints current line
1834 */
1835 if (ea.skip) /* skip this if inside :if */
1836 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001837 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 {
1839 ea.cmdidx = CMD_print;
1840 ea.argt = RANGE+COUNT+TRLBAR;
1841 if ((errormsg = invalid_range(&ea)) == NULL)
1842 {
1843 correct_range(&ea);
1844 ex_print(&ea);
1845 }
1846 }
1847 else if (ea.addr_count != 0)
1848 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001849 if (ea.line2 > curbuf->b_ml.ml_line_count)
1850 {
1851 /* With '-' in 'cpoptions' a line number past the file is an
1852 * error, otherwise put it at the end of the file. */
1853 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1854 ea.line2 = -1;
1855 else
1856 ea.line2 = curbuf->b_ml.ml_line_count;
1857 }
1858
1859 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001860 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 else
1862 {
1863 if (ea.line2 == 0)
1864 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 else
1866 curwin->w_cursor.lnum = ea.line2;
1867 beginline(BL_SOL | BL_FIX);
1868 }
1869 }
1870 goto doend;
1871 }
1872
Bram Moolenaard5005162014-08-22 23:05:54 +02001873 /* If this looks like an undefined user command and there are CmdUndefined
1874 * autocommands defined, trigger the matching autocommands. */
1875 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1876 && ASCII_ISUPPER(*ea.cmd)
1877 && has_cmdundefined())
1878 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001879 int ret;
1880
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001881 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001882 while (ASCII_ISALNUM(*p))
1883 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001884 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001885 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1886 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001887 /* If the autocommands did something and didn't cause an error, try
1888 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001889 p = (ret
1890#ifdef FEAT_EVAL
1891 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001892#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001893 ) ? find_command(&ea, NULL) : ea.cmd;
1894 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001895
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (p == NULL)
1897 {
1898 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001899 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 goto doend;
1901 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001902 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001903 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1904 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 errormsg = uc_fun_cmd();
1907 goto doend;
1908 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (ea.cmdidx == CMD_SIZE)
1911 {
1912 if (!ea.skip)
1913 {
1914 STRCPY(IObuff, _("E492: Not an editor command"));
1915 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001916 {
1917 /* If the modifier was parsed OK the error must be in the
1918 * following command */
1919 if (after_modifier != NULL)
1920 append_command(after_modifier);
1921 else
1922 append_command(*cmdlinep);
1923 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001924 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001925 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 }
1927 goto doend;
1928 }
1929
Bram Moolenaar958636c2014-10-21 20:01:58 +02001930 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1931 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001932#ifdef HAVE_EX_SCRIPT_NI
1933 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1934#endif
1935 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
1937#ifndef FEAT_EVAL
1938 /*
1939 * When the expression evaluation is disabled, recognize the ":if" and
1940 * ":endif" commands and ignore everything in between it.
1941 */
1942 if (ea.cmdidx == CMD_if)
1943 ++if_level;
1944 if (if_level)
1945 {
1946 if (ea.cmdidx == CMD_endif)
1947 --if_level;
1948 goto doend;
1949 }
1950
1951#endif
1952
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001953 /* forced commands */
1954 if (*p == '!' && ea.cmdidx != CMD_substitute
1955 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 ++p;
1958 ea.forceit = TRUE;
1959 }
1960 else
1961 ea.forceit = FALSE;
1962
1963/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001964 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001966 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001967 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968
1969 if (!ea.skip)
1970 {
1971#ifdef HAVE_SANDBOX
1972 if (sandbox != 0 && !(ea.argt & SBOXOK))
1973 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001974 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001975 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 goto doend;
1977 }
1978#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001979 if (restricted != 0 && (ea.argt & RESTRICT))
1980 {
1981 errormsg = _("E981: Command not allowed in rvim");
1982 goto doend;
1983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
1985 {
1986 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001987 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 goto doend;
1989 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001990
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001991 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001992 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001994 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001995 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 goto doend;
1997 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001998
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001999 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002000 * Do allow ":checktime" (it is postponed).
2001 * Do allow ":edit" (check for an argument later).
2002 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002003 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002004 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002005 && ea.cmdidx != CMD_edit
2006 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002007 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002008 && curbuf_locked())
2009 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010
2011 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2012 {
2013 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002014 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 goto doend;
2016 }
2017 }
2018
2019 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002021 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 goto doend;
2023 }
2024
2025 /*
2026 * Don't complain about the range if it is not used
2027 * (could happen if line_count is accidentally set to 0).
2028 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002029 if (!ea.skip && !ni && (ea.argt & RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
2031 /*
2032 * If the range is backwards, ask for confirmation and, if given, swap
2033 * ea.line1 & ea.line2 so it's forwards again.
2034 * When global command is busy, don't ask, will fail below.
2035 */
2036 if (!global_busy && ea.line1 > ea.line2)
2037 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002038 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002040 if (sourcing || exmode_active)
2041 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002042 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002043 goto doend;
2044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 if (ask_yesno((char_u *)
2046 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002047 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 }
2049 lnum = ea.line1;
2050 ea.line1 = ea.line2;
2051 ea.line2 = lnum;
2052 }
2053 if ((errormsg = invalid_range(&ea)) != NULL)
2054 goto doend;
2055 }
2056
Bram Moolenaarb7316892019-05-01 18:08:42 +02002057 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2058 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 ea.line2 = 1;
2060
2061 correct_range(&ea);
2062
2063#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002064 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2065 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
2067 /* Put the first line at the start of a closed fold, put the last line
2068 * at the end of a closed fold. */
2069 (void)hasFolding(ea.line1, &ea.line1, NULL);
2070 (void)hasFolding(ea.line2, NULL, &ea.line2);
2071 }
2072#endif
2073
2074#ifdef FEAT_QUICKFIX
2075 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002076 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 * option here, so things like % get expanded.
2078 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002079 p = replace_makeprg(&ea, p, cmdlinep);
2080 if (p == NULL)
2081 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082#endif
2083
2084 /*
2085 * Skip to start of argument.
2086 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2087 */
2088 if (ea.cmdidx == CMD_bang)
2089 ea.arg = p;
2090 else
2091 ea.arg = skipwhite(p);
2092
Bram Moolenaar379fb762018-08-30 15:58:28 +02002093 // ":file" cannot be run with an argument when "curbuf_lock" is set
2094 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2095 goto doend;
2096
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 /*
2098 * Check for "++opt=val" argument.
2099 * Must be first, allow ":w ++enc=utf8 !cmd"
2100 */
2101 if (ea.argt & ARGOPT)
2102 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2103 if (getargopt(&ea) == FAIL && !ni)
2104 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002105 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 goto doend;
2107 }
2108
2109 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2110 {
2111 if (*ea.arg == '>') /* append */
2112 {
2113 if (*++ea.arg != '>') /* typed wrong */
2114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002115 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 goto doend;
2117 }
2118 ea.arg = skipwhite(ea.arg + 1);
2119 ea.append = TRUE;
2120 }
2121 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2122 {
2123 ++ea.arg;
2124 ea.usefilter = TRUE;
2125 }
2126 }
2127
2128 if (ea.cmdidx == CMD_read)
2129 {
2130 if (ea.forceit)
2131 {
2132 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2133 ea.forceit = FALSE;
2134 }
2135 else if (*ea.arg == '!') /* :r !filter */
2136 {
2137 ++ea.arg;
2138 ea.usefilter = TRUE;
2139 }
2140 }
2141
2142 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2143 {
2144 ea.amount = 1;
2145 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2146 {
2147 ++ea.arg;
2148 ++ea.amount;
2149 }
2150 ea.arg = skipwhite(ea.arg);
2151 }
2152
2153 /*
2154 * Check for "+command" argument, before checking for next command.
2155 * Don't do this for ":read !cmd" and ":write !cmd".
2156 */
2157 if ((ea.argt & EDITCMD) && !ea.usefilter)
2158 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2159
2160 /*
2161 * Check for '|' to separate commands and '"' to start comments.
2162 * Don't do this for ":read !cmd" and ":write !cmd".
2163 */
2164 if ((ea.argt & TRLBAR) && !ea.usefilter)
2165 separate_nextcmd(&ea);
2166
2167 /*
2168 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002169 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2170 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002172 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002173 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002174 || ea.cmdidx == CMD_global
2175 || ea.cmdidx == CMD_vglobal
2176 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
2178 for (p = ea.arg; *p; ++p)
2179 {
2180 /* Remove one backslash before a newline, so that it's possible to
2181 * pass a newline to the shell and also a newline that is preceded
2182 * with a backslash. This makes it impossible to end a shell
2183 * command in a backslash, but that doesn't appear useful.
2184 * Halving the number of backslashes is incompatible with previous
2185 * versions. */
2186 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002187 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 else if (*p == '\n')
2189 {
2190 ea.nextcmd = p + 1;
2191 *p = NUL;
2192 break;
2193 }
2194 }
2195 }
2196
2197 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2198 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002199 buf_T *buf;
2200
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002202 switch (ea.addr_type)
2203 {
2204 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002205 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002206 ea.line2 = curbuf->b_ml.ml_line_count;
2207 break;
2208 case ADDR_LOADED_BUFFERS:
2209 buf = firstbuf;
2210 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2211 buf = buf->b_next;
2212 ea.line1 = buf->b_fnum;
2213 buf = lastbuf;
2214 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2215 buf = buf->b_prev;
2216 ea.line2 = buf->b_fnum;
2217 break;
2218 case ADDR_BUFFERS:
2219 ea.line1 = firstbuf->b_fnum;
2220 ea.line2 = lastbuf->b_fnum;
2221 break;
2222 case ADDR_WINDOWS:
2223 ea.line2 = LAST_WIN_NR;
2224 break;
2225 case ADDR_TABS:
2226 ea.line2 = LAST_TAB_NR;
2227 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002228 case ADDR_TABS_RELATIVE:
2229 ea.line2 = 1;
2230 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002231 case ADDR_ARGUMENTS:
2232 if (ARGCOUNT == 0)
2233 ea.line1 = ea.line2 = 0;
2234 else
2235 ea.line2 = ARGCOUNT;
2236 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002237 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002238#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002239 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002240 if (ea.line2 == 0)
2241 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002242#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002243 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002244 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002245 case ADDR_UNSIGNED:
2246 case ADDR_QUICKFIX:
2247 iemsg(_("INTERNAL: Cannot use DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002248 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 }
2251
2252 /* accept numbered register only when no count allowed (:put) */
2253 if ( (ea.argt & REGSTR)
2254 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002255 /* Do not allow register = for user commands */
2256 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2258 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002259#ifndef FEAT_CLIPBOARD
2260 /* check these explicitly for a more specific error message */
2261 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002263 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002264 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
2266#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002267 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2268 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002269 {
2270 ea.regname = *ea.arg++;
2271#ifdef FEAT_EVAL
2272 /* for '=' register: accept the rest of the line as an expression */
2273 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2274 {
2275 set_expr_line(vim_strsave(ea.arg));
2276 ea.arg += STRLEN(ea.arg);
2277 }
2278#endif
2279 ea.arg = skipwhite(ea.arg);
2280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282
2283 /*
2284 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2285 * count, it's a buffer name.
2286 */
2287 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2288 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002289 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 n = getdigits(&ea.arg);
2292 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002293 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002295 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 goto doend;
2297 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002298 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
2300 ea.line2 = n;
2301 if (ea.addr_count == 0)
2302 ea.addr_count = 1;
2303 }
2304 else
2305 {
2306 ea.line1 = ea.line2;
2307 ea.line2 += n - 1;
2308 ++ea.addr_count;
2309 /*
2310 * Be vi compatible: no error message for out of range.
2311 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002312 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ea.line2 = curbuf->b_ml.ml_line_count;
2314 }
2315 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002316
2317 /*
2318 * Check for flags: 'l', 'p' and '#'.
2319 */
2320 if (ea.argt & EXFLAGS)
2321 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002323 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002324 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002326 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 goto doend;
2328 }
2329
2330 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002332 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 goto doend;
2334 }
2335
2336#ifdef FEAT_EVAL
2337 /*
2338 * Skip the command when it's not going to be executed.
2339 * The commands like :if, :endif, etc. always need to be executed.
2340 * Also make an exception for commands that handle a trailing command
2341 * themselves.
2342 */
2343 if (ea.skip)
2344 {
2345 switch (ea.cmdidx)
2346 {
2347 /* commands that need evaluation */
2348 case CMD_while:
2349 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002350 case CMD_for:
2351 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 case CMD_if:
2353 case CMD_elseif:
2354 case CMD_else:
2355 case CMD_endif:
2356 case CMD_try:
2357 case CMD_catch:
2358 case CMD_finally:
2359 case CMD_endtry:
2360 case CMD_function:
2361 break;
2362
2363 /* Commands that handle '|' themselves. Check: A command should
2364 * either have the TRLBAR flag, appear in this list or appear in
2365 * the list at ":help :bar". */
2366 case CMD_aboveleft:
2367 case CMD_and:
2368 case CMD_belowright:
2369 case CMD_botright:
2370 case CMD_browse:
2371 case CMD_call:
2372 case CMD_confirm:
2373 case CMD_delfunction:
2374 case CMD_djump:
2375 case CMD_dlist:
2376 case CMD_dsearch:
2377 case CMD_dsplit:
2378 case CMD_echo:
2379 case CMD_echoerr:
2380 case CMD_echomsg:
2381 case CMD_echon:
2382 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002383 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 case CMD_help:
2385 case CMD_hide:
2386 case CMD_ijump:
2387 case CMD_ilist:
2388 case CMD_isearch:
2389 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002390 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_keepjumps:
2392 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002393 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 case CMD_leftabove:
2395 case CMD_let:
2396 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002397 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002399 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002400 case CMD_noautocmd:
2401 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 case CMD_perl:
2403 case CMD_psearch:
2404 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002405 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002406 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 case CMD_return:
2408 case CMD_rightbelow:
2409 case CMD_ruby:
2410 case CMD_silent:
2411 case CMD_smagic:
2412 case CMD_snomagic:
2413 case CMD_substitute:
2414 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002415 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 case CMD_tcl:
2417 case CMD_throw:
2418 case CMD_tilde:
2419 case CMD_topleft:
2420 case CMD_unlet:
2421 case CMD_verbose:
2422 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002423 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 break;
2425
2426 default: goto doend;
2427 }
2428 }
2429#endif
2430
2431 if (ea.argt & XFILE)
2432 {
2433 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2434 goto doend;
2435 }
2436
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 /*
2438 * Accept buffer name. Cannot be used at the same time with a buffer
2439 * number. Don't do this for a user command.
2440 */
2441 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002442 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
2444 /*
2445 * :bdelete, :bwipeout and :bunload take several arguments, separated
2446 * by spaces: find next space (skipping over escaped characters).
2447 * The others take one argument: ignore trailing spaces.
2448 */
2449 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2450 || ea.cmdidx == CMD_bunload)
2451 p = skiptowhite_esc(ea.arg);
2452 else
2453 {
2454 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002455 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 --p;
2457 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002458 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2459 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if (ea.line2 < 0) /* failed */
2461 goto doend;
2462 ea.addr_count = 1;
2463 ea.arg = skipwhite(p);
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
Bram Moolenaar2be57332018-02-13 18:05:18 +01002466 /* The :try command saves the emsg_silent flag, reset it here when
2467 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002468 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002469 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002470 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002471 if (emsg_silent < 0)
2472 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002473 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002474 }
2475
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002477 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
Bram Moolenaar958636c2014-10-21 20:01:58 +02002480 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
2482 /*
2483 * Execute a user-defined command.
2484 */
2485 do_ucmd(&ea);
2486 }
2487 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002490 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 */
2492 ea.errmsg = NULL;
2493 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2494 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002495 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 }
2497
2498#ifdef FEAT_EVAL
2499 /*
2500 * If the command just executed called do_cmdline(), any throw or ":return"
2501 * or ":finish" encountered there must also check the cstack of the still
2502 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2503 * exception, or reanimate a returned function or finished script file and
2504 * return or finish it again.
2505 */
2506 if (need_rethrow)
2507 do_throw(cstack);
2508 else if (check_cstack)
2509 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002510 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002512 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 && current_func_returned())
2514 do_return(&ea, TRUE, FALSE, NULL);
2515 }
2516 need_rethrow = check_cstack = FALSE;
2517#endif
2518
2519doend:
2520 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002521 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002523 curwin->w_cursor.col = 0;
2524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
2526 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2527 {
2528 if (sourcing)
2529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002530 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002533 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002535 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537 emsg(errormsg);
2538 }
2539#ifdef FEAT_EVAL
2540 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002541 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2542 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543#endif
2544
Bram Moolenaareffed932018-08-14 13:38:17 +02002545 if (ea.verbose_save >= 0)
2546 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002547
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002548 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002550 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
Bram Moolenaareffed932018-08-14 13:38:17 +02002552 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 {
2554 /* messages could be enabled for a serious error, need to check if the
2555 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002556 if (!did_emsg || msg_silent > ea.save_msg_silent)
2557 msg_silent = ea.save_msg_silent;
2558 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (emsg_silent < 0)
2560 emsg_silent = 0;
2561 /* Restore msg_scroll, it's set by file I/O commands, even when no
2562 * message is actually displayed. */
2563 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002564
2565 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2566 * somewhere in the line. Put it back in the first column. */
2567 if (redirecting())
2568 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 }
2570
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002571#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002572 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002573 --sandbox;
2574#endif
2575
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2577 ea.nextcmd = NULL;
2578
2579#ifdef FEAT_EVAL
2580 --ex_nesting_level;
2581#endif
2582
2583 return ea.nextcmd;
2584}
2585#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002586 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587#endif
2588
2589/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002590 * Parse and skip over command modifiers:
2591 * - update eap->cmd
2592 * - store flags in "cmdmod".
2593 * - Set ex_pressedreturn for an empty command line.
2594 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002595 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002596 * - set p_verbose for ":verbose"
2597 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002598 * When "skip_only" is TRUE the global variables are not changed, except for
2599 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002600 * Return FAIL when the command is not to be executed.
2601 * May set "errormsg" to an error message.
2602 */
2603 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002604parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002605{
2606 char_u *p;
2607
2608 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2609 eap->verbose_save = -1;
2610 eap->save_msg_silent = -1;
2611
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002612 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002613 for (;;)
2614 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002615 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2616 ++eap->cmd;
2617
2618 /* in ex mode, an empty line works like :+ */
2619 if (*eap->cmd == NUL && exmode_active
2620 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2621 || getline_equal(eap->getline, eap->cookie, getexline))
2622 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2623 {
2624 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002625 if (!skip_only)
2626 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 }
2628
2629 /* ignore comment and empty lines */
2630 if (*eap->cmd == '"')
2631 return FAIL;
2632 if (*eap->cmd == NUL)
2633 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002634 if (!skip_only)
2635 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 return FAIL;
2637 }
2638
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 p = skip_range(eap->cmd, NULL);
2640 switch (*p)
2641 {
2642 /* When adding an entry, also modify cmd_exists(). */
2643 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2644 break;
2645 cmdmod.split |= WSP_ABOVE;
2646 continue;
2647
2648 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2649 {
2650 cmdmod.split |= WSP_BELOW;
2651 continue;
2652 }
2653 if (checkforcmd(&eap->cmd, "browse", 3))
2654 {
2655#ifdef FEAT_BROWSE_CMD
2656 cmdmod.browse = TRUE;
2657#endif
2658 continue;
2659 }
2660 if (!checkforcmd(&eap->cmd, "botright", 2))
2661 break;
2662 cmdmod.split |= WSP_BOT;
2663 continue;
2664
2665 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2666 break;
2667#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2668 cmdmod.confirm = TRUE;
2669#endif
2670 continue;
2671
2672 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2673 {
2674 cmdmod.keepmarks = TRUE;
2675 continue;
2676 }
2677 if (checkforcmd(&eap->cmd, "keepalt", 5))
2678 {
2679 cmdmod.keepalt = TRUE;
2680 continue;
2681 }
2682 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2683 {
2684 cmdmod.keeppatterns = TRUE;
2685 continue;
2686 }
2687 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2688 break;
2689 cmdmod.keepjumps = TRUE;
2690 continue;
2691
2692 case 'f': /* only accept ":filter {pat} cmd" */
2693 {
2694 char_u *reg_pat;
2695
2696 if (!checkforcmd(&p, "filter", 4)
2697 || *p == NUL || ends_excmd(*p))
2698 break;
2699 if (*p == '!')
2700 {
2701 cmdmod.filter_force = TRUE;
2702 p = skipwhite(p + 1);
2703 if (*p == NUL || ends_excmd(*p))
2704 break;
2705 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002706 if (skip_only)
2707 p = skip_vimgrep_pat(p, NULL, NULL);
2708 else
2709 // NOTE: This puts a NUL after the pattern.
2710 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002711 if (p == NULL || *p == NUL)
2712 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002713 if (!skip_only)
2714 {
2715 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002716 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002717 if (cmdmod.filter_regmatch.regprog == NULL)
2718 break;
2719 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 eap->cmd = p;
2721 continue;
2722 }
2723
2724 /* ":hide" and ":hide | cmd" are not modifiers */
2725 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2726 || *p == NUL || ends_excmd(*p))
2727 break;
2728 eap->cmd = p;
2729 cmdmod.hide = TRUE;
2730 continue;
2731
2732 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2733 {
2734 cmdmod.lockmarks = TRUE;
2735 continue;
2736 }
2737
2738 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2739 break;
2740 cmdmod.split |= WSP_ABOVE;
2741 continue;
2742
2743 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2744 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002745 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002746 {
2747 /* Set 'eventignore' to "all". Restore the
2748 * existing option value later. */
2749 cmdmod.save_ei = vim_strsave(p_ei);
2750 set_string_option_direct((char_u *)"ei", -1,
2751 (char_u *)"all", OPT_FREE, SID_NONE);
2752 }
2753 continue;
2754 }
2755 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2756 break;
2757 cmdmod.noswapfile = TRUE;
2758 continue;
2759
2760 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2761 break;
2762 cmdmod.split |= WSP_BELOW;
2763 continue;
2764
2765 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2766 {
2767#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002768 if (!skip_only)
2769 {
2770 if (!eap->did_sandbox)
2771 ++sandbox;
2772 eap->did_sandbox = TRUE;
2773 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002774#endif
2775 continue;
2776 }
2777 if (!checkforcmd(&eap->cmd, "silent", 3))
2778 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002779 if (!skip_only)
2780 {
2781 if (eap->save_msg_silent == -1)
2782 eap->save_msg_silent = msg_silent;
2783 ++msg_silent;
2784 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002785 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2786 {
2787 /* ":silent!", but not "silent !cmd" */
2788 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002789 if (!skip_only)
2790 {
2791 ++emsg_silent;
2792 ++eap->did_esilent;
2793 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 }
2795 continue;
2796
2797 case 't': if (checkforcmd(&p, "tab", 3))
2798 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002799 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002800 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002801 long tabnr = get_address(eap, &eap->cmd,
2802 ADDR_TABS, eap->skip,
2803 skip_only, FALSE, 1);
2804 if (tabnr == MAXLNUM)
2805 cmdmod.tab = tabpage_index(curtab) + 1;
2806 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002808 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002810 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002811 return FAIL;
2812 }
2813 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 }
2816 eap->cmd = p;
2817 continue;
2818 }
2819 if (!checkforcmd(&eap->cmd, "topleft", 2))
2820 break;
2821 cmdmod.split |= WSP_TOP;
2822 continue;
2823
2824 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2825 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002826 if (!skip_only)
2827 {
2828 if (eap->save_msg_silent == -1)
2829 eap->save_msg_silent = msg_silent;
2830 msg_silent = 0;
2831 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 continue;
2833
2834 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2835 {
2836 cmdmod.split |= WSP_VERT;
2837 continue;
2838 }
2839 if (!checkforcmd(&p, "verbose", 4))
2840 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 if (!skip_only)
2842 {
2843 if (eap->verbose_save < 0)
2844 eap->verbose_save = p_verbose;
2845 if (vim_isdigit(*eap->cmd))
2846 p_verbose = atoi((char *)eap->cmd);
2847 else
2848 p_verbose = 1;
2849 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 eap->cmd = p;
2851 continue;
2852 }
2853 break;
2854 }
2855
2856 return OK;
2857}
2858
2859/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002860 * Free contents of "cmdmod".
2861 */
2862 static void
2863free_cmdmod(void)
2864{
2865 if (cmdmod.save_ei != NULL)
2866 {
2867 /* Restore 'eventignore' to the value before ":noautocmd". */
2868 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2869 OPT_FREE, SID_NONE);
2870 free_string_option(cmdmod.save_ei);
2871 }
2872
2873 if (cmdmod.filter_regmatch.regprog != NULL)
2874 vim_regfree(cmdmod.filter_regmatch.regprog);
2875}
2876
2877/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002878 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002879 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002880 * Return FAIL and set "errormsg" or return OK.
2881 */
2882 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002883parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002884{
2885 int address_count = 1;
2886 linenr_T lnum;
2887
2888 // Repeat for all ',' or ';' separated addresses.
2889 for (;;)
2890 {
2891 eap->line1 = eap->line2;
2892 switch (eap->addr_type)
2893 {
2894 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002895 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002896 // default is current line number
2897 eap->line2 = curwin->w_cursor.lnum;
2898 break;
2899 case ADDR_WINDOWS:
2900 eap->line2 = CURRENT_WIN_NR;
2901 break;
2902 case ADDR_ARGUMENTS:
2903 eap->line2 = curwin->w_arg_idx + 1;
2904 if (eap->line2 > ARGCOUNT)
2905 eap->line2 = ARGCOUNT;
2906 break;
2907 case ADDR_LOADED_BUFFERS:
2908 case ADDR_BUFFERS:
2909 eap->line2 = curbuf->b_fnum;
2910 break;
2911 case ADDR_TABS:
2912 eap->line2 = CURRENT_TAB_NR;
2913 break;
2914 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002915 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002916 eap->line2 = 1;
2917 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002918 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002919#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002920 eap->line2 = qf_get_cur_idx(eap);
2921#endif
2922 break;
2923 case ADDR_QUICKFIX_VALID:
2924#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002925 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002926#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002927 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002928 case ADDR_NONE:
2929 // Will give an error later if a range is found.
2930 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002931 }
2932 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002933 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002934 eap->addr_count == 0, address_count++);
2935 if (eap->cmd == NULL) // error detected
2936 return FAIL;
2937 if (lnum == MAXLNUM)
2938 {
2939 if (*eap->cmd == '%') // '%' - all lines
2940 {
2941 ++eap->cmd;
2942 switch (eap->addr_type)
2943 {
2944 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002945 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002946 eap->line1 = 1;
2947 eap->line2 = curbuf->b_ml.ml_line_count;
2948 break;
2949 case ADDR_LOADED_BUFFERS:
2950 {
2951 buf_T *buf = firstbuf;
2952
2953 while (buf->b_next != NULL
2954 && buf->b_ml.ml_mfp == NULL)
2955 buf = buf->b_next;
2956 eap->line1 = buf->b_fnum;
2957 buf = lastbuf;
2958 while (buf->b_prev != NULL
2959 && buf->b_ml.ml_mfp == NULL)
2960 buf = buf->b_prev;
2961 eap->line2 = buf->b_fnum;
2962 break;
2963 }
2964 case ADDR_BUFFERS:
2965 eap->line1 = firstbuf->b_fnum;
2966 eap->line2 = lastbuf->b_fnum;
2967 break;
2968 case ADDR_WINDOWS:
2969 case ADDR_TABS:
2970 if (IS_USER_CMDIDX(eap->cmdidx))
2971 {
2972 eap->line1 = 1;
2973 eap->line2 = eap->addr_type == ADDR_WINDOWS
2974 ? LAST_WIN_NR : LAST_TAB_NR;
2975 }
2976 else
2977 {
2978 // there is no Vim command which uses '%' and
2979 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002980 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002981 return FAIL;
2982 }
2983 break;
2984 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002985 case ADDR_UNSIGNED:
2986 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002987 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002988 return FAIL;
2989 case ADDR_ARGUMENTS:
2990 if (ARGCOUNT == 0)
2991 eap->line1 = eap->line2 = 0;
2992 else
2993 {
2994 eap->line1 = 1;
2995 eap->line2 = ARGCOUNT;
2996 }
2997 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002998 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002999#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003000 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003001 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002 if (eap->line2 == 0)
3003 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003004#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003005 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003006 case ADDR_NONE:
3007 // Will give an error later if a range is found.
3008 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 }
3010 ++eap->addr_count;
3011 }
3012 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3013 {
3014 pos_T *fp;
3015
3016 // '*' - visual area
3017 if (eap->addr_type != ADDR_LINES)
3018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003019 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003020 return FAIL;
3021 }
3022
3023 ++eap->cmd;
3024 if (!eap->skip)
3025 {
3026 fp = getmark('<', FALSE);
3027 if (check_mark(fp) == FAIL)
3028 return FAIL;
3029 eap->line1 = fp->lnum;
3030 fp = getmark('>', FALSE);
3031 if (check_mark(fp) == FAIL)
3032 return FAIL;
3033 eap->line2 = fp->lnum;
3034 ++eap->addr_count;
3035 }
3036 }
3037 }
3038 else
3039 eap->line2 = lnum;
3040 eap->addr_count++;
3041
3042 if (*eap->cmd == ';')
3043 {
3044 if (!eap->skip)
3045 {
3046 curwin->w_cursor.lnum = eap->line2;
3047 // don't leave the cursor on an illegal line or column
3048 check_cursor();
3049 }
3050 }
3051 else if (*eap->cmd != ',')
3052 break;
3053 ++eap->cmd;
3054 }
3055
3056 // One address given: set start and end lines.
3057 if (eap->addr_count == 1)
3058 {
3059 eap->line1 = eap->line2;
3060 // ... but only implicit: really no address given
3061 if (lnum == MAXLNUM)
3062 eap->addr_count = 0;
3063 }
3064 return OK;
3065}
3066
3067/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003068 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 * If there is a match advance "pp" to the argument and return TRUE.
3070 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003071 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003072checkforcmd(
3073 char_u **pp, /* start of command */
3074 char *cmd, /* name of command */
3075 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
3077 int i;
3078
3079 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003080 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 break;
3082 if (i >= len && !isalpha((*pp)[i]))
3083 {
3084 *pp = skipwhite(*pp + i);
3085 return TRUE;
3086 }
3087 return FALSE;
3088}
3089
3090/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003091 * Append "cmd" to the error message in IObuff.
3092 * Takes care of limiting the length and handling 0xa0, which would be
3093 * invisible otherwise.
3094 */
3095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003096append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003097{
3098 char_u *s = cmd;
3099 char_u *d;
3100
3101 STRCAT(IObuff, ": ");
3102 d = IObuff + STRLEN(IObuff);
3103 while (*s != NUL && d - IObuff < IOSIZE - 7)
3104 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003105 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003106 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003107 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003108 STRCPY(d, "<a0>");
3109 d += 4;
3110 }
3111 else
3112 MB_COPY_CHAR(s, d);
3113 }
3114 *d = NUL;
3115}
3116
3117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003119 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003120 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003121 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * Returns NULL for an ambiguous user command.
3123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003125find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126{
3127 int len;
3128 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003129 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130
3131 /*
3132 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003133 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 * - the 'k' command can directly be followed by any character.
3135 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003136 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003138 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 */
3140 p = eap->cmd;
3141 if (*p == 'k')
3142 {
3143 eap->cmdidx = CMD_k;
3144 ++p;
3145 }
3146 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003147 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3148 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 || p[1] == 'g'
3150 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3151 || p[1] == 'I'
3152 || (p[1] == 'r' && p[2] != 'e')))
3153 {
3154 eap->cmdidx = CMD_substitute;
3155 ++p;
3156 }
3157 else
3158 {
3159 while (ASCII_ISALPHA(*p))
3160 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003161 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003162 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003163 while (ASCII_ISALNUM(*p))
3164 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003165
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 /* check for non-alpha command */
3167 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3168 ++p;
3169 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003170 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3171 {
3172 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3173 * :delete with the 'l' flag. Same for 'p'. */
3174 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003175 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003176 break;
3177 if (i == len - 1)
3178 {
3179 --len;
3180 if (p[-1] == 'l')
3181 eap->flags |= EXFLAG_LIST;
3182 else
3183 eap->flags |= EXFLAG_PRINT;
3184 }
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003187 if (ASCII_ISLOWER(eap->cmd[0]))
3188 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003189 int c1 = eap->cmd[0];
3190 int c2 = eap->cmd[1];
3191
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003192 if (command_count != (int)CMD_SIZE)
3193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003194 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003195 getout(1);
3196 }
3197
3198 /* Use a precomputed index for fast look-up in cmdnames[]
3199 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003200 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3201 if (ASCII_ISLOWER(c2))
3202 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003205 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
3207 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3208 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3209 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3210 (size_t)len) == 0)
3211 {
3212#ifdef FEAT_EVAL
3213 if (full != NULL
3214 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3215 *full = TRUE;
3216#endif
3217 break;
3218 }
3219
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003220 // Look for a user defined command as a last resort. Let ":Print" be
3221 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003222 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3223 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003225 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 while (ASCII_ISALNUM(*p))
3227 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003228 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003230 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 eap->cmdidx = CMD_SIZE;
3232 }
3233
3234 return p;
3235}
3236
3237#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003238static struct cmdmod
3239{
3240 char *name;
3241 int minlen;
3242 int has_count; /* :123verbose :3tab */
3243} cmdmods[] = {
3244 {"aboveleft", 3, FALSE},
3245 {"belowright", 3, FALSE},
3246 {"botright", 2, FALSE},
3247 {"browse", 3, FALSE},
3248 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003249 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003250 {"hide", 3, FALSE},
3251 {"keepalt", 5, FALSE},
3252 {"keepjumps", 5, FALSE},
3253 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003254 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003255 {"leftabove", 5, FALSE},
3256 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003257 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003258 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003259 {"rightbelow", 6, FALSE},
3260 {"sandbox", 3, FALSE},
3261 {"silent", 3, FALSE},
3262 {"tab", 3, TRUE},
3263 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003264 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003265 {"verbose", 4, TRUE},
3266 {"vertical", 4, FALSE},
3267};
3268
3269/*
3270 * Return length of a command modifier (including optional count).
3271 * Return zero when it's not a modifier.
3272 */
3273 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003274modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003275{
3276 int i, j;
3277 char_u *p = cmd;
3278
3279 if (VIM_ISDIGIT(*cmd))
3280 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003281 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003282 {
3283 for (j = 0; p[j] != NUL; ++j)
3284 if (p[j] != cmdmods[i].name[j])
3285 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003286 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003287 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003288 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003289 }
3290 return 0;
3291}
3292
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293/*
3294 * Return > 0 if an Ex command "name" exists.
3295 * Return 2 if there is an exact match.
3296 * Return 3 if there is an ambiguous match.
3297 */
3298 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003299cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301 exarg_T ea;
3302 int full = FALSE;
3303 int i;
3304 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003305 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003308 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 {
3310 for (j = 0; name[j] != NUL; ++j)
3311 if (name[j] != cmdmods[i].name[j])
3312 break;
3313 if (name[j] == NUL && j >= cmdmods[i].minlen)
3314 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3315 }
3316
Bram Moolenaara9587612006-05-04 21:47:50 +00003317 /* Check built-in commands and user defined commands.
3318 * For ":2match" and ":3match" we need to skip the number. */
3319 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003321 p = find_command(&ea, &full);
3322 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003324 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3325 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003326 if (*skipwhite(p) != NUL)
3327 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3329}
3330#endif
3331
3332/*
3333 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3334 * we don't need/want deleted. Maybe this could be done better if we didn't
3335 * repeat all this stuff. The only problem is that they may not stay
3336 * perfectly compatible with each other, but then the command line syntax
3337 * probably won't change that much -- webb.
3338 */
3339 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003340set_one_cmd_context(
3341 expand_T *xp,
3342 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 char_u *p;
3345 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003346 int len = 0;
3347 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003349 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 int delim;
3351#endif
3352 int forceit = FALSE;
3353 int usefilter = FALSE; /* filter instead of file name */
3354
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003355 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 xp->xp_pattern = buff;
3357 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003358 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003361 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 */
3363 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3364 ;
3365 xp->xp_pattern = cmd;
3366
3367 if (*cmd == NUL)
3368 return NULL;
3369 if (*cmd == '"') /* ignore comment lines */
3370 {
3371 xp->xp_context = EXPAND_NOTHING;
3372 return NULL;
3373 }
3374
3375/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003376 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 */
3378 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 xp->xp_pattern = cmd;
3380 if (*cmd == NUL)
3381 return NULL;
3382 if (*cmd == '"')
3383 {
3384 xp->xp_context = EXPAND_NOTHING;
3385 return NULL;
3386 }
3387
3388 if (*cmd == '|' || *cmd == '\n')
3389 return cmd + 1; /* There's another command */
3390
3391 /*
3392 * Isolate the command and search for it in the command table.
3393 * Exceptions:
3394 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003395 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3397 */
3398 if (*cmd == 'k' && cmd[1] != 'e')
3399 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003400 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 p = cmd + 1;
3402 }
3403 else
3404 {
3405 p = cmd;
3406 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3407 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003408 /* a user command may contain digits */
3409 if (ASCII_ISUPPER(cmd[0]))
3410 while (ASCII_ISALNUM(*p) || *p == '*')
3411 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003412 /* for python 3.x: ":py3*" commands completion */
3413 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003414 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003415 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003416 while (ASCII_ISALPHA(*p) || *p == '*')
3417 ++p;
3418 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003419 /* check for non-alpha command */
3420 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3421 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003422 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003424 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 {
3426 xp->xp_context = EXPAND_UNSUCCESSFUL;
3427 return NULL;
3428 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003430 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3431 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3432 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 break;
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003436 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439
3440 /*
3441 * If the cursor is touching the command, and it ends in an alpha-numeric
3442 * character, complete the command name.
3443 */
3444 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3445 return NULL;
3446
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003447 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 {
3449 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3450 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003451 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 p = cmd + 1;
3453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3455 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456 ea.cmd = cmd;
3457 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003458#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003459 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003460#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003461 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003462#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003463 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003464 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003465 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 /* Not still touching the command and it was an illegal one */
3471 xp->xp_context = EXPAND_UNSUCCESSFUL;
3472 return NULL;
3473 }
3474
3475 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3476
3477 if (*p == '!') /* forced commands */
3478 {
3479 forceit = TRUE;
3480 ++p;
3481 }
3482
3483/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003484 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003486 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003487 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 arg = skipwhite(p);
3490
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003491 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
3493 if (*arg == '>') /* append */
3494 {
3495 if (*++arg == '>')
3496 ++arg;
3497 arg = skipwhite(arg);
3498 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 ++arg;
3502 usefilter = TRUE;
3503 }
3504 }
3505
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003506 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 usefilter = forceit; /* :r! filter if forced */
3509 if (*arg == '!') /* :r !filter */
3510 {
3511 ++arg;
3512 usefilter = TRUE;
3513 }
3514 }
3515
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003516 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
3518 while (*arg == *cmd) /* allow any number of '>' or '<' */
3519 ++arg;
3520 arg = skipwhite(arg);
3521 }
3522
3523 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 /* Check if we're in the +command */
3527 p = arg + 1;
3528 arg = skip_cmd_arg(arg, FALSE);
3529
3530 /* Still touching the command after '+'? */
3531 if (*arg == NUL)
3532 return p;
3533
3534 /* Skip space(s) after +command to get to the real argument */
3535 arg = skipwhite(arg);
3536 }
3537
3538 /*
3539 * Check for '|' to separate commands and '"' to start comments.
3540 * Don't do this for ":read !cmd" and ":write !cmd".
3541 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003542 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 p = arg;
3545 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003546 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 p += 2;
3548 while (*p)
3549 {
3550 if (*p == Ctrl_V)
3551 {
3552 if (p[1] != NUL)
3553 ++p;
3554 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 || *p == '|' || *p == '\n')
3557 {
3558 if (*(p - 1) != '\\')
3559 {
3560 if (*p == '|' || *p == '\n')
3561 return p + 1;
3562 return NULL; /* It's a comment */
3563 }
3564 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003565 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 }
3567 }
3568
3569 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003570 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 vim_strchr((char_u *)"|\"", *arg) == NULL)
3572 return NULL;
3573
3574 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003575 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003577 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003578 while (*p && p < buff + len)
3579 {
3580 if (*p == ' ' || *p == TAB)
3581 {
3582 /* argument starts after a space */
3583 xp->xp_pattern = ++p;
3584 }
3585 else
3586 {
3587 if (*p == '\\' && *(p + 1) != NUL)
3588 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003589 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003590 }
3591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003593 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003595 int c;
3596 int in_quote = FALSE;
3597 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
3599 /*
3600 * Allow spaces within back-quotes to count as part of the argument
3601 * being expanded.
3602 */
3603 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003604 p = xp->xp_pattern;
3605 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003607 if (has_mbyte)
3608 c = mb_ptr2char(p);
3609 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003610 c = *p;
3611 if (c == '\\' && p[1] != NUL)
3612 ++p;
3613 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
3615 if (!in_quote)
3616 {
3617 xp->xp_pattern = p;
3618 bow = p + 1;
3619 }
3620 in_quote = !in_quote;
3621 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003622 /* An argument can contain just about everything, except
3623 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003624 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003625#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003626 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003627#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003628 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003629 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003630 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003631 while (*p != NUL)
3632 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003633 if (has_mbyte)
3634 c = mb_ptr2char(p);
3635 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003636 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003637 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003638 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003639 if (has_mbyte)
3640 len = (*mb_ptr2len)(p);
3641 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003642 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003643 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003644 }
3645 if (in_quote)
3646 bow = p;
3647 else
3648 xp->xp_pattern = p;
3649 p -= len;
3650 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003651 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 }
3653
3654 /*
3655 * If we are still inside the quotes, and we passed a space, just
3656 * expand from there.
3657 */
3658 if (bow != NULL && in_quote)
3659 xp->xp_pattern = bow;
3660 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003661
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003662 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003663 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003664 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003665#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003666 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003667#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003668 /* When still after the command name expand executables. */
3669 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003670 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 /* Check for environment variable */
3674 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003675#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 || *xp->xp_pattern == '%'
3677#endif
3678 )
3679 {
3680 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3681 if (!vim_isIDc(*p))
3682 break;
3683 if (*p == NUL)
3684 {
3685 xp->xp_context = EXPAND_ENV_VARS;
3686 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003687#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003688 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003689 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003690 compl = EXPAND_ENV_VARS;
3691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 }
3693 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003694#if defined(FEAT_CMDL_COMPL)
3695 /* Check for user names */
3696 if (*xp->xp_pattern == '~')
3697 {
3698 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3699 ;
3700 /* Complete ~user only if it partially matches a user name.
3701 * A full match ~user<Tab> will be replaced by user's home
3702 * directory i.e. something like ~user<Tab> -> /home/user/ */
3703 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003704 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003705 {
3706 xp->xp_context = EXPAND_USER;
3707 ++xp->xp_pattern;
3708 }
3709 }
3710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 }
3712
3713/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003714 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003716 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003718 case CMD_find:
3719 case CMD_sfind:
3720 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003721 if (xp->xp_context == EXPAND_FILES)
3722 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003723 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 case CMD_cd:
3725 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003726 case CMD_tcd:
3727 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 case CMD_lcd:
3729 case CMD_lchdir:
3730 if (xp->xp_context == EXPAND_FILES)
3731 xp->xp_context = EXPAND_DIRECTORIES;
3732 break;
3733 case CMD_help:
3734 xp->xp_context = EXPAND_HELP;
3735 xp->xp_pattern = arg;
3736 break;
3737
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003738 /* Command modifiers: return the argument.
3739 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003741 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 case CMD_belowright:
3743 case CMD_botright:
3744 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003745 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003746 case CMD_cdo:
3747 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003749 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 case CMD_folddoclosed:
3751 case CMD_folddoopen:
3752 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003753 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 case CMD_keepjumps:
3755 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003756 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003757 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003759 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003761 case CMD_noautocmd:
3762 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003764 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003766 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003767 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 case CMD_topleft:
3769 case CMD_verbose:
3770 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003771 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return arg;
3773
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003774 case CMD_filter:
3775 if (*arg != NUL)
3776 arg = skip_vimgrep_pat(arg, NULL, NULL);
3777 if (arg == NULL || *arg == NUL)
3778 {
3779 xp->xp_context = EXPAND_NOTHING;
3780 return NULL;
3781 }
3782 return skipwhite(arg);
3783
Bram Moolenaar4f688582007-07-24 12:34:30 +00003784#ifdef FEAT_CMDL_COMPL
3785# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 case CMD_match:
3787 if (*arg == NUL || !ends_excmd(*arg))
3788 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003789 /* also complete "None" */
3790 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 arg = skipwhite(skiptowhite(arg));
3792 if (*arg != NUL)
3793 {
3794 xp->xp_context = EXPAND_NOTHING;
3795 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3796 }
3797 }
3798 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801/*
3802 * All completion for the +cmdline_compl feature goes here.
3803 */
3804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003806 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
3808 case CMD_delcommand:
3809 xp->xp_context = EXPAND_USER_COMMANDS;
3810 xp->xp_pattern = arg;
3811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 case CMD_global:
3814 case CMD_vglobal:
3815 delim = *arg; /* get the delimiter */
3816 if (delim)
3817 ++arg; /* skip delimiter if there is one */
3818
3819 while (arg[0] != NUL && arg[0] != delim)
3820 {
3821 if (arg[0] == '\\' && arg[1] != NUL)
3822 ++arg;
3823 ++arg;
3824 }
3825 if (arg[0] != NUL)
3826 return arg + 1;
3827 break;
3828 case CMD_and:
3829 case CMD_substitute:
3830 delim = *arg;
3831 if (delim)
3832 {
3833 /* skip "from" part */
3834 ++arg;
3835 arg = skip_regexp(arg, delim, p_magic, NULL);
3836 }
3837 /* skip "to" part */
3838 while (arg[0] != NUL && arg[0] != delim)
3839 {
3840 if (arg[0] == '\\' && arg[1] != NUL)
3841 ++arg;
3842 ++arg;
3843 }
3844 if (arg[0] != NUL) /* skip delimiter */
3845 ++arg;
3846 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3847 ++arg;
3848 if (arg[0] != NUL)
3849 return arg;
3850 break;
3851 case CMD_isearch:
3852 case CMD_dsearch:
3853 case CMD_ilist:
3854 case CMD_dlist:
3855 case CMD_ijump:
3856 case CMD_psearch:
3857 case CMD_djump:
3858 case CMD_isplit:
3859 case CMD_dsplit:
3860 arg = skipwhite(skipdigits(arg)); /* skip count */
3861 if (*arg == '/') /* Match regexp, not just whole words */
3862 {
3863 for (++arg; *arg && *arg != '/'; arg++)
3864 if (*arg == '\\' && arg[1] != NUL)
3865 arg++;
3866 if (*arg)
3867 {
3868 arg = skipwhite(arg + 1);
3869
3870 /* Check for trailing illegal characters */
3871 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3872 xp->xp_context = EXPAND_NOTHING;
3873 else
3874 return arg;
3875 }
3876 }
3877 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 case CMD_autocmd:
3880 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003882 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case CMD_set:
3885 set_context_in_set_cmd(xp, arg, 0);
3886 break;
3887 case CMD_setglobal:
3888 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3889 break;
3890 case CMD_setlocal:
3891 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3892 break;
3893 case CMD_tag:
3894 case CMD_stag:
3895 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003896 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 case CMD_tselect:
3898 case CMD_stselect:
3899 case CMD_ptselect:
3900 case CMD_tjump:
3901 case CMD_stjump:
3902 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003903 if (*p_wop != NUL)
3904 xp->xp_context = EXPAND_TAGS_LISTFILES;
3905 else
3906 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 xp->xp_pattern = arg;
3908 break;
3909 case CMD_augroup:
3910 xp->xp_context = EXPAND_AUGROUP;
3911 xp->xp_pattern = arg;
3912 break;
3913#ifdef FEAT_SYN_HL
3914 case CMD_syntax:
3915 set_context_in_syntax_cmd(xp, arg);
3916 break;
3917#endif
3918#ifdef FEAT_EVAL
3919 case CMD_let:
3920 case CMD_if:
3921 case CMD_elseif:
3922 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003923 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 case CMD_echo:
3925 case CMD_echon:
3926 case CMD_execute:
3927 case CMD_echomsg:
3928 case CMD_echoerr:
3929 case CMD_call:
3930 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003931 case CMD_cexpr:
3932 case CMD_caddexpr:
3933 case CMD_cgetexpr:
3934 case CMD_lexpr:
3935 case CMD_laddexpr:
3936 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003937 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 break;
3939
3940 case CMD_unlet:
3941 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3942 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 xp->xp_context = EXPAND_USER_VARS;
3945 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003946
3947 if (*xp->xp_pattern == '$')
3948 {
3949 xp->xp_context = EXPAND_ENV_VARS;
3950 ++xp->xp_pattern;
3951 }
3952
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 break;
3954
3955 case CMD_function:
3956 case CMD_delfunction:
3957 xp->xp_context = EXPAND_USER_FUNC;
3958 xp->xp_pattern = arg;
3959 break;
3960
3961 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003962 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 break;
3964#endif
3965 case CMD_highlight:
3966 set_context_in_highlight_cmd(xp, arg);
3967 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003968#ifdef FEAT_CSCOPE
3969 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003970 case CMD_lcscope:
3971 case CMD_scscope:
3972 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003973 break;
3974#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003975#ifdef FEAT_SIGNS
3976 case CMD_sign:
3977 set_context_in_sign_cmd(xp, arg);
3978 break;
3979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 case CMD_bdelete:
3981 case CMD_bwipeout:
3982 case CMD_bunload:
3983 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3984 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003985 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 case CMD_buffer:
3987 case CMD_sbuffer:
3988 case CMD_checktime:
3989 xp->xp_context = EXPAND_BUFFERS;
3990 xp->xp_pattern = arg;
3991 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 case CMD_USER:
3994 case CMD_USER_BUF:
3995 if (compl != EXPAND_NOTHING)
3996 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003997 // XFILE: file names are handled above
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003998 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004000#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 if (compl == EXPAND_MENUS)
4002 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (compl == EXPAND_COMMANDS)
4005 return arg;
4006 if (compl == EXPAND_MAPPINGS)
4007 return set_context_in_map_cmd(xp, (char_u *)"map",
4008 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004009 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004010 p = arg;
4011 while (*p)
4012 {
4013 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004014 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004015 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004016 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004017 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004018 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 xp->xp_pattern = arg;
4021 }
4022 xp->xp_context = compl;
4023 }
4024 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004025
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 case CMD_map: case CMD_noremap:
4027 case CMD_nmap: case CMD_nnoremap:
4028 case CMD_vmap: case CMD_vnoremap:
4029 case CMD_omap: case CMD_onoremap:
4030 case CMD_imap: case CMD_inoremap:
4031 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004032 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004033 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004034 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004035 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004037 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 case CMD_unmap:
4039 case CMD_nunmap:
4040 case CMD_vunmap:
4041 case CMD_ounmap:
4042 case CMD_iunmap:
4043 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004044 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004045 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004046 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004047 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004049 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004050 case CMD_mapclear:
4051 case CMD_nmapclear:
4052 case CMD_vmapclear:
4053 case CMD_omapclear:
4054 case CMD_imapclear:
4055 case CMD_cmapclear:
4056 case CMD_lmapclear:
4057 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004058 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004059 case CMD_xmapclear:
4060 xp->xp_context = EXPAND_MAPCLEAR;
4061 xp->xp_pattern = arg;
4062 break;
4063
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 case CMD_abbreviate: case CMD_noreabbrev:
4065 case CMD_cabbrev: case CMD_cnoreabbrev:
4066 case CMD_iabbrev: case CMD_inoreabbrev:
4067 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004068 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 case CMD_unabbreviate:
4070 case CMD_cunabbrev:
4071 case CMD_iunabbrev:
4072 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004073 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074#ifdef FEAT_MENU
4075 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4076 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4077 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4078 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4079 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4080 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4081 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004082 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 case CMD_tmenu: case CMD_tunmenu:
4084 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4085 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4086#endif
4087
4088 case CMD_colorscheme:
4089 xp->xp_context = EXPAND_COLORS;
4090 xp->xp_pattern = arg;
4091 break;
4092
4093 case CMD_compiler:
4094 xp->xp_context = EXPAND_COMPILER;
4095 xp->xp_pattern = arg;
4096 break;
4097
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004098 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004099 xp->xp_context = EXPAND_OWNSYNTAX;
4100 xp->xp_pattern = arg;
4101 break;
4102
4103 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004104 xp->xp_context = EXPAND_FILETYPE;
4105 xp->xp_pattern = arg;
4106 break;
4107
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004108 case CMD_packadd:
4109 xp->xp_context = EXPAND_PACKADD;
4110 xp->xp_pattern = arg;
4111 break;
4112
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004113#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004115 p = skiptowhite(arg);
4116 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 {
4118 xp->xp_context = EXPAND_LANGUAGE;
4119 xp->xp_pattern = arg;
4120 }
4121 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004122 {
4123 if ( STRNCMP(arg, "messages", p - arg) == 0
4124 || STRNCMP(arg, "ctype", p - arg) == 0
4125 || STRNCMP(arg, "time", p - arg) == 0)
4126 {
4127 xp->xp_context = EXPAND_LOCALES;
4128 xp->xp_pattern = skipwhite(p);
4129 }
4130 else
4131 xp->xp_context = EXPAND_NOTHING;
4132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004135#if defined(FEAT_PROFILE)
4136 case CMD_profile:
4137 set_context_in_profile_cmd(xp, arg);
4138 break;
4139#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004140 case CMD_behave:
4141 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004142 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004145 case CMD_messages:
4146 xp->xp_context = EXPAND_MESSAGES;
4147 xp->xp_pattern = arg;
4148 break;
4149
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004150#if defined(FEAT_CMDHIST)
4151 case CMD_history:
4152 xp->xp_context = EXPAND_HISTORY;
4153 xp->xp_pattern = arg;
4154 break;
4155#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004156#if defined(FEAT_PROFILE)
4157 case CMD_syntime:
4158 xp->xp_context = EXPAND_SYNTIME;
4159 xp->xp_pattern = arg;
4160 break;
4161#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004162
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004163 case CMD_argdelete:
4164 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4165 arg = xp->xp_pattern + 1;
4166 xp->xp_context = EXPAND_ARGLIST;
4167 xp->xp_pattern = arg;
4168 break;
4169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170#endif /* FEAT_CMDL_COMPL */
4171
4172 default:
4173 break;
4174 }
4175 return NULL;
4176}
4177
4178/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004179 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 *
4181 * Backslashed delimiters after / or ? will be skipped, and commands will
4182 * not be expanded between /'s and ?'s or after "'".
4183 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004184 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 * Returns the "cmd" pointer advanced to beyond the range.
4186 */
4187 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004188skip_range(
4189 char_u *cmd,
4190 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004192 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004194 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004196 if (*cmd == '\\')
4197 {
4198 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4199 ++cmd;
4200 else
4201 break;
4202 }
4203 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
4205 if (*++cmd == NUL && ctx != NULL)
4206 *ctx = EXPAND_NOTHING;
4207 }
4208 else if (*cmd == '/' || *cmd == '?')
4209 {
4210 delim = *cmd++;
4211 while (*cmd != NUL && *cmd != delim)
4212 if (*cmd++ == '\\' && *cmd != NUL)
4213 ++cmd;
4214 if (*cmd == NUL && ctx != NULL)
4215 *ctx = EXPAND_NOTHING;
4216 }
4217 if (*cmd != NUL)
4218 ++cmd;
4219 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004220
4221 /* Skip ":" and white space. */
4222 while (*cmd == ':')
4223 cmd = skipwhite(cmd + 1);
4224
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return cmd;
4226}
4227
4228/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004229 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 *
4231 * Set ptr to the next character after the part that was interpreted.
4232 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004233 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 *
4235 * Return MAXLNUM when no Ex address was found.
4236 */
4237 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004238get_address(
4239 exarg_T *eap UNUSED,
4240 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004241 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004242 int skip, // only skip the address, don't use it
4243 int silent, // no errors or side effects
4244 int to_other_file, // flag: may jump to other file
4245 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004247 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 int c;
4249 int i;
4250 long n;
4251 char_u *cmd;
4252 pos_T pos;
4253 pos_T *fp;
4254 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004255 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 cmd = skipwhite(*ptr);
4258 lnum = MAXLNUM;
4259 do
4260 {
4261 switch (*cmd)
4262 {
4263 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004264 ++cmd;
4265 switch (addr_type)
4266 {
4267 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004268 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 lnum = curwin->w_cursor.lnum;
4270 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004272 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004273 break;
4274 case ADDR_ARGUMENTS:
4275 lnum = curwin->w_arg_idx + 1;
4276 break;
4277 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004278 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 lnum = curbuf->b_fnum;
4280 break;
4281 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004282 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004283 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004284 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004285 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004286 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004287 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004288 cmd = NULL;
4289 goto error;
4290 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004291 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004292#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004293 lnum = qf_get_cur_idx(eap);
4294#endif
4295 break;
4296 case ADDR_QUICKFIX_VALID:
4297#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004298 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004299#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004300 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004301 }
4302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004305 ++cmd;
4306 switch (addr_type)
4307 {
4308 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004309 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 lnum = curbuf->b_ml.ml_line_count;
4311 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004313 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004314 break;
4315 case ADDR_ARGUMENTS:
4316 lnum = ARGCOUNT;
4317 break;
4318 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004319 buf = lastbuf;
4320 while (buf->b_ml.ml_mfp == NULL)
4321 {
4322 if (buf->b_prev == NULL)
4323 break;
4324 buf = buf->b_prev;
4325 }
4326 lnum = buf->b_fnum;
4327 break;
4328 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004329 lnum = lastbuf->b_fnum;
4330 break;
4331 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004332 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004333 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004334 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004335 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004336 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004337 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004338 cmd = NULL;
4339 goto error;
4340 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004341 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004342#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004343 lnum = qf_get_size(eap);
4344 if (lnum == 0)
4345 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004346#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004347 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004348 case ADDR_QUICKFIX_VALID:
4349#ifdef FEAT_QUICKFIX
4350 lnum = qf_get_valid_size(eap);
4351 if (lnum == 0)
4352 lnum = 1;
4353#endif
4354 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004355 }
4356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
4358 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 if (*++cmd == NUL)
4360 {
4361 cmd = NULL;
4362 goto error;
4363 }
4364 if (addr_type != ADDR_LINES)
4365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004366 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004367 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004368 goto error;
4369 }
4370 if (skip)
4371 ++cmd;
4372 else
4373 {
4374 /* Only accept a mark in another file when it is
4375 * used by itself: ":'M". */
4376 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4377 ++cmd;
4378 if (fp == (pos_T *)-1)
4379 /* Jumped to another file. */
4380 lnum = curwin->w_cursor.lnum;
4381 else
4382 {
4383 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
4385 cmd = NULL;
4386 goto error;
4387 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004388 lnum = fp->lnum;
4389 }
4390 }
4391 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
4393 case '/':
4394 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 c = *cmd++;
4396 if (addr_type != ADDR_LINES)
4397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004398 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004399 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 goto error;
4401 }
4402 if (skip) /* skip "/pat/" */
4403 {
4404 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4405 if (*cmd == c)
4406 ++cmd;
4407 }
4408 else
4409 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004410 int flags;
4411
4412 pos = curwin->w_cursor; // save curwin->w_cursor
4413
4414 // When '/' or '?' follows another address, start from
4415 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004416 if (lnum != MAXLNUM)
4417 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004418
4419 // Start a forward search at the end of the line (unless
4420 // before the first line).
4421 // Start a backward search at the start of the line.
4422 // This makes sure we never match in the current
4423 // line, and can match anywhere in the
4424 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004425 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004426 curwin->w_cursor.col = MAXCOL;
4427 else
4428 curwin->w_cursor.col = 0;
4429 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004430 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4431 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 {
4433 curwin->w_cursor = pos;
4434 cmd = NULL;
4435 goto error;
4436 }
4437 lnum = curwin->w_cursor.lnum;
4438 curwin->w_cursor = pos;
4439 /* adjust command string pointer */
4440 cmd += searchcmdlen;
4441 }
4442 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
4444 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004445 ++cmd;
4446 if (addr_type != ADDR_LINES)
4447 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004448 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004449 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 goto error;
4451 }
4452 if (*cmd == '&')
4453 i = RE_SUBST;
4454 else if (*cmd == '?' || *cmd == '/')
4455 i = RE_SEARCH;
4456 else
4457 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004458 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 cmd = NULL;
4460 goto error;
4461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 if (!skip)
4464 {
4465 /*
4466 * When search follows another address, start from
4467 * there.
4468 */
4469 if (lnum != MAXLNUM)
4470 pos.lnum = lnum;
4471 else
4472 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 /*
4475 * Start the search just like for the above
4476 * do_search().
4477 */
4478 if (*cmd != '?')
4479 pos.col = MAXCOL;
4480 else
4481 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004482 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004483 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004484 *cmd == '?' ? BACKWARD : FORWARD,
4485 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004486 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 lnum = pos.lnum;
4488 else
4489 {
4490 cmd = NULL;
4491 goto error;
4492 }
4493 }
4494 ++cmd;
4495 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004498 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4499 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501
4502 for (;;)
4503 {
4504 cmd = skipwhite(cmd);
4505 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4506 break;
4507
4508 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004509 {
4510 switch (addr_type)
4511 {
4512 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004513 case ADDR_OTHER:
4514 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004515 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 break;
4517 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004518 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 break;
4520 case ADDR_ARGUMENTS:
4521 lnum = curwin->w_arg_idx + 1;
4522 break;
4523 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004524 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004525 lnum = curbuf->b_fnum;
4526 break;
4527 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004528 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004530 case ADDR_TABS_RELATIVE:
4531 lnum = 1;
4532 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004533 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004534#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004535 lnum = qf_get_cur_idx(eap);
4536#endif
4537 break;
4538 case ADDR_QUICKFIX_VALID:
4539#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004540 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004541#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004542 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004543 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004544 case ADDR_UNSIGNED:
4545 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004546 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004547 }
4548 }
4549
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (VIM_ISDIGIT(*cmd))
4551 i = '+'; /* "number" is same as "+number" */
4552 else
4553 i = *cmd++;
4554 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4555 n = 1;
4556 else
4557 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004558
4559 if (addr_type == ADDR_TABS_RELATIVE)
4560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004561 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004562 cmd = NULL;
4563 goto error;
4564 }
4565 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004566 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004567 lnum = compute_buffer_local_count(
4568 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004570 {
4571#ifdef FEAT_FOLDING
4572 /* Relative line addressing, need to adjust for folded lines
4573 * now, but only do it after the first address. */
4574 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4575 && address_count >= 2)
4576 (void)hasFolding(lnum, NULL, &lnum);
4577#endif
4578 if (i == '-')
4579 lnum -= n;
4580 else
4581 lnum += n;
4582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
4584 } while (*cmd == '/' || *cmd == '?');
4585
4586error:
4587 *ptr = cmd;
4588 return lnum;
4589}
4590
4591/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004592 * Get flags from an Ex command argument.
4593 */
4594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004595get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004596{
4597 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4598 {
4599 if (*eap->arg == 'l')
4600 eap->flags |= EXFLAG_LIST;
4601 else if (*eap->arg == 'p')
4602 eap->flags |= EXFLAG_PRINT;
4603 else
4604 eap->flags |= EXFLAG_NR;
4605 eap->arg = skipwhite(eap->arg + 1);
4606 }
4607}
4608
4609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 * Function called for command which is Not Implemented. NI!
4611 */
4612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004613ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
4615 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004616 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617}
4618
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004619#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620/*
4621 * Function called for script command which is Not Implemented. NI!
4622 * Skips over ":perl <<EOF" constructs.
4623 */
4624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004625ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
4627 if (!eap->skip)
4628 ex_ni(eap);
4629 else
4630 vim_free(script_get(eap, eap->arg));
4631}
4632#endif
4633
4634/*
4635 * Check range in Ex command for validity.
4636 * Return NULL when valid, error message when invalid.
4637 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004638 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004639invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004641 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 if ( eap->line1 < 0
4644 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004645 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004646 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004647
4648 if (eap->argt & RANGE)
4649 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004650 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004651 {
4652 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004653 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654#ifdef FEAT_DIFF
4655 + (eap->cmdidx == CMD_diffget)
4656#endif
4657 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004658 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659 break;
4660 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004661 /* add 1 if ARGCOUNT is 0 */
4662 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004663 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004664 break;
4665 case ADDR_BUFFERS:
4666 if (eap->line1 < firstbuf->b_fnum
4667 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004668 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004669 break;
4670 case ADDR_LOADED_BUFFERS:
4671 buf = firstbuf;
4672 while (buf->b_ml.ml_mfp == NULL)
4673 {
4674 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004675 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004676 buf = buf->b_next;
4677 }
4678 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004679 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004680 buf = lastbuf;
4681 while (buf->b_ml.ml_mfp == NULL)
4682 {
4683 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004684 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004685 buf = buf->b_prev;
4686 }
4687 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004688 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004689 break;
4690 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004691 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004692 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004693 break;
4694 case ADDR_TABS:
4695 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004696 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004697 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004698 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004699 case ADDR_OTHER:
4700 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004701 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004702 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004703#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004704 // No error for value that is too big, will use the last entry.
4705 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004706 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004707#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004708 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004709 case ADDR_QUICKFIX_VALID:
4710#ifdef FEAT_QUICKFIX
4711 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4712 || eap->line2 < 0)
4713 return _(e_invrange);
4714#endif
4715 break;
4716 case ADDR_UNSIGNED:
4717 if (eap->line2 < 0)
4718 return _(e_invrange);
4719 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004720 case ADDR_NONE:
4721 // Will give an error elsewhere.
4722 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004723 }
4724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 return NULL;
4726}
4727
4728/*
4729 * Correct the range for zero line number, if required.
4730 */
4731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4735 {
4736 if (eap->line1 == 0)
4737 eap->line1 = 1;
4738 if (eap->line2 == 0)
4739 eap->line2 = 1;
4740 }
4741}
4742
Bram Moolenaar748bf032005-02-02 23:04:36 +00004743#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004744/*
4745 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4746 * pattern. Otherwise return eap->arg.
4747 */
4748 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004749skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004750{
4751 char_u *p = eap->arg;
4752
Bram Moolenaara37420f2006-02-04 22:37:47 +00004753 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4754 || eap->cmdidx == CMD_vimgrepadd
4755 || eap->cmdidx == CMD_lvimgrepadd
4756 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004757 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004758 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004759 if (p == NULL)
4760 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004761 }
4762 return p;
4763}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004764
4765/*
4766 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4767 * in the command line, so that things like % get expanded.
4768 */
4769 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004770replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004771{
4772 char_u *new_cmdline;
4773 char_u *program;
4774 char_u *pos;
4775 char_u *ptr;
4776 int len;
4777 int i;
4778
4779 /*
4780 * Don't do it when ":vimgrep" is used for ":grep".
4781 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004782 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4783 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4784 || eap->cmdidx == CMD_grepadd
4785 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004786 && !grep_internal(eap->cmdidx))
4787 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004788 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4789 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004790 {
4791 if (*curbuf->b_p_gp == NUL)
4792 program = p_gp;
4793 else
4794 program = curbuf->b_p_gp;
4795 }
4796 else
4797 {
4798 if (*curbuf->b_p_mp == NUL)
4799 program = p_mp;
4800 else
4801 program = curbuf->b_p_mp;
4802 }
4803
4804 p = skipwhite(p);
4805
4806 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4807 {
4808 /* replace $* by given arguments */
4809 i = 1;
4810 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4811 ++i;
4812 len = (int)STRLEN(p);
4813 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4814 if (new_cmdline == NULL)
4815 return NULL; /* out of memory */
4816 ptr = new_cmdline;
4817 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4818 {
4819 i = (int)(pos - program);
4820 STRNCPY(ptr, program, i);
4821 STRCPY(ptr += i, p);
4822 ptr += len;
4823 program = pos + 2;
4824 }
4825 STRCPY(ptr, program);
4826 }
4827 else
4828 {
4829 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4830 if (new_cmdline == NULL)
4831 return NULL; /* out of memory */
4832 STRCPY(new_cmdline, program);
4833 STRCAT(new_cmdline, " ");
4834 STRCAT(new_cmdline, p);
4835 }
4836 msg_make(p);
4837
4838 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4839 vim_free(*cmdlinep);
4840 *cmdlinep = new_cmdline;
4841 p = new_cmdline;
4842 }
4843 return p;
4844}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004845#endif
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847/*
4848 * Expand file name in Ex command argument.
4849 * Return FAIL for failure, OK otherwise.
4850 */
4851 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852expand_filename(
4853 exarg_T *eap,
4854 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004855 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
4857 int has_wildcards; /* need to expand wildcards */
4858 char_u *repl;
4859 int srclen;
4860 char_u *p;
4861 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004862 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863
Bram Moolenaar748bf032005-02-02 23:04:36 +00004864#ifdef FEAT_QUICKFIX
4865 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4866 p = skip_grep_pat(eap);
4867#else
4868 p = eap->arg;
4869#endif
4870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 /*
4872 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4873 * the file name contains a wildcard it should not cause expanding.
4874 * (it will be expanded anyway if there is a wildcard before replacing).
4875 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004876 has_wildcards = mch_has_wildcard(p);
4877 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004879#ifdef FEAT_EVAL
4880 /* Skip over `=expr`, wildcards in it are not expanded. */
4881 if (p[0] == '`' && p[1] == '=')
4882 {
4883 p += 2;
4884 (void)skip_expr(&p);
4885 if (*p == '`')
4886 ++p;
4887 continue;
4888 }
4889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 /*
4891 * Quick check if this cannot be the start of a special string.
4892 * Also removes backslash before '%', '#' and '<'.
4893 */
4894 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4895 {
4896 ++p;
4897 continue;
4898 }
4899
4900 /*
4901 * Try to find a match at this position.
4902 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004903 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4904 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (*errormsgp != NULL) /* error detected */
4906 return FAIL;
4907 if (repl == NULL) /* no match found */
4908 {
4909 p += srclen;
4910 continue;
4911 }
4912
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004913 /* Wildcards won't be expanded below, the replacement is taken
4914 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4915 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4916 {
4917 char_u *l = repl;
4918
4919 repl = expand_env_save(repl);
4920 vim_free(l);
4921 }
4922
Bram Moolenaar63b92542007-03-27 14:57:09 +00004923 /* Need to escape white space et al. with a backslash.
4924 * Don't do this for:
4925 * - replacement that already has been escaped: "##"
4926 * - shell commands (may have to use quotes instead).
4927 * - non-unix systems when there is a single argument (spaces don't
4928 * separate arguments then).
4929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004931 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 && eap->cmdidx != CMD_grep
4934 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004935 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004936 && eap->cmdidx != CMD_lgrep
4937 && eap->cmdidx != CMD_lgrepadd
4938 && eap->cmdidx != CMD_lmake
4939 && eap->cmdidx != CMD_make
4940 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#ifndef UNIX
4942 && !(eap->argt & NOSPC)
4943#endif
4944 )
4945 {
4946 char_u *l;
4947#ifdef BACKSLASH_IN_FILENAME
4948 /* Don't escape a backslash here, because rem_backslash() doesn't
4949 * remove it later. */
4950 static char_u *nobslash = (char_u *)" \t\"|";
4951# define ESCAPE_CHARS nobslash
4952#else
4953# define ESCAPE_CHARS escape_chars
4954#endif
4955
4956 for (l = repl; *l; ++l)
4957 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4958 {
4959 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4960 if (l != NULL)
4961 {
4962 vim_free(repl);
4963 repl = l;
4964 }
4965 break;
4966 }
4967 }
4968
4969 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004970 if ((eap->usefilter || eap->cmdidx == CMD_bang
4971 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004972 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
4974 char_u *l;
4975
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004976 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 if (l != NULL)
4978 {
4979 vim_free(repl);
4980 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 }
4983
4984 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4985 vim_free(repl);
4986 if (p == NULL)
4987 return FAIL;
4988 }
4989
4990 /*
4991 * One file argument: Expand wildcards.
4992 * Don't do this with ":r !command" or ":w !command".
4993 */
4994 if ((eap->argt & NOSPC) && !eap->usefilter)
4995 {
4996 /*
4997 * May do this twice:
4998 * 1. Replace environment variables.
4999 * 2. Replace any other wildcards, remove backslashes.
5000 */
5001 for (n = 1; n <= 2; ++n)
5002 {
5003 if (n == 2)
5004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 /*
5006 * Halve the number of backslashes (this is Vi compatible).
5007 * For Unix and OS/2, when wildcards are expanded, this is
5008 * done by ExpandOne() below.
5009 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005010#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 if (!has_wildcards)
5012#endif
5013 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015
5016 if (has_wildcards)
5017 {
5018 if (n == 1)
5019 {
5020 /*
5021 * First loop: May expand environment variables. This
5022 * can be done much faster with expand_env() than with
5023 * something else (e.g., calling a shell).
5024 * After expanding environment variables, check again
5025 * if there are still wildcards present.
5026 */
5027 if (vim_strchr(eap->arg, '$') != NULL
5028 || vim_strchr(eap->arg, '~') != NULL)
5029 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005030 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005031 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 has_wildcards = mch_has_wildcard(NameBuff);
5033 p = NameBuff;
5034 }
5035 else
5036 p = NULL;
5037 }
5038 else /* n == 2 */
5039 {
5040 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005041 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042
5043 ExpandInit(&xpc);
5044 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005045 if (p_wic)
5046 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005048 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if (p == NULL)
5050 return FAIL;
5051 }
5052 if (p != NULL)
5053 {
5054 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5055 p, cmdlinep);
5056 if (n == 2) /* p came from ExpandOne() */
5057 vim_free(p);
5058 }
5059 }
5060 }
5061 }
5062 return OK;
5063}
5064
5065/*
5066 * Replace part of the command line, keeping eap->cmd, eap->arg and
5067 * eap->nextcmd correct.
5068 * "src" points to the part that is to be replaced, of length "srclen".
5069 * "repl" is the replacement string.
5070 * Returns a pointer to the character after the replaced string.
5071 * Returns NULL for failure.
5072 */
5073 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005074repl_cmdline(
5075 exarg_T *eap,
5076 char_u *src,
5077 int srclen,
5078 char_u *repl,
5079 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
5081 int len;
5082 int i;
5083 char_u *new_cmdline;
5084
5085 /*
5086 * The new command line is build in new_cmdline[].
5087 * First allocate it.
5088 * Careful: a "+cmd" argument may have been NUL terminated.
5089 */
5090 len = (int)STRLEN(repl);
5091 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005092 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5094 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5095 return NULL; /* out of memory! */
5096
5097 /*
5098 * Copy the stuff before the expanded part.
5099 * Copy the expanded stuff.
5100 * Copy what came after the expanded part.
5101 * Copy the next commands, if there are any.
5102 */
5103 i = (int)(src - *cmdlinep); /* length of part before match */
5104 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 mch_memmove(new_cmdline + i, repl, (size_t)len);
5107 i += len; /* remember the end of the string */
5108 STRCPY(new_cmdline + i, src + srclen);
5109 src = new_cmdline + i; /* remember where to continue */
5110
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005111 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
5113 i = (int)STRLEN(new_cmdline) + 1;
5114 STRCPY(new_cmdline + i, eap->nextcmd);
5115 eap->nextcmd = new_cmdline + i;
5116 }
5117 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5118 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5119 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5120 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5121 vim_free(*cmdlinep);
5122 *cmdlinep = new_cmdline;
5123
5124 return src;
5125}
5126
5127/*
5128 * Check for '|' to separate commands and '"' to start comments.
5129 */
5130 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005131separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
5133 char_u *p;
5134
Bram Moolenaar86b68352004-12-27 21:59:20 +00005135#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005136 p = skip_grep_pat(eap);
5137#else
5138 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005139#endif
5140
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005141 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
5143 if (*p == Ctrl_V)
5144 {
5145 if (eap->argt & (USECTRLV | XFILE))
5146 ++p; /* skip CTRL-V and next char */
5147 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005148 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005149 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 if (*p == NUL) /* stop at NUL after CTRL-V */
5151 break;
5152 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005153
5154#ifdef FEAT_EVAL
5155 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005156 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005157 {
5158 p += 2;
5159 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005160 }
5161#endif
5162
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 /* Check for '"': start of comment or '|': next command */
5164 /* :@" and :*" do not start a comment!
5165 * :redir @" doesn't either. */
5166 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5167 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5168 || p != eap->arg)
5169 && (eap->cmdidx != CMD_redir
5170 || p != eap->arg + 1 || p[-1] != '@'))
5171 || *p == '|' || *p == '\n')
5172 {
5173 /*
5174 * We remove the '\' before the '|', unless USECTRLV is used
5175 * AND 'b' is present in 'cpoptions'.
5176 */
5177 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5178 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5179 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005180 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 --p;
5182 }
5183 else
5184 {
5185 eap->nextcmd = check_nextcmd(p);
5186 *p = NUL;
5187 break;
5188 }
5189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005191
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5193 del_trailing_spaces(eap->arg);
5194}
5195
5196/*
5197 * get + command from ex argument
5198 */
5199 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005200getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
5202 char_u *arg = *argp;
5203 char_u *command = NULL;
5204
5205 if (*arg == '+') /* +[command] */
5206 {
5207 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005208 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 command = dollar_command;
5210 else
5211 {
5212 command = arg;
5213 arg = skip_cmd_arg(command, TRUE);
5214 if (*arg != NUL)
5215 *arg++ = NUL; /* terminate command with NUL */
5216 }
5217
5218 arg = skipwhite(arg); /* skip over spaces */
5219 *argp = arg;
5220 }
5221 return command;
5222}
5223
5224/*
5225 * Find end of "+command" argument. Skip over "\ " and "\\".
5226 */
5227 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005228skip_cmd_arg(
5229 char_u *p,
5230 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 while (*p && !vim_isspace(*p))
5233 {
5234 if (*p == '\\' && p[1] != NUL)
5235 {
5236 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005237 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 else
5239 ++p;
5240 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005241 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 return p;
5244}
5245
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005246 int
5247get_bad_opt(char_u *p, exarg_T *eap)
5248{
5249 if (STRICMP(p, "keep") == 0)
5250 eap->bad_char = BAD_KEEP;
5251 else if (STRICMP(p, "drop") == 0)
5252 eap->bad_char = BAD_DROP;
5253 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5254 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005255 else
5256 return FAIL;
5257 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005258}
5259
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260/*
5261 * Get "++opt=arg" argument.
5262 * Return FAIL or OK.
5263 */
5264 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005265getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 char_u *arg = eap->arg + 2;
5268 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005269 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271
5272 /* ":edit ++[no]bin[ary] file" */
5273 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5274 {
5275 if (*arg == 'n')
5276 {
5277 arg += 2;
5278 eap->force_bin = FORCE_NOBIN;
5279 }
5280 else
5281 eap->force_bin = FORCE_BIN;
5282 if (!checkforcmd(&arg, "binary", 3))
5283 return FAIL;
5284 eap->arg = skipwhite(arg);
5285 return OK;
5286 }
5287
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005288 /* ":read ++edit file" */
5289 if (STRNCMP(arg, "edit", 4) == 0)
5290 {
5291 eap->read_edit = TRUE;
5292 eap->arg = skipwhite(arg + 4);
5293 return OK;
5294 }
5295
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 if (STRNCMP(arg, "ff", 2) == 0)
5297 {
5298 arg += 2;
5299 pp = &eap->force_ff;
5300 }
5301 else if (STRNCMP(arg, "fileformat", 10) == 0)
5302 {
5303 arg += 10;
5304 pp = &eap->force_ff;
5305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 else if (STRNCMP(arg, "enc", 3) == 0)
5307 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005308 if (STRNCMP(arg, "encoding", 8) == 0)
5309 arg += 8;
5310 else
5311 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 pp = &eap->force_enc;
5313 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005314 else if (STRNCMP(arg, "bad", 3) == 0)
5315 {
5316 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005317 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 if (pp == NULL || *arg != '=')
5321 return FAIL;
5322
5323 ++arg;
5324 *pp = (int)(arg - eap->cmd);
5325 arg = skip_cmd_arg(arg, FALSE);
5326 eap->arg = skipwhite(arg);
5327 *arg = NUL;
5328
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (pp == &eap->force_ff)
5330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5332 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005333 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005335 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
5337 /* Make 'fileencoding' lower case. */
5338 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5339 *p = TOLOWER_ASC(*p);
5340 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005341 else
5342 {
5343 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5344 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005345 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005346 return FAIL;
5347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
5349 return OK;
5350}
5351
5352/*
5353 * ":abbreviate" and friends.
5354 */
5355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005356ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357{
5358 do_exmap(eap, TRUE); /* almost the same as mapping */
5359}
5360
5361/*
5362 * ":map" and friends.
5363 */
5364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005365ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
5367 /*
5368 * If we are sourcing .exrc or .vimrc in current directory we
5369 * print the mappings for security reasons.
5370 */
5371 if (secure)
5372 {
5373 secure = 2;
5374 msg_outtrans(eap->cmd);
5375 msg_putchar('\n');
5376 }
5377 do_exmap(eap, FALSE);
5378}
5379
5380/*
5381 * ":unmap" and friends.
5382 */
5383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005384ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
5386 do_exmap(eap, FALSE);
5387}
5388
5389/*
5390 * ":mapclear" and friends.
5391 */
5392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005393ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394{
5395 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5396}
5397
5398/*
5399 * ":abclear" and friends.
5400 */
5401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005402ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
5404 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5405}
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005411 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 * directory for security reasons.
5413 */
5414 if (secure)
5415 {
5416 secure = 2;
5417 eap->errmsg = e_curdir;
5418 }
5419 else if (eap->cmdidx == CMD_autocmd)
5420 do_autocmd(eap->arg, eap->forceit);
5421 else
5422 do_augroup(eap->arg, eap->forceit);
5423}
5424
5425/*
5426 * ":doautocmd": Apply the automatic commands to the current buffer.
5427 */
5428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005431 char_u *arg = eap->arg;
5432 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005433 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005434
Bram Moolenaar1610d052016-06-09 22:53:01 +02005435 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5436 /* Only when there is no <nomodeline>. */
5437 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005438 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441/*
5442 * :[N]bunload[!] [N] [bufname] unload buffer
5443 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5444 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5445 */
5446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 eap->errmsg = do_bufdel(
5450 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5451 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5452 : DOBUF_UNLOAD, eap->arg,
5453 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5454}
5455
5456/*
5457 * :[N]buffer [N] to buffer N
5458 * :[N]sbuffer [N] to buffer N
5459 */
5460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
5463 if (*eap->arg)
5464 eap->errmsg = e_trailing;
5465 else
5466 {
5467 if (eap->addr_count == 0) /* default is current buffer */
5468 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5469 else
5470 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005471 if (eap->do_ecmd_cmd != NULL)
5472 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474}
5475
5476/*
5477 * :[N]bmodified [N] to next mod. buffer
5478 * :[N]sbmodified [N] to next mod. buffer
5479 */
5480 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005481ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005484 if (eap->do_ecmd_cmd != NULL)
5485 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486}
5487
5488/*
5489 * :[N]bnext [N] to next buffer
5490 * :[N]sbnext [N] split and to next buffer
5491 */
5492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005496 if (eap->do_ecmd_cmd != NULL)
5497 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498}
5499
5500/*
5501 * :[N]bNext [N] to previous buffer
5502 * :[N]bprevious [N] to previous buffer
5503 * :[N]sbNext [N] split and to previous buffer
5504 * :[N]sbprevious [N] split and to previous buffer
5505 */
5506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005510 if (eap->do_ecmd_cmd != NULL)
5511 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512}
5513
5514/*
5515 * :brewind to first buffer
5516 * :bfirst to first buffer
5517 * :sbrewind split and to first buffer
5518 * :sbfirst split and to first buffer
5519 */
5520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005521ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005524 if (eap->do_ecmd_cmd != NULL)
5525 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526}
5527
5528/*
5529 * :blast to last buffer
5530 * :sblast split and to last buffer
5531 */
5532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005536 if (eap->do_ecmd_cmd != NULL)
5537 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005541ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
5543 return (c == NUL || c == '|' || c == '"' || c == '\n');
5544}
5545
5546#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5547 || defined(PROTO)
5548/*
5549 * Return the next command, after the first '|' or '\n'.
5550 * Return NULL if not found.
5551 */
5552 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 while (*p != '|' && *p != '\n')
5556 {
5557 if (*p == NUL)
5558 return NULL;
5559 ++p;
5560 }
5561 return (p + 1);
5562}
5563#endif
5564
5565/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005566 * Check if *p is a separator between Ex commands, skipping over white space.
5567 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 */
5569 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005570check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005572 char_u *s = skipwhite(p);
5573
5574 if (*s == '|' || *s == '\n')
5575 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 else
5577 return NULL;
5578}
5579
5580/*
5581 * - if there are more files to edit
5582 * - and this is the last window
5583 * - and forceit not used
5584 * - and not repeated twice on a row
5585 * return FAIL and give error message if 'message' TRUE
5586 * return OK otherwise
5587 */
5588 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589check_more(
5590 int message, /* when FALSE check only, no messages */
5591 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592{
5593 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5594
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005595 if (!forceit && only_one_window()
5596 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 {
5598 if (message)
5599 {
5600#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5601 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5602 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005603 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005605 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5606 NGETTEXT("%d more file to edit. Quit anyway?",
5607 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5609 return OK;
5610 return FAIL;
5611 }
5612#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005613 semsg(NGETTEXT("E173: %d more file to edit",
5614 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 quitmore = 2; /* next try to quit is allowed */
5616 }
5617 return FAIL;
5618 }
5619 return OK;
5620}
5621
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005622#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623/*
5624 * Function given to ExpandGeneric() to obtain the list of command names.
5625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005627get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628{
5629 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 return cmdnames[idx].cmd_name;
5632}
5633#endif
5634
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
Bram Moolenaare6850792010-05-14 15:28:44 +02005638 if (*eap->arg == NUL)
5639 {
5640#ifdef FEAT_EVAL
5641 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5642 char_u *p = NULL;
5643
5644 if (expr != NULL)
5645 {
5646 ++emsg_off;
5647 p = eval_to_string(expr, NULL, FALSE);
5648 --emsg_off;
5649 vim_free(expr);
5650 }
5651 if (p != NULL)
5652 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005653 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005654 vim_free(p);
5655 }
5656 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005657 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005658#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005659 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005660#endif
5661 }
5662 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005663 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005664
5665#ifdef FEAT_VTP
5666 else if (has_vtp_working())
5667 {
5668 // background color change requires clear + redraw
5669 update_screen(CLEAR);
5670 redrawcmd();
5671 }
5672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673}
5674
5675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005679 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 do_highlight(eap->arg, eap->forceit, FALSE);
5681}
5682
5683
5684/*
5685 * Call this function if we thought we were going to exit, but we won't
5686 * (because of an error). May need to restore the terminal mode.
5687 */
5688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
5691 exiting = FALSE;
5692 settmode(TMODE_RAW);
5693}
5694
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005695 static int
5696before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5697{
5698 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5699
5700 /* Bail out when autocommands closed the window.
5701 * Refuse to quit when the buffer in the last window is being closed (can
5702 * only happen in autocommands). */
5703 if (!win_valid(wp)
5704 || curbuf_locked()
5705 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5706 return TRUE;
5707
5708 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5709 {
5710 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5711 /* Refuse to quit when locked or when the buffer in the last window is
5712 * being closed (can only happen in autocommands). */
5713 if (curbuf_locked()
5714 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5715 return TRUE;
5716 }
5717
5718 return FALSE;
5719}
5720
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005722 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005723 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 */
5725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005726ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005728 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730#ifdef FEAT_CMDWIN
5731 if (cmdwin_type != 0)
5732 {
5733 cmdwin_result = Ctrl_C;
5734 return;
5735 }
5736#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005737 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005738 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005739 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005740 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005741 return;
5742 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005743 if (eap->addr_count > 0)
5744 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005745 int wnr = eap->line2;
5746
5747 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5748 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005749 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005750 }
5751 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005752 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005753
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005754 /* Refuse to quit when locked. */
5755 if (curbuf_locked())
5756 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005757
5758 /* Trigger QuitPre and maybe ExitPre */
5759 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005760 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761
5762#ifdef FEAT_NETBEANS_INTG
5763 netbeansForcedQuit = eap->forceit;
5764#endif
5765
5766 /*
5767 * If there are more files or windows we won't exit.
5768 */
5769 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5770 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005771 if ((!buf_hide(wp->w_buffer)
5772 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005773 | (eap->forceit ? CCGD_FORCEIT : 0)
5774 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005776 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {
5778 not_exiting();
5779 }
5780 else
5781 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005782 /* quit last window
5783 * Note: only_one_window() returns true, even so a help window is
5784 * still open. In that case only quit, if no address has been
5785 * specified. Example:
5786 * :h|wincmd w|1q - don't quit
5787 * :h|wincmd w|q - quit
5788 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005789 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005791 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005792#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005796 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 }
5798}
5799
5800/*
5801 * ":cquit".
5802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005804ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 getout(1); /* this does not always pass on the exit code to the Manx
5807 compiler. why? */
5808}
5809
5810/*
5811 * ":qall": try to quit all windows
5812 */
5813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005814ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816# ifdef FEAT_CMDWIN
5817 if (cmdwin_type != 0)
5818 {
5819 if (eap->forceit)
5820 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5821 else
5822 cmdwin_result = K_XF2;
5823 return;
5824 }
5825# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005826
5827 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005828 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005829 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005830 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005831 return;
5832 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005833
5834 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005835 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005836
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005838 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 getout(0);
5840 not_exiting();
5841}
5842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843/*
5844 * ":close": close current window, unless it is the last one
5845 */
5846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005847ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005849 win_T *win;
5850 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005851#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005853 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005855#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005856 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005857 {
5858 if (eap->addr_count == 0)
5859 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005860 else
5861 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005862 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005863 {
5864 winnr++;
5865 if (winnr == eap->line2)
5866 break;
5867 }
5868 if (win == NULL)
5869 win = lastwin;
5870 ex_win_close(eap->forceit, win, NULL);
5871 }
5872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873}
5874
Bram Moolenaar4033c552017-09-16 20:54:51 +02005875#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005876/*
5877 * ":pclose": Close any preview window.
5878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005880ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005881{
5882 win_T *win;
5883
Bram Moolenaar29323592016-07-24 22:04:11 +02005884 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005885 if (win->w_p_pvw)
5886 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005887 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005888 break;
5889 }
5890}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005891#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005892
Bram Moolenaarf740b292006-02-16 22:11:02 +00005893/*
5894 * Close window "win" and take care of handling closing the last window for a
5895 * modified buffer.
5896 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005898ex_win_close(
5899 int forceit,
5900 win_T *win,
5901 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 int need_hide;
5904 buf_T *buf = win->w_buffer;
5905
5906 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005907 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005909#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if ((p_confirm || cmdmod.confirm) && p_write)
5911 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005912 bufref_T bufref;
5913
5914 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005916 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 return;
5918 need_hide = FALSE;
5919 }
5920 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005923 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 return;
5925 }
5926 }
5927
Bram Moolenaar4033c552017-09-16 20:54:51 +02005928#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005930#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005933 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005934 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005935 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005936 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937}
5938
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005940 * Handle the argument for a tabpage related ex command.
5941 * Returns a tabpage number.
5942 * When an error is encountered then eap->errmsg is set.
5943 */
5944 static int
5945get_tabpage_arg(exarg_T *eap)
5946{
5947 int tab_number;
5948 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5949
5950 if (eap->arg && *eap->arg != NUL)
5951 {
5952 char_u *p = eap->arg;
5953 char_u *p_save;
5954 int relative = 0; /* argument +N/-N means: go to N places to the
5955 * right/left relative to the current position. */
5956
5957 if (*p == '-')
5958 {
5959 relative = -1;
5960 p++;
5961 }
5962 else if (*p == '+')
5963 {
5964 relative = 1;
5965 p++;
5966 }
5967
5968 p_save = p;
5969 tab_number = getdigits(&p);
5970
5971 if (relative == 0)
5972 {
5973 if (STRCMP(p, "$") == 0)
5974 tab_number = LAST_TAB_NR;
5975 else if (p == p_save || *p_save == '-' || *p != NUL
5976 || tab_number > LAST_TAB_NR)
5977 {
5978 /* No numbers as argument. */
5979 eap->errmsg = e_invarg;
5980 goto theend;
5981 }
5982 }
5983 else
5984 {
5985 if (*p_save == NUL)
5986 tab_number = 1;
5987 else if (p == p_save || *p_save == '-' || *p != NUL
5988 || tab_number == 0)
5989 {
5990 /* No numbers as argument. */
5991 eap->errmsg = e_invarg;
5992 goto theend;
5993 }
5994 tab_number = tab_number * relative + tabpage_index(curtab);
5995 if (!unaccept_arg0 && relative == -1)
5996 --tab_number;
5997 }
5998 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5999 eap->errmsg = e_invarg;
6000 }
6001 else if (eap->addr_count > 0)
6002 {
6003 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006004 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006005 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006006 tab_number = 0;
6007 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006008 else
6009 {
6010 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006011 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006012 {
6013 --tab_number;
6014 if (tab_number < unaccept_arg0)
6015 eap->errmsg = e_invarg;
6016 }
6017 }
6018 }
6019 else
6020 {
6021 switch (eap->cmdidx)
6022 {
6023 case CMD_tabnext:
6024 tab_number = tabpage_index(curtab) + 1;
6025 if (tab_number > LAST_TAB_NR)
6026 tab_number = 1;
6027 break;
6028 case CMD_tabmove:
6029 tab_number = LAST_TAB_NR;
6030 break;
6031 default:
6032 tab_number = tabpage_index(curtab);
6033 }
6034 }
6035
6036theend:
6037 return tab_number;
6038}
6039
6040/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006041 * ":tabclose": close current tab page, unless it is the last one.
6042 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 */
6044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006045ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006047 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006048 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006049
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006050# ifdef FEAT_CMDWIN
6051 if (cmdwin_type != 0)
6052 cmdwin_result = K_IGNORE;
6053 else
6054# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006055 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006056 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006057 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006059 tab_number = get_tabpage_arg(eap);
6060 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006061 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006062 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006063 if (tp == NULL)
6064 {
6065 beep_flush();
6066 return;
6067 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006068 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006069 {
6070 tabpage_close_other(tp, eap->forceit);
6071 return;
6072 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006073 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006074 tabpage_close(eap->forceit);
6075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006077}
6078
6079/*
6080 * ":tabonly": close all tab pages except the current one
6081 */
6082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006083ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006084{
6085 tabpage_T *tp;
6086 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006087 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006088
6089# ifdef FEAT_CMDWIN
6090 if (cmdwin_type != 0)
6091 cmdwin_result = K_IGNORE;
6092 else
6093# endif
6094 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006095 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006096 else
6097 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006098 tab_number = get_tabpage_arg(eap);
6099 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006100 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006101 goto_tabpage(tab_number);
6102 /* Repeat this up to a 1000 times, because autocommands may
6103 * mess up the lists. */
6104 for (done = 0; done < 1000; ++done)
6105 {
6106 FOR_ALL_TABPAGES(tp)
6107 if (tp->tp_topframe != topframe)
6108 {
6109 tabpage_close_other(tp, eap->forceit);
6110 /* if we failed to close it quit */
6111 if (valid_tabpage(tp))
6112 done = 1000;
6113 /* start over, "tp" is now invalid */
6114 break;
6115 }
6116 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006117 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006118 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006119 }
6120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122
6123/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006124 * Close the current tab page.
6125 */
6126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006127tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006128{
6129 /* First close all the windows but the current one. If that worked then
6130 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006131 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006132 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006133 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006134 ex_win_close(forceit, curwin, NULL);
6135# ifdef FEAT_GUI
6136 need_mouse_correct = TRUE;
6137# endif
6138}
6139
6140/*
6141 * Close tab page "tp", which is not the current tab page.
6142 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006143 * Also takes care of the tab pages line disappearing when closing the
6144 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006145 */
6146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006147tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006148{
6149 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006150 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006151 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006152
6153 /* Limit to 1000 windows, autocommands may add a window while we close
6154 * one. OK, so I'm paranoid... */
6155 while (++done < 1000)
6156 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006157 wp = tp->tp_firstwin;
6158 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006159
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006160 /* Autocommands may delete the tab page under our fingers and we may
6161 * fail to close a window with a modified buffer. */
6162 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006163 break;
6164 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006165
Bram Moolenaar29323592016-07-24 22:04:11 +02006166 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006167
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006168 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006169 if (h != tabline_height())
6170 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006171}
6172
6173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 * ":only".
6175 */
6176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006177ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006179 win_T *wp;
6180 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181# ifdef FEAT_GUI
6182 need_mouse_correct = TRUE;
6183# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006184 if (eap->addr_count > 0)
6185 {
6186 wnr = eap->line2;
6187 for (wp = firstwin; --wnr > 0; )
6188 {
6189 if (wp->w_next == NULL)
6190 break;
6191 else
6192 wp = wp->w_next;
6193 }
6194 win_goto(wp);
6195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 close_others(TRUE, eap->forceit);
6197}
6198
6199/*
6200 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006201 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006203 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006204ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 if (eap->addr_count == 0)
6207 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006208 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210
6211 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006212ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006214 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006215 if (!eap->skip)
6216 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006217#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006218 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006219#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006220 if (eap->addr_count == 0)
6221 win_close(curwin, FALSE); /* don't free buffer */
6222 else
6223 {
6224 int winnr = 0;
6225 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006226
Bram Moolenaar2256c992016-11-15 21:17:07 +01006227 FOR_ALL_WINDOWS(win)
6228 {
6229 winnr++;
6230 if (winnr == eap->line2)
6231 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006232 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006233 if (win == NULL)
6234 win = lastwin;
6235 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
6238}
6239
6240/*
6241 * ":stop" and ":suspend": Suspend Vim.
6242 */
6243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006244ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
6246 /*
6247 * Disallow suspending for "rvim".
6248 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006249 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 {
6251 if (!eap->forceit)
6252 autowrite_all();
6253 windgoto((int)Rows - 1, 0);
6254 out_char('\n');
6255 out_flush();
6256 stoptermcap();
6257 out_flush(); /* needed for SUN to restore xterm buffer */
6258#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006259 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260#endif
6261 ui_suspend(); /* call machine specific function */
6262#ifdef FEAT_TITLE
6263 maketitle();
6264 resettitle(); /* force updating the title */
6265#endif
6266 starttermcap();
6267 scroll_start(); /* scroll screen before redrawing */
6268 redraw_later_clear();
6269 shell_resized(); /* may have resized window */
6270 }
6271}
6272
6273/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006274 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 */
6276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279#ifdef FEAT_CMDWIN
6280 if (cmdwin_type != 0)
6281 {
6282 cmdwin_result = Ctrl_C;
6283 return;
6284 }
6285#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006286 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006287 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006288 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006289 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006290 return;
6291 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006292
6293 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006294 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295
6296 /*
6297 * if more files or windows we won't exit
6298 */
6299 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6300 exiting = TRUE;
6301 if ( ((eap->cmdidx == CMD_wq
6302 || curbufIsChanged())
6303 && do_write(eap) == FAIL)
6304 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006305 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
6307 not_exiting();
6308 }
6309 else
6310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006313 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314# ifdef FEAT_GUI
6315 need_mouse_correct = TRUE;
6316# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006317 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006318 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
6320}
6321
6322/*
6323 * ":print", ":list", ":number".
6324 */
6325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006326ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006328 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006329 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006332 for ( ;!got_int; ui_breakcheck())
6333 {
6334 print_line(eap->line1,
6335 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6336 || (eap->flags & EXFLAG_NR)),
6337 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6338 if (++eap->line1 > eap->line2)
6339 break;
6340 out_flush(); /* show one line at a time */
6341 }
6342 setpcmark();
6343 /* put cursor at last line */
6344 curwin->w_cursor.lnum = eap->line2;
6345 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 }
6347
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349}
6350
6351#ifdef FEAT_BYTEOFF
6352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006353ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
6355 goto_byte(eap->line2);
6356}
6357#endif
6358
6359/*
6360 * ":shell".
6361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006363ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 do_shell(NULL, 0);
6366}
6367
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006368#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006370static int drop_busy = FALSE;
6371static int drop_filec;
6372static char_u **drop_filev = NULL;
6373static int drop_split;
6374static void (*drop_callback)(void *);
6375static void *drop_cookie;
6376
6377 static void
6378handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 exarg_T ea;
6381 int save_msg_scroll = msg_scroll;
6382
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006383 // Setting the argument list may cause screen updates and being called
6384 // recursively. Avoid that by setting drop_busy.
6385 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
6387 /* Check whether the current buffer is changed. If so, we will need
6388 * to split the current window or data could be lost.
6389 * We don't need to check if the 'hidden' option is set, as in this
6390 * case the buffer won't be lost.
6391 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006392 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 {
6394 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006395 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 --emsg_off;
6397 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006398 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (win_split(0, 0) == FAIL)
6401 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006402 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404 /* When splitting the window, create a new alist. Otherwise the
6405 * existing one is overwritten. */
6406 alist_unlink(curwin->w_alist);
6407 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 }
6409
6410 /*
6411 * Set up the new argument list.
6412 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006413 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414
6415 /*
6416 * Move to the first file.
6417 */
6418 /* Fake up a minimal "next" command for do_argfile() */
6419 vim_memset(&ea, 0, sizeof(ea));
6420 ea.cmd = (char_u *)"next";
6421 do_argfile(&ea, 0);
6422
6423 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6424 * mode that is not needed here. */
6425 need_start_insertmode = FALSE;
6426
6427 /* Restore msg_scroll, otherwise a following command may cause scrolling
6428 * unexpectedly. The screen will be redrawn by the caller, thus
6429 * msg_scroll being set by displaying a message is irrelevant. */
6430 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006431
6432 if (drop_callback != NULL)
6433 drop_callback(drop_cookie);
6434
6435 drop_filev = NULL;
6436 drop_busy = FALSE;
6437}
6438
6439/*
6440 * Handle a file drop. The code is here because a drop is *nearly* like an
6441 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006442 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006443 * code is very similar to :args and hence needs access to a lot of the static
6444 * functions in this file.
6445 *
6446 * The "filev" list must have been allocated using alloc(), as should each item
6447 * in the list. This function takes over responsibility for freeing the "filev"
6448 * list.
6449 */
6450 void
6451handle_drop(
6452 int filec, // the number of files dropped
6453 char_u **filev, // the list of files dropped
6454 int split, // force splitting the window
6455 void (*callback)(void *), // to be called after setting the argument
6456 // list
6457 void *cookie) // argument for "callback" (allocated)
6458{
6459 // Cannot handle recursive drops, finish the pending one.
6460 if (drop_busy)
6461 {
6462 FreeWild(filec, filev);
6463 vim_free(cookie);
6464 return;
6465 }
6466
6467 // When calling handle_drop() more than once in a row we only use the last
6468 // one.
6469 if (drop_filev != NULL)
6470 {
6471 FreeWild(drop_filec, drop_filev);
6472 vim_free(drop_cookie);
6473 }
6474
6475 drop_filec = filec;
6476 drop_filev = filev;
6477 drop_split = split;
6478 drop_callback = callback;
6479 drop_cookie = cookie;
6480
6481 // Postpone this when:
6482 // - editing the command line
6483 // - not possible to change the current buffer
6484 // - updating the screen
6485 // As it may change buffers and window structures that are in use and cause
6486 // freed memory to be used.
6487 if (text_locked() || curbuf_locked() || updating_screen)
6488 return;
6489
6490 handle_drop_internal();
6491}
6492
6493/*
6494 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6495 * reset: Handle a postponed drop.
6496 */
6497 void
6498handle_any_postponed_drop(void)
6499{
6500 if (!drop_busy && drop_filev != NULL
6501 && !text_locked() && !curbuf_locked() && !updating_screen)
6502 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
6504#endif
6505
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506/*
6507 * Clear an argument list: free all file names and reset it to zero entries.
6508 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006510alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511{
6512 while (--al->al_ga.ga_len >= 0)
6513 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6514 ga_clear(&al->al_ga);
6515}
6516
6517/*
6518 * Init an argument list.
6519 */
6520 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006521alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522{
6523 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6524}
6525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
6527 * Remove a reference from an argument list.
6528 * Ignored when the argument list is the global one.
6529 * If the argument list is no longer used by any window, free it.
6530 */
6531 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006532alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533{
6534 if (al != &global_alist && --al->al_refcount <= 0)
6535 {
6536 alist_clear(al);
6537 vim_free(al);
6538 }
6539}
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541/*
6542 * Create a new argument list and use it for the current window.
6543 */
6544 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006545alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6548 if (curwin->w_alist == NULL)
6549 {
6550 curwin->w_alist = &global_alist;
6551 ++global_alist.al_refcount;
6552 }
6553 else
6554 {
6555 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006556 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 alist_init(curwin->w_alist);
6558 }
6559}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006561#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562/*
6563 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006564 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6565 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 */
6567 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006568alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
6570 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006571 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 char_u **new_arg_files;
6573 int new_arg_file_count;
6574 char_u *save_p_su = p_su;
6575 int i;
6576
6577 /* Don't use 'suffixes' here. This should work like the shell did the
6578 * expansion. Also, the vimrc file isn't read yet, thus the user
6579 * can't set the options. */
6580 p_su = empty_option;
6581 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6582 if (old_arg_files != NULL)
6583 {
6584 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006585 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6586 old_arg_count = GARGCOUNT;
6587 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006589 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 && new_arg_file_count > 0)
6591 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006592 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6593 TRUE, fnum_list, fnum_len);
6594 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
6597 p_su = save_p_su;
6598}
6599#endif
6600
6601/*
6602 * Set the argument list for the current window.
6603 * Takes over the allocated files[] and the allocated fnames in it.
6604 */
6605 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006606alist_set(
6607 alist_T *al,
6608 int count,
6609 char_u **files,
6610 int use_curbuf,
6611 int *fnum_list,
6612 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006615 static int recursive = 0;
6616
6617 if (recursive)
6618 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006619 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006620 return;
6621 }
6622 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
6624 alist_clear(al);
6625 if (ga_grow(&al->al_ga, count) == OK)
6626 {
6627 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006628 {
6629 if (got_int)
6630 {
6631 /* When adding many buffers this can take a long time. Allow
6632 * interrupting here. */
6633 while (i < count)
6634 vim_free(files[i++]);
6635 break;
6636 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006637
6638 /* May set buffer name of a buffer previously used for the
6639 * argument list, so that it's re-used by alist_add. */
6640 if (fnum_list != NULL && i < fnum_len)
6641 buf_set_name(fnum_list[i], files[i]);
6642
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006644 ui_breakcheck();
6645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 vim_free(files);
6647 }
6648 else
6649 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006652
6653 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654}
6655
6656/*
6657 * Add file "fname" to argument list "al".
6658 * "fname" must have been allocated and "al" must have been checked for room.
6659 */
6660 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006661alist_add(
6662 alist_T *al,
6663 char_u *fname,
6664 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 if (fname == NULL) /* don't add NULL file names */
6667 return;
6668#ifdef BACKSLASH_IN_FILENAME
6669 slash_adjust(fname);
6670#endif
6671 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6672 if (set_fnum > 0)
6673 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6674 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6675 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676}
6677
6678#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6679/*
6680 * Adjust slashes in file names. Called after 'shellslash' was set.
6681 */
6682 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006683alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006687 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
6689 for (i = 0; i < GARGCOUNT; ++i)
6690 if (GARGLIST[i].ae_fname != NULL)
6691 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006692 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (wp->w_alist != &global_alist)
6694 for (i = 0; i < WARGCOUNT(wp); ++i)
6695 if (WARGLIST(wp)[i].ae_fname != NULL)
6696 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697}
6698#endif
6699
6700/*
6701 * ":preserve".
6702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006704ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006706 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 ml_preserve(curbuf, TRUE);
6708}
6709
6710/*
6711 * ":recover".
6712 */
6713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006714ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
6716 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6717 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006718 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6719 | CCGD_MULTWIN
6720 | (eap->forceit ? CCGD_FORCEIT : 0)
6721 | CCGD_EXCMD)
6722
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 && (*eap->arg == NUL
6724 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6725 ml_recover();
6726 recoverymode = FALSE;
6727}
6728
6729/*
6730 * Command modifier used in a wrong way.
6731 */
6732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006733ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 eap->errmsg = e_invcmd;
6736}
6737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738/*
6739 * :sview [+command] file split window with new file, read-only
6740 * :split [[+command] file] split window with current or new file
6741 * :vsplit [[+command] file] split window vertically with current or new file
6742 * :new [[+command] file] split window with no or new file
6743 * :vnew [[+command] file] split vertically window with no or new file
6744 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006745 *
6746 * :tabedit open new Tab page with empty window
6747 * :tabedit [+command] file open new Tab page and edit "file"
6748 * :tabnew [[+command] file] just like :tabedit
6749 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 */
6751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006752ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006754 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006755#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006757#endif
6758#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006760#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006761 int use_tab = eap->cmdidx == CMD_tabedit
6762 || eap->cmdidx == CMD_tabfind
6763 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
Bram Moolenaar4033c552017-09-16 20:54:51 +02006765#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
Bram Moolenaar4033c552017-09-16 20:54:51 +02006769#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006771 * quickfix windows. But it's OK when doing ":tab split". */
6772 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 {
6774 if (eap->cmdidx == CMD_split)
6775 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 if (eap->cmdidx == CMD_vsplit)
6777 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780
Bram Moolenaar4033c552017-09-16 20:54:51 +02006781#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006782 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {
6784 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6785 FNAME_MESS, TRUE, curbuf->b_ffname);
6786 if (fname == NULL)
6787 goto theend;
6788 eap->arg = fname;
6789 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006790# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006791 else
6792# endif
6793#endif
6794#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 && eap->cmdidx != CMD_new)
6798 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006799 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006800# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006801 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006802# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006803 au_has_group((char_u *)"FileExplorer"))
6804 {
6805 /* No browsing supported but we do have the file explorer:
6806 * Edit the directory. */
6807 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6808 eap->arg = (char_u *)".";
6809 }
6810 else
6811 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006812 fname = do_browse(0, (char_u *)(use_tab
6813 ? _("Edit File in new tab page")
6814 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006816 if (fname == NULL)
6817 goto theend;
6818 eap->arg = fname;
6819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 }
6821 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006824 /*
6825 * Either open new tab page or split the window.
6826 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006827 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006828 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006829 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6830 : eap->addr_count == 0 ? 0
6831 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006832 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006833 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006834
6835 /* set the alternate buffer for the window we came from */
6836 if (curwin != old_curwin
6837 && win_valid(old_curwin)
6838 && old_curwin->w_buffer != curbuf
6839 && !cmdmod.keepalt)
6840 old_curwin->w_alt_fnum = curbuf->b_fnum;
6841 }
6842 }
6843 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 /* Reset 'scrollbind' when editing another file, but keep it when
6847 * doing ":split" without arguments. */
6848 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006849# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006853 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 else
6855 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 do_exedit(eap, old_curwin);
6857 }
6858
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006859# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006863# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864theend:
6865 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006868
6869/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006870 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006871 */
6872 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006873tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006874{
6875 exarg_T ea;
6876
6877 vim_memset(&ea, 0, sizeof(ea));
6878 ea.cmdidx = CMD_tabnew;
6879 ea.cmd = (char_u *)"tabn";
6880 ea.arg = (char_u *)"";
6881 ex_splitview(&ea);
6882}
6883
6884/*
6885 * :tabnext command
6886 */
6887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006888ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006889{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006890 int tab_number;
6891
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006892 switch (eap->cmdidx)
6893 {
6894 case CMD_tabfirst:
6895 case CMD_tabrewind:
6896 goto_tabpage(1);
6897 break;
6898 case CMD_tablast:
6899 goto_tabpage(9999);
6900 break;
6901 case CMD_tabprevious:
6902 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006903 if (eap->arg && *eap->arg != NUL)
6904 {
6905 char_u *p = eap->arg;
6906 char_u *p_save = p;
6907
6908 tab_number = getdigits(&p);
6909 if (p == p_save || *p_save == '-' || *p != NUL
6910 || tab_number == 0)
6911 {
6912 /* No numbers as argument. */
6913 eap->errmsg = e_invarg;
6914 return;
6915 }
6916 }
6917 else
6918 {
6919 if (eap->addr_count == 0)
6920 tab_number = 1;
6921 else
6922 {
6923 tab_number = eap->line2;
6924 if (tab_number < 1)
6925 {
6926 eap->errmsg = e_invrange;
6927 return;
6928 }
6929 }
6930 }
6931 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006932 break;
6933 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006934 tab_number = get_tabpage_arg(eap);
6935 if (eap->errmsg == NULL)
6936 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006937 break;
6938 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006939}
6940
6941/*
6942 * :tabmove command
6943 */
6944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006945ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006946{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006947 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006948
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006949 tab_number = get_tabpage_arg(eap);
6950 if (eap->errmsg == NULL)
6951 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006952}
6953
6954/*
6955 * :tabs command: List tabs and their contents.
6956 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006958ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006959{
6960 tabpage_T *tp;
6961 win_T *wp;
6962 int tabcount = 1;
6963
6964 msg_start();
6965 msg_scroll = TRUE;
6966 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6967 {
6968 msg_putchar('\n');
6969 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006970 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006971 out_flush(); /* output one line at a time */
6972 ui_breakcheck();
6973
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006974 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006975 wp = firstwin;
6976 else
6977 wp = tp->tp_firstwin;
6978 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6979 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006980 msg_putchar('\n');
6981 msg_putchar(wp == curwin ? '>' : ' ');
6982 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006983 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6984 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006985 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006986 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006987 else
6988 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6989 IObuff, IOSIZE, TRUE);
6990 msg_outtrans(IObuff);
6991 out_flush(); /* output one line at a time */
6992 ui_breakcheck();
6993 }
6994 }
6995}
6996
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997/*
6998 * ":mode": Set screen mode.
6999 * If no argument given, just get the screen size and redraw.
7000 */
7001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007002ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
7004 if (*eap->arg == NUL)
7005 shell_resized();
7006 else
7007 mch_screenmode(eap->arg);
7008}
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010/*
7011 * ":resize".
7012 * set, increment or decrement current window height
7013 */
7014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007015ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 int n;
7018 win_T *wp = curwin;
7019
7020 if (eap->addr_count > 0)
7021 {
7022 n = eap->line2;
7023 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7024 ;
7025 }
7026
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007027# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (cmdmod.split & WSP_VERT)
7032 {
7033 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007034 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7036 n = 9999;
7037 win_setwidth_win((int)n, wp);
7038 }
7039 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {
7041 if (*eap->arg == '-' || *eap->arg == '+')
7042 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007043 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 n = 9999;
7045 win_setheight_win((int)n, wp);
7046 }
7047}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
7049/*
7050 * ":find [+command] <file>" command.
7051 */
7052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007053ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
7055#ifdef FEAT_SEARCHPATH
7056 char_u *fname;
7057 int count;
7058
7059 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7060 TRUE, curbuf->b_ffname);
7061 if (eap->addr_count > 0)
7062 {
7063 /* Repeat finding the file "count" times. This matters when it
7064 * appears several times in the path. */
7065 count = eap->line2;
7066 while (fname != NULL && --count > 0)
7067 {
7068 vim_free(fname);
7069 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7070 FALSE, curbuf->b_ffname);
7071 }
7072 }
7073
7074 if (fname != NULL)
7075 {
7076 eap->arg = fname;
7077#endif
7078 do_exedit(eap, NULL);
7079#ifdef FEAT_SEARCHPATH
7080 vim_free(fname);
7081 }
7082#endif
7083}
7084
7085/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007086 * ":open" simulation: for now just work like ":visual".
7087 */
7088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007089ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007090{
7091 regmatch_T regmatch;
7092 char_u *p;
7093
7094 curwin->w_cursor.lnum = eap->line2;
7095 beginline(BL_SOL | BL_FIX);
7096 if (*eap->arg == '/')
7097 {
7098 /* ":open /pattern/": put cursor in column found with pattern */
7099 ++eap->arg;
7100 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7101 *p = NUL;
7102 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7103 if (regmatch.regprog != NULL)
7104 {
7105 regmatch.rm_ic = p_ic;
7106 p = ml_get_curline();
7107 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007108 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007109 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007110 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007111 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007112 }
7113 /* Move to the NUL, ignore any other arguments. */
7114 eap->arg += STRLEN(eap->arg);
7115 }
7116 check_cursor();
7117
7118 eap->cmdidx = CMD_visual;
7119 do_exedit(eap, NULL);
7120}
7121
7122/*
7123 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 */
7125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007126ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
7128 do_exedit(eap, NULL);
7129}
7130
7131/*
7132 * ":edit <file>" command and alikes.
7133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007135do_exedit(
7136 exarg_T *eap,
7137 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
7139 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007141 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
7143 /*
7144 * ":vi" command ends Ex mode.
7145 */
7146 if (exmode_active && (eap->cmdidx == CMD_visual
7147 || eap->cmdidx == CMD_view))
7148 {
7149 exmode_active = FALSE;
7150 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007151 {
7152 /* Special case: ":global/pat/visual\NLvi-commands" */
7153 if (global_busy)
7154 {
7155 int rd = RedrawingDisabled;
7156 int nwr = no_wait_return;
7157 int ms = msg_scroll;
7158#ifdef FEAT_GUI
7159 int he = hold_gui_events;
7160#endif
7161
7162 if (eap->nextcmd != NULL)
7163 {
7164 stuffReadbuff(eap->nextcmd);
7165 eap->nextcmd = NULL;
7166 }
7167
7168 if (exmode_was != EXMODE_VIM)
7169 settmode(TMODE_RAW);
7170 RedrawingDisabled = 0;
7171 no_wait_return = 0;
7172 need_wait_return = FALSE;
7173 msg_scroll = 0;
7174#ifdef FEAT_GUI
7175 hold_gui_events = 0;
7176#endif
7177 must_redraw = CLEAR;
7178
7179 main_loop(FALSE, TRUE);
7180
7181 RedrawingDisabled = rd;
7182 no_wait_return = nwr;
7183 msg_scroll = ms;
7184#ifdef FEAT_GUI
7185 hold_gui_events = he;
7186#endif
7187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 }
7191
7192 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007193 || eap->cmdidx == CMD_tabnew
7194 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007195 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007197 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 setpcmark();
7199 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007200 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7201 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007203 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 || *eap->arg != NUL
7205#ifdef FEAT_BROWSE
7206 || cmdmod.browse
7207#endif
7208 )
7209 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007210 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7211 * can bring us here, others are stopped earlier. */
7212 if (*eap->arg != NUL && curbuf_locked())
7213 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007214
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 n = readonlymode;
7216 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7217 readonlymode = TRUE;
7218 else if (eap->cmdidx == CMD_enew)
7219 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7220 empty buffer */
7221 setpcmark();
7222 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7223 NULL, eap,
7224 /* ":edit" goes to first line if Vi compatible */
7225 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7226 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7227 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007228 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007230 /* after a split we can use an existing buffer */
7231 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007233 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 {
7235 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 if (old_curwin != NULL)
7237 {
7238 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007239 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007241#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007242 cleanup_T cs;
7243
7244 /* Reset the error/interrupt/exception state here so that
7245 * aborting() returns FALSE when closing a window. */
7246 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007247#endif
7248#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007250#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007251 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007252
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007253#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007254 /* Restore the error/interrupt/exception state if not
7255 * discarded by a new aborting error, interrupt, or
7256 * uncaught exception. */
7257 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 }
7260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
7262 else if (readonlymode && curbuf->b_nwindows == 1)
7263 {
7264 /* When editing an already visited buffer, 'readonly' won't be set
7265 * but the previous value is kept. With ":view" and ":sview" we
7266 * want the file to be readonly, except when another window is
7267 * editing the same buffer. */
7268 curbuf->b_p_ro = TRUE;
7269 }
7270 readonlymode = n;
7271 }
7272 else
7273 {
7274 if (eap->do_ecmd_cmd != NULL)
7275 do_cmdline_cmd(eap->do_ecmd_cmd);
7276#ifdef FEAT_TITLE
7277 n = curwin->w_arg_idx_invalid;
7278#endif
7279 check_arg_idx(curwin);
7280#ifdef FEAT_TITLE
7281 if (n != curwin->w_arg_idx_invalid)
7282 maketitle();
7283#endif
7284 }
7285
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 /*
7287 * if ":split file" worked, set alternate file name in old window to new
7288 * file
7289 */
7290 if (old_curwin != NULL
7291 && *eap->arg != NUL
7292 && curwin != old_curwin
7293 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007294 && old_curwin->w_buffer != curbuf
7295 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
7298 ex_no_reprint = TRUE;
7299}
7300
7301#ifndef FEAT_GUI
7302/*
7303 * ":gui" and ":gvim" when there is no GUI.
7304 */
7305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007306ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
7308 eap->errmsg = e_nogvim;
7309}
7310#endif
7311
Bram Moolenaar4f974752019-02-17 17:44:42 +01007312#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007314ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 gui_make_tearoff(eap->arg);
7317}
7318#endif
7319
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007320#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7321 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007323ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007325# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7326 if (gui.in_use)
7327 gui_make_popup(eap->arg, eap->forceit);
7328# ifdef FEAT_TERM_POPUP_MENU
7329 else
7330# endif
7331# endif
7332# ifdef FEAT_TERM_POPUP_MENU
7333 pum_make_popup(eap->arg, eap->forceit);
7334# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335}
7336#endif
7337
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007339ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340{
7341 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007342 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007344 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345}
7346
7347/*
7348 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7349 * offset.
7350 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007353ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007356 win_T *save_curwin = curwin;
7357 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 long topline;
7359 long y;
7360 linenr_T old_linenr = curwin->w_cursor.lnum;
7361
7362 setpcmark();
7363
7364 /*
7365 * determine max topline
7366 */
7367 if (curwin->w_p_scb)
7368 {
7369 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007370 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
7372 if (wp->w_p_scb && wp->w_buffer)
7373 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007374 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 if (topline > y)
7376 topline = y;
7377 }
7378 }
7379 if (topline < 1)
7380 topline = 1;
7381 }
7382 else
7383 {
7384 topline = 1;
7385 }
7386
7387
7388 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007389 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007391 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 {
7393 if (curwin->w_p_scb)
7394 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007395 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 y = topline - curwin->w_topline;
7397 if (y > 0)
7398 scrollup(y, TRUE);
7399 else
7400 scrolldown(-y, TRUE);
7401 curwin->w_scbind_pos = topline;
7402 redraw_later(VALID);
7403 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007407 curwin = save_curwin;
7408 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 if (curwin->w_p_scb)
7410 {
7411 did_syncbind = TRUE;
7412 checkpcmark();
7413 if (old_linenr != curwin->w_cursor.lnum)
7414 {
7415 char_u ctrl_o[2];
7416
7417 ctrl_o[0] = Ctrl_O;
7418 ctrl_o[1] = 0;
7419 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7420 }
7421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422}
7423
7424
7425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007426ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007428 int i;
7429 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7430 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
7432 if (eap->usefilter) /* :r!cmd */
7433 do_bang(1, eap, FALSE, FALSE, TRUE);
7434 else
7435 {
7436 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7437 return;
7438
7439#ifdef FEAT_BROWSE
7440 if (cmdmod.browse)
7441 {
7442 char_u *browseFile;
7443
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007444 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 NULL, NULL, NULL, curbuf);
7446 if (browseFile != NULL)
7447 {
7448 i = readfile(browseFile, NULL,
7449 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7450 vim_free(browseFile);
7451 }
7452 else
7453 i = OK;
7454 }
7455 else
7456#endif
7457 if (*eap->arg == NUL)
7458 {
7459 if (check_fname() == FAIL) /* check for no file name */
7460 return;
7461 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7462 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7463 }
7464 else
7465 {
7466 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7467 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7468 i = readfile(eap->arg, NULL,
7469 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7470
7471 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007472 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007474#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 if (!aborting())
7476#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007477 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
7479 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007480 {
7481 if (empty && exmode_active)
7482 {
7483 /* Delete the empty line that remains. Historically ex does
7484 * this but vi doesn't. */
7485 if (eap->line2 == 0)
7486 lnum = curbuf->b_ml.ml_line_count;
7487 else
7488 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007489 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007490 {
7491 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007492 if (curwin->w_cursor.lnum > 1
7493 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007494 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007495 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007496 }
7497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501}
7502
Bram Moolenaarea408852005-06-25 22:49:46 +00007503static char_u *prev_dir = NULL;
7504
7505#if defined(EXITFREE) || defined(PROTO)
7506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007507free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007508{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007509 VIM_CLEAR(prev_dir);
7510 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007511}
7512#endif
7513
Bram Moolenaarf4258302013-06-02 18:20:17 +02007514/*
7515 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007516 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7517 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007518 */
7519 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007520post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007521{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007522 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007523 // Clear tab local directory for both :cd and :tcd
7524 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007525 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007526 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007527 {
7528 /* If still in global directory, need to remember current
7529 * directory as global directory. */
7530 if (globaldir == NULL && prev_dir != NULL)
7531 globaldir = vim_strsave(prev_dir);
7532 /* Remember this local directory for the window. */
7533 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007534 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007535 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007536 curtab->tp_localdir = vim_strsave(NameBuff);
7537 else
7538 curwin->w_localdir = vim_strsave(NameBuff);
7539 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007540 }
7541 else
7542 {
7543 /* We are now in the global directory, no need to remember its
7544 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007545 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007546 }
7547
7548 shorten_fnames(TRUE);
7549}
7550
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007551/*
7552 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7553 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7554 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7555 * Otherwise changes the global directory.
7556 * Returns TRUE if the directory is successfully changed.
7557 */
7558 int
7559changedir_func(
7560 char_u *new_dir,
7561 int forceit,
7562 cdscope_T scope)
7563{
7564 char_u *tofree;
7565 int dir_differs;
7566 int retval = FALSE;
7567
7568 if (allbuf_locked())
7569 return FALSE;
7570
7571 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7572 {
7573 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7574 return FALSE;
7575 }
7576
7577 // ":cd -": Change to previous directory
7578 if (STRCMP(new_dir, "-") == 0)
7579 {
7580 if (prev_dir == NULL)
7581 {
7582 emsg(_("E186: No previous directory"));
7583 return FALSE;
7584 }
7585 new_dir = prev_dir;
7586 }
7587
7588 // Save current directory for next ":cd -"
7589 tofree = prev_dir;
7590 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7591 prev_dir = vim_strsave(NameBuff);
7592 else
7593 prev_dir = NULL;
7594
7595#if defined(UNIX) || defined(VMS)
7596 // for UNIX ":cd" means: go to home directory
7597 if (*new_dir == NUL)
7598 {
7599 // use NameBuff for home directory name
7600# ifdef VMS
7601 char_u *p;
7602
7603 p = mch_getenv((char_u *)"SYS$LOGIN");
7604 if (p == NULL || *p == NUL) // empty is the same as not set
7605 NameBuff[0] = NUL;
7606 else
7607 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7608# else
7609 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7610# endif
7611 new_dir = NameBuff;
7612 }
7613#endif
7614 dir_differs = new_dir == NULL || prev_dir == NULL
7615 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7616 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7617 emsg(_(e_failed));
7618 else
7619 {
7620 char_u *acmd_fname;
7621
7622 post_chdir(scope);
7623
7624 if (dir_differs)
7625 {
7626 if (scope == CDSCOPE_WINDOW)
7627 acmd_fname = (char_u *)"window";
7628 else if (scope == CDSCOPE_TABPAGE)
7629 acmd_fname = (char_u *)"tabpage";
7630 else
7631 acmd_fname = (char_u *)"global";
7632 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7633 curbuf);
7634 }
7635 retval = TRUE;
7636 }
7637 vim_free(tofree);
7638
7639 return retval;
7640}
Bram Moolenaarea408852005-06-25 22:49:46 +00007641
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007643 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007646ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007648 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
7650 new_dir = eap->arg;
7651#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007652 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (*new_dir == NUL)
7654 ex_pwd(NULL);
7655 else
7656#endif
7657 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007658 cdscope_T scope = CDSCOPE_GLOBAL;
7659
7660 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7661 scope = CDSCOPE_WINDOW;
7662 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7663 scope = CDSCOPE_TABPAGE;
7664
7665 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007666 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007667 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007668 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 ex_pwd(eap);
7670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 }
7672}
7673
7674/*
7675 * ":pwd".
7676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007678ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679{
7680 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7681 {
7682#ifdef BACKSLASH_IN_FILENAME
7683 slash_adjust(NameBuff);
7684#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007685 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007688 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689}
7690
7691/*
7692 * ":=".
7693 */
7694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007695ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007697 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007698 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699}
7700
7701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007702ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007704 int n;
7705 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706
7707 if (cursor_valid())
7708 {
7709 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7710 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007711 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007713
7714 len = eap->line2;
7715 switch (*eap->arg)
7716 {
7717 case 'm': break;
7718 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007719 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007720 }
7721 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722}
7723
7724/*
7725 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7726 */
7727 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007728do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007731 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732
7733 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007734 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007735 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007737 wait_now = msec - done > 1000L ? 1000L : msec - done;
7738#ifdef FEAT_TIMERS
7739 {
7740 long due_time = check_due_timer();
7741
7742 if (due_time > 0 && due_time < wait_now)
7743 wait_now = due_time;
7744 }
7745#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007746#ifdef FEAT_JOB_CHANNEL
7747 if (has_any_channel() && wait_now > 100L)
7748 wait_now = 100L;
7749#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007750 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007751#ifdef FEAT_JOB_CHANNEL
7752 if (has_any_channel())
7753 ui_breakcheck_force(TRUE);
7754 else
7755#endif
7756 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007757#ifdef MESSAGE_QUEUE
7758 /* Process the netbeans and clientserver messages that may have been
7759 * received in the call to ui_breakcheck() when the GUI is in use. This
7760 * may occur when running a test case. */
7761 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007764
7765 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7766 // input buffer, otherwise a following call to input() fails.
7767 if (got_int)
7768 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769}
7770
7771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007772do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
7774 int mode;
7775 char_u *cmdp;
7776
7777 cmdp = eap->cmd;
7778 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7779
7780 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7781 eap->arg, mode, isabbrev))
7782 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007783 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007785 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 break;
7787 }
7788}
7789
7790/*
7791 * ":winsize" command (obsolete).
7792 */
7793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007794ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 int w, h;
7797 char_u *arg = eap->arg;
7798 char_u *p;
7799
7800 w = getdigits(&arg);
7801 arg = skipwhite(arg);
7802 p = arg;
7803 h = getdigits(&arg);
7804 if (*p != NUL && *arg == NUL)
7805 set_shellsize(w, h, TRUE);
7806 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007807 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808}
7809
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007811ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 int xchar = NUL;
7814 char_u *p;
7815
7816 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7817 {
7818 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7819 if (eap->arg[1] == NUL)
7820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007821 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 return;
7823 }
7824 xchar = eap->arg[1];
7825 p = eap->arg + 2;
7826 }
7827 else
7828 p = eap->arg + 1;
7829
7830 eap->nextcmd = check_nextcmd(p);
7831 p = skipwhite(p);
7832 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007833 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007834 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 {
7836 /* Pass flags on for ":vertical wincmd ]". */
7837 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007838 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7840 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007841 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 }
7843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
Bram Moolenaar843ee412004-06-30 16:16:41 +00007845#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846/*
7847 * ":winpos".
7848 */
7849 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007850ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
7852 int x, y;
7853 char_u *arg = eap->arg;
7854 char_u *p;
7855
7856 if (*arg == NUL)
7857 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007858# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007859# ifdef VIMDLL
7860 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7861 mch_get_winpos(&x, &y) != FAIL)
7862# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007864# else
7865 if (mch_get_winpos(&x, &y) != FAIL)
7866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {
7868 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007869 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871 else
7872# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007873 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 }
7875 else
7876 {
7877 x = getdigits(&arg);
7878 arg = skipwhite(arg);
7879 p = arg;
7880 y = getdigits(&arg);
7881 if (*p == NUL || *arg != NUL)
7882 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007883 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 return;
7885 }
7886# ifdef FEAT_GUI
7887 if (gui.in_use)
7888 gui_mch_set_winpos(x, y);
7889 else if (gui.starting)
7890 {
7891 /* Remember the coordinates for when the window is opened. */
7892 gui_win_x = x;
7893 gui_win_y = y;
7894 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007895# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 else
7897# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007898# endif
7899# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007900 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901# endif
7902# ifdef HAVE_TGETENT
7903 if (*T_CWP)
7904 term_set_winpos(x, y);
7905# endif
7906 }
7907}
7908#endif
7909
7910/*
7911 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7912 */
7913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007914ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 oparg_T oa;
7917
7918 clear_oparg(&oa);
7919 oa.regname = eap->regname;
7920 oa.start.lnum = eap->line1;
7921 oa.end.lnum = eap->line2;
7922 oa.line_count = eap->line2 - eap->line1 + 1;
7923 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7926 {
7927 setpcmark();
7928 curwin->w_cursor.lnum = eap->line1;
7929 beginline(BL_SOL | BL_FIX);
7930 }
7931
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007932 if (VIsual_active)
7933 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007934
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 switch (eap->cmdidx)
7936 {
7937 case CMD_delete:
7938 oa.op_type = OP_DELETE;
7939 op_delete(&oa);
7940 break;
7941
7942 case CMD_yank:
7943 oa.op_type = OP_YANK;
7944 (void)op_yank(&oa, FALSE, TRUE);
7945 break;
7946
7947 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007948 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007950 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7951#else
7952 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007954 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 oa.op_type = OP_RSHIFT;
7956 else
7957 oa.op_type = OP_LSHIFT;
7958 op_shift(&oa, FALSE, eap->amount);
7959 break;
7960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007962 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963}
7964
7965/*
7966 * ":put".
7967 */
7968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
7971 /* ":0put" works like ":1put!". */
7972 if (eap->line2 == 0)
7973 {
7974 eap->line2 = 1;
7975 eap->forceit = TRUE;
7976 }
7977 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007978 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7979 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980}
7981
7982/*
7983 * Handle ":copy" and ":move".
7984 */
7985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 long n;
7989
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007990 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 if (eap->arg == NULL) /* error detected */
7992 {
7993 eap->nextcmd = NULL;
7994 return;
7995 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007996 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997
7998 /*
7999 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8000 */
8001 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008003 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 return;
8005 }
8006
8007 if (eap->cmdidx == CMD_move)
8008 {
8009 if (do_move(eap->line1, eap->line2, n) == FAIL)
8010 return;
8011 }
8012 else
8013 ex_copy(eap->line1, eap->line2, n);
8014 u_clearline();
8015 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008016 ex_may_print(eap);
8017}
8018
8019/*
8020 * Print the current line if flags were given to the Ex command.
8021 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008022 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008023ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008024{
8025 if (eap->flags != 0)
8026 {
8027 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8028 (eap->flags & EXFLAG_LIST));
8029 ex_no_reprint = TRUE;
8030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031}
8032
8033/*
8034 * ":smagic" and ":snomagic".
8035 */
8036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008037ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
8039 int magic_save = p_magic;
8040
8041 p_magic = (eap->cmdidx == CMD_smagic);
8042 do_sub(eap);
8043 p_magic = magic_save;
8044}
8045
8046/*
8047 * ":join".
8048 */
8049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008050ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
8052 curwin->w_cursor.lnum = eap->line1;
8053 if (eap->line1 == eap->line2)
8054 {
8055 if (eap->addr_count >= 2) /* :2,2join does nothing */
8056 return;
8057 if (eap->line2 == curbuf->b_ml.ml_line_count)
8058 {
8059 beep_flush();
8060 return;
8061 }
8062 ++eap->line2;
8063 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008064 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008066 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
8069/*
8070 * ":[addr]@r" or ":[addr]*r": execute register
8071 */
8072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008073ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074{
8075 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008076 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077
8078 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008079 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080
8081#ifdef USE_ON_FLY_SCROLL
8082 dont_scroll = TRUE; /* disallow scrolling here */
8083#endif
8084
8085 /* get the register name. No name means to use the previous one */
8086 c = *eap->arg;
8087 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8088 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008089 /* Put the register in the typeahead buffer with the "silent" flag. */
8090 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8091 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008092 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008094 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 else
8096 {
8097 int save_efr = exec_from_reg;
8098
8099 exec_from_reg = TRUE;
8100
8101 /*
8102 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008103 * Continue until the stuff buffer is empty and all added characters
8104 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008106 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8108
8109 exec_from_reg = save_efr;
8110 }
8111}
8112
8113/*
8114 * ":!".
8115 */
8116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008117ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118{
8119 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8120}
8121
8122/*
8123 * ":undo".
8124 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008126ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008128 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008129 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008130 else
8131 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132}
8133
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008134#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008136ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008137{
8138 char_u hash[UNDO_HASH_SIZE];
8139
8140 u_compute_hash(hash);
8141 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8142}
8143
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008145ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008146{
8147 char_u hash[UNDO_HASH_SIZE];
8148
8149 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008150 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008151}
8152#endif
8153
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154/*
8155 * ":redo".
8156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
8160 u_redo(1);
8161}
8162
8163/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008164 * ":earlier" and ":later".
8165 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008167ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008168{
8169 long count = 0;
8170 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008171 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008172 char_u *p = eap->arg;
8173
8174 if (*p == NUL)
8175 count = 1;
8176 else if (isdigit(*p))
8177 {
8178 count = getdigits(&p);
8179 switch (*p)
8180 {
8181 case 's': ++p; sec = TRUE; break;
8182 case 'm': ++p; sec = TRUE; count *= 60; break;
8183 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008184 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8185 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008186 }
8187 }
8188
8189 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008190 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008191 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008192 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8193 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008194}
8195
8196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 * ":redir": start/stop redirection.
8198 */
8199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008200ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201{
8202 char *mode;
8203 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008204 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205
Bram Moolenaarba768492016-07-08 15:32:54 +02008206#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008207 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008209 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008210 return;
8211 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008212#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008213
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 if (STRICMP(eap->arg, "END") == 0)
8215 close_redir();
8216 else
8217 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008218 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008220 ++arg;
8221 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008223 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 mode = "a";
8225 }
8226 else
8227 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008228 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
8230 close_redir();
8231
8232 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008233 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 if (fname == NULL)
8235 return;
8236#ifdef FEAT_BROWSE
8237 if (cmdmod.browse)
8238 {
8239 char_u *browseFile;
8240
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008241 browseFile = do_browse(BROWSE_SAVE,
8242 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008243 fname, NULL, NULL,
8244 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 if (browseFile == NULL)
8246 return; /* operation cancelled */
8247 vim_free(fname);
8248 fname = browseFile;
8249 eap->forceit = TRUE; /* since dialog already asked */
8250 }
8251#endif
8252
8253 redir_fd = open_exfile(fname, eap->forceit, mode);
8254 vim_free(fname);
8255 }
8256#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008257 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 {
8259 /* redirect to a register a-z (resp. A-Z for appending) */
8260 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008261 ++arg;
8262 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008264 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008265 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008267 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008269 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008270 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008271 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008272 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008274 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008275 if (*arg == '>')
8276 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008277 /* Make register empty when not using @A-@Z and the
8278 * command is valid. */
8279 if (*arg == NUL && !isupper(redir_reg))
8280 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008282 }
8283 if (*arg != NUL)
8284 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008285 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008286 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008287 }
8288 }
8289 else if (*arg == '=' && arg[1] == '>')
8290 {
8291 int append;
8292
8293 /* redirect to a variable */
8294 close_redir();
8295 arg += 2;
8296
8297 if (*arg == '>')
8298 {
8299 ++arg;
8300 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 }
8302 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008303 append = FALSE;
8304
8305 if (var_redir_start(skipwhite(arg), append) == OK)
8306 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308#endif
8309
8310 /* TODO: redirect to a buffer */
8311
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008313 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008315
8316 /* Make sure redirection is not off. Can happen for cmdline completion
8317 * that indirectly invokes a command to catch its output. */
8318 if (redir_fd != NULL
8319#ifdef FEAT_EVAL
8320 || redir_reg || redir_vname
8321#endif
8322 )
8323 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324}
8325
8326/*
8327 * ":redraw": force redraw
8328 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008329 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008330ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 int r = RedrawingDisabled;
8333 int p = p_lz;
8334
8335 RedrawingDisabled = 0;
8336 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008337 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008339 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340#ifdef FEAT_TITLE
8341 if (need_maketitle)
8342 maketitle();
8343#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008344#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8345# ifdef VIMDLL
8346 if (!gui.in_use)
8347# endif
8348 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 RedrawingDisabled = r;
8351 p_lz = p;
8352
8353 /* Reset msg_didout, so that a message that's there is overwritten. */
8354 msg_didout = FALSE;
8355 msg_col = 0;
8356
Bram Moolenaar56667a52013-07-17 11:54:28 +02008357 /* No need to wait after an intentional redraw. */
8358 need_wait_return = FALSE;
8359
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 out_flush();
8361}
8362
8363/*
8364 * ":redrawstatus": force redraw of status line(s)
8365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008367ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 int r = RedrawingDisabled;
8370 int p = p_lz;
8371
8372 RedrawingDisabled = 0;
8373 p_lz = FALSE;
8374 if (eap->forceit)
8375 status_redraw_all();
8376 else
8377 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008378 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 RedrawingDisabled = r;
8380 p_lz = p;
8381 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382}
8383
Bram Moolenaare12bab32019-01-08 22:02:56 +01008384/*
8385 * ":redrawtabline": force redraw of the tabline
8386 */
8387 static void
8388ex_redrawtabline(exarg_T *eap UNUSED)
8389{
8390 int r = RedrawingDisabled;
8391 int p = p_lz;
8392
8393 RedrawingDisabled = 0;
8394 p_lz = FALSE;
8395
8396 draw_tabline();
8397
8398 RedrawingDisabled = r;
8399 p_lz = p;
8400 out_flush();
8401}
8402
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008404close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405{
8406 if (redir_fd != NULL)
8407 {
8408 fclose(redir_fd);
8409 redir_fd = NULL;
8410 }
8411#ifdef FEAT_EVAL
8412 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008413 if (redir_vname)
8414 {
8415 var_redir_stop();
8416 redir_vname = 0;
8417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#endif
8419}
8420
8421#if defined(FEAT_SESSION) && defined(USE_CRNL)
8422# define MKSESSION_NL
8423static int mksession_nl = FALSE; /* use NL only in put_eol() */
8424#endif
8425
8426/*
8427 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8428 */
8429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008430ex_mkrc(
8431 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432{
8433 FILE *fd;
8434 int failed = FALSE;
8435 char_u *fname;
8436#ifdef FEAT_BROWSE
8437 char_u *browseFile = NULL;
8438#endif
8439#ifdef FEAT_SESSION
8440 int view_session = FALSE;
8441 int using_vdir = FALSE; /* using 'viewdir'? */
8442 char_u *viewFile = NULL;
8443 unsigned *flagp;
8444#endif
8445
8446 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8447 {
8448#ifdef FEAT_SESSION
8449 view_session = TRUE;
8450#else
8451 ex_ni(eap);
8452 return;
8453#endif
8454 }
8455
8456#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008457 /* Use the short file name until ":lcd" is used. We also don't use the
8458 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008459 did_lcd = FALSE;
8460
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8462 if (eap->cmdidx == CMD_mkview
8463 && (*eap->arg == NUL
8464 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8465 {
8466 eap->forceit = TRUE;
8467 fname = get_view_file(*eap->arg);
8468 if (fname == NULL)
8469 return;
8470 viewFile = fname;
8471 using_vdir = TRUE;
8472 }
8473 else
8474#endif
8475 if (*eap->arg != NUL)
8476 fname = eap->arg;
8477 else if (eap->cmdidx == CMD_mkvimrc)
8478 fname = (char_u *)VIMRC_FILE;
8479#ifdef FEAT_SESSION
8480 else if (eap->cmdidx == CMD_mksession)
8481 fname = (char_u *)SESSION_FILE;
8482#endif
8483 else
8484 fname = (char_u *)EXRC_FILE;
8485
8486#ifdef FEAT_BROWSE
8487 if (cmdmod.browse)
8488 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008489 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490# ifdef FEAT_SESSION
8491 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8492 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8493# endif
8494 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008495 fname, (char_u *)"vim", NULL,
8496 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 if (browseFile == NULL)
8498 goto theend;
8499 fname = browseFile;
8500 eap->forceit = TRUE; /* since dialog already asked */
8501 }
8502#endif
8503
8504#if defined(FEAT_SESSION) && defined(vim_mkdir)
8505 /* When using 'viewdir' may have to create the directory. */
8506 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008507 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508#endif
8509
8510 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8511 if (fd != NULL)
8512 {
8513#ifdef FEAT_SESSION
8514 if (eap->cmdidx == CMD_mkview)
8515 flagp = &vop_flags;
8516 else
8517 flagp = &ssop_flags;
8518#endif
8519
8520#ifdef MKSESSION_NL
8521 /* "unix" in 'sessionoptions': use NL line separator */
8522 if (view_session && (*flagp & SSOP_UNIX))
8523 mksession_nl = TRUE;
8524#endif
8525
8526 /* Write the version command for :mkvimrc */
8527 if (eap->cmdidx == CMD_mkvimrc)
8528 (void)put_line(fd, "version 6.0");
8529
8530#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008531 if (eap->cmdidx == CMD_mksession)
8532 {
8533 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8534 failed = TRUE;
8535 }
8536
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 if (eap->cmdidx != CMD_mkview)
8538#endif
8539 {
8540 /* Write setting 'compatible' first, because it has side effects.
8541 * For that same reason only do it when needed. */
8542 if (p_cp)
8543 (void)put_line(fd, "if !&cp | set cp | endif");
8544 else
8545 (void)put_line(fd, "if &cp | set nocp | endif");
8546 }
8547
8548#ifdef FEAT_SESSION
8549 if (!view_session
8550 || (eap->cmdidx == CMD_mksession
8551 && (*flagp & SSOP_OPTIONS)))
8552#endif
8553 failed |= (makemap(fd, NULL) == FAIL
8554 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8555
8556#ifdef FEAT_SESSION
8557 if (!failed && view_session)
8558 {
8559 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8560 failed = TRUE;
8561 if (eap->cmdidx == CMD_mksession)
8562 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008563 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564
Bram Moolenaard9462e32011-04-11 21:35:11 +02008565 dirnow = alloc(MAXPATHL);
8566 if (dirnow == NULL)
8567 failed = TRUE;
8568 else
8569 {
8570 /*
8571 * Change to session file's dir.
8572 */
8573 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008575 *dirnow = NUL;
8576 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8577 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008578 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008579 shorten_fnames(TRUE);
8580 }
8581 else if (*dirnow != NUL
8582 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8583 {
8584 if (mch_chdir((char *)globaldir) == 0)
8585 shorten_fnames(TRUE);
8586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587
Bram Moolenaard9462e32011-04-11 21:35:11 +02008588 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589
Bram Moolenaard9462e32011-04-11 21:35:11 +02008590 /* restore original dir */
8591 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008593 {
8594 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008595 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008596 shorten_fnames(TRUE);
8597 }
8598 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 }
8600 }
8601 else
8602 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008603 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8604 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 }
8606 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8607 == FAIL)
8608 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008609# ifdef FEAT_SEARCH_EXTRA
8610 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8611 failed = TRUE;
8612# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008613 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8614 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008615 if (eap->cmdidx == CMD_mksession)
8616 {
8617 if (put_line(fd, "unlet SessionLoad") == FAIL)
8618 failed = TRUE;
8619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 }
8621#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008622 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8623 failed = TRUE;
8624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 failed |= fclose(fd);
8626
8627 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008628 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8630 else if (eap->cmdidx == CMD_mksession)
8631 {
8632 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008633 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634
Bram Moolenaard9462e32011-04-11 21:35:11 +02008635 tbuf = alloc(MAXPATHL);
8636 if (tbuf != NULL)
8637 {
8638 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8639 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8640 vim_free(tbuf);
8641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
8643#endif
8644#ifdef MKSESSION_NL
8645 mksession_nl = FALSE;
8646#endif
8647 }
8648
8649#ifdef FEAT_BROWSE
8650theend:
8651 vim_free(browseFile);
8652#endif
8653#ifdef FEAT_SESSION
8654 vim_free(viewFile);
8655#endif
8656}
8657
Bram Moolenaardf177f62005-02-22 08:39:57 +00008658#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8659 || defined(PROTO)
8660 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008661vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008662{
8663 if (vim_mkdir(name, prot) != 0)
8664 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008665 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008666 return FAIL;
8667 }
8668 return OK;
8669}
8670#endif
8671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672/*
8673 * Open a file for writing for an Ex command, with some checks.
8674 * Return file descriptor, or NULL on failure.
8675 */
8676 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008677open_exfile(
8678 char_u *fname,
8679 int forceit,
8680 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 FILE *fd;
8683
8684#ifdef UNIX
8685 /* with Unix it is possible to open a directory */
8686 if (mch_isdir(fname))
8687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008688 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 return NULL;
8690 }
8691#endif
8692 if (!forceit && *mode != 'a' && vim_fexists(fname))
8693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008694 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 return NULL;
8696 }
8697
8698 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008699 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700
8701 return fd;
8702}
8703
8704/*
8705 * ":mark" and ":k".
8706 */
8707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008708ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709{
8710 pos_T pos;
8711
8712 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008713 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008715 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 else
8717 {
8718 pos = curwin->w_cursor; /* save curwin->w_cursor */
8719 curwin->w_cursor.lnum = eap->line2;
8720 beginline(BL_WHITE | BL_FIX);
8721 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008722 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8724 }
8725}
8726
8727/*
8728 * Update w_topline, w_leftcol and the cursor position.
8729 */
8730 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008731update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
8733 check_cursor(); /* put cursor on valid line */
8734 update_topline();
8735 if (!curwin->w_p_wrap)
8736 validate_cursor();
8737 update_curswant();
8738}
8739
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008741 * Save the current State and go to Normal mode.
8742 * Return TRUE if the typeahead could be saved.
8743 */
8744 int
8745save_current_state(save_state_T *sst)
8746{
8747 sst->save_msg_scroll = msg_scroll;
8748 sst->save_restart_edit = restart_edit;
8749 sst->save_msg_didout = msg_didout;
8750 sst->save_State = State;
8751 sst->save_insertmode = p_im;
8752 sst->save_finish_op = finish_op;
8753 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008754 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008755
8756 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8757 restart_edit = 0; /* don't go to Insert mode */
8758 p_im = FALSE; /* don't use 'insertmode' */
8759
8760 /*
8761 * Save the current typeahead. This is required to allow using ":normal"
8762 * from an event handler and makes sure we don't hang when the argument
8763 * ends with half a command.
8764 */
8765 save_typeahead(&sst->tabuf);
8766 return sst->tabuf.typebuf_valid;
8767}
8768
8769 void
8770restore_current_state(save_state_T *sst)
8771{
8772 /* Restore the previous typeahead. */
8773 restore_typeahead(&sst->tabuf);
8774
8775 msg_scroll = sst->save_msg_scroll;
8776 restart_edit = sst->save_restart_edit;
8777 p_im = sst->save_insertmode;
8778 finish_op = sst->save_finish_op;
8779 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008780 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008781 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8782
8783 /* Restore the state (needed when called from a function executed for
8784 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8785 State = sst->save_State;
8786#ifdef CURSOR_SHAPE
8787 ui_cursor_shape(); /* may show different cursor shape */
8788#endif
8789}
8790
8791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 * ":normal[!] {commands}": Execute normal mode commands.
8793 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008794 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008795ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008797 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 char_u *arg = NULL;
8799 int l;
8800 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008802 if (ex_normal_lock > 0)
8803 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008804 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008805 return;
8806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 if (ex_normal_busy >= p_mmd)
8808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008809 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 return;
8811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 /*
8814 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8815 * this for the K_SPECIAL leading byte, otherwise special keys will not
8816 * work.
8817 */
8818 if (has_mbyte)
8819 {
8820 int len = 0;
8821
8822 /* Count the number of characters to be escaped. */
8823 for (p = eap->arg; *p != NUL; ++p)
8824 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008825#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 if (*p == CSI) /* leadbyte CSI */
8827 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008828#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008829 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008831#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 )
8835 len += 2;
8836 }
8837 if (len > 0)
8838 {
8839 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8840 if (arg != NULL)
8841 {
8842 len = 0;
8843 for (p = eap->arg; *p != NUL; ++p)
8844 {
8845 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008846#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 if (*p == CSI)
8848 {
8849 arg[len++] = KS_EXTRA;
8850 arg[len++] = (int)KE_CSI;
8851 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008852#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008853 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 {
8855 arg[len++] = *++p;
8856 if (*p == K_SPECIAL)
8857 {
8858 arg[len++] = KS_SPECIAL;
8859 arg[len++] = KE_FILLER;
8860 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008861#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 else if (*p == CSI)
8863 {
8864 arg[len++] = KS_EXTRA;
8865 arg[len++] = (int)KE_CSI;
8866 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 }
8869 arg[len] = NUL;
8870 }
8871 }
8872 }
8873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008875 ++ex_normal_busy;
8876 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 {
8878 /*
8879 * Repeat the :normal command for each line in the range. When no
8880 * range given, execute it just once, without positioning the cursor
8881 * first.
8882 */
8883 do
8884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 if (eap->addr_count != 0)
8886 {
8887 curwin->w_cursor.lnum = eap->line1++;
8888 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008889 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
8891
Bram Moolenaar13505972019-01-24 15:04:48 +01008892 exec_normal_cmd(arg != NULL
8893 ? arg
8894 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 }
8896 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8897 }
8898
8899 /* Might not return to the main loop when in an event handler. */
8900 update_topline_cursor();
8901
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008902 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008904#ifdef FEAT_MOUSE
8905 setmouse();
8906#endif
8907#ifdef CURSOR_SHAPE
8908 ui_cursor_shape(); /* may show different cursor shape */
8909#endif
8910
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912}
8913
8914/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008915 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 */
8917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008918ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008920 if (eap->forceit)
8921 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008922 /* cursor line can be zero on startup */
8923 if (!curwin->w_cursor.lnum)
8924 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008925 coladvance((colnr_T)MAXCOL);
8926 curwin->w_curswant = MAXCOL;
8927 curwin->w_set_curswant = FALSE;
8928 }
8929
Bram Moolenaara40c5002005-01-09 21:16:21 +00008930 /* Ignore the command when already in Insert mode. Inserting an
8931 * expression register that invokes a function can do this. */
8932 if (State & INSERT)
8933 return;
8934
Bram Moolenaar64969662005-12-14 21:59:55 +00008935 if (eap->cmdidx == CMD_startinsert)
8936 restart_edit = 'a';
8937 else if (eap->cmdidx == CMD_startreplace)
8938 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008940 restart_edit = 'V';
8941
8942 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008944 if (eap->cmdidx == CMD_startinsert)
8945 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 curwin->w_curswant = 0; /* avoid MAXCOL */
8947 }
8948}
8949
8950/*
8951 * ":stopinsert"
8952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008954ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956 restart_edit = 0;
8957 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008958 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008961/*
8962 * Execute normal mode command "cmd".
8963 * "remap" can be REMAP_NONE or REMAP_YES.
8964 */
8965 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008966exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008967{
Bram Moolenaar25281632016-01-21 23:32:32 +01008968 /* Stuff the argument into the typeahead buffer. */
8969 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008970 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008971}
Bram Moolenaar25281632016-01-21 23:32:32 +01008972
Bram Moolenaar25281632016-01-21 23:32:32 +01008973/*
8974 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008975 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008976 */
8977 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008978exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008979{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008980 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008981 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008982
Bram Moolenaar905dd902019-04-07 14:21:47 +02008983 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8984 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008985 clear_oparg(&oa);
8986 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008987 while ((!stuff_empty()
8988 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008989 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008990 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008991 {
8992 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008993#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008994 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008995 && oa.op_type == OP_NOP && oa.regname == NUL
8996 && !VIsual_active)
8997 {
8998 /* If terminal_loop() returns OK we got a key that is handled
8999 * in Normal model. With FAIL we first need to position the
9000 * cursor and the screen needs to be redrawn. */
9001 if (terminal_loop(TRUE) == OK)
9002 normal_cmd(&oa, TRUE);
9003 }
9004 else
9005#endif
9006 /* execute a Normal mode cmd */
9007 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009008 }
9009}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009010
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011#ifdef FEAT_FIND_ID
9012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009013ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
9015 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9016 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9017 (linenr_T)1, (linenr_T)MAXLNUM);
9018}
9019
Bram Moolenaar4033c552017-09-16 20:54:51 +02009020#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021/*
9022 * ":psearch"
9023 */
9024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009025ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027 g_do_tagpreview = p_pvh;
9028 ex_findpat(eap);
9029 g_do_tagpreview = 0;
9030}
9031#endif
9032
9033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009034ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
9036 int whole = TRUE;
9037 long n;
9038 char_u *p;
9039 int action;
9040
9041 switch (cmdnames[eap->cmdidx].cmd_name[2])
9042 {
9043 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9044 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9045 action = ACTION_GOTO;
9046 else
9047 action = ACTION_SHOW;
9048 break;
9049 case 'i': /* ":ilist" and ":dlist" */
9050 action = ACTION_SHOW_ALL;
9051 break;
9052 case 'u': /* ":ijump" and ":djump" */
9053 action = ACTION_GOTO;
9054 break;
9055 default: /* ":isplit" and ":dsplit" */
9056 action = ACTION_SPLIT;
9057 break;
9058 }
9059
9060 n = 1;
9061 if (vim_isdigit(*eap->arg)) /* get count */
9062 {
9063 n = getdigits(&eap->arg);
9064 eap->arg = skipwhite(eap->arg);
9065 }
9066 if (*eap->arg == '/') /* Match regexp, not just whole words */
9067 {
9068 whole = FALSE;
9069 ++eap->arg;
9070 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9071 if (*p)
9072 {
9073 *p++ = NUL;
9074 p = skipwhite(p);
9075
9076 /* Check for trailing illegal characters */
9077 if (!ends_excmd(*p))
9078 eap->errmsg = e_trailing;
9079 else
9080 eap->nextcmd = check_nextcmd(p);
9081 }
9082 }
9083 if (!eap->skip)
9084 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9085 whole, !eap->forceit,
9086 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9087 n, action, eap->line1, eap->line2);
9088}
9089#endif
9090
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
Bram Moolenaar4033c552017-09-16 20:54:51 +02009092#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093/*
9094 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9095 */
9096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009097ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009099 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9101}
9102
9103/*
9104 * ":pedit"
9105 */
9106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
9109 win_T *curwin_save = curwin;
9110
9111 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009112 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009113 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 do_exedit(eap, NULL);
9115 keep_help_flag = FALSE;
9116 if (curwin != curwin_save && win_valid(curwin_save))
9117 {
9118 /* Return cursor to where we were */
9119 validate_cursor();
9120 redraw_later(VALID);
9121 win_enter(curwin_save, TRUE);
9122 }
9123 g_do_tagpreview = 0;
9124}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126
9127/*
9128 * ":stag", ":stselect" and ":stjump".
9129 */
9130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009131ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133 postponed_split = -1;
9134 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009135 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9137 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009138 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140
9141/*
9142 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9143 */
9144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009145ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146{
9147 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9148}
9149
9150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009151ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152{
9153 int cmd;
9154
9155 switch (name[1])
9156 {
9157 case 'j': cmd = DT_JUMP; /* ":tjump" */
9158 break;
9159 case 's': cmd = DT_SELECT; /* ":tselect" */
9160 break;
9161 case 'p': cmd = DT_PREV; /* ":tprevious" */
9162 break;
9163 case 'N': cmd = DT_PREV; /* ":tNext" */
9164 break;
9165 case 'n': cmd = DT_NEXT; /* ":tnext" */
9166 break;
9167 case 'o': cmd = DT_POP; /* ":pop" */
9168 break;
9169 case 'f': /* ":tfirst" */
9170 case 'r': cmd = DT_FIRST; /* ":trewind" */
9171 break;
9172 case 'l': cmd = DT_LAST; /* ":tlast" */
9173 break;
9174 default: /* ":tag" */
9175#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009176 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009178 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 return;
9180 }
9181#endif
9182 cmd = DT_TAG;
9183 break;
9184 }
9185
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009186 if (name[0] == 'l')
9187 {
9188#ifndef FEAT_QUICKFIX
9189 ex_ni(eap);
9190 return;
9191#else
9192 cmd = DT_LTAG;
9193#endif
9194 }
9195
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9197 eap->forceit, TRUE);
9198}
9199
9200/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009201 * Check "str" for starting with a special cmdline variable.
9202 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9203 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9204 */
9205 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009206find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009207{
9208 int len;
9209 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009210 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009211 "%",
9212#define SPEC_PERC 0
9213 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009214#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009215 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009216#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009217 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009218#define SPEC_CCWORD (SPEC_CWORD + 1)
9219 "<cexpr>", /* expr under cursor */
9220#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009221 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009222#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009223 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009224#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009225 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009226#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009227 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009228#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009229 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009230#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009231 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009232#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009233 "<sflnum>", /* script file line number */
9234#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009235#ifdef FEAT_CLIENTSERVER
9236 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009237# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009238#endif
9239 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009240
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009241 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009242 {
9243 len = (int)STRLEN(spec_str[i]);
9244 if (STRNCMP(src, spec_str[i], len) == 0)
9245 {
9246 *usedlen = len;
9247 return i;
9248 }
9249 }
9250 return -1;
9251}
9252
9253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 * Evaluate cmdline variables.
9255 *
9256 * change '%' to curbuf->b_ffname
9257 * '#' to curwin->w_altfile
9258 * '<cword>' to word under the cursor
9259 * '<cWORD>' to WORD under the cursor
9260 * '<cfile>' to path name under the cursor
9261 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009262 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 * '<afile>' to file name for autocommand
9264 * '<abuf>' to buffer number for autocommand
9265 * '<amatch>' to matching name for autocommand
9266 *
9267 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9268 * "" for error without a message) and NULL is returned.
9269 * Returns an allocated string if a valid match was found.
9270 * Returns NULL if no match was found. "usedlen" then still contains the
9271 * number of characters to skip.
9272 */
9273 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009274eval_vars(
9275 char_u *src, /* pointer into commandline */
9276 char_u *srcstart, /* beginning of valid memory for src */
9277 int *usedlen, /* characters after src that are used */
9278 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009279 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009280 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009281 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283 int i;
9284 char_u *s;
9285 char_u *result;
9286 char_u *resultbuf = NULL;
9287 int resultlen;
9288 buf_T *buf;
9289 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9290 int spec_idx;
9291#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009292 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 int skip_mod = FALSE;
9294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296
9297 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009298 if (escaped != NULL)
9299 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300
9301 /*
9302 * Check if there is something to do.
9303 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009304 spec_idx = find_cmdline_var(src, usedlen);
9305 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 {
9307 *usedlen = 1;
9308 return NULL;
9309 }
9310
9311 /*
9312 * Skip when preceded with a backslash "\%" and "\#".
9313 * Note: In "\\%" the % is also not recognized!
9314 */
9315 if (src > srcstart && src[-1] == '\\')
9316 {
9317 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009318 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319 return NULL;
9320 }
9321
9322 /*
9323 * word or WORD under cursor
9324 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009325 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9326 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009328 resultlen = find_ident_under_cursor(&result,
9329 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9330 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9331 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 if (resultlen == 0)
9333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009334 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 return NULL;
9336 }
9337 }
9338
9339 /*
9340 * '#': Alternate file name
9341 * '%': Current file name
9342 * File name under the cursor
9343 * File name for autocommand
9344 * and following modifiers
9345 */
9346 else
9347 {
9348 switch (spec_idx)
9349 {
9350 case SPEC_PERC: /* '%': current file */
9351 if (curbuf->b_fname == NULL)
9352 {
9353 result = (char_u *)"";
9354 valid = 0; /* Must have ":p:h" to be valid */
9355 }
9356 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009359#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009360 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009361#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 break;
9364
9365 case SPEC_HASH: /* '#' or "#99": alternate file */
9366 if (src[1] == '#') /* "##": the argument list */
9367 {
9368 result = arg_all();
9369 resultbuf = result;
9370 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009371 if (escaped != NULL)
9372 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373#ifdef FEAT_MODIFY_FNAME
9374 skip_mod = TRUE;
9375#endif
9376 break;
9377 }
9378 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009379 if (*s == '<') /* "#<99" uses v:oldfiles */
9380 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009382 if (s == src + 2 && src[1] == '-')
9383 /* just a minus sign, don't skip over it */
9384 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 *usedlen = (int)(s - src); /* length of what we expand */
9386
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009387 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009389 if (*usedlen < 2)
9390 {
9391 /* Should we give an error message for #<text? */
9392 *usedlen = 1;
9393 return NULL;
9394 }
9395#ifdef FEAT_EVAL
9396 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9397 (long)i);
9398 if (result == NULL)
9399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009400 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009401 return NULL;
9402 }
9403#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009404 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 }
9408 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009409 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009410 if (i == 0 && src[1] == '<' && *usedlen > 1)
9411 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009412 buf = buflist_findnr(i);
9413 if (buf == NULL)
9414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009415 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009416 return NULL;
9417 }
9418 if (lnump != NULL)
9419 *lnump = ECMD_LAST;
9420 if (buf->b_fname == NULL)
9421 {
9422 result = (char_u *)"";
9423 valid = 0; /* Must have ":p:h" to be valid */
9424 }
9425 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009426 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009427 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009428#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009429 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009430#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009431 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 break;
9434
9435#ifdef FEAT_SEARCHPATH
9436 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009437 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 if (result == NULL)
9439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009440 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 return NULL;
9442 }
9443 resultbuf = result; /* remember allocated string */
9444 break;
9445#endif
9446
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 case SPEC_AFILE: /* file name for autocommand */
9448 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009449 if (result != NULL && !autocmd_fname_full)
9450 {
9451 /* Still need to turn the fname into a full path. It is
9452 * postponed to avoid a delay when <afile> is not used. */
9453 autocmd_fname_full = TRUE;
9454 result = FullName_save(autocmd_fname, FALSE);
9455 vim_free(autocmd_fname);
9456 autocmd_fname = result;
9457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 if (result == NULL)
9459 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009460 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 return NULL;
9462 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009463 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 break;
9465
9466 case SPEC_ABUF: /* buffer number for autocommand */
9467 if (autocmd_bufnr <= 0)
9468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009469 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 return NULL;
9471 }
9472 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9473 result = strbuf;
9474 break;
9475
9476 case SPEC_AMATCH: /* match name for autocommand */
9477 result = autocmd_match;
9478 if (result == NULL)
9479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009480 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 return NULL;
9482 }
9483 break;
9484
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 case SPEC_SFILE: /* file name for ":so" command */
9486 result = sourcing_name;
9487 if (result == NULL)
9488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009489 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 return NULL;
9491 }
9492 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009493
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009494 case SPEC_SLNUM: /* line in file for ":so" command */
9495 if (sourcing_name == NULL || sourcing_lnum == 0)
9496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009497 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009498 return NULL;
9499 }
9500 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9501 result = strbuf;
9502 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009503
9504#ifdef FEAT_EVAL
9505 case SPEC_SFLNUM: /* line in script file */
9506 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009508 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009509 return NULL;
9510 }
9511 sprintf((char *)strbuf, "%ld",
9512 (long)(current_sctx.sc_lnum + sourcing_lnum));
9513 result = strbuf;
9514 break;
9515#endif
9516
9517#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009519 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9520 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 result = strbuf;
9522 break;
9523#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009524
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009525 default:
9526 result = (char_u *)""; /* avoid gcc warning */
9527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 }
9529
9530 resultlen = (int)STRLEN(result); /* length of new string */
9531 if (src[*usedlen] == '<') /* remove the file name extension */
9532 {
9533 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 resultlen = (int)(s - result);
9536 }
9537#ifdef FEAT_MODIFY_FNAME
9538 else if (!skip_mod)
9539 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009540 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 &resultlen);
9542 if (result == NULL)
9543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009544 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 return NULL;
9546 }
9547 }
9548#endif
9549 }
9550
9551 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9552 {
9553 if (valid != VALID_HEAD + VALID_PATH)
9554 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009555 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009557 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 result = NULL;
9559 }
9560 else
9561 result = vim_strnsave(result, resultlen);
9562 vim_free(resultbuf);
9563 return result;
9564}
9565
9566/*
9567 * Concatenate all files in the argument list, separated by spaces, and return
9568 * it in one allocated string.
9569 * Spaces and backslashes in the file names are escaped with a backslash.
9570 * Returns NULL when out of memory.
9571 */
9572 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009573arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
9575 int len;
9576 int idx;
9577 char_u *retval = NULL;
9578 char_u *p;
9579
9580 /*
9581 * Do this loop two times:
9582 * first time: compute the total length
9583 * second time: concatenate the names
9584 */
9585 for (;;)
9586 {
9587 len = 0;
9588 for (idx = 0; idx < ARGCOUNT; ++idx)
9589 {
9590 p = alist_name(&ARGLIST[idx]);
9591 if (p != NULL)
9592 {
9593 if (len > 0)
9594 {
9595 /* insert a space in between names */
9596 if (retval != NULL)
9597 retval[len] = ' ';
9598 ++len;
9599 }
9600 for ( ; *p != NUL; ++p)
9601 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009602 if (*p == ' '
9603#ifndef BACKSLASH_IN_FILENAME
9604 || *p == '\\'
9605#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009606 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 {
9608 /* insert a backslash */
9609 if (retval != NULL)
9610 retval[len] = '\\';
9611 ++len;
9612 }
9613 if (retval != NULL)
9614 retval[len] = *p;
9615 ++len;
9616 }
9617 }
9618 }
9619
9620 /* second time: break here */
9621 if (retval != NULL)
9622 {
9623 retval[len] = NUL;
9624 break;
9625 }
9626
9627 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009628 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 if (retval == NULL)
9630 break;
9631 }
9632
9633 return retval;
9634}
9635
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636/*
9637 * Expand the <sfile> string in "arg".
9638 *
9639 * Returns an allocated string, or NULL for any error.
9640 */
9641 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009642expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009644 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 int len;
9646 char_u *result;
9647 char_u *newres;
9648 char_u *repl;
9649 int srclen;
9650 char_u *p;
9651
9652 result = vim_strsave(arg);
9653 if (result == NULL)
9654 return NULL;
9655
9656 for (p = result; *p; )
9657 {
9658 if (STRNCMP(p, "<sfile>", 7) != 0)
9659 ++p;
9660 else
9661 {
9662 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009663 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (errormsg != NULL)
9665 {
9666 if (*errormsg)
9667 emsg(errormsg);
9668 vim_free(result);
9669 return NULL;
9670 }
9671 if (repl == NULL) /* no match (cannot happen) */
9672 {
9673 p += srclen;
9674 continue;
9675 }
9676 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9677 newres = alloc(len);
9678 if (newres == NULL)
9679 {
9680 vim_free(repl);
9681 vim_free(result);
9682 return NULL;
9683 }
9684 mch_memmove(newres, result, (size_t)(p - result));
9685 STRCPY(newres + (p - result), repl);
9686 len = (int)STRLEN(newres);
9687 STRCAT(newres, p + srclen);
9688 vim_free(repl);
9689 vim_free(result);
9690 result = newres;
9691 p = newres + len; /* continue after the match */
9692 }
9693 }
9694
9695 return result;
9696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697
9698#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009699static int ses_winsizes(FILE *fd, int restore_size,
9700 win_T *tab_firstwin);
9701static int ses_win_rec(FILE *fd, frame_T *fr);
9702static frame_T *ses_skipframe(frame_T *fr);
9703static int ses_do_frame(frame_T *fr);
9704static int ses_do_win(win_T *wp);
9705static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9706static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009707static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708
9709/*
9710 * Write openfile commands for the current buffers to an .exrc file.
9711 * Return FAIL on error, OK otherwise.
9712 */
9713 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009714makeopens(
9715 FILE *fd,
9716 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717{
9718 buf_T *buf;
9719 int only_save_windows = TRUE;
9720 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 int restore_size = TRUE;
9722 win_T *wp;
9723 char_u *sname;
9724 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009725 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009726 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009727 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009728 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009729 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009730 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731
9732 if (ssop_flags & SSOP_BUFFERS)
9733 only_save_windows = FALSE; /* Save ALL buffers */
9734
9735 /*
9736 * Begin by setting the this_session variable, and then other
9737 * sessionable variables.
9738 */
9739#ifdef FEAT_EVAL
9740 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9741 return FAIL;
9742 if (ssop_flags & SSOP_GLOBALS)
9743 if (store_session_globals(fd) == FAIL)
9744 return FAIL;
9745#endif
9746
9747 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009748 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749 */
9750 if (put_line(fd, "silent only") == FAIL)
9751 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009752 if ((ssop_flags & SSOP_TABPAGES)
9753 && put_line(fd, "silent tabonly") == FAIL)
9754 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755
9756 /*
9757 * Now a :cd command to the session directory or the current directory
9758 */
9759 if (ssop_flags & SSOP_SESDIR)
9760 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009761 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9762 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 return FAIL;
9764 }
9765 else if (ssop_flags & SSOP_CURDIR)
9766 {
9767 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9768 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009769 || fputs("cd ", fd) < 0
9770 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9771 || put_eol(fd) == FAIL)
9772 {
9773 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 vim_free(sname);
9777 }
9778
9779 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009780 * If there is an empty, unnamed buffer we will wipe it out later.
9781 * Remember the buffer number.
9782 */
9783 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9784 return FAIL;
9785 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9786 return FAIL;
9787 if (put_line(fd, "endif") == FAIL)
9788 return FAIL;
9789
9790 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 * Now save the current files, current buffer first.
9792 */
9793 if (put_line(fd, "set shortmess=aoO") == FAIL)
9794 return FAIL;
9795
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009797 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9799 return FAIL;
9800
9801 if (ssop_flags & SSOP_RESIZE)
9802 {
9803 /* Note: after the restore we still check it worked!*/
9804 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9805 || put_eol(fd) == FAIL)
9806 return FAIL;
9807 }
9808
9809#ifdef FEAT_GUI
9810 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9811 {
9812 int x, y;
9813
9814 if (gui_mch_get_winpos(&x, &y) == OK)
9815 {
9816 /* Note: after the restore we still check it worked!*/
9817 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9818 return FAIL;
9819 }
9820 }
9821#endif
9822
9823 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009824 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9825 * will be displayed when creating the next tab. That resizes the windows
9826 * in the first tab, which may cause problems. Set 'showtabline' to 2
9827 * temporarily to avoid that.
9828 */
9829 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9830 {
9831 if (put_line(fd, "set stal=2") == FAIL)
9832 return FAIL;
9833 restore_stal = TRUE;
9834 }
9835
9836 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009837 * May repeat putting Windows for each tab, when "tabpages" is in
9838 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009839 * Don't use goto_tabpage(), it may change directory and trigger
9840 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009842 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009843 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009844 if ((ssop_flags & SSOP_TABPAGES))
9845 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009846 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009847
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009848 // Similar to ses_win_rec() below, populate the tab pages first so
9849 // later local options won't be copied to the new tabs.
9850 FOR_ALL_TABPAGES(tp)
9851 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009852 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009853 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009854 return FAIL;
9855 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009856 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009858 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009859 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009860 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009861
Bram Moolenaar18144c82006-04-12 21:52:12 +00009862 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009864 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009865
9866 if (tp == NULL)
9867 break; /* done all tab pages */
9868 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009869 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009870 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009871 tab_topframe = topframe;
9872 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009873 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009874 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009875 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009876 tab_topframe = tp->tp_topframe;
9877 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009878 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009879 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881
Bram Moolenaar18144c82006-04-12 21:52:12 +00009882 /*
9883 * Before creating the window layout, try loading one file. If this
9884 * is aborted we don't end up with a number of useless windows.
9885 * This may have side effects! (e.g., compressed or network file).
9886 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009887 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009888 {
9889 if (ses_do_win(wp)
9890 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009891 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009892#ifdef FEAT_QUICKFIX
9893 && !bt_nofile(wp->w_buffer)
9894#endif
9895 )
9896 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009897 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9898 return FAIL;
9899 need_tabnext = FALSE;
9900
9901 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009902 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9903 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009904 return FAIL;
9905 if (!wp->w_arg_idx_invalid)
9906 edited_win = wp;
9907 break;
9908 }
9909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009911 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009912 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009913 return FAIL;
9914
Bram Moolenaar18144c82006-04-12 21:52:12 +00009915 /*
9916 * Save current window layout.
9917 */
9918 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009920 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009922 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9923 return FAIL;
9924 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
Bram Moolenaar18144c82006-04-12 21:52:12 +00009927 /*
9928 * Check if window sizes can be restored (no windows omitted).
9929 * Remember the window number of the current window after restoring.
9930 */
9931 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009932 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009933 {
9934 if (ses_do_win(wp))
9935 ++nr;
9936 else
9937 restore_size = FALSE;
9938 if (curwin == wp)
9939 cnr = nr;
9940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
Bram Moolenaar18144c82006-04-12 21:52:12 +00009942 /* Go to the first window. */
9943 if (put_line(fd, "wincmd t") == FAIL)
9944 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009945
Bram Moolenaar18144c82006-04-12 21:52:12 +00009946 /*
9947 * If more than one window, see if sizes can be restored.
9948 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9949 * resized when moving between windows.
9950 * Do this before restoring the view, so that the topline and the
9951 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009952 * winminheight and winminwidth need to be set to avoid an error if the
9953 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009954 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009955 if (put_line(fd, "set winminheight=0") == FAIL
9956 || put_line(fd, "set winheight=1") == FAIL
9957 || put_line(fd, "set winminwidth=0") == FAIL
9958 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009959 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009960 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009961 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009963 // Restore the tab-local working directory if specified
9964 // Do this before the windows, so that the window-local directory can
9965 // override the tab-local directory.
9966 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9967 {
9968 if (fputs("tcd ", fd) < 0
9969 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9970 || put_eol(fd) == FAIL)
9971 return FAIL;
9972 did_lcd = TRUE;
9973 }
9974
Bram Moolenaar18144c82006-04-12 21:52:12 +00009975 /*
9976 * Restore the view of the window (options, file, cursor, etc.).
9977 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009978 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009979 {
9980 if (!ses_do_win(wp))
9981 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009982 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
9983 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009984 return FAIL;
9985 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9986 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009987 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009988 }
9989
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009990 /* The argument index in the first tab page is zero, need to set it in
9991 * each window. For further tab pages it's the window where we do
9992 * "tabedit". */
9993 cur_arg_idx = next_arg_idx;
9994
Bram Moolenaar18144c82006-04-12 21:52:12 +00009995 /*
9996 * Restore cursor to the current window if it's not the first one.
9997 */
9998 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
9999 || put_eol(fd) == FAIL))
10000 return FAIL;
10001
10002 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010003 * Restore window sizes again after jumping around in windows, because
10004 * the current window has a minimum size while others may not.
10005 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010006 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010007 return FAIL;
10008
Bram Moolenaar18144c82006-04-12 21:52:12 +000010009 /* Don't continue in another tab page when doing only the current one
10010 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010011 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010012 break;
10013 }
10014
10015 if (ssop_flags & SSOP_TABPAGES)
10016 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010017 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10018 || put_eol(fd) == FAIL)
10019 return FAIL;
10020 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010021 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10022 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010023
Bram Moolenaard39e2752019-01-26 20:07:38 +010010024 // Now put the remaining buffers into the buffer list.
10025 // This is near the end, so that when 'hidden' is set we don't create extra
10026 // buffers. If the buffer was already created with another command the
10027 // ":badd" will have no effect.
10028 FOR_ALL_BUFFERS(buf)
10029 {
10030 if (!(only_save_windows && buf->b_nwindows == 0)
10031 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10032#ifdef FEAT_TERMINAL
10033 // Skip terminal buffers: finished ones are not useful, others
10034 // will be resurrected and result in a new buffer.
10035 && !bt_terminal(buf)
10036#endif
10037 && buf->b_fname != NULL
10038 && buf->b_p_bl)
10039 {
10040 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10041 : buf->b_wininfo->wi_fpos.lnum) < 0
10042 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
10043 return FAIL;
10044 }
10045 }
10046
Bram Moolenaar9c102382006-05-03 21:26:49 +000010047 /*
10048 * Wipe out an empty unnamed buffer we started in.
10049 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020010050 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010051 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010052 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010053 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010054 return FAIL;
10055 if (put_line(fd, "endif") == FAIL)
10056 return FAIL;
10057 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10058 return FAIL;
10059
10060 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10061 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10062 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10063 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010010064 /* Re-apply 'winminheight' and 'winminwidth'. */
10065 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
10066 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
10067 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
10069 /*
10070 * Lastly, execute the x.vim file if it exists.
10071 */
10072 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10073 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010074 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 || put_line(fd, "endif") == FAIL)
10076 return FAIL;
10077
10078 return OK;
10079}
10080
10081 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010082ses_winsizes(
10083 FILE *fd,
10084 int restore_size,
10085 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086{
10087 int n = 0;
10088 win_T *wp;
10089
10090 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10091 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010092 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
10094 if (!ses_do_win(wp))
10095 continue;
10096 ++n;
10097
10098 /* restore height when not full height */
10099 if (wp->w_height + wp->w_status_height < topframe->fr_height
10100 && (fprintf(fd,
10101 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10102 n, (long)wp->w_height, Rows / 2, Rows) < 0
10103 || put_eol(fd) == FAIL))
10104 return FAIL;
10105
10106 /* restore width when not full width */
10107 if (wp->w_width < Columns && (fprintf(fd,
10108 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10109 n, (long)wp->w_width, Columns / 2, Columns) < 0
10110 || put_eol(fd) == FAIL))
10111 return FAIL;
10112 }
10113 }
10114 else
10115 {
10116 /* Just equalise window sizes */
10117 if (put_line(fd, "wincmd =") == FAIL)
10118 return FAIL;
10119 }
10120 return OK;
10121}
10122
10123/*
10124 * Write commands to "fd" to recursively create windows for frame "fr",
10125 * horizontally and vertically split.
10126 * After the commands the last window in the frame is the current window.
10127 * Returns FAIL when writing the commands to "fd" fails.
10128 */
10129 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010130ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131{
10132 frame_T *frc;
10133 int count = 0;
10134
10135 if (fr->fr_layout != FR_LEAF)
10136 {
10137 /* Find first frame that's not skipped and then create a window for
10138 * each following one (first frame is already there). */
10139 frc = ses_skipframe(fr->fr_child);
10140 if (frc != NULL)
10141 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10142 {
10143 /* Make window as big as possible so that we have lots of room
10144 * to split. */
10145 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10146 || put_line(fd, fr->fr_layout == FR_COL
10147 ? "split" : "vsplit") == FAIL)
10148 return FAIL;
10149 ++count;
10150 }
10151
10152 /* Go back to the first window. */
10153 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10154 ? "%dwincmd k" : "%dwincmd h", count) < 0
10155 || put_eol(fd) == FAIL))
10156 return FAIL;
10157
10158 /* Recursively create frames/windows in each window of this column or
10159 * row. */
10160 frc = ses_skipframe(fr->fr_child);
10161 while (frc != NULL)
10162 {
10163 ses_win_rec(fd, frc);
10164 frc = ses_skipframe(frc->fr_next);
10165 /* Go to next window. */
10166 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10167 return FAIL;
10168 }
10169 }
10170 return OK;
10171}
10172
10173/*
10174 * Skip frames that don't contain windows we want to save in the Session.
10175 * Returns NULL when there none.
10176 */
10177 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010178ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180 frame_T *frc;
10181
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010182 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 if (ses_do_frame(frc))
10184 break;
10185 return frc;
10186}
10187
10188/*
10189 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10190 * the Session.
10191 */
10192 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010193ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194{
10195 frame_T *frc;
10196
10197 if (fr->fr_layout == FR_LEAF)
10198 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010199 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 if (ses_do_frame(frc))
10201 return TRUE;
10202 return FALSE;
10203}
10204
10205/*
10206 * Return non-zero if window "wp" is to be stored in the Session.
10207 */
10208 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010209ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010211#ifdef FEAT_TERMINAL
10212 if (bt_terminal(wp->w_buffer))
10213 return !term_is_finished(wp->w_buffer)
10214 && (ssop_flags & SSOP_TERMINAL)
10215 && term_should_restore(wp->w_buffer);
10216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 if (wp->w_buffer->b_fname == NULL
10218#ifdef FEAT_QUICKFIX
10219 /* When 'buftype' is "nofile" can't restore the window contents. */
10220 || bt_nofile(wp->w_buffer)
10221#endif
10222 )
10223 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010224 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 return (ssop_flags & SSOP_HELP);
10226 return TRUE;
10227}
10228
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010229 static int
10230put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10231{
10232 int r;
10233
10234 if (wp->w_curswant == MAXCOL)
10235 r = fprintf(fd, "%snormal! $", spaces);
10236 else
10237 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10238 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10239}
10240
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241/*
10242 * Write commands to "fd" to restore the view of a window.
10243 * Caller must make sure 'scrolloff' is zero.
10244 */
10245 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010246put_view(
10247 FILE *fd,
10248 win_T *wp,
10249 int add_edit, /* add ":edit" command to view */
10250 unsigned *flagp, /* vop_flags or ssop_flags */
10251 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010252 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253{
10254 win_T *save_curwin;
10255 int f;
10256 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010257 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258
10259 /* Always restore cursor position for ":mksession". For ":mkview" only
10260 * when 'viewoptions' contains "cursor". */
10261 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10262
10263 /*
10264 * Local argument list.
10265 */
10266 if (wp->w_alist == &global_alist)
10267 {
10268 if (put_line(fd, "argglobal") == FAIL)
10269 return FAIL;
10270 }
10271 else
10272 {
10273 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10274 flagp == &vop_flags
10275 || !(*flagp & SSOP_CURDIR)
10276 || wp->w_localdir != NULL, flagp) == FAIL)
10277 return FAIL;
10278 }
10279
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010280 /* Only when part of a session: restore the argument index. Some
10281 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010282 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010283 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010285 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 || put_eol(fd) == FAIL)
10287 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010288 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 }
10290
10291 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010292 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010294# ifdef FEAT_TERMINAL
10295 if (bt_terminal(wp->w_buffer))
10296 {
10297 if (term_write_session(fd, wp) == FAIL)
10298 return FAIL;
10299 }
10300 else
10301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 /*
10303 * Load the file.
10304 */
10305 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010306# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010308# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 )
10310 {
10311 /*
10312 * Editing a file in this buffer: use ":edit file".
10313 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010314 *
10315 * Note, if a buffer for that file already exists, use :badd to
10316 * edit that buffer, to not lose folding information (:edit resets
10317 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010319 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010320 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010321 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010322 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10323 || fputs(" | else | edit ", fd) < 0
10324 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10325 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010326 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 return FAIL;
10328 }
10329 else
10330 {
10331 /* No file in this buffer, just make it empty. */
10332 if (put_line(fd, "enew") == FAIL)
10333 return FAIL;
10334#ifdef FEAT_QUICKFIX
10335 if (wp->w_buffer->b_ffname != NULL)
10336 {
10337 /* The buffer does have a name, but it's not a file name. */
10338 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010339 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 return FAIL;
10341 }
10342#endif
10343 do_cursor = FALSE;
10344 }
10345 }
10346
10347 /*
10348 * Local mappings and abbreviations.
10349 */
10350 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10351 && makemap(fd, wp->w_buffer) == FAIL)
10352 return FAIL;
10353
10354 /*
10355 * Local options. Need to go to the window temporarily.
10356 * Store only local values when using ":mkview" and when ":mksession" is
10357 * used and 'sessionoptions' doesn't include "options".
10358 * Some folding options are always stored when "folds" is included,
10359 * otherwise the folds would not be restored correctly.
10360 */
10361 save_curwin = curwin;
10362 curwin = wp;
10363 curbuf = curwin->w_buffer;
10364 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10365 f = makeset(fd, OPT_LOCAL,
10366 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10367#ifdef FEAT_FOLDING
10368 else if (*flagp & SSOP_FOLDS)
10369 f = makefoldset(fd);
10370#endif
10371 else
10372 f = OK;
10373 curwin = save_curwin;
10374 curbuf = curwin->w_buffer;
10375 if (f == FAIL)
10376 return FAIL;
10377
10378#ifdef FEAT_FOLDING
10379 /*
10380 * Save Folds when 'buftype' is empty and for help files.
10381 */
10382 if ((*flagp & SSOP_FOLDS)
10383 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010384 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 {
10386 if (put_folds(fd, wp) == FAIL)
10387 return FAIL;
10388 }
10389#endif
10390
10391 /*
10392 * Set the cursor after creating folds, since that moves the cursor.
10393 */
10394 if (do_cursor)
10395 {
10396
10397 /* Restore the cursor line in the file and relatively in the
10398 * window. Don't use "G", it changes the jumplist. */
10399 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10400 (long)wp->w_cursor.lnum,
10401 (long)(wp->w_cursor.lnum - wp->w_topline),
10402 (long)wp->w_height / 2, (long)wp->w_height) < 0
10403 || put_eol(fd) == FAIL
10404 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10405 || put_line(fd, "exe s:l") == FAIL
10406 || put_line(fd, "normal! zt") == FAIL
10407 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10408 || put_eol(fd) == FAIL)
10409 return FAIL;
10410 /* Restore the cursor column and left offset when not wrapping. */
10411 if (wp->w_cursor.col == 0)
10412 {
10413 if (put_line(fd, "normal! 0") == FAIL)
10414 return FAIL;
10415 }
10416 else
10417 {
10418 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10419 {
10420 if (fprintf(fd,
10421 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010422 (long)wp->w_virtcol + 1,
10423 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 (long)wp->w_width / 2, (long)wp->w_width) < 0
10425 || put_eol(fd) == FAIL
10426 || put_line(fd, "if s:c > 0") == FAIL
10427 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010428 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10429 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 || put_eol(fd) == FAIL
10431 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010432 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 || put_line(fd, "endif") == FAIL)
10434 return FAIL;
10435 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010436 else if (put_view_curpos(fd, wp, "") == FAIL)
10437 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 }
10439 }
10440
10441 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010442 * Local directory, if the current flag is not view options or the "curdir"
10443 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010445 if (wp->w_localdir != NULL
10446 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 {
10448 if (fputs("lcd ", fd) < 0
10449 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10450 || put_eol(fd) == FAIL)
10451 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010452 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 }
10454
10455 return OK;
10456}
10457
10458/*
10459 * Write an argument list to the session file.
10460 * Returns FAIL if writing fails.
10461 */
10462 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010463ses_arglist(
10464 FILE *fd,
10465 char *cmd,
10466 garray_T *gap,
10467 int fullname, /* TRUE: use full path name */
10468 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469{
10470 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010471 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 char_u *s;
10473
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010474 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10475 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010476 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 return FAIL;
10478 for (i = 0; i < gap->ga_len; ++i)
10479 {
10480 /* NULL file names are skipped (only happens when out of memory). */
10481 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10482 if (s != NULL)
10483 {
10484 if (fullname)
10485 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010486 buf = alloc(MAXPATHL);
10487 if (buf != NULL)
10488 {
10489 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10490 s = buf;
10491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010493 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010494 || ses_put_fname(fd, s, flagp) == FAIL
10495 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010496 {
10497 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010499 }
10500 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 }
10502 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010503 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504}
10505
10506/*
10507 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010508 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 * Returns FAIL if writing fails.
10510 */
10511 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010512ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513{
10514 char_u *name;
10515
10516 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010517 * the session file will be sourced.
10518 * Don't do this for ":mkview", we don't know the current directory.
10519 * Don't do this after ":lcd", we don't keep track of what the current
10520 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 if (buf->b_sfname != NULL
10522 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010523 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010524#ifdef FEAT_AUTOCHDIR
10525 && !p_acd
10526#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010527 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 name = buf->b_sfname;
10529 else
10530 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010531 if (ses_put_fname(fd, name, flagp) == FAIL
10532 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 return FAIL;
10534 return OK;
10535}
10536
10537/*
10538 * Write a file name to the session file.
10539 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10540 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010541 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542 */
10543 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010544ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010547 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549
10550 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010551 if (sname == NULL)
10552 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010554 if (*flagp & SSOP_SLASH)
10555 {
10556 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010557 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010558 if (*p == '\\')
10559 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010561
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010562 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010563 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010565 if (p == NULL)
10566 return FAIL;
10567
10568 /* write the result */
10569 if (fputs((char *)p, fd) < 0)
10570 retval = FAIL;
10571
10572 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 return retval;
10574}
10575
10576/*
10577 * ":loadview [nr]"
10578 */
10579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010580ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581{
10582 char_u *fname;
10583
10584 fname = get_view_file(*eap->arg);
10585 if (fname != NULL)
10586 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010587 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588 vim_free(fname);
10589 }
10590}
10591
10592/*
10593 * Get the name of the view file for the current buffer.
10594 */
10595 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010596get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010597{
10598 int len = 0;
10599 char_u *p, *s;
10600 char_u *retval;
10601 char_u *sname;
10602
10603 if (curbuf->b_ffname == NULL)
10604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010605 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 return NULL;
10607 }
10608 sname = home_replace_save(NULL, curbuf->b_ffname);
10609 if (sname == NULL)
10610 return NULL;
10611
10612 /*
10613 * We want a file name without separators, because we're not going to make
10614 * a directory.
10615 * "normal" path separator -> "=+"
10616 * "=" -> "=="
10617 * ":" path separator -> "=-"
10618 */
10619 for (p = sname; *p; ++p)
10620 if (*p == '=' || vim_ispathsep(*p))
10621 ++len;
10622 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10623 if (retval != NULL)
10624 {
10625 STRCPY(retval, p_vdir);
10626 add_pathsep(retval);
10627 s = retval + STRLEN(retval);
10628 for (p = sname; *p; ++p)
10629 {
10630 if (*p == '=')
10631 {
10632 *s++ = '=';
10633 *s++ = '=';
10634 }
10635 else if (vim_ispathsep(*p))
10636 {
10637 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010638#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 if (*p == ':')
10640 *s++ = '-';
10641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010643 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 }
10645 else
10646 *s++ = *p;
10647 }
10648 *s++ = '=';
10649 *s++ = c;
10650 STRCPY(s, ".vim");
10651 }
10652
10653 vim_free(sname);
10654 return retval;
10655}
10656
10657#endif /* FEAT_SESSION */
10658
10659/*
10660 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10661 * Return FAIL for a write error.
10662 */
10663 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010664put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665{
10666 if (
10667#ifdef USE_CRNL
10668 (
10669# ifdef MKSESSION_NL
10670 !mksession_nl &&
10671# endif
10672 (putc('\r', fd) < 0)) ||
10673#endif
10674 (putc('\n', fd) < 0))
10675 return FAIL;
10676 return OK;
10677}
10678
10679/*
10680 * Write a line to "fd".
10681 * Return FAIL for a write error.
10682 */
10683 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010684put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10687 return FAIL;
10688 return OK;
10689}
10690
10691#ifdef FEAT_VIMINFO
10692/*
10693 * ":rviminfo" and ":wviminfo".
10694 */
10695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010696ex_viminfo(
10697 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698{
10699 char_u *save_viminfo;
10700
10701 save_viminfo = p_viminfo;
10702 if (*p_viminfo == NUL)
10703 p_viminfo = (char_u *)"'100";
10704 if (eap->cmdidx == CMD_rviminfo)
10705 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010706 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10707 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010708 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 }
10710 else
10711 write_viminfo(eap->arg, eap->forceit);
10712 p_viminfo = save_viminfo;
10713}
10714#endif
10715
10716#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010717/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010718 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010719 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010722dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 if (fname == NULL)
10725 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010726 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727}
10728#endif
10729
10730/*
10731 * ":behave {mswin,xterm}"
10732 */
10733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010734ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735{
10736 if (STRCMP(eap->arg, "mswin") == 0)
10737 {
10738 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10739 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10740 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10741 set_option_value((char_u *)"keymodel", 0L,
10742 (char_u *)"startsel,stopsel", 0);
10743 }
10744 else if (STRCMP(eap->arg, "xterm") == 0)
10745 {
10746 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10747 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10748 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10749 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10750 }
10751 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010752 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753}
10754
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010755#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10756/*
10757 * Function given to ExpandGeneric() to obtain the possible arguments of the
10758 * ":behave {mswin,xterm}" command.
10759 */
10760 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010761get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010762{
10763 if (idx == 0)
10764 return (char_u *)"mswin";
10765 if (idx == 1)
10766 return (char_u *)"xterm";
10767 return NULL;
10768}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010769
10770/*
10771 * Function given to ExpandGeneric() to obtain the possible arguments of the
10772 * ":messages {clear}" command.
10773 */
10774 char_u *
10775get_messages_arg(expand_T *xp UNUSED, int idx)
10776{
10777 if (idx == 0)
10778 return (char_u *)"clear";
10779 return NULL;
10780}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010781#endif
10782
Bram Moolenaar113e1072019-01-20 15:30:40 +010010783#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010784 char_u *
10785get_mapclear_arg(expand_T *xp UNUSED, int idx)
10786{
10787 if (idx == 0)
10788 return (char_u *)"<buffer>";
10789 return NULL;
10790}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010791#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010792
Bram Moolenaar071d4272004-06-13 20:20:40 +000010793static int filetype_detect = FALSE;
10794static int filetype_plugin = FALSE;
10795static int filetype_indent = FALSE;
10796
10797/*
10798 * ":filetype [plugin] [indent] {on,off,detect}"
10799 * on: Load the filetype.vim file to install autocommands for file types.
10800 * off: Load the ftoff.vim file to remove all autocommands for file types.
10801 * plugin on: load filetype.vim and ftplugin.vim
10802 * plugin off: load ftplugof.vim
10803 * indent on: load filetype.vim and indent.vim
10804 * indent off: load indoff.vim
10805 */
10806 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010807ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808{
10809 char_u *arg = eap->arg;
10810 int plugin = FALSE;
10811 int indent = FALSE;
10812
10813 if (*eap->arg == NUL)
10814 {
10815 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010816 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 filetype_detect ? "ON" : "OFF",
10818 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10819 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10820 return;
10821 }
10822
10823 /* Accept "plugin" and "indent" in any order. */
10824 for (;;)
10825 {
10826 if (STRNCMP(arg, "plugin", 6) == 0)
10827 {
10828 plugin = TRUE;
10829 arg = skipwhite(arg + 6);
10830 continue;
10831 }
10832 if (STRNCMP(arg, "indent", 6) == 0)
10833 {
10834 indent = TRUE;
10835 arg = skipwhite(arg + 6);
10836 continue;
10837 }
10838 break;
10839 }
10840 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10841 {
10842 if (*arg == 'o' || !filetype_detect)
10843 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010844 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 filetype_detect = TRUE;
10846 if (plugin)
10847 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010848 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849 filetype_plugin = TRUE;
10850 }
10851 if (indent)
10852 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010853 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 filetype_indent = TRUE;
10855 }
10856 }
10857 if (*arg == 'd')
10858 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010859 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010860 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861 }
10862 }
10863 else if (STRCMP(arg, "off") == 0)
10864 {
10865 if (plugin || indent)
10866 {
10867 if (plugin)
10868 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010869 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 filetype_plugin = FALSE;
10871 }
10872 if (indent)
10873 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010874 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 filetype_indent = FALSE;
10876 }
10877 }
10878 else
10879 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010880 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 filetype_detect = FALSE;
10882 }
10883 }
10884 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010885 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886}
10887
10888/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010889 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 */
10891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010892ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893{
10894 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010895 {
10896 char_u *arg = eap->arg;
10897
10898 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10899 arg += 9;
10900
10901 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10902 if (arg != eap->arg)
10903 did_filetype = FALSE;
10904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010908ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909{
10910#ifdef FEAT_DIGRAPHS
10911 if (*eap->arg != NUL)
10912 putdigraph(eap->arg);
10913 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010914 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010916 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917#endif
10918}
10919
10920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010921ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922{
10923 int flags = 0;
10924
10925 if (eap->cmdidx == CMD_setlocal)
10926 flags = OPT_LOCAL;
10927 else if (eap->cmdidx == CMD_setglobal)
10928 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010929#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 if (cmdmod.browse && flags == 0)
10931 ex_options(eap);
10932 else
10933#endif
10934 (void)do_set(eap->arg, flags);
10935}
10936
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010937#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10938 void
10939set_no_hlsearch(int flag)
10940{
10941 no_hlsearch = flag;
10942# ifdef FEAT_EVAL
10943 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10944# endif
10945}
10946
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947/*
10948 * ":nohlsearch"
10949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010951ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010953 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010954 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955}
10956
10957/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010958 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 * Sets nextcmd to the start of the next command, if any. Also called when
10960 * skipping commands to find the next command.
10961 */
10962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010963ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964{
10965 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010966 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 char_u *end;
10968 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010969 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010970
10971 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010972 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010973 else
10974 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010975 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010976 return;
10977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978
10979 /* First clear any old pattern. */
10980 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010981 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982
10983 if (ends_excmd(*eap->arg))
10984 end = eap->arg;
10985 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010010986 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 end = eap->arg + 4;
10988 else
10989 {
10990 p = skiptowhite(eap->arg);
10991 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010992 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 p = skipwhite(p);
10994 if (*p == NUL)
10995 {
10996 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010997 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010998 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 return;
11000 }
11001 end = skip_regexp(p + 1, *p, TRUE, NULL);
11002 if (!eap->skip)
11003 {
11004 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11005 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011006 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007 eap->errmsg = e_trailing;
11008 return;
11009 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011010 if (*end != *p)
11011 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011012 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011013 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011014 return;
11015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016
11017 c = *end;
11018 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011019 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011020 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011021 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022 }
11023 }
11024 eap->nextcmd = find_nextcmd(end);
11025}
11026#endif
11027
11028#ifdef FEAT_CRYPT
11029/*
11030 * ":X": Get crypt key
11031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011033ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011035 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011036 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037}
11038#endif
11039
11040#ifdef FEAT_FOLDING
11041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011042ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043{
11044 if (foldManualAllowed(TRUE))
11045 foldCreate(eap->line1, eap->line2);
11046}
11047
11048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011049ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050{
11051 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11052 eap->forceit, FALSE);
11053}
11054
11055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011056ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057{
11058 linenr_T lnum;
11059
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011060#ifdef FEAT_CLIPBOARD
11061 start_global_changes();
11062#endif
11063
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 /* First set the marks for all lines closed/open. */
11065 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11066 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11067 ml_setmarked(lnum);
11068
11069 /* Execute the command on the marked lines. */
11070 global_exe(eap->arg);
11071 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011072#ifdef FEAT_CLIPBOARD
11073 end_global_changes();
11074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075}
11076#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011077
Bram Moolenaar39665952018-08-15 20:59:48 +020011078#ifdef FEAT_QUICKFIX
11079/*
11080 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11081 * instead of a quickfix command.
11082 */
11083 int
11084is_loclist_cmd(int cmdidx)
11085{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011086 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011087 return FALSE;
11088 return cmdnames[cmdidx].cmd_name[0] == 'l';
11089}
11090#endif
11091
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011092# if defined(FEAT_TIMERS) || defined(PROTO)
11093 int
11094get_pressedreturn(void)
11095{
11096 return ex_pressedreturn;
11097}
11098
11099 void
11100set_pressedreturn(int val)
11101{
11102 ex_pressedreturn = val;
11103}
11104#endif