blob: 438cb355aa02e6392515c71feaf050315828b563 [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
Bram Moolenaar0abb4272019-06-15 16:06:00 +0200299# define ex_const ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000301# define ex_lockvar ex_ni
302# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_function ex_ni
304# define ex_delfunction ex_ni
305# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000306# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100308static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100310static int makeopens(FILE *fd, char_u *dirnow);
311static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
312static void ex_loadview(exarg_T *eap);
313static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000314static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#else
316# define ex_loadview ex_ni
317#endif
318#ifndef FEAT_EVAL
319# define ex_compiler ex_ni
320#endif
321#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#else
324# define ex_viminfo ex_ni
325#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_filetype(exarg_T *eap);
328static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000330# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331# define ex_diffpatch ex_ni
332# define ex_diffgetput ex_ni
333# define ex_diffsplit ex_ni
334# define ex_diffthis ex_ni
335# define ex_diffupdate ex_ni
336#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_digraphs(exarg_T *eap);
338static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100339#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340# define ex_options ex_ni
341#endif
342#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static void ex_nohlsearch(exarg_T *eap);
344static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#else
346# define ex_nohlsearch ex_ni
347# define ex_match ex_ni
348#endif
349#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100350static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#else
352# define ex_X ex_ni
353#endif
354#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100355static void ex_fold(exarg_T *eap);
356static void ex_foldopen(exarg_T *eap);
357static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#else
359# define ex_fold ex_ni
360# define ex_foldopen ex_ni
361# define ex_folddo ex_ni
362#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100363#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_language ex_ni
365#endif
366#ifndef FEAT_SIGNS
367# define ex_sign ex_ni
368#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000369#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200370# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000371# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200372# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifndef FEAT_EVAL
376# define ex_debug ex_ni
377# define ex_breakadd ex_ni
378# define ex_debuggreedy ex_ni
379# define ex_breakdel ex_ni
380# define ex_breaklist ex_ni
381#endif
382
383#ifndef FEAT_CMDHIST
384# define ex_history ex_ni
385#endif
386#ifndef FEAT_JUMPLIST
387# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200388# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389# define ex_changes ex_ni
390#endif
391
Bram Moolenaar05159a02005-02-26 23:04:13 +0000392#ifndef FEAT_PROFILE
393# define ex_profile ex_ni
394#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200395#ifndef FEAT_TERMINAL
396# define ex_terminal ex_ni
397#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200398#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
399# define ex_xrestore ex_ni
400#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200401#if !defined(FEAT_TEXT_PROP)
402# define ex_popupclear ex_ni
403#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
406 * Declare cmdnames[].
407 */
408#define DO_DECLARE_EXCMD
409#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200410#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100411
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static char_u dollar_command[2] = {'$', 0};
413
414
415#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000416/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417typedef struct
418{
419 char_u *line; /* command line */
420 linenr_T lnum; /* sourcing_lnum of the line */
421} wcmd_T;
422
423/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000424 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000426 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000428struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430 garray_T *lines_gap; /* growarray with line info */
431 int current_line; /* last read line from growarray */
432 int repeating; /* TRUE when looping a second time */
433 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100434 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 void *cookie;
436};
437
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100438static char_u *get_loop_line(int c, void *cookie, int indent);
439static int store_loop_line(garray_T *gap, char_u *line);
440static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000441
442/* Struct to save a few things while debugging. Used in do_cmdline() only. */
443struct dbg_stuff
444{
445 int trylevel;
446 int force_abort;
447 except_T *caught_stack;
448 char_u *vv_exception;
449 char_u *vv_throwpoint;
450 int did_emsg;
451 int got_int;
452 int did_throw;
453 int need_rethrow;
454 int check_cstack;
455 except_T *current_exception;
456};
457
Bram Moolenaared203462004-06-16 11:19:22 +0000458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100459save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000460{
461 dsp->trylevel = trylevel; trylevel = 0;
462 dsp->force_abort = force_abort; force_abort = FALSE;
463 dsp->caught_stack = caught_stack; caught_stack = NULL;
464 dsp->vv_exception = v_exception(NULL);
465 dsp->vv_throwpoint = v_throwpoint(NULL);
466
467 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
468 dsp->did_emsg = did_emsg; did_emsg = FALSE;
469 dsp->got_int = got_int; got_int = FALSE;
470 dsp->did_throw = did_throw; did_throw = FALSE;
471 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
472 dsp->check_cstack = check_cstack; check_cstack = FALSE;
473 dsp->current_exception = current_exception; current_exception = NULL;
474}
475
476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100477restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000478{
479 suppress_errthrow = FALSE;
480 trylevel = dsp->trylevel;
481 force_abort = dsp->force_abort;
482 caught_stack = dsp->caught_stack;
483 (void)v_exception(dsp->vv_exception);
484 (void)v_throwpoint(dsp->vv_throwpoint);
485 did_emsg = dsp->did_emsg;
486 got_int = dsp->got_int;
487 did_throw = dsp->did_throw;
488 need_rethrow = dsp->need_rethrow;
489 check_cstack = dsp->check_cstack;
490 current_exception = dsp->current_exception;
491}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492#endif
493
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494/*
495 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
496 * command is given.
497 */
498 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100499do_exmode(
500 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 int save_msg_scroll;
503 int prev_msg_row;
504 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100505 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000506
507 if (improved)
508 exmode_active = EXMODE_VIM;
509 else
510 exmode_active = EXMODE_NORMAL;
511 State = NORMAL;
512
513 /* When using ":global /pat/ visual" and then "Q" we return to continue
514 * the :global command. */
515 if (global_busy)
516 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
518 save_msg_scroll = msg_scroll;
519 ++RedrawingDisabled; /* don't redisplay the window */
520 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521#ifdef FEAT_GUI
522 /* Ignore scrollbar and mouse events in Ex mode */
523 ++hold_gui_events;
524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
Bram Moolenaar32526b32019-01-19 17:43:09 +0100526 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 while (exmode_active)
528 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000529 /* Check for a ":normal" command and no more characters left. */
530 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
531 {
532 exmode_active = FALSE;
533 break;
534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 msg_scroll = TRUE;
536 need_wait_return = FALSE;
537 ex_pressedreturn = FALSE;
538 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100539 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 prev_msg_row = msg_row;
541 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 if (improved)
543 {
544 cmdline_row = msg_row;
545 do_cmdline(NULL, getexline, NULL, 0);
546 }
547 else
548 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
549 lines_left = Rows - 1;
550
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100552 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100555 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000558 if (ex_pressedreturn)
559 {
560 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000561 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000562 msg_row = prev_msg_row;
563 if (prev_msg_row == Rows - 1)
564 msg_row--;
565 }
566 msg_col = 0;
567 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
568 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000571 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
572 {
573 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100574 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000575 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100576 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
579
580#ifdef FEAT_GUI
581 --hold_gui_events;
582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 --RedrawingDisabled;
584 --no_wait_return;
585 update_screen(CLEAR);
586 need_wait_return = FALSE;
587 msg_scroll = save_msg_scroll;
588}
589
590/*
591 * Execute a simple command line. Used for translated commands like "*".
592 */
593 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100594do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595{
596 return do_cmdline(cmd, NULL, NULL,
597 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
598}
599
600/*
601 * do_cmdline(): execute one Ex command line
602 *
603 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100604 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 * 2. Split up in parts separated with '|'.
606 *
607 * This function can be called recursively!
608 *
609 * flags:
610 * DOCMD_VERBOSE - The command will be included in the error message.
611 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100612 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * DOCMD_KEYTYPED - Don't reset KeyTyped.
614 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
615 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
616 *
617 * return FAIL if cmdline could not be executed, OK otherwise
618 */
619 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100620do_cmdline(
621 char_u *cmdline,
622 char_u *(*fgetline)(int, void *, int),
623 void *cookie, /* argument for fgetline() */
624 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 char_u *next_cmdline; /* next cmd to execute */
627 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100628 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 static int recursive = 0; /* recursive depth */
630 int msg_didout_before_start = 0;
631 int count = 0; /* line number count */
632 int did_inc = FALSE; /* incremented RedrawingDisabled */
633 int retval = OK;
634#ifdef FEAT_EVAL
635 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000636 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int current_line = 0; /* active line in lines_ga */
638 char_u *fname = NULL; /* function or script name */
639 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
640 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000641 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 int initial_trylevel;
643 struct msglist **saved_msg_list = NULL;
644 struct msglist *private_msg_list;
645
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100646 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100647 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000649 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000651 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100653# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654# define cmd_cookie cookie
655#endif
656 static int call_depth = 0; /* recursiveness */
657
658#ifdef FEAT_EVAL
659 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
660 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000661 * This ensures that the do_errthrow() call in do_one_cmd() does not
662 * combine the messages stored by an earlier invocation of do_one_cmd()
663 * with the command name of the later one. This would happen when
664 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 saved_msg_list = msg_list;
666 msg_list = &private_msg_list;
667 private_msg_list = NULL;
668#endif
669
670 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100671 * here. The value of 200 allows nested function calls, ":source", etc.
672 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100673 if (call_depth >= 200
674#ifdef FEAT_EVAL
675 && call_depth >= p_mfd
676#endif
677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100679 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680#ifdef FEAT_EVAL
681 /* When converting to an exception, we do not include the command name
682 * since this is not an error of the specific command. */
683 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
684 msg_list = saved_msg_list;
685#endif
686 return FAIL;
687 }
688 ++call_depth;
689
690#ifdef FEAT_EVAL
691 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000692 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 cstack.cs_trylevel = 0;
694 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000695 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
697
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
700 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100701 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 ++ex_nesting_level;
704
705 /* Get the function or script name and the address where the next breakpoint
706 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000707 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
709 fname = func_name(real_cookie);
710 breakpoint = func_breakpoint(real_cookie);
711 dbg_tick = func_dbg_tick(real_cookie);
712 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100713 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
715 fname = sourcing_name;
716 breakpoint = source_breakpoint(real_cookie);
717 dbg_tick = source_dbg_tick(real_cookie);
718 }
719
720 /*
721 * Initialize "force_abort" and "suppress_errthrow" at the top level.
722 */
723 if (!recursive)
724 {
725 force_abort = FALSE;
726 suppress_errthrow = FALSE;
727 }
728
729 /*
730 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 * exception handling (used when debugging). Otherwise clear it to avoid
732 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000734 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000735 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200737 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
739 initial_trylevel = trylevel;
740
741 /*
742 * "did_throw" will be set to TRUE when an exception is being thrown.
743 */
744 did_throw = FALSE;
745#endif
746 /*
747 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000748 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 * If force_abort is set, we cancel everything.
750 */
751 did_emsg = FALSE;
752
753 /*
754 * KeyTyped is only set when calling vgetc(). Reset it here when not
755 * calling vgetc() (sourced command lines).
756 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 if (!(flags & DOCMD_KEYTYPED)
758 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 KeyTyped = FALSE;
760
761 /*
762 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000763 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * - for multiple commands on one line, separated with '|'
765 * - when repeating until there are no more lines (for ":source")
766 */
767 next_cmdline = cmdline;
768 do
769 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100771 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#endif
773
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000774 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (next_cmdline == NULL
776#ifdef FEAT_EVAL
777 && !force_abort
778 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 )
782 did_emsg = FALSE;
783
784 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100786 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * 3. If a line is given: Make a copy, so we can mess with it.
788 */
789
790#ifdef FEAT_EVAL
791 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 /* Each '|' separated command is stored separately in lines_ga, to
795 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100796 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
798 /* Check if a function has returned or, unless it has an unclosed
799 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000803 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804 func_line_end(real_cookie);
805# endif
806 if (func_has_ended(real_cookie))
807 {
808 retval = FAIL;
809 break;
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000813 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 script_line_end();
816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
818 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100819 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {
821 retval = FAIL;
822 break;
823 }
824
825 /* If breakpoints have been added/deleted need to check for it. */
826 if (breakpoint != NULL && dbg_tick != NULL
827 && *dbg_tick != debug_tick)
828 {
829 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100830 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 fname, sourcing_lnum);
832 *dbg_tick = debug_tick;
833 }
834
835 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
836 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
837
838 /* Did we encounter a breakpoint? */
839 if (breakpoint != NULL && *breakpoint != 0
840 && *breakpoint <= sourcing_lnum)
841 {
842 dbg_breakpoint(fname, sourcing_lnum);
843 /* Find next breakpoint. */
844 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100845 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 fname, sourcing_lnum);
847 *dbg_tick = debug_tick;
848 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000850 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 {
852 if (getline_is_func)
853 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100854 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000855 script_line_start();
856 }
857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000860 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * below, so that it stores lines in or reads them from
865 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000866 * while/for loop. */
867 cmd_getline = get_loop_line;
868 cmd_cookie = (void *)&cmd_loop_cookie;
869 cmd_loop_cookie.lines_gap = &lines_ga;
870 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000872 cmd_loop_cookie.cookie = cookie;
873 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875 else
876 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 cmd_cookie = cookie;
879 }
880#endif
881
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (next_cmdline == NULL)
884 {
885 /*
886 * Need to set msg_didout for the first line after an ":if",
887 * otherwise the ":if" will be overwritten.
888 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100889 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100891 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892#ifdef FEAT_EVAL
893 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
894#else
895 0
896#endif
897 )) == NULL)
898 {
899 /* Don't call wait_return for aborted command line. The NULL
900 * returned for the end of a sourced file or executed function
901 * doesn't do this. */
902 if (KeyTyped && !(flags & DOCMD_REPEAT))
903 need_wait_return = FALSE;
904 retval = FAIL;
905 break;
906 }
907 used_getline = TRUE;
908
909 /*
910 * Keep the first typed line. Clear it when more lines are typed.
911 */
912 if (flags & DOCMD_KEEPLINE)
913 {
914 vim_free(repeat_cmdline);
915 if (count == 0)
916 repeat_cmdline = vim_strsave(next_cmdline);
917 else
918 repeat_cmdline = NULL;
919 }
920 }
921
922 /* 3. Make a copy of the command so we can mess with it. */
923 else if (cmdline_copy == NULL)
924 {
925 next_cmdline = vim_strsave(next_cmdline);
926 if (next_cmdline == NULL)
927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100928 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 retval = FAIL;
930 break;
931 }
932 }
933 cmdline_copy = next_cmdline;
934
935#ifdef FEAT_EVAL
936 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 * Save the current line when inside a ":while" or ":for", and when
938 * the command looks like a ":while" or ":for", because we may need it
939 * later. When there is a '|' and another command, it is stored
940 * separately, because we need to be able to jump back to it from an
941 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000943 if (current_line == lines_ga.ga_len
944 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000946 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 retval = FAIL;
949 break;
950 }
951 }
952 did_endif = FALSE;
953#endif
954
955 if (count++ == 0)
956 {
957 /*
958 * All output from the commands is put below each other, without
959 * waiting for a return. Don't do this when executing commands
960 * from a script or when being called recursive (e.g. for ":e
961 * +command file").
962 */
963 if (!(flags & DOCMD_NOWAIT) && !recursive)
964 {
965 msg_didout_before_start = msg_didout;
966 msg_didany = FALSE; /* no output yet */
967 msg_start();
968 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200969 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 ++RedrawingDisabled;
971 did_inc = TRUE;
972 }
973 }
974
975 if (p_verbose >= 15 && sourcing_name != NULL)
976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000978 verbose_enter_scroll();
979
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100980 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000982 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100983 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000984
985 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 --no_wait_return;
987 }
988
989 /*
990 * 2. Execute one '|' separated command.
991 * do_one_cmd() will return NULL if there is no trailing '|'.
992 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
993 */
994 ++recursive;
995 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
996#ifdef FEAT_EVAL
997 &cstack,
998#endif
999 cmd_getline, cmd_cookie);
1000 --recursive;
1001
1002#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 if (cmd_cookie == (void *)&cmd_loop_cookie)
1004 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001006 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#endif
1008
1009 if (next_cmdline == NULL)
1010 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001011 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_CMDHIST
1013 /*
1014 * If the command was typed, remember it for the ':' register.
1015 * Do this AFTER executing the command to make :@: work.
1016 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 && new_last_cmdline != NULL)
1019 {
1020 vim_free(last_cmdline);
1021 last_cmdline = new_last_cmdline;
1022 new_last_cmdline = NULL;
1023 }
1024#endif
1025 }
1026 else
1027 {
1028 /* need to copy the command after the '|' to cmdline_copy, for the
1029 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001030 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 next_cmdline = cmdline_copy;
1032 }
1033
1034
1035#ifdef FEAT_EVAL
1036 /* reset did_emsg for a function that is not aborted by an error */
1037 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001038 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 && !func_has_abort(real_cookie))
1040 did_emsg = FALSE;
1041
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 ++current_line;
1045
1046 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 * An ":endwhile", ":endfor" and ":continue" is handled here.
1048 * If we were executing commands, jump back to the ":while" or
1049 * ":for".
1050 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 /* Jump back to the matching ":while" or ":for". Be careful
1057 * not to use a cs_line[] from an entry that isn't a ":while"
1058 * or ":for": It would make "current_line" invalid and can
1059 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (!did_emsg && !got_int && !did_throw
1061 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 && (cstack.cs_flags[cstack.cs_idx]
1063 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 && cstack.cs_line[cstack.cs_idx] >= 0
1065 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1066 {
1067 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 /* remember we jumped there */
1069 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 line_breakcheck(); /* check if CTRL-C typed */
1071
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 /* Check for the next breakpoint at or after the ":while"
1073 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (breakpoint != NULL)
1075 {
1076 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001077 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 fname,
1079 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1080 *dbg_tick = debug_tick;
1081 }
1082 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001087 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1088 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091
1092 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1099 }
1100 }
1101
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001102 /* Check for the next breakpoint after a watchexpression */
1103 if (breakpoint != NULL && has_watchexpr())
1104 {
1105 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1106 *dbg_tick = debug_tick;
1107 }
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 /*
1110 * When not inside any ":while" loop, clear remembered lines.
1111 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
1114 if (lines_ga.ga_len > 0)
1115 {
1116 sourcing_lnum =
1117 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1118 free_cmdlines(&lines_ga);
1119 }
1120 current_line = 0;
1121 }
1122
1123 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1125 * being restored at the ":endtry". Reset them here and set the
1126 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1127 * This includes the case where a missing ":endif", ":endwhile" or
1128 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 cstack.cs_lflags &= ~CSL_HAD_FINA;
1133 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1134 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 did_throw ? (void *)current_exception : NULL);
1136 did_emsg = got_int = did_throw = FALSE;
1137 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1138 }
1139
1140 /* Update global "trylevel" for recursive calls to do_cmdline() from
1141 * within this loop. */
1142 trylevel = initial_trylevel + cstack.cs_trylevel;
1143
1144 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001145 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * files) is aborted because of an error, an interrupt, or an uncaught
1147 * exception, cancel everything. If it is left normally, reset
1148 * force_abort to get the non-EH compatible abortion behavior for
1149 * the rest of the script.
1150 */
1151 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1152 force_abort = FALSE;
1153
1154 /* Convert an interrupt to an exception if appropriate. */
1155 (void)do_intthrow(&cstack);
1156#endif /* FEAT_EVAL */
1157
1158 }
1159 /*
1160 * Continue executing command lines when:
1161 * - no CTRL-C typed, no aborting error, no exception thrown or try
1162 * conditionals need to be checked for executing finally clauses or
1163 * catching an interrupt exception
1164 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 * looping for ":source" command or function call.
1167 */
1168 while (!((got_int
1169#ifdef FEAT_EVAL
1170 || (did_emsg && force_abort) || did_throw
1171#endif
1172 )
1173#ifdef FEAT_EVAL
1174 && cstack.cs_trylevel == 0
1175#endif
1176 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001177 && !(did_emsg
1178#ifdef FEAT_EVAL
1179 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001180 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001181 * the :endtry to be missed. */
1182 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1183#endif
1184 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001185 && (getline_equal(fgetline, cookie, getexmodeline)
1186 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && (next_cmdline != NULL
1188#ifdef FEAT_EVAL
1189 || cstack.cs_idx >= 0
1190#endif
1191 || (flags & DOCMD_REPEAT)));
1192
1193 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001194 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#ifdef FEAT_EVAL
1196 free_cmdlines(&lines_ga);
1197 ga_clear(&lines_ga);
1198
1199 if (cstack.cs_idx >= 0)
1200 {
1201 /*
1202 * If a sourced file or executed function ran to its end, report the
1203 * unclosed conditional.
1204 */
1205 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001206 && ((getline_equal(fgetline, cookie, getsourceline)
1207 && !source_finished(fgetline, cookie))
1208 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 && !func_has_ended(real_cookie))))
1210 {
1211 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001212 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220
1221 /*
1222 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1223 * ":endtry" in a sourced file or executed function. If the try
1224 * conditional is in its finally clause, ignore anything pending.
1225 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001226 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
1228 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001229 {
1230 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1231
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001232 if (idx >= 0)
1233 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001234 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1235 &cstack.cs_looplevel);
1236 }
1237 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 trylevel = initial_trylevel;
1239 }
1240
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001241 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1242 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001244 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 ? (char_u *)"endfunction" : (char_u *)NULL);
1246
1247 if (trylevel == 0)
1248 {
1249 /*
1250 * When an exception is being thrown out of the outermost try
1251 * conditional, discard the uncaught exception, disable the conversion
1252 * of interrupts or errors to exceptions, and ensure that no more
1253 * commands are executed.
1254 */
1255 if (did_throw)
1256 {
1257 void *p = NULL;
1258 char_u *saved_sourcing_name;
1259 int saved_sourcing_lnum;
1260 struct msglist *messages = NULL, *next;
1261
1262 /*
1263 * If the uncaught exception is a user exception, report it as an
1264 * error. If it is an error exception, display the saved error
1265 * message now. For an interrupt exception, do nothing; the
1266 * interrupt message is given elsewhere.
1267 */
1268 switch (current_exception->type)
1269 {
1270 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001271 vim_snprintf((char *)IObuff, IOSIZE,
1272 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 current_exception->value);
1274 p = vim_strsave(IObuff);
1275 break;
1276 case ET_ERROR:
1277 messages = current_exception->messages;
1278 current_exception->messages = NULL;
1279 break;
1280 case ET_INTERRUPT:
1281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283
1284 saved_sourcing_name = sourcing_name;
1285 saved_sourcing_lnum = sourcing_lnum;
1286 sourcing_name = current_exception->throw_name;
1287 sourcing_lnum = current_exception->throw_lnum;
1288 current_exception->throw_name = NULL;
1289
1290 discard_current_exception(); /* uses IObuff if 'verbose' */
1291 suppress_errthrow = TRUE;
1292 force_abort = TRUE;
1293
1294 if (messages != NULL)
1295 {
1296 do
1297 {
1298 next = messages->next;
1299 emsg(messages->msg);
1300 vim_free(messages->msg);
1301 vim_free(messages);
1302 messages = next;
1303 }
1304 while (messages != NULL);
1305 }
1306 else if (p != NULL)
1307 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001308 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 vim_free(p);
1310 }
1311 vim_free(sourcing_name);
1312 sourcing_name = saved_sourcing_name;
1313 sourcing_lnum = saved_sourcing_lnum;
1314 }
1315
1316 /*
1317 * On an interrupt or an aborting error not converted to an exception,
1318 * disable the conversion of errors to exceptions. (Interrupts are not
1319 * converted any more, here.) This enables also the interrupt message
1320 * when force_abort is set and did_emsg unset in case of an interrupt
1321 * from a finally clause after an error.
1322 */
1323 else if (got_int || (did_emsg && force_abort))
1324 suppress_errthrow = TRUE;
1325 }
1326
1327 /*
1328 * The current cstack will be freed when do_cmdline() returns. An uncaught
1329 * exception will have to be rethrown in the previous cstack. If a function
1330 * has just returned or a script file was just finished and the previous
1331 * cstack belongs to the same function or, respectively, script file, it
1332 * will have to be checked for finally clauses to be executed due to the
1333 * ":return" or ":finish". This is done in do_one_cmd().
1334 */
1335 if (did_throw)
1336 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && ex_nesting_level > func_level(real_cookie) + 1))
1341 {
1342 if (!did_throw)
1343 check_cstack = TRUE;
1344 }
1345 else
1346 {
1347 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 --ex_nesting_level;
1350 /*
1351 * Go to debug mode when returning from a function in which we are
1352 * single-stepping.
1353 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001354 if ((getline_equal(fgetline, cookie, getsourceline)
1355 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001357 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 ? (char_u *)_("End of sourced file")
1359 : (char_u *)_("End of function"));
1360 }
1361
1362 /*
1363 * Restore the exception environment (done after returning from the
1364 * debugger).
1365 */
1366 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001367 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 msg_list = saved_msg_list;
1370#endif /* FEAT_EVAL */
1371
1372 /*
1373 * If there was too much output to fit on the command line, ask the user to
1374 * hit return before redrawing the screen. With the ":global" command we do
1375 * this only once after the command is finished.
1376 */
1377 if (did_inc)
1378 {
1379 --RedrawingDisabled;
1380 --no_wait_return;
1381 msg_scroll = FALSE;
1382
1383 /*
1384 * When just finished an ":if"-":else" which was typed, no need to
1385 * wait for hit-return. Also for an error situation.
1386 */
1387 if (retval == FAIL
1388#ifdef FEAT_EVAL
1389 || (did_endif && KeyTyped && !did_emsg)
1390#endif
1391 )
1392 {
1393 need_wait_return = FALSE;
1394 msg_didany = FALSE; /* don't wait when restarting edit */
1395 }
1396 else if (need_wait_return)
1397 {
1398 /*
1399 * The msg_start() above clears msg_didout. The wait_return we do
1400 * here should not overwrite the command that may be shown before
1401 * doing that.
1402 */
1403 msg_didout |= msg_didout_before_start;
1404 wait_return(FALSE);
1405 }
1406 }
1407
Bram Moolenaar2a942252012-11-28 23:03:07 +01001408#ifdef FEAT_EVAL
1409 did_endif = FALSE; /* in case do_cmdline used recursively */
1410#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 /*
1412 * Reset if_level, in case a sourced script file contains more ":if" than
1413 * ":endif" (could be ":if x | foo | endif").
1414 */
1415 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001416#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 --call_depth;
1419 return retval;
1420}
1421
1422#ifdef FEAT_EVAL
1423/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001424 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 */
1426 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001427get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001429 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 wcmd_T *wp;
1431 char_u *line;
1432
1433 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1434 {
1435 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001436 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001438 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (cp->getline == NULL)
1440 line = getcmdline(c, 0L, indent);
1441 else
1442 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001443 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 ++cp->current_line;
1445
1446 return line;
1447 }
1448
1449 KeyTyped = FALSE;
1450 ++cp->current_line;
1451 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1452 sourcing_lnum = wp->lnum;
1453 return vim_strsave(wp->line);
1454}
1455
1456/*
1457 * Store a line in "gap" so that a ":while" loop can execute it again.
1458 */
1459 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001460store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 if (ga_grow(gap, 1) == FAIL)
1463 return FAIL;
1464 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1465 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1466 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 return OK;
1468}
1469
1470/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001471 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 */
1473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001474free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 while (gap->ga_len > 0)
1477 {
1478 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1479 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481}
1482#endif
1483
1484/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001485 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1486 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001489getline_equal(
1490 char_u *(*fgetline)(int, void *, int),
1491 void *cookie UNUSED, /* argument for fgetline() */
1492 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
1494#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001495 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001496 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaar89d40322006-08-29 15:30:07 +00001498 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001499 * function that's originally used to obtain the lines. This may be
1500 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001501 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 cp = (struct loop_cookie *)cookie;
1503 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 {
1505 gp = cp->getline;
1506 cp = cp->cookie;
1507 }
1508 return gp == func;
1509#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
1512}
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001515 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 * getline function. Otherwise return "cookie".
1517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001519getline_cookie(
1520 char_u *(*fgetline)(int, void *, int) UNUSED,
1521 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar13505972019-01-24 15:04:48 +01001523#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001524 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaar89d40322006-08-29 15:30:07 +00001527 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001528 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001530 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 cp = (struct loop_cookie *)cookie;
1532 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
1534 gp = cp->getline;
1535 cp = cp->cookie;
1536 }
1537 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543
1544/*
1545 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1546 * ":bwipeout", etc.
1547 * Returns the buffer number.
1548 */
1549 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001550compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001551{
1552 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001553 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001554 int count = offset;
1555
1556 buf = firstbuf;
1557 while (buf->b_next != NULL && buf->b_fnum < lnum)
1558 buf = buf->b_next;
1559 while (count != 0)
1560 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001561 count += (offset < 0) ? 1 : -1;
1562 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1563 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001564 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001565 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001566 if (addr_type == ADDR_LOADED_BUFFERS)
1567 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001568 while (buf->b_ml.ml_mfp == NULL)
1569 {
1570 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1571 if (nextbuf == NULL)
1572 break;
1573 buf = nextbuf;
1574 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001575 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001576 /* we might have gone too far, last buffer is not loadedd */
1577 if (addr_type == ADDR_LOADED_BUFFERS)
1578 while (buf->b_ml.ml_mfp == NULL)
1579 {
1580 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1581 if (nextbuf == NULL)
1582 break;
1583 buf = nextbuf;
1584 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001585 return buf->b_fnum;
1586}
1587
Bram Moolenaarb7316892019-05-01 18:08:42 +02001588/*
1589 * Return the window number of "win".
1590 * When "win" is NULL return the number of windows.
1591 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001592 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001593current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001594{
1595 win_T *wp;
1596 int nr = 0;
1597
Bram Moolenaar29323592016-07-24 22:04:11 +02001598 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001599 {
1600 ++nr;
1601 if (wp == win)
1602 break;
1603 }
1604 return nr;
1605}
1606
1607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001608current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001609{
1610 tabpage_T *tp;
1611 int nr = 0;
1612
Bram Moolenaar29323592016-07-24 22:04:11 +02001613 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001614 {
1615 ++nr;
1616 if (tp == tab)
1617 break;
1618 }
1619 return nr;
1620}
1621
1622# define CURRENT_WIN_NR current_win_nr(curwin)
1623# define LAST_WIN_NR current_win_nr(NULL)
1624# define CURRENT_TAB_NR current_tab_nr(curtab)
1625# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627/*
1628 * Execute one Ex command.
1629 *
1630 * If 'sourcing' is TRUE, the command will be included in the error message.
1631 *
1632 * 1. skip comment lines and leading space
1633 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001634 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001635 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001636 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001637 * 6. parse arguments
1638 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 *
1642 * This function may be called recursively!
1643 */
1644#if (_MSC_VER == 1200)
1645/*
Bram Moolenaared203462004-06-16 11:19:22 +00001646 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001648 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
1650 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001651do_one_cmd(
1652 char_u **cmdlinep,
1653 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001655 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001657 char_u *(*fgetline)(int, void *, int),
1658 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 char_u *p;
1661 linenr_T lnum;
1662 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001663 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001664 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001666 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001668 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001670 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672 vim_memset(&ea, 0, sizeof(ea));
1673 ea.line1 = 1;
1674 ea.line2 = 1;
1675#ifdef FEAT_EVAL
1676 ++ex_nesting_level;
1677#endif
1678
Bram Moolenaar21691f82012-12-05 19:13:18 +01001679 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (quitmore
1681#ifdef FEAT_EVAL
1682 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001683 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001684#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001685 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001686 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 --quitmore;
1688
1689 /*
1690 * Reset browse, confirm, etc.. They are restored when returning, for
1691 * recursive calls.
1692 */
1693 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001695 /* "#!anything" is handled like a comment. */
1696 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1697 goto doend;
1698
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001699/*
1700 * 1. Skip comment lines and leading white space and colons.
1701 * 2. Handle command modifiers.
1702 */
1703 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001705 ea.cmdlinep = cmdlinep;
1706 ea.getline = fgetline;
1707 ea.cookie = cookie;
1708#ifdef FEAT_EVAL
1709 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001711 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001712 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001714 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
1716#ifdef FEAT_EVAL
1717 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1718 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1719#else
1720 ea.skip = (if_level > 0);
1721#endif
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001724 * 3. Skip over the range to find the command. Let "p" point to after it.
1725 *
1726 * We need the command to know what kind of range it uses.
1727 */
1728 cmd = ea.cmd;
1729 ea.cmd = skip_range(ea.cmd, NULL);
1730 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1731 ea.cmd = skipwhite(ea.cmd + 1);
1732 p = find_command(&ea, NULL);
1733
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001734#ifdef FEAT_EVAL
1735# ifdef FEAT_PROFILE
1736 // Count this line for profiling if skip is TRUE.
1737 if (do_profiling == PROF_YES
1738 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1739 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1740 {
1741 int skip = did_emsg || got_int || did_throw;
1742
1743 if (ea.cmdidx == CMD_catch)
1744 skip = !skip && !(cstack->cs_idx >= 0
1745 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1746 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1747 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1748 skip = skip || !(cstack->cs_idx >= 0
1749 && !(cstack->cs_flags[cstack->cs_idx]
1750 & (CSF_ACTIVE | CSF_TRUE)));
1751 else if (ea.cmdidx == CMD_finally)
1752 skip = FALSE;
1753 else if (ea.cmdidx != CMD_endif
1754 && ea.cmdidx != CMD_endfor
1755 && ea.cmdidx != CMD_endtry
1756 && ea.cmdidx != CMD_endwhile)
1757 skip = ea.skip;
1758
1759 if (!skip)
1760 {
1761 if (getline_equal(fgetline, cookie, get_func_line))
1762 func_line_exec(getline_cookie(fgetline, cookie));
1763 else if (getline_equal(fgetline, cookie, getsourceline))
1764 script_line_exec();
1765 }
1766 }
1767# endif
1768
1769 /* May go to debug mode. If this happens and the ">quit" debug command is
1770 * used, throw an interrupt exception and skip the next command. */
1771 dbg_check_breakpoint(&ea);
1772 if (!ea.skip && got_int)
1773 {
1774 ea.skip = TRUE;
1775 (void)do_intthrow(cstack);
1776 }
1777#endif
1778
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001779/*
1780 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 *
1782 * where 'addr' is:
1783 *
1784 * % (entire file)
1785 * $ [+-NUM]
1786 * 'x [+-NUM] (where x denotes a currently defined mark)
1787 * . [+-NUM]
1788 * [+-NUM]..
1789 * NUM
1790 *
1791 * The ea.cmd pointer is updated to point to the first character following the
1792 * range spec. If an initial address is found, but no second, the upper bound
1793 * is equal to the lower.
1794 */
1795
Bram Moolenaar25190db2019-05-04 15:05:28 +02001796 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001797 if (!IS_USER_CMDIDX(ea.cmdidx))
1798 {
1799 if (ea.cmdidx != CMD_SIZE)
1800 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1801 else
1802 ea.addr_type = ADDR_LINES;
1803
Bram Moolenaar25190db2019-05-04 15:05:28 +02001804 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001805 if (ea.cmdidx == CMD_wincmd && p != NULL)
1806 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001807#ifdef FEAT_QUICKFIX
1808 // :.cc in quickfix window uses line number
1809 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1810 ea.addr_type = ADDR_OTHER;
1811#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001812 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001813
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001814 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001815 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001816 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001819 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821
1822 /*
1823 * Skip ':' and any white space
1824 */
1825 ea.cmd = skipwhite(ea.cmd);
1826 while (*ea.cmd == ':')
1827 ea.cmd = skipwhite(ea.cmd + 1);
1828
1829 /*
1830 * If we got a line, but no command, then go to the line.
1831 * If we find a '|' or '\n' we set ea.nextcmd.
1832 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001833 if (*ea.cmd == NUL || *ea.cmd == '"'
1834 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 /*
1837 * strange vi behaviour:
1838 * ":3" jumps to line 3
1839 * ":3|..." prints line 3
1840 * ":|" prints current line
1841 */
1842 if (ea.skip) /* skip this if inside :if */
1843 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001844 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
1846 ea.cmdidx = CMD_print;
1847 ea.argt = RANGE+COUNT+TRLBAR;
1848 if ((errormsg = invalid_range(&ea)) == NULL)
1849 {
1850 correct_range(&ea);
1851 ex_print(&ea);
1852 }
1853 }
1854 else if (ea.addr_count != 0)
1855 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001856 if (ea.line2 > curbuf->b_ml.ml_line_count)
1857 {
1858 /* With '-' in 'cpoptions' a line number past the file is an
1859 * error, otherwise put it at the end of the file. */
1860 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1861 ea.line2 = -1;
1862 else
1863 ea.line2 = curbuf->b_ml.ml_line_count;
1864 }
1865
1866 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001867 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 else
1869 {
1870 if (ea.line2 == 0)
1871 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 else
1873 curwin->w_cursor.lnum = ea.line2;
1874 beginline(BL_SOL | BL_FIX);
1875 }
1876 }
1877 goto doend;
1878 }
1879
Bram Moolenaard5005162014-08-22 23:05:54 +02001880 /* If this looks like an undefined user command and there are CmdUndefined
1881 * autocommands defined, trigger the matching autocommands. */
1882 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1883 && ASCII_ISUPPER(*ea.cmd)
1884 && has_cmdundefined())
1885 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001886 int ret;
1887
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001888 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001889 while (ASCII_ISALNUM(*p))
1890 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001891 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001892 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1893 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001894 /* If the autocommands did something and didn't cause an error, try
1895 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001896 p = (ret
1897#ifdef FEAT_EVAL
1898 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001899#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001900 ) ? find_command(&ea, NULL) : ea.cmd;
1901 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001902
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (p == NULL)
1904 {
1905 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001906 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 goto doend;
1908 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001909 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001910 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1911 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
1913 errormsg = uc_fun_cmd();
1914 goto doend;
1915 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (ea.cmdidx == CMD_SIZE)
1918 {
1919 if (!ea.skip)
1920 {
1921 STRCPY(IObuff, _("E492: Not an editor command"));
1922 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001923 {
1924 /* If the modifier was parsed OK the error must be in the
1925 * following command */
1926 if (after_modifier != NULL)
1927 append_command(after_modifier);
1928 else
1929 append_command(*cmdlinep);
1930 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001931 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001932 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
1934 goto doend;
1935 }
1936
Bram Moolenaar958636c2014-10-21 20:01:58 +02001937 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1938 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001939#ifdef HAVE_EX_SCRIPT_NI
1940 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1941#endif
1942 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
1944#ifndef FEAT_EVAL
1945 /*
1946 * When the expression evaluation is disabled, recognize the ":if" and
1947 * ":endif" commands and ignore everything in between it.
1948 */
1949 if (ea.cmdidx == CMD_if)
1950 ++if_level;
1951 if (if_level)
1952 {
1953 if (ea.cmdidx == CMD_endif)
1954 --if_level;
1955 goto doend;
1956 }
1957
1958#endif
1959
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001960 /* forced commands */
1961 if (*p == '!' && ea.cmdidx != CMD_substitute
1962 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 ++p;
1965 ea.forceit = TRUE;
1966 }
1967 else
1968 ea.forceit = FALSE;
1969
1970/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001971 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001973 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001974 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 if (!ea.skip)
1977 {
1978#ifdef HAVE_SANDBOX
1979 if (sandbox != 0 && !(ea.argt & SBOXOK))
1980 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001981 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 goto doend;
1984 }
1985#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001986 if (restricted != 0 && (ea.argt & RESTRICT))
1987 {
1988 errormsg = _("E981: Command not allowed in rvim");
1989 goto doend;
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
1992 {
1993 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001994 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 goto doend;
1996 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001997
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001998 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001999 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002001 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002002 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 goto doend;
2004 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002005
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002006 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002007 * Do allow ":checktime" (it is postponed).
2008 * Do allow ":edit" (check for an argument later).
2009 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002010 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002011 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002012 && ea.cmdidx != CMD_edit
2013 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002014 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002015 && curbuf_locked())
2016 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2019 {
2020 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002021 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 goto doend;
2023 }
2024 }
2025
2026 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 goto doend;
2030 }
2031
2032 /*
2033 * Don't complain about the range if it is not used
2034 * (could happen if line_count is accidentally set to 0).
2035 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002036 if (!ea.skip && !ni && (ea.argt & RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
2038 /*
2039 * If the range is backwards, ask for confirmation and, if given, swap
2040 * ea.line1 & ea.line2 so it's forwards again.
2041 * When global command is busy, don't ask, will fail below.
2042 */
2043 if (!global_busy && ea.line1 > ea.line2)
2044 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002045 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002047 if (sourcing || exmode_active)
2048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002049 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002050 goto doend;
2051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (ask_yesno((char_u *)
2053 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002054 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 lnum = ea.line1;
2057 ea.line1 = ea.line2;
2058 ea.line2 = lnum;
2059 }
2060 if ((errormsg = invalid_range(&ea)) != NULL)
2061 goto doend;
2062 }
2063
Bram Moolenaarb7316892019-05-01 18:08:42 +02002064 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2065 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 ea.line2 = 1;
2067
2068 correct_range(&ea);
2069
2070#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002071 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2072 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
2074 /* Put the first line at the start of a closed fold, put the last line
2075 * at the end of a closed fold. */
2076 (void)hasFolding(ea.line1, &ea.line1, NULL);
2077 (void)hasFolding(ea.line2, NULL, &ea.line2);
2078 }
2079#endif
2080
2081#ifdef FEAT_QUICKFIX
2082 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002083 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 * option here, so things like % get expanded.
2085 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002086 p = replace_makeprg(&ea, p, cmdlinep);
2087 if (p == NULL)
2088 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#endif
2090
2091 /*
2092 * Skip to start of argument.
2093 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2094 */
2095 if (ea.cmdidx == CMD_bang)
2096 ea.arg = p;
2097 else
2098 ea.arg = skipwhite(p);
2099
Bram Moolenaar379fb762018-08-30 15:58:28 +02002100 // ":file" cannot be run with an argument when "curbuf_lock" is set
2101 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2102 goto doend;
2103
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 /*
2105 * Check for "++opt=val" argument.
2106 * Must be first, allow ":w ++enc=utf8 !cmd"
2107 */
2108 if (ea.argt & ARGOPT)
2109 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2110 if (getargopt(&ea) == FAIL && !ni)
2111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002112 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 goto doend;
2114 }
2115
2116 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2117 {
2118 if (*ea.arg == '>') /* append */
2119 {
2120 if (*++ea.arg != '>') /* typed wrong */
2121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002122 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 goto doend;
2124 }
2125 ea.arg = skipwhite(ea.arg + 1);
2126 ea.append = TRUE;
2127 }
2128 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2129 {
2130 ++ea.arg;
2131 ea.usefilter = TRUE;
2132 }
2133 }
2134
2135 if (ea.cmdidx == CMD_read)
2136 {
2137 if (ea.forceit)
2138 {
2139 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2140 ea.forceit = FALSE;
2141 }
2142 else if (*ea.arg == '!') /* :r !filter */
2143 {
2144 ++ea.arg;
2145 ea.usefilter = TRUE;
2146 }
2147 }
2148
2149 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2150 {
2151 ea.amount = 1;
2152 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2153 {
2154 ++ea.arg;
2155 ++ea.amount;
2156 }
2157 ea.arg = skipwhite(ea.arg);
2158 }
2159
2160 /*
2161 * Check for "+command" argument, before checking for next command.
2162 * Don't do this for ":read !cmd" and ":write !cmd".
2163 */
2164 if ((ea.argt & EDITCMD) && !ea.usefilter)
2165 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2166
2167 /*
2168 * Check for '|' to separate commands and '"' to start comments.
2169 * Don't do this for ":read !cmd" and ":write !cmd".
2170 */
2171 if ((ea.argt & TRLBAR) && !ea.usefilter)
2172 separate_nextcmd(&ea);
2173
2174 /*
2175 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002176 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2177 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002179 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002180 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002181 || ea.cmdidx == CMD_global
2182 || ea.cmdidx == CMD_vglobal
2183 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
2185 for (p = ea.arg; *p; ++p)
2186 {
2187 /* Remove one backslash before a newline, so that it's possible to
2188 * pass a newline to the shell and also a newline that is preceded
2189 * with a backslash. This makes it impossible to end a shell
2190 * command in a backslash, but that doesn't appear useful.
2191 * Halving the number of backslashes is incompatible with previous
2192 * versions. */
2193 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002194 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 else if (*p == '\n')
2196 {
2197 ea.nextcmd = p + 1;
2198 *p = NUL;
2199 break;
2200 }
2201 }
2202 }
2203
2204 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2205 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002206 buf_T *buf;
2207
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002209 switch (ea.addr_type)
2210 {
2211 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002212 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 ea.line2 = curbuf->b_ml.ml_line_count;
2214 break;
2215 case ADDR_LOADED_BUFFERS:
2216 buf = firstbuf;
2217 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2218 buf = buf->b_next;
2219 ea.line1 = buf->b_fnum;
2220 buf = lastbuf;
2221 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2222 buf = buf->b_prev;
2223 ea.line2 = buf->b_fnum;
2224 break;
2225 case ADDR_BUFFERS:
2226 ea.line1 = firstbuf->b_fnum;
2227 ea.line2 = lastbuf->b_fnum;
2228 break;
2229 case ADDR_WINDOWS:
2230 ea.line2 = LAST_WIN_NR;
2231 break;
2232 case ADDR_TABS:
2233 ea.line2 = LAST_TAB_NR;
2234 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002235 case ADDR_TABS_RELATIVE:
2236 ea.line2 = 1;
2237 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002238 case ADDR_ARGUMENTS:
2239 if (ARGCOUNT == 0)
2240 ea.line1 = ea.line2 = 0;
2241 else
2242 ea.line2 = ARGCOUNT;
2243 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002244 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002245#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002246 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002247 if (ea.line2 == 0)
2248 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002249#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002250 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002251 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002252 case ADDR_UNSIGNED:
2253 case ADDR_QUICKFIX:
2254 iemsg(_("INTERNAL: Cannot use DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002255 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258
2259 /* accept numbered register only when no count allowed (:put) */
2260 if ( (ea.argt & REGSTR)
2261 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002262 /* Do not allow register = for user commands */
2263 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2265 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002266#ifndef FEAT_CLIPBOARD
2267 /* check these explicitly for a more specific error message */
2268 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002270 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002271 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002274 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2275 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002276 {
2277 ea.regname = *ea.arg++;
2278#ifdef FEAT_EVAL
2279 /* for '=' register: accept the rest of the line as an expression */
2280 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2281 {
2282 set_expr_line(vim_strsave(ea.arg));
2283 ea.arg += STRLEN(ea.arg);
2284 }
2285#endif
2286 ea.arg = skipwhite(ea.arg);
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289
2290 /*
2291 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2292 * count, it's a buffer name.
2293 */
2294 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2295 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002296 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 n = getdigits(&ea.arg);
2299 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002300 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002302 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 goto doend;
2304 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002305 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 ea.line2 = n;
2308 if (ea.addr_count == 0)
2309 ea.addr_count = 1;
2310 }
2311 else
2312 {
2313 ea.line1 = ea.line2;
2314 ea.line2 += n - 1;
2315 ++ea.addr_count;
2316 /*
2317 * Be vi compatible: no error message for out of range.
2318 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002319 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 ea.line2 = curbuf->b_ml.ml_line_count;
2321 }
2322 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002323
2324 /*
2325 * Check for flags: 'l', 'p' and '#'.
2326 */
2327 if (ea.argt & EXFLAGS)
2328 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002330 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002331 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002333 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 goto doend;
2335 }
2336
2337 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002339 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 goto doend;
2341 }
2342
2343#ifdef FEAT_EVAL
2344 /*
2345 * Skip the command when it's not going to be executed.
2346 * The commands like :if, :endif, etc. always need to be executed.
2347 * Also make an exception for commands that handle a trailing command
2348 * themselves.
2349 */
2350 if (ea.skip)
2351 {
2352 switch (ea.cmdidx)
2353 {
2354 /* commands that need evaluation */
2355 case CMD_while:
2356 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002357 case CMD_for:
2358 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 case CMD_if:
2360 case CMD_elseif:
2361 case CMD_else:
2362 case CMD_endif:
2363 case CMD_try:
2364 case CMD_catch:
2365 case CMD_finally:
2366 case CMD_endtry:
2367 case CMD_function:
2368 break;
2369
2370 /* Commands that handle '|' themselves. Check: A command should
2371 * either have the TRLBAR flag, appear in this list or appear in
2372 * the list at ":help :bar". */
2373 case CMD_aboveleft:
2374 case CMD_and:
2375 case CMD_belowright:
2376 case CMD_botright:
2377 case CMD_browse:
2378 case CMD_call:
2379 case CMD_confirm:
2380 case CMD_delfunction:
2381 case CMD_djump:
2382 case CMD_dlist:
2383 case CMD_dsearch:
2384 case CMD_dsplit:
2385 case CMD_echo:
2386 case CMD_echoerr:
2387 case CMD_echomsg:
2388 case CMD_echon:
2389 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002390 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_help:
2392 case CMD_hide:
2393 case CMD_ijump:
2394 case CMD_ilist:
2395 case CMD_isearch:
2396 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002397 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 case CMD_keepjumps:
2399 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002400 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 case CMD_leftabove:
2402 case CMD_let:
2403 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002404 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002406 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002407 case CMD_noautocmd:
2408 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 case CMD_perl:
2410 case CMD_psearch:
2411 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002412 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002413 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_return:
2415 case CMD_rightbelow:
2416 case CMD_ruby:
2417 case CMD_silent:
2418 case CMD_smagic:
2419 case CMD_snomagic:
2420 case CMD_substitute:
2421 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002422 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_tcl:
2424 case CMD_throw:
2425 case CMD_tilde:
2426 case CMD_topleft:
2427 case CMD_unlet:
2428 case CMD_verbose:
2429 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002430 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 break;
2432
2433 default: goto doend;
2434 }
2435 }
2436#endif
2437
2438 if (ea.argt & XFILE)
2439 {
2440 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2441 goto doend;
2442 }
2443
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 /*
2445 * Accept buffer name. Cannot be used at the same time with a buffer
2446 * number. Don't do this for a user command.
2447 */
2448 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002449 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
2451 /*
2452 * :bdelete, :bwipeout and :bunload take several arguments, separated
2453 * by spaces: find next space (skipping over escaped characters).
2454 * The others take one argument: ignore trailing spaces.
2455 */
2456 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2457 || ea.cmdidx == CMD_bunload)
2458 p = skiptowhite_esc(ea.arg);
2459 else
2460 {
2461 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002462 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 --p;
2464 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002465 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2466 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (ea.line2 < 0) /* failed */
2468 goto doend;
2469 ea.addr_count = 1;
2470 ea.arg = skipwhite(p);
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
Bram Moolenaar2be57332018-02-13 18:05:18 +01002473 /* The :try command saves the emsg_silent flag, reset it here when
2474 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002475 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002476 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002477 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002478 if (emsg_silent < 0)
2479 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002480 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002481 }
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002484 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaar958636c2014-10-21 20:01:58 +02002487 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 /*
2490 * Execute a user-defined command.
2491 */
2492 do_ucmd(&ea);
2493 }
2494 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002497 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
2499 ea.errmsg = NULL;
2500 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2501 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002502 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
2504
2505#ifdef FEAT_EVAL
2506 /*
2507 * If the command just executed called do_cmdline(), any throw or ":return"
2508 * or ":finish" encountered there must also check the cstack of the still
2509 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2510 * exception, or reanimate a returned function or finished script file and
2511 * return or finish it again.
2512 */
2513 if (need_rethrow)
2514 do_throw(cstack);
2515 else if (check_cstack)
2516 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002517 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002519 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 && current_func_returned())
2521 do_return(&ea, TRUE, FALSE, NULL);
2522 }
2523 need_rethrow = check_cstack = FALSE;
2524#endif
2525
2526doend:
2527 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002530 curwin->w_cursor.col = 0;
2531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2534 {
2535 if (sourcing)
2536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002537 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002540 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002542 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544 emsg(errormsg);
2545 }
2546#ifdef FEAT_EVAL
2547 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002548 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2549 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#endif
2551
Bram Moolenaareffed932018-08-14 13:38:17 +02002552 if (ea.verbose_save >= 0)
2553 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002554
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002555 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002557 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaareffed932018-08-14 13:38:17 +02002559 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 /* messages could be enabled for a serious error, need to check if the
2562 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002563 if (!did_emsg || msg_silent > ea.save_msg_silent)
2564 msg_silent = ea.save_msg_silent;
2565 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (emsg_silent < 0)
2567 emsg_silent = 0;
2568 /* Restore msg_scroll, it's set by file I/O commands, even when no
2569 * message is actually displayed. */
2570 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002571
2572 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2573 * somewhere in the line. Put it back in the first column. */
2574 if (redirecting())
2575 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002578#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002579 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002580 --sandbox;
2581#endif
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2584 ea.nextcmd = NULL;
2585
2586#ifdef FEAT_EVAL
2587 --ex_nesting_level;
2588#endif
2589
2590 return ea.nextcmd;
2591}
2592#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002593 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594#endif
2595
2596/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002597 * Parse and skip over command modifiers:
2598 * - update eap->cmd
2599 * - store flags in "cmdmod".
2600 * - Set ex_pressedreturn for an empty command line.
2601 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002602 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 * - set p_verbose for ":verbose"
2604 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002605 * When "skip_only" is TRUE the global variables are not changed, except for
2606 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002607 * Return FAIL when the command is not to be executed.
2608 * May set "errormsg" to an error message.
2609 */
2610 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002611parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002612{
2613 char_u *p;
2614
2615 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2616 eap->verbose_save = -1;
2617 eap->save_msg_silent = -1;
2618
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002619 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002620 for (;;)
2621 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2623 ++eap->cmd;
2624
2625 /* in ex mode, an empty line works like :+ */
2626 if (*eap->cmd == NUL && exmode_active
2627 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2628 || getline_equal(eap->getline, eap->cookie, getexline))
2629 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2630 {
2631 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002632 if (!skip_only)
2633 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002634 }
2635
2636 /* ignore comment and empty lines */
2637 if (*eap->cmd == '"')
2638 return FAIL;
2639 if (*eap->cmd == NUL)
2640 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002641 if (!skip_only)
2642 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 return FAIL;
2644 }
2645
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 p = skip_range(eap->cmd, NULL);
2647 switch (*p)
2648 {
2649 /* When adding an entry, also modify cmd_exists(). */
2650 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2651 break;
2652 cmdmod.split |= WSP_ABOVE;
2653 continue;
2654
2655 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2656 {
2657 cmdmod.split |= WSP_BELOW;
2658 continue;
2659 }
2660 if (checkforcmd(&eap->cmd, "browse", 3))
2661 {
2662#ifdef FEAT_BROWSE_CMD
2663 cmdmod.browse = TRUE;
2664#endif
2665 continue;
2666 }
2667 if (!checkforcmd(&eap->cmd, "botright", 2))
2668 break;
2669 cmdmod.split |= WSP_BOT;
2670 continue;
2671
2672 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2673 break;
2674#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2675 cmdmod.confirm = TRUE;
2676#endif
2677 continue;
2678
2679 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2680 {
2681 cmdmod.keepmarks = TRUE;
2682 continue;
2683 }
2684 if (checkforcmd(&eap->cmd, "keepalt", 5))
2685 {
2686 cmdmod.keepalt = TRUE;
2687 continue;
2688 }
2689 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2690 {
2691 cmdmod.keeppatterns = TRUE;
2692 continue;
2693 }
2694 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2695 break;
2696 cmdmod.keepjumps = TRUE;
2697 continue;
2698
2699 case 'f': /* only accept ":filter {pat} cmd" */
2700 {
2701 char_u *reg_pat;
2702
2703 if (!checkforcmd(&p, "filter", 4)
2704 || *p == NUL || ends_excmd(*p))
2705 break;
2706 if (*p == '!')
2707 {
2708 cmdmod.filter_force = TRUE;
2709 p = skipwhite(p + 1);
2710 if (*p == NUL || ends_excmd(*p))
2711 break;
2712 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002713 if (skip_only)
2714 p = skip_vimgrep_pat(p, NULL, NULL);
2715 else
2716 // NOTE: This puts a NUL after the pattern.
2717 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002718 if (p == NULL || *p == NUL)
2719 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002720 if (!skip_only)
2721 {
2722 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002723 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002724 if (cmdmod.filter_regmatch.regprog == NULL)
2725 break;
2726 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002727 eap->cmd = p;
2728 continue;
2729 }
2730
2731 /* ":hide" and ":hide | cmd" are not modifiers */
2732 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2733 || *p == NUL || ends_excmd(*p))
2734 break;
2735 eap->cmd = p;
2736 cmdmod.hide = TRUE;
2737 continue;
2738
2739 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2740 {
2741 cmdmod.lockmarks = TRUE;
2742 continue;
2743 }
2744
2745 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2746 break;
2747 cmdmod.split |= WSP_ABOVE;
2748 continue;
2749
2750 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2751 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002752 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002753 {
2754 /* Set 'eventignore' to "all". Restore the
2755 * existing option value later. */
2756 cmdmod.save_ei = vim_strsave(p_ei);
2757 set_string_option_direct((char_u *)"ei", -1,
2758 (char_u *)"all", OPT_FREE, SID_NONE);
2759 }
2760 continue;
2761 }
2762 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2763 break;
2764 cmdmod.noswapfile = TRUE;
2765 continue;
2766
2767 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2768 break;
2769 cmdmod.split |= WSP_BELOW;
2770 continue;
2771
2772 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2773 {
2774#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002775 if (!skip_only)
2776 {
2777 if (!eap->did_sandbox)
2778 ++sandbox;
2779 eap->did_sandbox = TRUE;
2780 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002781#endif
2782 continue;
2783 }
2784 if (!checkforcmd(&eap->cmd, "silent", 3))
2785 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002786 if (!skip_only)
2787 {
2788 if (eap->save_msg_silent == -1)
2789 eap->save_msg_silent = msg_silent;
2790 ++msg_silent;
2791 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2793 {
2794 /* ":silent!", but not "silent !cmd" */
2795 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002796 if (!skip_only)
2797 {
2798 ++emsg_silent;
2799 ++eap->did_esilent;
2800 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 }
2802 continue;
2803
2804 case 't': if (checkforcmd(&p, "tab", 3))
2805 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002806 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002808 long tabnr = get_address(eap, &eap->cmd,
2809 ADDR_TABS, eap->skip,
2810 skip_only, FALSE, 1);
2811 if (tabnr == MAXLNUM)
2812 cmdmod.tab = tabpage_index(curtab) + 1;
2813 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002815 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002817 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002818 return FAIL;
2819 }
2820 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 }
2823 eap->cmd = p;
2824 continue;
2825 }
2826 if (!checkforcmd(&eap->cmd, "topleft", 2))
2827 break;
2828 cmdmod.split |= WSP_TOP;
2829 continue;
2830
2831 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2832 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002833 if (!skip_only)
2834 {
2835 if (eap->save_msg_silent == -1)
2836 eap->save_msg_silent = msg_silent;
2837 msg_silent = 0;
2838 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 continue;
2840
2841 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2842 {
2843 cmdmod.split |= WSP_VERT;
2844 continue;
2845 }
2846 if (!checkforcmd(&p, "verbose", 4))
2847 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002848 if (!skip_only)
2849 {
2850 if (eap->verbose_save < 0)
2851 eap->verbose_save = p_verbose;
2852 if (vim_isdigit(*eap->cmd))
2853 p_verbose = atoi((char *)eap->cmd);
2854 else
2855 p_verbose = 1;
2856 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 eap->cmd = p;
2858 continue;
2859 }
2860 break;
2861 }
2862
2863 return OK;
2864}
2865
2866/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002867 * Free contents of "cmdmod".
2868 */
2869 static void
2870free_cmdmod(void)
2871{
2872 if (cmdmod.save_ei != NULL)
2873 {
2874 /* Restore 'eventignore' to the value before ":noautocmd". */
2875 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2876 OPT_FREE, SID_NONE);
2877 free_string_option(cmdmod.save_ei);
2878 }
2879
2880 if (cmdmod.filter_regmatch.regprog != NULL)
2881 vim_regfree(cmdmod.filter_regmatch.regprog);
2882}
2883
2884/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002885 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002886 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002887 * Return FAIL and set "errormsg" or return OK.
2888 */
2889 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002890parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002891{
2892 int address_count = 1;
2893 linenr_T lnum;
2894
2895 // Repeat for all ',' or ';' separated addresses.
2896 for (;;)
2897 {
2898 eap->line1 = eap->line2;
2899 switch (eap->addr_type)
2900 {
2901 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002902 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002903 // default is current line number
2904 eap->line2 = curwin->w_cursor.lnum;
2905 break;
2906 case ADDR_WINDOWS:
2907 eap->line2 = CURRENT_WIN_NR;
2908 break;
2909 case ADDR_ARGUMENTS:
2910 eap->line2 = curwin->w_arg_idx + 1;
2911 if (eap->line2 > ARGCOUNT)
2912 eap->line2 = ARGCOUNT;
2913 break;
2914 case ADDR_LOADED_BUFFERS:
2915 case ADDR_BUFFERS:
2916 eap->line2 = curbuf->b_fnum;
2917 break;
2918 case ADDR_TABS:
2919 eap->line2 = CURRENT_TAB_NR;
2920 break;
2921 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002922 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002923 eap->line2 = 1;
2924 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002925 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002926#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002927 eap->line2 = qf_get_cur_idx(eap);
2928#endif
2929 break;
2930 case ADDR_QUICKFIX_VALID:
2931#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002932 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002933#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002934 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002935 case ADDR_NONE:
2936 // Will give an error later if a range is found.
2937 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002938 }
2939 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002940 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002941 eap->addr_count == 0, address_count++);
2942 if (eap->cmd == NULL) // error detected
2943 return FAIL;
2944 if (lnum == MAXLNUM)
2945 {
2946 if (*eap->cmd == '%') // '%' - all lines
2947 {
2948 ++eap->cmd;
2949 switch (eap->addr_type)
2950 {
2951 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002952 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953 eap->line1 = 1;
2954 eap->line2 = curbuf->b_ml.ml_line_count;
2955 break;
2956 case ADDR_LOADED_BUFFERS:
2957 {
2958 buf_T *buf = firstbuf;
2959
2960 while (buf->b_next != NULL
2961 && buf->b_ml.ml_mfp == NULL)
2962 buf = buf->b_next;
2963 eap->line1 = buf->b_fnum;
2964 buf = lastbuf;
2965 while (buf->b_prev != NULL
2966 && buf->b_ml.ml_mfp == NULL)
2967 buf = buf->b_prev;
2968 eap->line2 = buf->b_fnum;
2969 break;
2970 }
2971 case ADDR_BUFFERS:
2972 eap->line1 = firstbuf->b_fnum;
2973 eap->line2 = lastbuf->b_fnum;
2974 break;
2975 case ADDR_WINDOWS:
2976 case ADDR_TABS:
2977 if (IS_USER_CMDIDX(eap->cmdidx))
2978 {
2979 eap->line1 = 1;
2980 eap->line2 = eap->addr_type == ADDR_WINDOWS
2981 ? LAST_WIN_NR : LAST_TAB_NR;
2982 }
2983 else
2984 {
2985 // there is no Vim command which uses '%' and
2986 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002987 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002988 return FAIL;
2989 }
2990 break;
2991 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002992 case ADDR_UNSIGNED:
2993 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002994 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002995 return FAIL;
2996 case ADDR_ARGUMENTS:
2997 if (ARGCOUNT == 0)
2998 eap->line1 = eap->line2 = 0;
2999 else
3000 {
3001 eap->line1 = 1;
3002 eap->line2 = ARGCOUNT;
3003 }
3004 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003005 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003006#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003008 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 if (eap->line2 == 0)
3010 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003011#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003012 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003013 case ADDR_NONE:
3014 // Will give an error later if a range is found.
3015 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003016 }
3017 ++eap->addr_count;
3018 }
3019 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3020 {
3021 pos_T *fp;
3022
3023 // '*' - visual area
3024 if (eap->addr_type != ADDR_LINES)
3025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003026 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003027 return FAIL;
3028 }
3029
3030 ++eap->cmd;
3031 if (!eap->skip)
3032 {
3033 fp = getmark('<', FALSE);
3034 if (check_mark(fp) == FAIL)
3035 return FAIL;
3036 eap->line1 = fp->lnum;
3037 fp = getmark('>', FALSE);
3038 if (check_mark(fp) == FAIL)
3039 return FAIL;
3040 eap->line2 = fp->lnum;
3041 ++eap->addr_count;
3042 }
3043 }
3044 }
3045 else
3046 eap->line2 = lnum;
3047 eap->addr_count++;
3048
3049 if (*eap->cmd == ';')
3050 {
3051 if (!eap->skip)
3052 {
3053 curwin->w_cursor.lnum = eap->line2;
3054 // don't leave the cursor on an illegal line or column
3055 check_cursor();
3056 }
3057 }
3058 else if (*eap->cmd != ',')
3059 break;
3060 ++eap->cmd;
3061 }
3062
3063 // One address given: set start and end lines.
3064 if (eap->addr_count == 1)
3065 {
3066 eap->line1 = eap->line2;
3067 // ... but only implicit: really no address given
3068 if (lnum == MAXLNUM)
3069 eap->addr_count = 0;
3070 }
3071 return OK;
3072}
3073
3074/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003075 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * If there is a match advance "pp" to the argument and return TRUE.
3077 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003078 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003079checkforcmd(
3080 char_u **pp, /* start of command */
3081 char *cmd, /* name of command */
3082 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 int i;
3085
3086 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003087 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 break;
3089 if (i >= len && !isalpha((*pp)[i]))
3090 {
3091 *pp = skipwhite(*pp + i);
3092 return TRUE;
3093 }
3094 return FALSE;
3095}
3096
3097/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003098 * Append "cmd" to the error message in IObuff.
3099 * Takes care of limiting the length and handling 0xa0, which would be
3100 * invisible otherwise.
3101 */
3102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003103append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003104{
3105 char_u *s = cmd;
3106 char_u *d;
3107
3108 STRCAT(IObuff, ": ");
3109 d = IObuff + STRLEN(IObuff);
3110 while (*s != NUL && d - IObuff < IOSIZE - 7)
3111 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003112 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003113 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003114 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003115 STRCPY(d, "<a0>");
3116 d += 4;
3117 }
3118 else
3119 MB_COPY_CHAR(s, d);
3120 }
3121 *d = NUL;
3122}
3123
3124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003127 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003128 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 * Returns NULL for an ambiguous user command.
3130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003132find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 int len;
3135 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003136 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
3138 /*
3139 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003140 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 * - the 'k' command can directly be followed by any character.
3142 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003143 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003145 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
3147 p = eap->cmd;
3148 if (*p == 'k')
3149 {
3150 eap->cmdidx = CMD_k;
3151 ++p;
3152 }
3153 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003154 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3155 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 || p[1] == 'g'
3157 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3158 || p[1] == 'I'
3159 || (p[1] == 'r' && p[2] != 'e')))
3160 {
3161 eap->cmdidx = CMD_substitute;
3162 ++p;
3163 }
3164 else
3165 {
3166 while (ASCII_ISALPHA(*p))
3167 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003168 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003169 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003170 while (ASCII_ISALNUM(*p))
3171 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 /* check for non-alpha command */
3174 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3175 ++p;
3176 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003177 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3178 {
3179 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3180 * :delete with the 'l' flag. Same for 'p'. */
3181 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003182 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003183 break;
3184 if (i == len - 1)
3185 {
3186 --len;
3187 if (p[-1] == 'l')
3188 eap->flags |= EXFLAG_LIST;
3189 else
3190 eap->flags |= EXFLAG_PRINT;
3191 }
3192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003194 if (ASCII_ISLOWER(eap->cmd[0]))
3195 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003196 int c1 = eap->cmd[0];
3197 int c2 = eap->cmd[1];
3198
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003199 if (command_count != (int)CMD_SIZE)
3200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003201 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003202 getout(1);
3203 }
3204
3205 /* Use a precomputed index for fast look-up in cmdnames[]
3206 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003207 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3208 if (ASCII_ISLOWER(c2))
3209 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003212 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3215 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3216 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3217 (size_t)len) == 0)
3218 {
3219#ifdef FEAT_EVAL
3220 if (full != NULL
3221 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3222 *full = TRUE;
3223#endif
3224 break;
3225 }
3226
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003227 // Look for a user defined command as a last resort. Let ":Print" be
3228 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003229 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3230 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003232 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 while (ASCII_ISALNUM(*p))
3234 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 eap->cmdidx = CMD_SIZE;
3239 }
3240
3241 return p;
3242}
3243
3244#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003245static struct cmdmod
3246{
3247 char *name;
3248 int minlen;
3249 int has_count; /* :123verbose :3tab */
3250} cmdmods[] = {
3251 {"aboveleft", 3, FALSE},
3252 {"belowright", 3, FALSE},
3253 {"botright", 2, FALSE},
3254 {"browse", 3, FALSE},
3255 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003256 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003257 {"hide", 3, FALSE},
3258 {"keepalt", 5, FALSE},
3259 {"keepjumps", 5, FALSE},
3260 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003261 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003262 {"leftabove", 5, FALSE},
3263 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003264 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003265 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003266 {"rightbelow", 6, FALSE},
3267 {"sandbox", 3, FALSE},
3268 {"silent", 3, FALSE},
3269 {"tab", 3, TRUE},
3270 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003271 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003272 {"verbose", 4, TRUE},
3273 {"vertical", 4, FALSE},
3274};
3275
3276/*
3277 * Return length of a command modifier (including optional count).
3278 * Return zero when it's not a modifier.
3279 */
3280 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003281modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003282{
3283 int i, j;
3284 char_u *p = cmd;
3285
3286 if (VIM_ISDIGIT(*cmd))
3287 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003288 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003289 {
3290 for (j = 0; p[j] != NUL; ++j)
3291 if (p[j] != cmdmods[i].name[j])
3292 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003293 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003294 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003295 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003296 }
3297 return 0;
3298}
3299
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300/*
3301 * Return > 0 if an Ex command "name" exists.
3302 * Return 2 if there is an exact match.
3303 * Return 3 if there is an ambiguous match.
3304 */
3305 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003306cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 exarg_T ea;
3309 int full = FALSE;
3310 int i;
3311 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003312 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
3314 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003315 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
3317 for (j = 0; name[j] != NUL; ++j)
3318 if (name[j] != cmdmods[i].name[j])
3319 break;
3320 if (name[j] == NUL && j >= cmdmods[i].minlen)
3321 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3322 }
3323
Bram Moolenaara9587612006-05-04 21:47:50 +00003324 /* Check built-in commands and user defined commands.
3325 * For ":2match" and ":3match" we need to skip the number. */
3326 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003328 p = find_command(&ea, &full);
3329 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003331 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3332 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003333 if (*skipwhite(p) != NUL)
3334 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3336}
3337#endif
3338
3339/*
3340 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3341 * we don't need/want deleted. Maybe this could be done better if we didn't
3342 * repeat all this stuff. The only problem is that they may not stay
3343 * perfectly compatible with each other, but then the command line syntax
3344 * probably won't change that much -- webb.
3345 */
3346 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003347set_one_cmd_context(
3348 expand_T *xp,
3349 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 char_u *p;
3352 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003353 int len = 0;
3354 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003356 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 int delim;
3358#endif
3359 int forceit = FALSE;
3360 int usefilter = FALSE; /* filter instead of file name */
3361
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003362 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 xp->xp_pattern = buff;
3364 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003365 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003368 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 */
3370 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3371 ;
3372 xp->xp_pattern = cmd;
3373
3374 if (*cmd == NUL)
3375 return NULL;
3376 if (*cmd == '"') /* ignore comment lines */
3377 {
3378 xp->xp_context = EXPAND_NOTHING;
3379 return NULL;
3380 }
3381
3382/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003383 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
3385 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 xp->xp_pattern = cmd;
3387 if (*cmd == NUL)
3388 return NULL;
3389 if (*cmd == '"')
3390 {
3391 xp->xp_context = EXPAND_NOTHING;
3392 return NULL;
3393 }
3394
3395 if (*cmd == '|' || *cmd == '\n')
3396 return cmd + 1; /* There's another command */
3397
3398 /*
3399 * Isolate the command and search for it in the command table.
3400 * Exceptions:
3401 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003402 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3404 */
3405 if (*cmd == 'k' && cmd[1] != 'e')
3406 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003407 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 p = cmd + 1;
3409 }
3410 else
3411 {
3412 p = cmd;
3413 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3414 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003415 /* a user command may contain digits */
3416 if (ASCII_ISUPPER(cmd[0]))
3417 while (ASCII_ISALNUM(*p) || *p == '*')
3418 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003419 /* for python 3.x: ":py3*" commands completion */
3420 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003421 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003422 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003423 while (ASCII_ISALPHA(*p) || *p == '*')
3424 ++p;
3425 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003426 /* check for non-alpha command */
3427 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3428 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
3433 xp->xp_context = EXPAND_UNSUCCESSFUL;
3434 return NULL;
3435 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003436 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003437 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3438 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3439 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 break;
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003443 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446
3447 /*
3448 * If the cursor is touching the command, and it ends in an alpha-numeric
3449 * character, complete the command name.
3450 */
3451 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3452 return NULL;
3453
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003454 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
3456 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3457 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003458 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 p = cmd + 1;
3460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3462 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003463 ea.cmd = cmd;
3464 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003465#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003467#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003469#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003470 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003471 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003472 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
3477 /* Not still touching the command and it was an illegal one */
3478 xp->xp_context = EXPAND_UNSUCCESSFUL;
3479 return NULL;
3480 }
3481
3482 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3483
3484 if (*p == '!') /* forced commands */
3485 {
3486 forceit = TRUE;
3487 ++p;
3488 }
3489
3490/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003491 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003493 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003494 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 arg = skipwhite(p);
3497
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
3500 if (*arg == '>') /* append */
3501 {
3502 if (*++arg == '>')
3503 ++arg;
3504 arg = skipwhite(arg);
3505 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003506 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 ++arg;
3509 usefilter = TRUE;
3510 }
3511 }
3512
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003513 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 usefilter = forceit; /* :r! filter if forced */
3516 if (*arg == '!') /* :r !filter */
3517 {
3518 ++arg;
3519 usefilter = TRUE;
3520 }
3521 }
3522
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003523 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 while (*arg == *cmd) /* allow any number of '>' or '<' */
3526 ++arg;
3527 arg = skipwhite(arg);
3528 }
3529
3530 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003531 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 /* Check if we're in the +command */
3534 p = arg + 1;
3535 arg = skip_cmd_arg(arg, FALSE);
3536
3537 /* Still touching the command after '+'? */
3538 if (*arg == NUL)
3539 return p;
3540
3541 /* Skip space(s) after +command to get to the real argument */
3542 arg = skipwhite(arg);
3543 }
3544
3545 /*
3546 * Check for '|' to separate commands and '"' to start comments.
3547 * Don't do this for ":read !cmd" and ":write !cmd".
3548 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 p = arg;
3552 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003553 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 p += 2;
3555 while (*p)
3556 {
3557 if (*p == Ctrl_V)
3558 {
3559 if (p[1] != NUL)
3560 ++p;
3561 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 || *p == '|' || *p == '\n')
3564 {
3565 if (*(p - 1) != '\\')
3566 {
3567 if (*p == '|' || *p == '\n')
3568 return p + 1;
3569 return NULL; /* It's a comment */
3570 }
3571 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003572 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574 }
3575
3576 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 vim_strchr((char_u *)"|\"", *arg) == NULL)
3579 return NULL;
3580
3581 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003582 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003584 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003585 while (*p && p < buff + len)
3586 {
3587 if (*p == ' ' || *p == TAB)
3588 {
3589 /* argument starts after a space */
3590 xp->xp_pattern = ++p;
3591 }
3592 else
3593 {
3594 if (*p == '\\' && *(p + 1) != NUL)
3595 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003596 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003597 }
3598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003600 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003602 int c;
3603 int in_quote = FALSE;
3604 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
3606 /*
3607 * Allow spaces within back-quotes to count as part of the argument
3608 * being expanded.
3609 */
3610 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003611 p = xp->xp_pattern;
3612 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003614 if (has_mbyte)
3615 c = mb_ptr2char(p);
3616 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003617 c = *p;
3618 if (c == '\\' && p[1] != NUL)
3619 ++p;
3620 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 if (!in_quote)
3623 {
3624 xp->xp_pattern = p;
3625 bow = p + 1;
3626 }
3627 in_quote = !in_quote;
3628 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003629 /* An argument can contain just about everything, except
3630 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003631 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003632#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003633 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003634#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003635 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003636 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003637 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003638 while (*p != NUL)
3639 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003640 if (has_mbyte)
3641 c = mb_ptr2char(p);
3642 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003643 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003644 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003645 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003646 if (has_mbyte)
3647 len = (*mb_ptr2len)(p);
3648 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003649 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003650 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003651 }
3652 if (in_quote)
3653 bow = p;
3654 else
3655 xp->xp_pattern = p;
3656 p -= len;
3657 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003658 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660
3661 /*
3662 * If we are still inside the quotes, and we passed a space, just
3663 * expand from there.
3664 */
3665 if (bow != NULL && in_quote)
3666 xp->xp_pattern = bow;
3667 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003668
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003669 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003670 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003671 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003672#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003673 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003674#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003675 /* When still after the command name expand executables. */
3676 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003677 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 /* Check for environment variable */
3681 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003682#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 || *xp->xp_pattern == '%'
3684#endif
3685 )
3686 {
3687 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3688 if (!vim_isIDc(*p))
3689 break;
3690 if (*p == NUL)
3691 {
3692 xp->xp_context = EXPAND_ENV_VARS;
3693 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003694#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003695 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003696 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003697 compl = EXPAND_ENV_VARS;
3698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003701#if defined(FEAT_CMDL_COMPL)
3702 /* Check for user names */
3703 if (*xp->xp_pattern == '~')
3704 {
3705 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3706 ;
3707 /* Complete ~user only if it partially matches a user name.
3708 * A full match ~user<Tab> will be replaced by user's home
3709 * directory i.e. something like ~user<Tab> -> /home/user/ */
3710 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003711 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003712 {
3713 xp->xp_context = EXPAND_USER;
3714 ++xp->xp_pattern;
3715 }
3716 }
3717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719
3720/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003721 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003723 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003725 case CMD_find:
3726 case CMD_sfind:
3727 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003728 if (xp->xp_context == EXPAND_FILES)
3729 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003730 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 case CMD_cd:
3732 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003733 case CMD_tcd:
3734 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 case CMD_lcd:
3736 case CMD_lchdir:
3737 if (xp->xp_context == EXPAND_FILES)
3738 xp->xp_context = EXPAND_DIRECTORIES;
3739 break;
3740 case CMD_help:
3741 xp->xp_context = EXPAND_HELP;
3742 xp->xp_pattern = arg;
3743 break;
3744
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003745 /* Command modifiers: return the argument.
3746 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003748 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 case CMD_belowright:
3750 case CMD_botright:
3751 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003752 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003753 case CMD_cdo:
3754 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003756 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 case CMD_folddoclosed:
3758 case CMD_folddoopen:
3759 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003760 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 case CMD_keepjumps:
3762 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003763 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003764 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003766 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003768 case CMD_noautocmd:
3769 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003771 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003773 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003774 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 case CMD_topleft:
3776 case CMD_verbose:
3777 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003778 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 return arg;
3780
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003781 case CMD_filter:
3782 if (*arg != NUL)
3783 arg = skip_vimgrep_pat(arg, NULL, NULL);
3784 if (arg == NULL || *arg == NUL)
3785 {
3786 xp->xp_context = EXPAND_NOTHING;
3787 return NULL;
3788 }
3789 return skipwhite(arg);
3790
Bram Moolenaar4f688582007-07-24 12:34:30 +00003791#ifdef FEAT_CMDL_COMPL
3792# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 case CMD_match:
3794 if (*arg == NUL || !ends_excmd(*arg))
3795 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003796 /* also complete "None" */
3797 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 arg = skipwhite(skiptowhite(arg));
3799 if (*arg != NUL)
3800 {
3801 xp->xp_context = EXPAND_NOTHING;
3802 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3803 }
3804 }
3805 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808/*
3809 * All completion for the +cmdline_compl feature goes here.
3810 */
3811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003813 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 case CMD_delcommand:
3816 xp->xp_context = EXPAND_USER_COMMANDS;
3817 xp->xp_pattern = arg;
3818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 case CMD_global:
3821 case CMD_vglobal:
3822 delim = *arg; /* get the delimiter */
3823 if (delim)
3824 ++arg; /* skip delimiter if there is one */
3825
3826 while (arg[0] != NUL && arg[0] != delim)
3827 {
3828 if (arg[0] == '\\' && arg[1] != NUL)
3829 ++arg;
3830 ++arg;
3831 }
3832 if (arg[0] != NUL)
3833 return arg + 1;
3834 break;
3835 case CMD_and:
3836 case CMD_substitute:
3837 delim = *arg;
3838 if (delim)
3839 {
3840 /* skip "from" part */
3841 ++arg;
3842 arg = skip_regexp(arg, delim, p_magic, NULL);
3843 }
3844 /* skip "to" part */
3845 while (arg[0] != NUL && arg[0] != delim)
3846 {
3847 if (arg[0] == '\\' && arg[1] != NUL)
3848 ++arg;
3849 ++arg;
3850 }
3851 if (arg[0] != NUL) /* skip delimiter */
3852 ++arg;
3853 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3854 ++arg;
3855 if (arg[0] != NUL)
3856 return arg;
3857 break;
3858 case CMD_isearch:
3859 case CMD_dsearch:
3860 case CMD_ilist:
3861 case CMD_dlist:
3862 case CMD_ijump:
3863 case CMD_psearch:
3864 case CMD_djump:
3865 case CMD_isplit:
3866 case CMD_dsplit:
3867 arg = skipwhite(skipdigits(arg)); /* skip count */
3868 if (*arg == '/') /* Match regexp, not just whole words */
3869 {
3870 for (++arg; *arg && *arg != '/'; arg++)
3871 if (*arg == '\\' && arg[1] != NUL)
3872 arg++;
3873 if (*arg)
3874 {
3875 arg = skipwhite(arg + 1);
3876
3877 /* Check for trailing illegal characters */
3878 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3879 xp->xp_context = EXPAND_NOTHING;
3880 else
3881 return arg;
3882 }
3883 }
3884 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_autocmd:
3887 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003889 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 case CMD_set:
3892 set_context_in_set_cmd(xp, arg, 0);
3893 break;
3894 case CMD_setglobal:
3895 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3896 break;
3897 case CMD_setlocal:
3898 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3899 break;
3900 case CMD_tag:
3901 case CMD_stag:
3902 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003903 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 case CMD_tselect:
3905 case CMD_stselect:
3906 case CMD_ptselect:
3907 case CMD_tjump:
3908 case CMD_stjump:
3909 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003910 if (*p_wop != NUL)
3911 xp->xp_context = EXPAND_TAGS_LISTFILES;
3912 else
3913 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 xp->xp_pattern = arg;
3915 break;
3916 case CMD_augroup:
3917 xp->xp_context = EXPAND_AUGROUP;
3918 xp->xp_pattern = arg;
3919 break;
3920#ifdef FEAT_SYN_HL
3921 case CMD_syntax:
3922 set_context_in_syntax_cmd(xp, arg);
3923 break;
3924#endif
3925#ifdef FEAT_EVAL
3926 case CMD_let:
3927 case CMD_if:
3928 case CMD_elseif:
3929 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003930 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 case CMD_echo:
3932 case CMD_echon:
3933 case CMD_execute:
3934 case CMD_echomsg:
3935 case CMD_echoerr:
3936 case CMD_call:
3937 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003938 case CMD_cexpr:
3939 case CMD_caddexpr:
3940 case CMD_cgetexpr:
3941 case CMD_lexpr:
3942 case CMD_laddexpr:
3943 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003944 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 break;
3946
3947 case CMD_unlet:
3948 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3949 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 xp->xp_context = EXPAND_USER_VARS;
3952 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003953
3954 if (*xp->xp_pattern == '$')
3955 {
3956 xp->xp_context = EXPAND_ENV_VARS;
3957 ++xp->xp_pattern;
3958 }
3959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 break;
3961
3962 case CMD_function:
3963 case CMD_delfunction:
3964 xp->xp_context = EXPAND_USER_FUNC;
3965 xp->xp_pattern = arg;
3966 break;
3967
3968 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003969 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 break;
3971#endif
3972 case CMD_highlight:
3973 set_context_in_highlight_cmd(xp, arg);
3974 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003975#ifdef FEAT_CSCOPE
3976 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003977 case CMD_lcscope:
3978 case CMD_scscope:
3979 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003980 break;
3981#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003982#ifdef FEAT_SIGNS
3983 case CMD_sign:
3984 set_context_in_sign_cmd(xp, arg);
3985 break;
3986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 case CMD_bdelete:
3988 case CMD_bwipeout:
3989 case CMD_bunload:
3990 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3991 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003992 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 case CMD_buffer:
3994 case CMD_sbuffer:
3995 case CMD_checktime:
3996 xp->xp_context = EXPAND_BUFFERS;
3997 xp->xp_pattern = arg;
3998 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 case CMD_USER:
4001 case CMD_USER_BUF:
4002 if (compl != EXPAND_NOTHING)
4003 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004004 // XFILE: file names are handled above
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004005 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004007#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if (compl == EXPAND_MENUS)
4009 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (compl == EXPAND_COMMANDS)
4012 return arg;
4013 if (compl == EXPAND_MAPPINGS)
4014 return set_context_in_map_cmd(xp, (char_u *)"map",
4015 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004016 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004017 p = arg;
4018 while (*p)
4019 {
4020 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004021 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004022 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004023 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004024 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004025 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 xp->xp_pattern = arg;
4028 }
4029 xp->xp_context = compl;
4030 }
4031 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004032
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 case CMD_map: case CMD_noremap:
4034 case CMD_nmap: case CMD_nnoremap:
4035 case CMD_vmap: case CMD_vnoremap:
4036 case CMD_omap: case CMD_onoremap:
4037 case CMD_imap: case CMD_inoremap:
4038 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004039 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004040 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004041 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004042 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004044 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 case CMD_unmap:
4046 case CMD_nunmap:
4047 case CMD_vunmap:
4048 case CMD_ounmap:
4049 case CMD_iunmap:
4050 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004051 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004052 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004053 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004054 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004056 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004057 case CMD_mapclear:
4058 case CMD_nmapclear:
4059 case CMD_vmapclear:
4060 case CMD_omapclear:
4061 case CMD_imapclear:
4062 case CMD_cmapclear:
4063 case CMD_lmapclear:
4064 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004065 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004066 case CMD_xmapclear:
4067 xp->xp_context = EXPAND_MAPCLEAR;
4068 xp->xp_pattern = arg;
4069 break;
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 case CMD_abbreviate: case CMD_noreabbrev:
4072 case CMD_cabbrev: case CMD_cnoreabbrev:
4073 case CMD_iabbrev: case CMD_inoreabbrev:
4074 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004075 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 case CMD_unabbreviate:
4077 case CMD_cunabbrev:
4078 case CMD_iunabbrev:
4079 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004080 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#ifdef FEAT_MENU
4082 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4083 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4084 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4085 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4086 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4087 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4088 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004089 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 case CMD_tmenu: case CMD_tunmenu:
4091 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4092 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4093#endif
4094
4095 case CMD_colorscheme:
4096 xp->xp_context = EXPAND_COLORS;
4097 xp->xp_pattern = arg;
4098 break;
4099
4100 case CMD_compiler:
4101 xp->xp_context = EXPAND_COMPILER;
4102 xp->xp_pattern = arg;
4103 break;
4104
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004105 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004106 xp->xp_context = EXPAND_OWNSYNTAX;
4107 xp->xp_pattern = arg;
4108 break;
4109
4110 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004111 xp->xp_context = EXPAND_FILETYPE;
4112 xp->xp_pattern = arg;
4113 break;
4114
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004115 case CMD_packadd:
4116 xp->xp_context = EXPAND_PACKADD;
4117 xp->xp_pattern = arg;
4118 break;
4119
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004120#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004122 p = skiptowhite(arg);
4123 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
4125 xp->xp_context = EXPAND_LANGUAGE;
4126 xp->xp_pattern = arg;
4127 }
4128 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004129 {
4130 if ( STRNCMP(arg, "messages", p - arg) == 0
4131 || STRNCMP(arg, "ctype", p - arg) == 0
4132 || STRNCMP(arg, "time", p - arg) == 0)
4133 {
4134 xp->xp_context = EXPAND_LOCALES;
4135 xp->xp_pattern = skipwhite(p);
4136 }
4137 else
4138 xp->xp_context = EXPAND_NOTHING;
4139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 break;
4141#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004142#if defined(FEAT_PROFILE)
4143 case CMD_profile:
4144 set_context_in_profile_cmd(xp, arg);
4145 break;
4146#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004147 case CMD_behave:
4148 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004149 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004150 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004152 case CMD_messages:
4153 xp->xp_context = EXPAND_MESSAGES;
4154 xp->xp_pattern = arg;
4155 break;
4156
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004157#if defined(FEAT_CMDHIST)
4158 case CMD_history:
4159 xp->xp_context = EXPAND_HISTORY;
4160 xp->xp_pattern = arg;
4161 break;
4162#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004163#if defined(FEAT_PROFILE)
4164 case CMD_syntime:
4165 xp->xp_context = EXPAND_SYNTIME;
4166 xp->xp_pattern = arg;
4167 break;
4168#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004169
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004170 case CMD_argdelete:
4171 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4172 arg = xp->xp_pattern + 1;
4173 xp->xp_context = EXPAND_ARGLIST;
4174 xp->xp_pattern = arg;
4175 break;
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#endif /* FEAT_CMDL_COMPL */
4178
4179 default:
4180 break;
4181 }
4182 return NULL;
4183}
4184
4185/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004186 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 *
4188 * Backslashed delimiters after / or ? will be skipped, and commands will
4189 * not be expanded between /'s and ?'s or after "'".
4190 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004191 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 * Returns the "cmd" pointer advanced to beyond the range.
4193 */
4194 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004195skip_range(
4196 char_u *cmd,
4197 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004199 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004201 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004203 if (*cmd == '\\')
4204 {
4205 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4206 ++cmd;
4207 else
4208 break;
4209 }
4210 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
4212 if (*++cmd == NUL && ctx != NULL)
4213 *ctx = EXPAND_NOTHING;
4214 }
4215 else if (*cmd == '/' || *cmd == '?')
4216 {
4217 delim = *cmd++;
4218 while (*cmd != NUL && *cmd != delim)
4219 if (*cmd++ == '\\' && *cmd != NUL)
4220 ++cmd;
4221 if (*cmd == NUL && ctx != NULL)
4222 *ctx = EXPAND_NOTHING;
4223 }
4224 if (*cmd != NUL)
4225 ++cmd;
4226 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004227
4228 /* Skip ":" and white space. */
4229 while (*cmd == ':')
4230 cmd = skipwhite(cmd + 1);
4231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return cmd;
4233}
4234
4235/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004236 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 *
4238 * Set ptr to the next character after the part that was interpreted.
4239 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004240 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 *
4242 * Return MAXLNUM when no Ex address was found.
4243 */
4244 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004245get_address(
4246 exarg_T *eap UNUSED,
4247 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004248 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004249 int skip, // only skip the address, don't use it
4250 int silent, // no errors or side effects
4251 int to_other_file, // flag: may jump to other file
4252 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004254 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 int c;
4256 int i;
4257 long n;
4258 char_u *cmd;
4259 pos_T pos;
4260 pos_T *fp;
4261 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004262 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 cmd = skipwhite(*ptr);
4265 lnum = MAXLNUM;
4266 do
4267 {
4268 switch (*cmd)
4269 {
4270 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 ++cmd;
4272 switch (addr_type)
4273 {
4274 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004275 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 lnum = curwin->w_cursor.lnum;
4277 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004278 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004279 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004280 break;
4281 case ADDR_ARGUMENTS:
4282 lnum = curwin->w_arg_idx + 1;
4283 break;
4284 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004285 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 lnum = curbuf->b_fnum;
4287 break;
4288 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004289 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004290 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004291 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004292 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004293 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004294 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004295 cmd = NULL;
4296 goto error;
4297 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004298 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004299#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004300 lnum = qf_get_cur_idx(eap);
4301#endif
4302 break;
4303 case ADDR_QUICKFIX_VALID:
4304#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004305 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004306#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004307 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004308 }
4309 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 ++cmd;
4313 switch (addr_type)
4314 {
4315 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004316 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 lnum = curbuf->b_ml.ml_line_count;
4318 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004319 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004320 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004321 break;
4322 case ADDR_ARGUMENTS:
4323 lnum = ARGCOUNT;
4324 break;
4325 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004326 buf = lastbuf;
4327 while (buf->b_ml.ml_mfp == NULL)
4328 {
4329 if (buf->b_prev == NULL)
4330 break;
4331 buf = buf->b_prev;
4332 }
4333 lnum = buf->b_fnum;
4334 break;
4335 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 lnum = lastbuf->b_fnum;
4337 break;
4338 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004339 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004340 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004341 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004342 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004343 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004344 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004345 cmd = NULL;
4346 goto error;
4347 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004348 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004349#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004350 lnum = qf_get_size(eap);
4351 if (lnum == 0)
4352 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004353#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004354 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004355 case ADDR_QUICKFIX_VALID:
4356#ifdef FEAT_QUICKFIX
4357 lnum = qf_get_valid_size(eap);
4358 if (lnum == 0)
4359 lnum = 1;
4360#endif
4361 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004362 }
4363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
4365 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 if (*++cmd == NUL)
4367 {
4368 cmd = NULL;
4369 goto error;
4370 }
4371 if (addr_type != ADDR_LINES)
4372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004373 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004374 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004375 goto error;
4376 }
4377 if (skip)
4378 ++cmd;
4379 else
4380 {
4381 /* Only accept a mark in another file when it is
4382 * used by itself: ":'M". */
4383 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4384 ++cmd;
4385 if (fp == (pos_T *)-1)
4386 /* Jumped to another file. */
4387 lnum = curwin->w_cursor.lnum;
4388 else
4389 {
4390 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
4392 cmd = NULL;
4393 goto error;
4394 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 lnum = fp->lnum;
4396 }
4397 }
4398 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400 case '/':
4401 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 c = *cmd++;
4403 if (addr_type != ADDR_LINES)
4404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004405 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004406 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 goto error;
4408 }
4409 if (skip) /* skip "/pat/" */
4410 {
4411 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4412 if (*cmd == c)
4413 ++cmd;
4414 }
4415 else
4416 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004417 int flags;
4418
4419 pos = curwin->w_cursor; // save curwin->w_cursor
4420
4421 // When '/' or '?' follows another address, start from
4422 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 if (lnum != MAXLNUM)
4424 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004425
4426 // Start a forward search at the end of the line (unless
4427 // before the first line).
4428 // Start a backward search at the start of the line.
4429 // This makes sure we never match in the current
4430 // line, and can match anywhere in the
4431 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004432 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 curwin->w_cursor.col = MAXCOL;
4434 else
4435 curwin->w_cursor.col = 0;
4436 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004437 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4438 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004439 {
4440 curwin->w_cursor = pos;
4441 cmd = NULL;
4442 goto error;
4443 }
4444 lnum = curwin->w_cursor.lnum;
4445 curwin->w_cursor = pos;
4446 /* adjust command string pointer */
4447 cmd += searchcmdlen;
4448 }
4449 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004452 ++cmd;
4453 if (addr_type != ADDR_LINES)
4454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004455 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004456 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 goto error;
4458 }
4459 if (*cmd == '&')
4460 i = RE_SUBST;
4461 else if (*cmd == '?' || *cmd == '/')
4462 i = RE_SEARCH;
4463 else
4464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004465 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 cmd = NULL;
4467 goto error;
4468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 if (!skip)
4471 {
4472 /*
4473 * When search follows another address, start from
4474 * there.
4475 */
4476 if (lnum != MAXLNUM)
4477 pos.lnum = lnum;
4478 else
4479 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 /*
4482 * Start the search just like for the above
4483 * do_search().
4484 */
4485 if (*cmd != '?')
4486 pos.col = MAXCOL;
4487 else
4488 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004489 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004490 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 *cmd == '?' ? BACKWARD : FORWARD,
4492 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004493 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 lnum = pos.lnum;
4495 else
4496 {
4497 cmd = NULL;
4498 goto error;
4499 }
4500 }
4501 ++cmd;
4502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4506 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
4509 for (;;)
4510 {
4511 cmd = skipwhite(cmd);
4512 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4513 break;
4514
4515 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 {
4517 switch (addr_type)
4518 {
4519 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004520 case ADDR_OTHER:
4521 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004522 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 break;
4524 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004525 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 break;
4527 case ADDR_ARGUMENTS:
4528 lnum = curwin->w_arg_idx + 1;
4529 break;
4530 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004531 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 lnum = curbuf->b_fnum;
4533 break;
4534 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004535 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004536 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004537 case ADDR_TABS_RELATIVE:
4538 lnum = 1;
4539 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004540 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004541#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004542 lnum = qf_get_cur_idx(eap);
4543#endif
4544 break;
4545 case ADDR_QUICKFIX_VALID:
4546#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004547 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004548#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004549 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004550 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004551 case ADDR_UNSIGNED:
4552 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004553 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 }
4555 }
4556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (VIM_ISDIGIT(*cmd))
4558 i = '+'; /* "number" is same as "+number" */
4559 else
4560 i = *cmd++;
4561 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4562 n = 1;
4563 else
4564 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004565
4566 if (addr_type == ADDR_TABS_RELATIVE)
4567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004568 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004569 cmd = NULL;
4570 goto error;
4571 }
4572 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004573 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004574 lnum = compute_buffer_local_count(
4575 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004577 {
4578#ifdef FEAT_FOLDING
4579 /* Relative line addressing, need to adjust for folded lines
4580 * now, but only do it after the first address. */
4581 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4582 && address_count >= 2)
4583 (void)hasFolding(lnum, NULL, &lnum);
4584#endif
4585 if (i == '-')
4586 lnum -= n;
4587 else
4588 lnum += n;
4589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 } while (*cmd == '/' || *cmd == '?');
4592
4593error:
4594 *ptr = cmd;
4595 return lnum;
4596}
4597
4598/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004599 * Get flags from an Ex command argument.
4600 */
4601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004602get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004603{
4604 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4605 {
4606 if (*eap->arg == 'l')
4607 eap->flags |= EXFLAG_LIST;
4608 else if (*eap->arg == 'p')
4609 eap->flags |= EXFLAG_PRINT;
4610 else
4611 eap->flags |= EXFLAG_NR;
4612 eap->arg = skipwhite(eap->arg + 1);
4613 }
4614}
4615
4616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 * Function called for command which is Not Implemented. NI!
4618 */
4619 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004620ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621{
4622 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004623 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624}
4625
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004626#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627/*
4628 * Function called for script command which is Not Implemented. NI!
4629 * Skips over ":perl <<EOF" constructs.
4630 */
4631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004632ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
4634 if (!eap->skip)
4635 ex_ni(eap);
4636 else
4637 vim_free(script_get(eap, eap->arg));
4638}
4639#endif
4640
4641/*
4642 * Check range in Ex command for validity.
4643 * Return NULL when valid, error message when invalid.
4644 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004645 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004646invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004648 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if ( eap->line1 < 0
4651 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004652 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004653 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654
4655 if (eap->argt & RANGE)
4656 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004657 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004658 {
4659 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004660 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004661#ifdef FEAT_DIFF
4662 + (eap->cmdidx == CMD_diffget)
4663#endif
4664 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004665 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004666 break;
4667 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004668 /* add 1 if ARGCOUNT is 0 */
4669 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004670 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004671 break;
4672 case ADDR_BUFFERS:
4673 if (eap->line1 < firstbuf->b_fnum
4674 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004675 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004676 break;
4677 case ADDR_LOADED_BUFFERS:
4678 buf = firstbuf;
4679 while (buf->b_ml.ml_mfp == NULL)
4680 {
4681 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004682 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004683 buf = buf->b_next;
4684 }
4685 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004686 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004687 buf = lastbuf;
4688 while (buf->b_ml.ml_mfp == NULL)
4689 {
4690 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004691 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004692 buf = buf->b_prev;
4693 }
4694 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004695 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004696 break;
4697 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004698 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004699 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004700 break;
4701 case ADDR_TABS:
4702 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004703 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004704 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004705 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004706 case ADDR_OTHER:
4707 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004708 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004709 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004710#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004711 // No error for value that is too big, will use the last entry.
4712 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004713 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004714#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004715 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004716 case ADDR_QUICKFIX_VALID:
4717#ifdef FEAT_QUICKFIX
4718 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4719 || eap->line2 < 0)
4720 return _(e_invrange);
4721#endif
4722 break;
4723 case ADDR_UNSIGNED:
4724 if (eap->line2 < 0)
4725 return _(e_invrange);
4726 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004727 case ADDR_NONE:
4728 // Will give an error elsewhere.
4729 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004730 }
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return NULL;
4733}
4734
4735/*
4736 * Correct the range for zero line number, if required.
4737 */
4738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004739correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
4741 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4742 {
4743 if (eap->line1 == 0)
4744 eap->line1 = 1;
4745 if (eap->line2 == 0)
4746 eap->line2 = 1;
4747 }
4748}
4749
Bram Moolenaar748bf032005-02-02 23:04:36 +00004750#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004751/*
4752 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4753 * pattern. Otherwise return eap->arg.
4754 */
4755 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004757{
4758 char_u *p = eap->arg;
4759
Bram Moolenaara37420f2006-02-04 22:37:47 +00004760 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4761 || eap->cmdidx == CMD_vimgrepadd
4762 || eap->cmdidx == CMD_lvimgrepadd
4763 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004765 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004766 if (p == NULL)
4767 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004768 }
4769 return p;
4770}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004771
4772/*
4773 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4774 * in the command line, so that things like % get expanded.
4775 */
4776 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004777replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004778{
4779 char_u *new_cmdline;
4780 char_u *program;
4781 char_u *pos;
4782 char_u *ptr;
4783 int len;
4784 int i;
4785
4786 /*
4787 * Don't do it when ":vimgrep" is used for ":grep".
4788 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004789 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4790 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4791 || eap->cmdidx == CMD_grepadd
4792 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004793 && !grep_internal(eap->cmdidx))
4794 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004795 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4796 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004797 {
4798 if (*curbuf->b_p_gp == NUL)
4799 program = p_gp;
4800 else
4801 program = curbuf->b_p_gp;
4802 }
4803 else
4804 {
4805 if (*curbuf->b_p_mp == NUL)
4806 program = p_mp;
4807 else
4808 program = curbuf->b_p_mp;
4809 }
4810
4811 p = skipwhite(p);
4812
4813 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4814 {
4815 /* replace $* by given arguments */
4816 i = 1;
4817 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4818 ++i;
4819 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004820 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004821 if (new_cmdline == NULL)
4822 return NULL; /* out of memory */
4823 ptr = new_cmdline;
4824 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4825 {
4826 i = (int)(pos - program);
4827 STRNCPY(ptr, program, i);
4828 STRCPY(ptr += i, p);
4829 ptr += len;
4830 program = pos + 2;
4831 }
4832 STRCPY(ptr, program);
4833 }
4834 else
4835 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004836 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004837 if (new_cmdline == NULL)
4838 return NULL; /* out of memory */
4839 STRCPY(new_cmdline, program);
4840 STRCAT(new_cmdline, " ");
4841 STRCAT(new_cmdline, p);
4842 }
4843 msg_make(p);
4844
4845 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4846 vim_free(*cmdlinep);
4847 *cmdlinep = new_cmdline;
4848 p = new_cmdline;
4849 }
4850 return p;
4851}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004852#endif
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854/*
4855 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004856 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 * Return FAIL for failure, OK otherwise.
4858 */
4859 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004860expand_filename(
4861 exarg_T *eap,
4862 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004863 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 int has_wildcards; /* need to expand wildcards */
4866 char_u *repl;
4867 int srclen;
4868 char_u *p;
4869 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004870 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
Bram Moolenaar748bf032005-02-02 23:04:36 +00004872#ifdef FEAT_QUICKFIX
4873 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4874 p = skip_grep_pat(eap);
4875#else
4876 p = eap->arg;
4877#endif
4878
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 /*
4880 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4881 * the file name contains a wildcard it should not cause expanding.
4882 * (it will be expanded anyway if there is a wildcard before replacing).
4883 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004884 has_wildcards = mch_has_wildcard(p);
4885 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004887#ifdef FEAT_EVAL
4888 /* Skip over `=expr`, wildcards in it are not expanded. */
4889 if (p[0] == '`' && p[1] == '=')
4890 {
4891 p += 2;
4892 (void)skip_expr(&p);
4893 if (*p == '`')
4894 ++p;
4895 continue;
4896 }
4897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 /*
4899 * Quick check if this cannot be the start of a special string.
4900 * Also removes backslash before '%', '#' and '<'.
4901 */
4902 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4903 {
4904 ++p;
4905 continue;
4906 }
4907
4908 /*
4909 * Try to find a match at this position.
4910 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004911 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4912 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (*errormsgp != NULL) /* error detected */
4914 return FAIL;
4915 if (repl == NULL) /* no match found */
4916 {
4917 p += srclen;
4918 continue;
4919 }
4920
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004921 /* Wildcards won't be expanded below, the replacement is taken
4922 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4923 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4924 {
4925 char_u *l = repl;
4926
4927 repl = expand_env_save(repl);
4928 vim_free(l);
4929 }
4930
Bram Moolenaar63b92542007-03-27 14:57:09 +00004931 /* Need to escape white space et al. with a backslash.
4932 * Don't do this for:
4933 * - replacement that already has been escaped: "##"
4934 * - shell commands (may have to use quotes instead).
4935 * - non-unix systems when there is a single argument (spaces don't
4936 * separate arguments then).
4937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004939 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 && eap->cmdidx != CMD_grep
4942 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004943 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004944 && eap->cmdidx != CMD_lgrep
4945 && eap->cmdidx != CMD_lgrepadd
4946 && eap->cmdidx != CMD_lmake
4947 && eap->cmdidx != CMD_make
4948 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#ifndef UNIX
4950 && !(eap->argt & NOSPC)
4951#endif
4952 )
4953 {
4954 char_u *l;
4955#ifdef BACKSLASH_IN_FILENAME
4956 /* Don't escape a backslash here, because rem_backslash() doesn't
4957 * remove it later. */
4958 static char_u *nobslash = (char_u *)" \t\"|";
4959# define ESCAPE_CHARS nobslash
4960#else
4961# define ESCAPE_CHARS escape_chars
4962#endif
4963
4964 for (l = repl; *l; ++l)
4965 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4966 {
4967 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4968 if (l != NULL)
4969 {
4970 vim_free(repl);
4971 repl = l;
4972 }
4973 break;
4974 }
4975 }
4976
4977 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004978 if ((eap->usefilter || eap->cmdidx == CMD_bang
4979 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004980 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 char_u *l;
4983
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004984 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (l != NULL)
4986 {
4987 vim_free(repl);
4988 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 }
4991
4992 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4993 vim_free(repl);
4994 if (p == NULL)
4995 return FAIL;
4996 }
4997
4998 /*
4999 * One file argument: Expand wildcards.
5000 * Don't do this with ":r !command" or ":w !command".
5001 */
5002 if ((eap->argt & NOSPC) && !eap->usefilter)
5003 {
5004 /*
5005 * May do this twice:
5006 * 1. Replace environment variables.
5007 * 2. Replace any other wildcards, remove backslashes.
5008 */
5009 for (n = 1; n <= 2; ++n)
5010 {
5011 if (n == 2)
5012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 /*
5014 * Halve the number of backslashes (this is Vi compatible).
5015 * For Unix and OS/2, when wildcards are expanded, this is
5016 * done by ExpandOne() below.
5017 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005018#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (!has_wildcards)
5020#endif
5021 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023
5024 if (has_wildcards)
5025 {
5026 if (n == 1)
5027 {
5028 /*
5029 * First loop: May expand environment variables. This
5030 * can be done much faster with expand_env() than with
5031 * something else (e.g., calling a shell).
5032 * After expanding environment variables, check again
5033 * if there are still wildcards present.
5034 */
5035 if (vim_strchr(eap->arg, '$') != NULL
5036 || vim_strchr(eap->arg, '~') != NULL)
5037 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005038 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005039 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 has_wildcards = mch_has_wildcard(NameBuff);
5041 p = NameBuff;
5042 }
5043 else
5044 p = NULL;
5045 }
5046 else /* n == 2 */
5047 {
5048 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005049 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
5051 ExpandInit(&xpc);
5052 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005053 if (p_wic)
5054 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005056 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (p == NULL)
5058 return FAIL;
5059 }
5060 if (p != NULL)
5061 {
5062 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5063 p, cmdlinep);
5064 if (n == 2) /* p came from ExpandOne() */
5065 vim_free(p);
5066 }
5067 }
5068 }
5069 }
5070 return OK;
5071}
5072
5073/*
5074 * Replace part of the command line, keeping eap->cmd, eap->arg and
5075 * eap->nextcmd correct.
5076 * "src" points to the part that is to be replaced, of length "srclen".
5077 * "repl" is the replacement string.
5078 * Returns a pointer to the character after the replaced string.
5079 * Returns NULL for failure.
5080 */
5081 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005082repl_cmdline(
5083 exarg_T *eap,
5084 char_u *src,
5085 int srclen,
5086 char_u *repl,
5087 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 int len;
5090 int i;
5091 char_u *new_cmdline;
5092
5093 /*
5094 * The new command line is build in new_cmdline[].
5095 * First allocate it.
5096 * Careful: a "+cmd" argument may have been NUL terminated.
5097 */
5098 len = (int)STRLEN(repl);
5099 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005100 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005102 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 return NULL; /* out of memory! */
5104
5105 /*
5106 * Copy the stuff before the expanded part.
5107 * Copy the expanded stuff.
5108 * Copy what came after the expanded part.
5109 * Copy the next commands, if there are any.
5110 */
5111 i = (int)(src - *cmdlinep); /* length of part before match */
5112 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 mch_memmove(new_cmdline + i, repl, (size_t)len);
5115 i += len; /* remember the end of the string */
5116 STRCPY(new_cmdline + i, src + srclen);
5117 src = new_cmdline + i; /* remember where to continue */
5118
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005119 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 i = (int)STRLEN(new_cmdline) + 1;
5122 STRCPY(new_cmdline + i, eap->nextcmd);
5123 eap->nextcmd = new_cmdline + i;
5124 }
5125 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5126 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5127 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5128 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5129 vim_free(*cmdlinep);
5130 *cmdlinep = new_cmdline;
5131
5132 return src;
5133}
5134
5135/*
5136 * Check for '|' to separate commands and '"' to start comments.
5137 */
5138 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005139separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 char_u *p;
5142
Bram Moolenaar86b68352004-12-27 21:59:20 +00005143#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005144 p = skip_grep_pat(eap);
5145#else
5146 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005147#endif
5148
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005149 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 if (*p == Ctrl_V)
5152 {
5153 if (eap->argt & (USECTRLV | XFILE))
5154 ++p; /* skip CTRL-V and next char */
5155 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005156 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005157 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (*p == NUL) /* stop at NUL after CTRL-V */
5159 break;
5160 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005161
5162#ifdef FEAT_EVAL
5163 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005164 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005165 {
5166 p += 2;
5167 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005168 }
5169#endif
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 /* Check for '"': start of comment or '|': next command */
5172 /* :@" and :*" do not start a comment!
5173 * :redir @" doesn't either. */
5174 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5175 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5176 || p != eap->arg)
5177 && (eap->cmdidx != CMD_redir
5178 || p != eap->arg + 1 || p[-1] != '@'))
5179 || *p == '|' || *p == '\n')
5180 {
5181 /*
5182 * We remove the '\' before the '|', unless USECTRLV is used
5183 * AND 'b' is present in 'cpoptions'.
5184 */
5185 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5186 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5187 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005188 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 --p;
5190 }
5191 else
5192 {
5193 eap->nextcmd = check_nextcmd(p);
5194 *p = NUL;
5195 break;
5196 }
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005199
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5201 del_trailing_spaces(eap->arg);
5202}
5203
5204/*
5205 * get + command from ex argument
5206 */
5207 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005208getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 char_u *arg = *argp;
5211 char_u *command = NULL;
5212
5213 if (*arg == '+') /* +[command] */
5214 {
5215 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005216 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 command = dollar_command;
5218 else
5219 {
5220 command = arg;
5221 arg = skip_cmd_arg(command, TRUE);
5222 if (*arg != NUL)
5223 *arg++ = NUL; /* terminate command with NUL */
5224 }
5225
5226 arg = skipwhite(arg); /* skip over spaces */
5227 *argp = arg;
5228 }
5229 return command;
5230}
5231
5232/*
5233 * Find end of "+command" argument. Skip over "\ " and "\\".
5234 */
5235 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005236skip_cmd_arg(
5237 char_u *p,
5238 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 while (*p && !vim_isspace(*p))
5241 {
5242 if (*p == '\\' && p[1] != NUL)
5243 {
5244 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005245 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 else
5247 ++p;
5248 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005249 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251 return p;
5252}
5253
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005254 int
5255get_bad_opt(char_u *p, exarg_T *eap)
5256{
5257 if (STRICMP(p, "keep") == 0)
5258 eap->bad_char = BAD_KEEP;
5259 else if (STRICMP(p, "drop") == 0)
5260 eap->bad_char = BAD_DROP;
5261 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5262 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005263 else
5264 return FAIL;
5265 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005266}
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268/*
5269 * Get "++opt=arg" argument.
5270 * Return FAIL or OK.
5271 */
5272 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005273getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 char_u *arg = eap->arg + 2;
5276 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005277 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
5280 /* ":edit ++[no]bin[ary] file" */
5281 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5282 {
5283 if (*arg == 'n')
5284 {
5285 arg += 2;
5286 eap->force_bin = FORCE_NOBIN;
5287 }
5288 else
5289 eap->force_bin = FORCE_BIN;
5290 if (!checkforcmd(&arg, "binary", 3))
5291 return FAIL;
5292 eap->arg = skipwhite(arg);
5293 return OK;
5294 }
5295
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005296 /* ":read ++edit file" */
5297 if (STRNCMP(arg, "edit", 4) == 0)
5298 {
5299 eap->read_edit = TRUE;
5300 eap->arg = skipwhite(arg + 4);
5301 return OK;
5302 }
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (STRNCMP(arg, "ff", 2) == 0)
5305 {
5306 arg += 2;
5307 pp = &eap->force_ff;
5308 }
5309 else if (STRNCMP(arg, "fileformat", 10) == 0)
5310 {
5311 arg += 10;
5312 pp = &eap->force_ff;
5313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 else if (STRNCMP(arg, "enc", 3) == 0)
5315 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005316 if (STRNCMP(arg, "encoding", 8) == 0)
5317 arg += 8;
5318 else
5319 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 pp = &eap->force_enc;
5321 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005322 else if (STRNCMP(arg, "bad", 3) == 0)
5323 {
5324 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005325 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
5328 if (pp == NULL || *arg != '=')
5329 return FAIL;
5330
5331 ++arg;
5332 *pp = (int)(arg - eap->cmd);
5333 arg = skip_cmd_arg(arg, FALSE);
5334 eap->arg = skipwhite(arg);
5335 *arg = NUL;
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (pp == &eap->force_ff)
5338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5340 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005341 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005343 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
5345 /* Make 'fileencoding' lower case. */
5346 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5347 *p = TOLOWER_ASC(*p);
5348 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005349 else
5350 {
5351 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5352 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005353 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005354 return FAIL;
5355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 return OK;
5358}
5359
5360/*
5361 * ":abbreviate" and friends.
5362 */
5363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005364ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 do_exmap(eap, TRUE); /* almost the same as mapping */
5367}
5368
5369/*
5370 * ":map" and friends.
5371 */
5372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005373ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
5375 /*
5376 * If we are sourcing .exrc or .vimrc in current directory we
5377 * print the mappings for security reasons.
5378 */
5379 if (secure)
5380 {
5381 secure = 2;
5382 msg_outtrans(eap->cmd);
5383 msg_putchar('\n');
5384 }
5385 do_exmap(eap, FALSE);
5386}
5387
5388/*
5389 * ":unmap" and friends.
5390 */
5391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005392ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 do_exmap(eap, FALSE);
5395}
5396
5397/*
5398 * ":mapclear" and friends.
5399 */
5400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5404}
5405
5406/*
5407 * ":abclear" and friends.
5408 */
5409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5413}
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005419 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 * directory for security reasons.
5421 */
5422 if (secure)
5423 {
5424 secure = 2;
5425 eap->errmsg = e_curdir;
5426 }
5427 else if (eap->cmdidx == CMD_autocmd)
5428 do_autocmd(eap->arg, eap->forceit);
5429 else
5430 do_augroup(eap->arg, eap->forceit);
5431}
5432
5433/*
5434 * ":doautocmd": Apply the automatic commands to the current buffer.
5435 */
5436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005437ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005439 char_u *arg = eap->arg;
5440 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005441 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005442
Bram Moolenaar1610d052016-06-09 22:53:01 +02005443 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5444 /* Only when there is no <nomodeline>. */
5445 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005446 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449/*
5450 * :[N]bunload[!] [N] [bufname] unload buffer
5451 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5452 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5453 */
5454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005455ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005457 if (NOT_IN_POPUP_WINDOW)
5458 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 eap->errmsg = do_bufdel(
5460 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5461 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5462 : DOBUF_UNLOAD, eap->arg,
5463 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5464}
5465
5466/*
5467 * :[N]buffer [N] to buffer N
5468 * :[N]sbuffer [N] to buffer N
5469 */
5470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005471ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005473 if (NOT_IN_POPUP_WINDOW)
5474 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (*eap->arg)
5476 eap->errmsg = e_trailing;
5477 else
5478 {
5479 if (eap->addr_count == 0) /* default is current buffer */
5480 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5481 else
5482 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005483 if (eap->do_ecmd_cmd != NULL)
5484 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486}
5487
5488/*
5489 * :[N]bmodified [N] to next mod. buffer
5490 * :[N]sbmodified [N] to next mod. buffer
5491 */
5492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 goto_buffer(eap, DOBUF_MOD, 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 next buffer
5502 * :[N]sbnext [N] split and to next buffer
5503 */
5504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005508 if (eap->do_ecmd_cmd != NULL)
5509 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510}
5511
5512/*
5513 * :[N]bNext [N] to previous buffer
5514 * :[N]bprevious [N] to previous buffer
5515 * :[N]sbNext [N] split and to previous buffer
5516 * :[N]sbprevious [N] split and to previous buffer
5517 */
5518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005522 if (eap->do_ecmd_cmd != NULL)
5523 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524}
5525
5526/*
5527 * :brewind to first buffer
5528 * :bfirst to first buffer
5529 * :sbrewind split and to first buffer
5530 * :sbfirst split and to first buffer
5531 */
5532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 goto_buffer(eap, DOBUF_FIRST, FORWARD, 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}
5539
5540/*
5541 * :blast to last buffer
5542 * :sblast split and to last buffer
5543 */
5544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005545ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005548 if (eap->do_ecmd_cmd != NULL)
5549 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 return (c == NUL || c == '|' || c == '"' || c == '\n');
5556}
5557
5558#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5559 || defined(PROTO)
5560/*
5561 * Return the next command, after the first '|' or '\n'.
5562 * Return NULL if not found.
5563 */
5564 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 while (*p != '|' && *p != '\n')
5568 {
5569 if (*p == NUL)
5570 return NULL;
5571 ++p;
5572 }
5573 return (p + 1);
5574}
5575#endif
5576
5577/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005578 * Check if *p is a separator between Ex commands, skipping over white space.
5579 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 */
5581 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005582check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005584 char_u *s = skipwhite(p);
5585
5586 if (*s == '|' || *s == '\n')
5587 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 else
5589 return NULL;
5590}
5591
5592/*
5593 * - if there are more files to edit
5594 * - and this is the last window
5595 * - and forceit not used
5596 * - and not repeated twice on a row
5597 * return FAIL and give error message if 'message' TRUE
5598 * return OK otherwise
5599 */
5600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005601check_more(
5602 int message, /* when FALSE check only, no messages */
5603 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5606
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005607 if (!forceit && only_one_window()
5608 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
5610 if (message)
5611 {
5612#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5613 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5614 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005615 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005617 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5618 NGETTEXT("%d more file to edit. Quit anyway?",
5619 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5621 return OK;
5622 return FAIL;
5623 }
5624#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005625 semsg(NGETTEXT("E173: %d more file to edit",
5626 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 quitmore = 2; /* next try to quit is allowed */
5628 }
5629 return FAIL;
5630 }
5631 return OK;
5632}
5633
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005634#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635/*
5636 * Function given to ExpandGeneric() to obtain the list of command names.
5637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 return cmdnames[idx].cmd_name;
5644}
5645#endif
5646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
Bram Moolenaare6850792010-05-14 15:28:44 +02005650 if (*eap->arg == NUL)
5651 {
5652#ifdef FEAT_EVAL
5653 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5654 char_u *p = NULL;
5655
5656 if (expr != NULL)
5657 {
5658 ++emsg_off;
5659 p = eval_to_string(expr, NULL, FALSE);
5660 --emsg_off;
5661 vim_free(expr);
5662 }
5663 if (p != NULL)
5664 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005665 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005666 vim_free(p);
5667 }
5668 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005669 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005670#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005671 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005672#endif
5673 }
5674 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005675 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005676
5677#ifdef FEAT_VTP
5678 else if (has_vtp_working())
5679 {
5680 // background color change requires clear + redraw
5681 update_screen(CLEAR);
5682 redrawcmd();
5683 }
5684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
5686
5687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005691 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 do_highlight(eap->arg, eap->forceit, FALSE);
5693}
5694
5695
5696/*
5697 * Call this function if we thought we were going to exit, but we won't
5698 * (because of an error). May need to restore the terminal mode.
5699 */
5700 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005701not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 exiting = FALSE;
5704 settmode(TMODE_RAW);
5705}
5706
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005707 static int
5708before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5709{
5710 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5711
5712 /* Bail out when autocommands closed the window.
5713 * Refuse to quit when the buffer in the last window is being closed (can
5714 * only happen in autocommands). */
5715 if (!win_valid(wp)
5716 || curbuf_locked()
5717 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5718 return TRUE;
5719
5720 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5721 {
5722 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5723 /* Refuse to quit when locked or when the buffer in the last window is
5724 * being closed (can only happen in autocommands). */
5725 if (curbuf_locked()
5726 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5727 return TRUE;
5728 }
5729
5730 return FALSE;
5731}
5732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005734 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005735 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 */
5737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005740 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742#ifdef FEAT_CMDWIN
5743 if (cmdwin_type != 0)
5744 {
5745 cmdwin_result = Ctrl_C;
5746 return;
5747 }
5748#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005749 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005750 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005751 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005752 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005753 return;
5754 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005755 if (eap->addr_count > 0)
5756 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005757 int wnr = eap->line2;
5758
5759 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5760 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005761 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005762 }
5763 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005764 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005765
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005766 /* Refuse to quit when locked. */
5767 if (curbuf_locked())
5768 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005769
5770 /* Trigger QuitPre and maybe ExitPre */
5771 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005772 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774#ifdef FEAT_NETBEANS_INTG
5775 netbeansForcedQuit = eap->forceit;
5776#endif
5777
5778 /*
5779 * If there are more files or windows we won't exit.
5780 */
5781 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5782 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005783 if ((!buf_hide(wp->w_buffer)
5784 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005785 | (eap->forceit ? CCGD_FORCEIT : 0)
5786 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005788 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 not_exiting();
5791 }
5792 else
5793 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005794 /* quit last window
5795 * Note: only_one_window() returns true, even so a help window is
5796 * still open. In that case only quit, if no address has been
5797 * specified. Example:
5798 * :h|wincmd w|1q - don't quit
5799 * :h|wincmd w|q - quit
5800 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005801 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005803 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005804#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005808 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 }
5810}
5811
5812/*
5813 * ":cquit".
5814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005816ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 getout(1); /* this does not always pass on the exit code to the Manx
5819 compiler. why? */
5820}
5821
5822/*
5823 * ":qall": try to quit all windows
5824 */
5825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005826ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828# ifdef FEAT_CMDWIN
5829 if (cmdwin_type != 0)
5830 {
5831 if (eap->forceit)
5832 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5833 else
5834 cmdwin_result = K_XF2;
5835 return;
5836 }
5837# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005838
5839 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005840 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005841 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005842 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005843 return;
5844 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005845
5846 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005847 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005850 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 getout(0);
5852 not_exiting();
5853}
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855/*
5856 * ":close": close current window, unless it is the last one
5857 */
5858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005859ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005861 win_T *win;
5862 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005863#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005865 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005867#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005868 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005869 {
5870 if (eap->addr_count == 0)
5871 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005872 else
5873 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005874 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005875 {
5876 winnr++;
5877 if (winnr == eap->line2)
5878 break;
5879 }
5880 if (win == NULL)
5881 win = lastwin;
5882 ex_win_close(eap->forceit, win, NULL);
5883 }
5884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885}
5886
Bram Moolenaar4033c552017-09-16 20:54:51 +02005887#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005888/*
5889 * ":pclose": Close any preview window.
5890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005892ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893{
5894 win_T *win;
5895
Bram Moolenaar29323592016-07-24 22:04:11 +02005896 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005897 if (win->w_p_pvw)
5898 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005899 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005900 break;
5901 }
5902}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005903#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005904
Bram Moolenaarf740b292006-02-16 22:11:02 +00005905/*
5906 * Close window "win" and take care of handling closing the last window for a
5907 * modified buffer.
5908 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005910ex_win_close(
5911 int forceit,
5912 win_T *win,
5913 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
5915 int need_hide;
5916 buf_T *buf = win->w_buffer;
5917
5918 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005919 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005921#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 if ((p_confirm || cmdmod.confirm) && p_write)
5923 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005924 bufref_T bufref;
5925
5926 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005928 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 return;
5930 need_hide = FALSE;
5931 }
5932 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005935 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 return;
5937 }
5938 }
5939
Bram Moolenaar4033c552017-09-16 20:54:51 +02005940#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005942#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005945 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005946 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005947 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005948 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949}
5950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005952 * Handle the argument for a tabpage related ex command.
5953 * Returns a tabpage number.
5954 * When an error is encountered then eap->errmsg is set.
5955 */
5956 static int
5957get_tabpage_arg(exarg_T *eap)
5958{
5959 int tab_number;
5960 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5961
5962 if (eap->arg && *eap->arg != NUL)
5963 {
5964 char_u *p = eap->arg;
5965 char_u *p_save;
5966 int relative = 0; /* argument +N/-N means: go to N places to the
5967 * right/left relative to the current position. */
5968
5969 if (*p == '-')
5970 {
5971 relative = -1;
5972 p++;
5973 }
5974 else if (*p == '+')
5975 {
5976 relative = 1;
5977 p++;
5978 }
5979
5980 p_save = p;
5981 tab_number = getdigits(&p);
5982
5983 if (relative == 0)
5984 {
5985 if (STRCMP(p, "$") == 0)
5986 tab_number = LAST_TAB_NR;
5987 else if (p == p_save || *p_save == '-' || *p != NUL
5988 || tab_number > LAST_TAB_NR)
5989 {
5990 /* No numbers as argument. */
5991 eap->errmsg = e_invarg;
5992 goto theend;
5993 }
5994 }
5995 else
5996 {
5997 if (*p_save == NUL)
5998 tab_number = 1;
5999 else if (p == p_save || *p_save == '-' || *p != NUL
6000 || tab_number == 0)
6001 {
6002 /* No numbers as argument. */
6003 eap->errmsg = e_invarg;
6004 goto theend;
6005 }
6006 tab_number = tab_number * relative + tabpage_index(curtab);
6007 if (!unaccept_arg0 && relative == -1)
6008 --tab_number;
6009 }
6010 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6011 eap->errmsg = e_invarg;
6012 }
6013 else if (eap->addr_count > 0)
6014 {
6015 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006016 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006017 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006018 tab_number = 0;
6019 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006020 else
6021 {
6022 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006023 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006024 {
6025 --tab_number;
6026 if (tab_number < unaccept_arg0)
6027 eap->errmsg = e_invarg;
6028 }
6029 }
6030 }
6031 else
6032 {
6033 switch (eap->cmdidx)
6034 {
6035 case CMD_tabnext:
6036 tab_number = tabpage_index(curtab) + 1;
6037 if (tab_number > LAST_TAB_NR)
6038 tab_number = 1;
6039 break;
6040 case CMD_tabmove:
6041 tab_number = LAST_TAB_NR;
6042 break;
6043 default:
6044 tab_number = tabpage_index(curtab);
6045 }
6046 }
6047
6048theend:
6049 return tab_number;
6050}
6051
6052/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006053 * ":tabclose": close current tab page, unless it is the last one.
6054 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 */
6056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006057ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006059 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006060 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006061
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006062# ifdef FEAT_CMDWIN
6063 if (cmdwin_type != 0)
6064 cmdwin_result = K_IGNORE;
6065 else
6066# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006067 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006068 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006071 tab_number = get_tabpage_arg(eap);
6072 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006073 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006074 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006075 if (tp == NULL)
6076 {
6077 beep_flush();
6078 return;
6079 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006080 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006081 {
6082 tabpage_close_other(tp, eap->forceit);
6083 return;
6084 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006085 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006086 tabpage_close(eap->forceit);
6087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006089}
6090
6091/*
6092 * ":tabonly": close all tab pages except the current one
6093 */
6094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006096{
6097 tabpage_T *tp;
6098 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006099 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006100
6101# ifdef FEAT_CMDWIN
6102 if (cmdwin_type != 0)
6103 cmdwin_result = K_IGNORE;
6104 else
6105# endif
6106 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006107 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006108 else
6109 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006110 tab_number = get_tabpage_arg(eap);
6111 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006112 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006113 goto_tabpage(tab_number);
6114 /* Repeat this up to a 1000 times, because autocommands may
6115 * mess up the lists. */
6116 for (done = 0; done < 1000; ++done)
6117 {
6118 FOR_ALL_TABPAGES(tp)
6119 if (tp->tp_topframe != topframe)
6120 {
6121 tabpage_close_other(tp, eap->forceit);
6122 /* if we failed to close it quit */
6123 if (valid_tabpage(tp))
6124 done = 1000;
6125 /* start over, "tp" is now invalid */
6126 break;
6127 }
6128 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006129 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006130 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006131 }
6132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006136 * Close the current tab page.
6137 */
6138 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006139tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006140{
6141 /* First close all the windows but the current one. If that worked then
6142 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006143 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006144 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006145 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006146 ex_win_close(forceit, curwin, NULL);
6147# ifdef FEAT_GUI
6148 need_mouse_correct = TRUE;
6149# endif
6150}
6151
6152/*
6153 * Close tab page "tp", which is not the current tab page.
6154 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006155 * Also takes care of the tab pages line disappearing when closing the
6156 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006157 */
6158 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006159tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006160{
6161 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006162 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006163 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006164
6165 /* Limit to 1000 windows, autocommands may add a window while we close
6166 * one. OK, so I'm paranoid... */
6167 while (++done < 1000)
6168 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006169 wp = tp->tp_firstwin;
6170 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006171
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006172 /* Autocommands may delete the tab page under our fingers and we may
6173 * fail to close a window with a modified buffer. */
6174 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006175 break;
6176 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006177
Bram Moolenaar29323592016-07-24 22:04:11 +02006178 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006179
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006180 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006181 if (h != tabline_height())
6182 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006183}
6184
6185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 * ":only".
6187 */
6188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006189ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006191 win_T *wp;
6192 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193# ifdef FEAT_GUI
6194 need_mouse_correct = TRUE;
6195# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006196 if (eap->addr_count > 0)
6197 {
6198 wnr = eap->line2;
6199 for (wp = firstwin; --wnr > 0; )
6200 {
6201 if (wp->w_next == NULL)
6202 break;
6203 else
6204 wp = wp->w_next;
6205 }
6206 win_goto(wp);
6207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 close_others(TRUE, eap->forceit);
6209}
6210
6211/*
6212 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006213 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006215 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006216ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
6218 if (eap->addr_count == 0)
6219 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006220 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006224ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006226 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006227 if (!eap->skip)
6228 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006229#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006230 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006231#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006232 if (eap->addr_count == 0)
6233 win_close(curwin, FALSE); /* don't free buffer */
6234 else
6235 {
6236 int winnr = 0;
6237 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006238
Bram Moolenaar2256c992016-11-15 21:17:07 +01006239 FOR_ALL_WINDOWS(win)
6240 {
6241 winnr++;
6242 if (winnr == eap->line2)
6243 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006244 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006245 if (win == NULL)
6246 win = lastwin;
6247 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
6250}
6251
6252/*
6253 * ":stop" and ":suspend": Suspend Vim.
6254 */
6255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006256ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
6258 /*
6259 * Disallow suspending for "rvim".
6260 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006261 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 {
6263 if (!eap->forceit)
6264 autowrite_all();
6265 windgoto((int)Rows - 1, 0);
6266 out_char('\n');
6267 out_flush();
6268 stoptermcap();
6269 out_flush(); /* needed for SUN to restore xterm buffer */
6270#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006271 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272#endif
6273 ui_suspend(); /* call machine specific function */
6274#ifdef FEAT_TITLE
6275 maketitle();
6276 resettitle(); /* force updating the title */
6277#endif
6278 starttermcap();
6279 scroll_start(); /* scroll screen before redrawing */
6280 redraw_later_clear();
6281 shell_resized(); /* may have resized window */
6282 }
6283}
6284
6285/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006286 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 */
6288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006289ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291#ifdef FEAT_CMDWIN
6292 if (cmdwin_type != 0)
6293 {
6294 cmdwin_result = Ctrl_C;
6295 return;
6296 }
6297#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006298 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006299 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006300 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006301 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006302 return;
6303 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006304
6305 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006306 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
6308 /*
6309 * if more files or windows we won't exit
6310 */
6311 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6312 exiting = TRUE;
6313 if ( ((eap->cmdidx == CMD_wq
6314 || curbufIsChanged())
6315 && do_write(eap) == FAIL)
6316 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006317 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 {
6319 not_exiting();
6320 }
6321 else
6322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006325 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326# ifdef FEAT_GUI
6327 need_mouse_correct = TRUE;
6328# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006329 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006330 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
6332}
6333
6334/*
6335 * ":print", ":list", ":number".
6336 */
6337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006338ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006340 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006341 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006342 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006344 for ( ;!got_int; ui_breakcheck())
6345 {
6346 print_line(eap->line1,
6347 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6348 || (eap->flags & EXFLAG_NR)),
6349 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6350 if (++eap->line1 > eap->line2)
6351 break;
6352 out_flush(); /* show one line at a time */
6353 }
6354 setpcmark();
6355 /* put cursor at last line */
6356 curwin->w_cursor.lnum = eap->line2;
6357 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 }
6359
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361}
6362
6363#ifdef FEAT_BYTEOFF
6364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006365ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
6367 goto_byte(eap->line2);
6368}
6369#endif
6370
6371/*
6372 * ":shell".
6373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006375ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 do_shell(NULL, 0);
6378}
6379
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006380#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006382static int drop_busy = FALSE;
6383static int drop_filec;
6384static char_u **drop_filev = NULL;
6385static int drop_split;
6386static void (*drop_callback)(void *);
6387static void *drop_cookie;
6388
6389 static void
6390handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391{
6392 exarg_T ea;
6393 int save_msg_scroll = msg_scroll;
6394
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006395 // Setting the argument list may cause screen updates and being called
6396 // recursively. Avoid that by setting drop_busy.
6397 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
6399 /* Check whether the current buffer is changed. If so, we will need
6400 * to split the current window or data could be lost.
6401 * We don't need to check if the 'hidden' option is set, as in this
6402 * case the buffer won't be lost.
6403 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006404 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
6406 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006407 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 --emsg_off;
6409 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006410 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (win_split(0, 0) == FAIL)
6413 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006414 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415
6416 /* When splitting the window, create a new alist. Otherwise the
6417 * existing one is overwritten. */
6418 alist_unlink(curwin->w_alist);
6419 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 }
6421
6422 /*
6423 * Set up the new argument list.
6424 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006425 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426
6427 /*
6428 * Move to the first file.
6429 */
6430 /* Fake up a minimal "next" command for do_argfile() */
6431 vim_memset(&ea, 0, sizeof(ea));
6432 ea.cmd = (char_u *)"next";
6433 do_argfile(&ea, 0);
6434
6435 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6436 * mode that is not needed here. */
6437 need_start_insertmode = FALSE;
6438
6439 /* Restore msg_scroll, otherwise a following command may cause scrolling
6440 * unexpectedly. The screen will be redrawn by the caller, thus
6441 * msg_scroll being set by displaying a message is irrelevant. */
6442 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006443
6444 if (drop_callback != NULL)
6445 drop_callback(drop_cookie);
6446
6447 drop_filev = NULL;
6448 drop_busy = FALSE;
6449}
6450
6451/*
6452 * Handle a file drop. The code is here because a drop is *nearly* like an
6453 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006454 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006455 * code is very similar to :args and hence needs access to a lot of the static
6456 * functions in this file.
6457 *
6458 * The "filev" list must have been allocated using alloc(), as should each item
6459 * in the list. This function takes over responsibility for freeing the "filev"
6460 * list.
6461 */
6462 void
6463handle_drop(
6464 int filec, // the number of files dropped
6465 char_u **filev, // the list of files dropped
6466 int split, // force splitting the window
6467 void (*callback)(void *), // to be called after setting the argument
6468 // list
6469 void *cookie) // argument for "callback" (allocated)
6470{
6471 // Cannot handle recursive drops, finish the pending one.
6472 if (drop_busy)
6473 {
6474 FreeWild(filec, filev);
6475 vim_free(cookie);
6476 return;
6477 }
6478
6479 // When calling handle_drop() more than once in a row we only use the last
6480 // one.
6481 if (drop_filev != NULL)
6482 {
6483 FreeWild(drop_filec, drop_filev);
6484 vim_free(drop_cookie);
6485 }
6486
6487 drop_filec = filec;
6488 drop_filev = filev;
6489 drop_split = split;
6490 drop_callback = callback;
6491 drop_cookie = cookie;
6492
6493 // Postpone this when:
6494 // - editing the command line
6495 // - not possible to change the current buffer
6496 // - updating the screen
6497 // As it may change buffers and window structures that are in use and cause
6498 // freed memory to be used.
6499 if (text_locked() || curbuf_locked() || updating_screen)
6500 return;
6501
6502 handle_drop_internal();
6503}
6504
6505/*
6506 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6507 * reset: Handle a postponed drop.
6508 */
6509 void
6510handle_any_postponed_drop(void)
6511{
6512 if (!drop_busy && drop_filev != NULL
6513 && !text_locked() && !curbuf_locked() && !updating_screen)
6514 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516#endif
6517
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518/*
6519 * Clear an argument list: free all file names and reset it to zero entries.
6520 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006521 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006522alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 while (--al->al_ga.ga_len >= 0)
6525 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6526 ga_clear(&al->al_ga);
6527}
6528
6529/*
6530 * Init an argument list.
6531 */
6532 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006533alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534{
6535 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6536}
6537
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538/*
6539 * Remove a reference from an argument list.
6540 * Ignored when the argument list is the global one.
6541 * If the argument list is no longer used by any window, free it.
6542 */
6543 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006544alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 if (al != &global_alist && --al->al_refcount <= 0)
6547 {
6548 alist_clear(al);
6549 vim_free(al);
6550 }
6551}
6552
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553/*
6554 * Create a new argument list and use it for the current window.
6555 */
6556 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006557alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006559 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 if (curwin->w_alist == NULL)
6561 {
6562 curwin->w_alist = &global_alist;
6563 ++global_alist.al_refcount;
6564 }
6565 else
6566 {
6567 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006568 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 alist_init(curwin->w_alist);
6570 }
6571}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006573#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574/*
6575 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006576 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6577 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 */
6579 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006580alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006583 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 char_u **new_arg_files;
6585 int new_arg_file_count;
6586 char_u *save_p_su = p_su;
6587 int i;
6588
6589 /* Don't use 'suffixes' here. This should work like the shell did the
6590 * expansion. Also, the vimrc file isn't read yet, thus the user
6591 * can't set the options. */
6592 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006593 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 if (old_arg_files != NULL)
6595 {
6596 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006597 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6598 old_arg_count = GARGCOUNT;
6599 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006601 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 && new_arg_file_count > 0)
6603 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006604 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6605 TRUE, fnum_list, fnum_len);
6606 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 p_su = save_p_su;
6610}
6611#endif
6612
6613/*
6614 * Set the argument list for the current window.
6615 * Takes over the allocated files[] and the allocated fnames in it.
6616 */
6617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006618alist_set(
6619 alist_T *al,
6620 int count,
6621 char_u **files,
6622 int use_curbuf,
6623 int *fnum_list,
6624 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006627 static int recursive = 0;
6628
6629 if (recursive)
6630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006631 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006632 return;
6633 }
6634 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636 alist_clear(al);
6637 if (ga_grow(&al->al_ga, count) == OK)
6638 {
6639 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006640 {
6641 if (got_int)
6642 {
6643 /* When adding many buffers this can take a long time. Allow
6644 * interrupting here. */
6645 while (i < count)
6646 vim_free(files[i++]);
6647 break;
6648 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006649
6650 /* May set buffer name of a buffer previously used for the
6651 * argument list, so that it's re-used by alist_add. */
6652 if (fnum_list != NULL && i < fnum_len)
6653 buf_set_name(fnum_list[i], files[i]);
6654
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006656 ui_breakcheck();
6657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 vim_free(files);
6659 }
6660 else
6661 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006664
6665 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666}
6667
6668/*
6669 * Add file "fname" to argument list "al".
6670 * "fname" must have been allocated and "al" must have been checked for room.
6671 */
6672 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006673alist_add(
6674 alist_T *al,
6675 char_u *fname,
6676 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 if (fname == NULL) /* don't add NULL file names */
6679 return;
6680#ifdef BACKSLASH_IN_FILENAME
6681 slash_adjust(fname);
6682#endif
6683 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6684 if (set_fnum > 0)
6685 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6686 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6687 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688}
6689
6690#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6691/*
6692 * Adjust slashes in file names. Called after 'shellslash' was set.
6693 */
6694 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006695alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
6697 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006699 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701 for (i = 0; i < GARGCOUNT; ++i)
6702 if (GARGLIST[i].ae_fname != NULL)
6703 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006704 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (wp->w_alist != &global_alist)
6706 for (i = 0; i < WARGCOUNT(wp); ++i)
6707 if (WARGLIST(wp)[i].ae_fname != NULL)
6708 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709}
6710#endif
6711
6712/*
6713 * ":preserve".
6714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006716ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006718 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 ml_preserve(curbuf, TRUE);
6720}
6721
6722/*
6723 * ":recover".
6724 */
6725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006726ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6729 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006730 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6731 | CCGD_MULTWIN
6732 | (eap->forceit ? CCGD_FORCEIT : 0)
6733 | CCGD_EXCMD)
6734
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 && (*eap->arg == NUL
6736 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006737 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 recoverymode = FALSE;
6739}
6740
6741/*
6742 * Command modifier used in a wrong way.
6743 */
6744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006745ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 eap->errmsg = e_invcmd;
6748}
6749
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750/*
6751 * :sview [+command] file split window with new file, read-only
6752 * :split [[+command] file] split window with current or new file
6753 * :vsplit [[+command] file] split window vertically with current or new file
6754 * :new [[+command] file] split window with no or new file
6755 * :vnew [[+command] file] split vertically window with no or new file
6756 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006757 *
6758 * :tabedit open new Tab page with empty window
6759 * :tabedit [+command] file open new Tab page and edit "file"
6760 * :tabnew [[+command] file] just like :tabedit
6761 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 */
6763 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006764ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006766 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006767#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006769#endif
6770#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006772#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006773 int use_tab = eap->cmdidx == CMD_tabedit
6774 || eap->cmdidx == CMD_tabfind
6775 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006777 if (NOT_IN_POPUP_WINDOW)
6778 return;
6779
Bram Moolenaar4033c552017-09-16 20:54:51 +02006780#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783
Bram Moolenaar4033c552017-09-16 20:54:51 +02006784#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006786 * quickfix windows. But it's OK when doing ":tab split". */
6787 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 {
6789 if (eap->cmdidx == CMD_split)
6790 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 if (eap->cmdidx == CMD_vsplit)
6792 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795
Bram Moolenaar4033c552017-09-16 20:54:51 +02006796#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006797 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
6799 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6800 FNAME_MESS, TRUE, curbuf->b_ffname);
6801 if (fname == NULL)
6802 goto theend;
6803 eap->arg = fname;
6804 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006805# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006806 else
6807# endif
6808#endif
6809#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 && eap->cmdidx != CMD_new)
6813 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006814 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006815# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006816 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006817# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006818 au_has_group((char_u *)"FileExplorer"))
6819 {
6820 /* No browsing supported but we do have the file explorer:
6821 * Edit the directory. */
6822 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6823 eap->arg = (char_u *)".";
6824 }
6825 else
6826 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006827 fname = do_browse(0, (char_u *)(use_tab
6828 ? _("Edit File in new tab page")
6829 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006831 if (fname == NULL)
6832 goto theend;
6833 eap->arg = fname;
6834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
6836 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006839 /*
6840 * Either open new tab page or split the window.
6841 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006842 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006843 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006844 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6845 : eap->addr_count == 0 ? 0
6846 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006847 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006848 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006849
6850 /* set the alternate buffer for the window we came from */
6851 if (curwin != old_curwin
6852 && win_valid(old_curwin)
6853 && old_curwin->w_buffer != curbuf
6854 && !cmdmod.keepalt)
6855 old_curwin->w_alt_fnum = curbuf->b_fnum;
6856 }
6857 }
6858 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6860 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 /* Reset 'scrollbind' when editing another file, but keep it when
6862 * doing ":split" without arguments. */
6863 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006864# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006868 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 else
6870 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 do_exedit(eap, old_curwin);
6872 }
6873
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006874# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006878# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879theend:
6880 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006883
6884/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006885 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006886 */
6887 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006888tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006889{
6890 exarg_T ea;
6891
6892 vim_memset(&ea, 0, sizeof(ea));
6893 ea.cmdidx = CMD_tabnew;
6894 ea.cmd = (char_u *)"tabn";
6895 ea.arg = (char_u *)"";
6896 ex_splitview(&ea);
6897}
6898
6899/*
6900 * :tabnext command
6901 */
6902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006903ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006904{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006905 int tab_number;
6906
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006907 if (NOT_IN_POPUP_WINDOW)
6908 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006909 switch (eap->cmdidx)
6910 {
6911 case CMD_tabfirst:
6912 case CMD_tabrewind:
6913 goto_tabpage(1);
6914 break;
6915 case CMD_tablast:
6916 goto_tabpage(9999);
6917 break;
6918 case CMD_tabprevious:
6919 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006920 if (eap->arg && *eap->arg != NUL)
6921 {
6922 char_u *p = eap->arg;
6923 char_u *p_save = p;
6924
6925 tab_number = getdigits(&p);
6926 if (p == p_save || *p_save == '-' || *p != NUL
6927 || tab_number == 0)
6928 {
6929 /* No numbers as argument. */
6930 eap->errmsg = e_invarg;
6931 return;
6932 }
6933 }
6934 else
6935 {
6936 if (eap->addr_count == 0)
6937 tab_number = 1;
6938 else
6939 {
6940 tab_number = eap->line2;
6941 if (tab_number < 1)
6942 {
6943 eap->errmsg = e_invrange;
6944 return;
6945 }
6946 }
6947 }
6948 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006949 break;
6950 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006951 tab_number = get_tabpage_arg(eap);
6952 if (eap->errmsg == NULL)
6953 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006954 break;
6955 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006956}
6957
6958/*
6959 * :tabmove command
6960 */
6961 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006962ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006963{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006964 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006965
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006966 tab_number = get_tabpage_arg(eap);
6967 if (eap->errmsg == NULL)
6968 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006969}
6970
6971/*
6972 * :tabs command: List tabs and their contents.
6973 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006975ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006976{
6977 tabpage_T *tp;
6978 win_T *wp;
6979 int tabcount = 1;
6980
6981 msg_start();
6982 msg_scroll = TRUE;
6983 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6984 {
6985 msg_putchar('\n');
6986 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006987 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006988 out_flush(); /* output one line at a time */
6989 ui_breakcheck();
6990
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006991 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006992 wp = firstwin;
6993 else
6994 wp = tp->tp_firstwin;
6995 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6996 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006997 msg_putchar('\n');
6998 msg_putchar(wp == curwin ? '>' : ' ');
6999 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007000 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7001 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007002 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007003 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007004 else
7005 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7006 IObuff, IOSIZE, TRUE);
7007 msg_outtrans(IObuff);
7008 out_flush(); /* output one line at a time */
7009 ui_breakcheck();
7010 }
7011 }
7012}
7013
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014/*
7015 * ":mode": Set screen mode.
7016 * If no argument given, just get the screen size and redraw.
7017 */
7018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007019ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 if (*eap->arg == NUL)
7022 shell_resized();
7023 else
7024 mch_screenmode(eap->arg);
7025}
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027/*
7028 * ":resize".
7029 * set, increment or decrement current window height
7030 */
7031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007032ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 int n;
7035 win_T *wp = curwin;
7036
7037 if (eap->addr_count > 0)
7038 {
7039 n = eap->line2;
7040 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7041 ;
7042 }
7043
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007044# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 if (cmdmod.split & WSP_VERT)
7049 {
7050 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007051 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7053 n = 9999;
7054 win_setwidth_win((int)n, wp);
7055 }
7056 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
7058 if (*eap->arg == '-' || *eap->arg == '+')
7059 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007060 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 n = 9999;
7062 win_setheight_win((int)n, wp);
7063 }
7064}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
7066/*
7067 * ":find [+command] <file>" command.
7068 */
7069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007070ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
7072#ifdef FEAT_SEARCHPATH
7073 char_u *fname;
7074 int count;
7075
7076 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7077 TRUE, curbuf->b_ffname);
7078 if (eap->addr_count > 0)
7079 {
7080 /* Repeat finding the file "count" times. This matters when it
7081 * appears several times in the path. */
7082 count = eap->line2;
7083 while (fname != NULL && --count > 0)
7084 {
7085 vim_free(fname);
7086 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7087 FALSE, curbuf->b_ffname);
7088 }
7089 }
7090
7091 if (fname != NULL)
7092 {
7093 eap->arg = fname;
7094#endif
7095 do_exedit(eap, NULL);
7096#ifdef FEAT_SEARCHPATH
7097 vim_free(fname);
7098 }
7099#endif
7100}
7101
7102/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007103 * ":open" simulation: for now just work like ":visual".
7104 */
7105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007106ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007107{
7108 regmatch_T regmatch;
7109 char_u *p;
7110
7111 curwin->w_cursor.lnum = eap->line2;
7112 beginline(BL_SOL | BL_FIX);
7113 if (*eap->arg == '/')
7114 {
7115 /* ":open /pattern/": put cursor in column found with pattern */
7116 ++eap->arg;
7117 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7118 *p = NUL;
7119 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7120 if (regmatch.regprog != NULL)
7121 {
7122 regmatch.rm_ic = p_ic;
7123 p = ml_get_curline();
7124 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007125 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007126 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007127 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007128 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007129 }
7130 /* Move to the NUL, ignore any other arguments. */
7131 eap->arg += STRLEN(eap->arg);
7132 }
7133 check_cursor();
7134
7135 eap->cmdidx = CMD_visual;
7136 do_exedit(eap, NULL);
7137}
7138
7139/*
7140 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 */
7142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007143ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 do_exedit(eap, NULL);
7146}
7147
7148/*
7149 * ":edit <file>" command and alikes.
7150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152do_exedit(
7153 exarg_T *eap,
7154 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
7156 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007158 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007160 if (NOT_IN_POPUP_WINDOW)
7161 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 /*
7163 * ":vi" command ends Ex mode.
7164 */
7165 if (exmode_active && (eap->cmdidx == CMD_visual
7166 || eap->cmdidx == CMD_view))
7167 {
7168 exmode_active = FALSE;
7169 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007170 {
7171 /* Special case: ":global/pat/visual\NLvi-commands" */
7172 if (global_busy)
7173 {
7174 int rd = RedrawingDisabled;
7175 int nwr = no_wait_return;
7176 int ms = msg_scroll;
7177#ifdef FEAT_GUI
7178 int he = hold_gui_events;
7179#endif
7180
7181 if (eap->nextcmd != NULL)
7182 {
7183 stuffReadbuff(eap->nextcmd);
7184 eap->nextcmd = NULL;
7185 }
7186
7187 if (exmode_was != EXMODE_VIM)
7188 settmode(TMODE_RAW);
7189 RedrawingDisabled = 0;
7190 no_wait_return = 0;
7191 need_wait_return = FALSE;
7192 msg_scroll = 0;
7193#ifdef FEAT_GUI
7194 hold_gui_events = 0;
7195#endif
7196 must_redraw = CLEAR;
7197
7198 main_loop(FALSE, TRUE);
7199
7200 RedrawingDisabled = rd;
7201 no_wait_return = nwr;
7202 msg_scroll = ms;
7203#ifdef FEAT_GUI
7204 hold_gui_events = he;
7205#endif
7206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 }
7210
7211 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007212 || eap->cmdidx == CMD_tabnew
7213 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007214 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007216 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 setpcmark();
7218 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007219 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7220 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007222 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 || *eap->arg != NUL
7224#ifdef FEAT_BROWSE
7225 || cmdmod.browse
7226#endif
7227 )
7228 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007229 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7230 * can bring us here, others are stopped earlier. */
7231 if (*eap->arg != NUL && curbuf_locked())
7232 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007233
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 n = readonlymode;
7235 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7236 readonlymode = TRUE;
7237 else if (eap->cmdidx == CMD_enew)
7238 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7239 empty buffer */
7240 setpcmark();
7241 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7242 NULL, eap,
7243 /* ":edit" goes to first line if Vi compatible */
7244 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7245 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7246 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007247 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007249 /* after a split we can use an existing buffer */
7250 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007252 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 {
7254 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 if (old_curwin != NULL)
7256 {
7257 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007258 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007260#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007261 cleanup_T cs;
7262
7263 /* Reset the error/interrupt/exception state here so that
7264 * aborting() returns FALSE when closing a window. */
7265 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007266#endif
7267#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007269#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007270 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007271
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007272#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007273 /* Restore the error/interrupt/exception state if not
7274 * discarded by a new aborting error, interrupt, or
7275 * uncaught exception. */
7276 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281 else if (readonlymode && curbuf->b_nwindows == 1)
7282 {
7283 /* When editing an already visited buffer, 'readonly' won't be set
7284 * but the previous value is kept. With ":view" and ":sview" we
7285 * want the file to be readonly, except when another window is
7286 * editing the same buffer. */
7287 curbuf->b_p_ro = TRUE;
7288 }
7289 readonlymode = n;
7290 }
7291 else
7292 {
7293 if (eap->do_ecmd_cmd != NULL)
7294 do_cmdline_cmd(eap->do_ecmd_cmd);
7295#ifdef FEAT_TITLE
7296 n = curwin->w_arg_idx_invalid;
7297#endif
7298 check_arg_idx(curwin);
7299#ifdef FEAT_TITLE
7300 if (n != curwin->w_arg_idx_invalid)
7301 maketitle();
7302#endif
7303 }
7304
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 /*
7306 * if ":split file" worked, set alternate file name in old window to new
7307 * file
7308 */
7309 if (old_curwin != NULL
7310 && *eap->arg != NUL
7311 && curwin != old_curwin
7312 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007313 && old_curwin->w_buffer != curbuf
7314 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
7317 ex_no_reprint = TRUE;
7318}
7319
7320#ifndef FEAT_GUI
7321/*
7322 * ":gui" and ":gvim" when there is no GUI.
7323 */
7324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007325ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326{
7327 eap->errmsg = e_nogvim;
7328}
7329#endif
7330
Bram Moolenaar4f974752019-02-17 17:44:42 +01007331#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007333ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
7335 gui_make_tearoff(eap->arg);
7336}
7337#endif
7338
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007339#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7340 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007342ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007344# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7345 if (gui.in_use)
7346 gui_make_popup(eap->arg, eap->forceit);
7347# ifdef FEAT_TERM_POPUP_MENU
7348 else
7349# endif
7350# endif
7351# ifdef FEAT_TERM_POPUP_MENU
7352 pum_make_popup(eap->arg, eap->forceit);
7353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354}
7355#endif
7356
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007358ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
7360 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007361 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007363 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364}
7365
7366/*
7367 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7368 * offset.
7369 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007372ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007375 win_T *save_curwin = curwin;
7376 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 long topline;
7378 long y;
7379 linenr_T old_linenr = curwin->w_cursor.lnum;
7380
7381 setpcmark();
7382
7383 /*
7384 * determine max topline
7385 */
7386 if (curwin->w_p_scb)
7387 {
7388 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007389 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 {
7391 if (wp->w_p_scb && wp->w_buffer)
7392 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007393 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 if (topline > y)
7395 topline = y;
7396 }
7397 }
7398 if (topline < 1)
7399 topline = 1;
7400 }
7401 else
7402 {
7403 topline = 1;
7404 }
7405
7406
7407 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007408 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007410 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 {
7412 if (curwin->w_p_scb)
7413 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007414 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 y = topline - curwin->w_topline;
7416 if (y > 0)
7417 scrollup(y, TRUE);
7418 else
7419 scrolldown(-y, TRUE);
7420 curwin->w_scbind_pos = topline;
7421 redraw_later(VALID);
7422 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007426 curwin = save_curwin;
7427 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 if (curwin->w_p_scb)
7429 {
7430 did_syncbind = TRUE;
7431 checkpcmark();
7432 if (old_linenr != curwin->w_cursor.lnum)
7433 {
7434 char_u ctrl_o[2];
7435
7436 ctrl_o[0] = Ctrl_O;
7437 ctrl_o[1] = 0;
7438 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7439 }
7440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441}
7442
7443
7444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007445ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007447 int i;
7448 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7449 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
7451 if (eap->usefilter) /* :r!cmd */
7452 do_bang(1, eap, FALSE, FALSE, TRUE);
7453 else
7454 {
7455 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7456 return;
7457
7458#ifdef FEAT_BROWSE
7459 if (cmdmod.browse)
7460 {
7461 char_u *browseFile;
7462
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007463 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 NULL, NULL, NULL, curbuf);
7465 if (browseFile != NULL)
7466 {
7467 i = readfile(browseFile, NULL,
7468 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7469 vim_free(browseFile);
7470 }
7471 else
7472 i = OK;
7473 }
7474 else
7475#endif
7476 if (*eap->arg == NUL)
7477 {
7478 if (check_fname() == FAIL) /* check for no file name */
7479 return;
7480 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7481 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7482 }
7483 else
7484 {
7485 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7486 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7487 i = readfile(eap->arg, NULL,
7488 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7489
7490 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007491 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007493#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 if (!aborting())
7495#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007496 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 }
7498 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007499 {
7500 if (empty && exmode_active)
7501 {
7502 /* Delete the empty line that remains. Historically ex does
7503 * this but vi doesn't. */
7504 if (eap->line2 == 0)
7505 lnum = curbuf->b_ml.ml_line_count;
7506 else
7507 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007508 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007509 {
7510 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007511 if (curwin->w_cursor.lnum > 1
7512 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007513 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007514 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007515 }
7516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 }
7520}
7521
Bram Moolenaarea408852005-06-25 22:49:46 +00007522static char_u *prev_dir = NULL;
7523
7524#if defined(EXITFREE) || defined(PROTO)
7525 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007526free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007527{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007528 VIM_CLEAR(prev_dir);
7529 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007530}
7531#endif
7532
Bram Moolenaarf4258302013-06-02 18:20:17 +02007533/*
7534 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007535 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7536 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007537 */
7538 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007539post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007540{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007541 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007542 // Clear tab local directory for both :cd and :tcd
7543 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007544 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007545 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007546 {
7547 /* If still in global directory, need to remember current
7548 * directory as global directory. */
7549 if (globaldir == NULL && prev_dir != NULL)
7550 globaldir = vim_strsave(prev_dir);
7551 /* Remember this local directory for the window. */
7552 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007553 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007554 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007555 curtab->tp_localdir = vim_strsave(NameBuff);
7556 else
7557 curwin->w_localdir = vim_strsave(NameBuff);
7558 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007559 }
7560 else
7561 {
7562 /* We are now in the global directory, no need to remember its
7563 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007564 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007565 }
7566
7567 shorten_fnames(TRUE);
7568}
7569
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007570/*
7571 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7572 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7573 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7574 * Otherwise changes the global directory.
7575 * Returns TRUE if the directory is successfully changed.
7576 */
7577 int
7578changedir_func(
7579 char_u *new_dir,
7580 int forceit,
7581 cdscope_T scope)
7582{
7583 char_u *tofree;
7584 int dir_differs;
7585 int retval = FALSE;
7586
7587 if (allbuf_locked())
7588 return FALSE;
7589
7590 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7591 {
7592 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7593 return FALSE;
7594 }
7595
7596 // ":cd -": Change to previous directory
7597 if (STRCMP(new_dir, "-") == 0)
7598 {
7599 if (prev_dir == NULL)
7600 {
7601 emsg(_("E186: No previous directory"));
7602 return FALSE;
7603 }
7604 new_dir = prev_dir;
7605 }
7606
7607 // Save current directory for next ":cd -"
7608 tofree = prev_dir;
7609 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7610 prev_dir = vim_strsave(NameBuff);
7611 else
7612 prev_dir = NULL;
7613
7614#if defined(UNIX) || defined(VMS)
7615 // for UNIX ":cd" means: go to home directory
7616 if (*new_dir == NUL)
7617 {
7618 // use NameBuff for home directory name
7619# ifdef VMS
7620 char_u *p;
7621
7622 p = mch_getenv((char_u *)"SYS$LOGIN");
7623 if (p == NULL || *p == NUL) // empty is the same as not set
7624 NameBuff[0] = NUL;
7625 else
7626 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7627# else
7628 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7629# endif
7630 new_dir = NameBuff;
7631 }
7632#endif
7633 dir_differs = new_dir == NULL || prev_dir == NULL
7634 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7635 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7636 emsg(_(e_failed));
7637 else
7638 {
7639 char_u *acmd_fname;
7640
7641 post_chdir(scope);
7642
7643 if (dir_differs)
7644 {
7645 if (scope == CDSCOPE_WINDOW)
7646 acmd_fname = (char_u *)"window";
7647 else if (scope == CDSCOPE_TABPAGE)
7648 acmd_fname = (char_u *)"tabpage";
7649 else
7650 acmd_fname = (char_u *)"global";
7651 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7652 curbuf);
7653 }
7654 retval = TRUE;
7655 }
7656 vim_free(tofree);
7657
7658 return retval;
7659}
Bram Moolenaarea408852005-06-25 22:49:46 +00007660
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007662 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007664 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007665ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007667 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668
7669 new_dir = eap->arg;
7670#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007671 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 if (*new_dir == NUL)
7673 ex_pwd(NULL);
7674 else
7675#endif
7676 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007677 cdscope_T scope = CDSCOPE_GLOBAL;
7678
7679 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7680 scope = CDSCOPE_WINDOW;
7681 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7682 scope = CDSCOPE_TABPAGE;
7683
7684 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007685 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007686 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007687 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 ex_pwd(eap);
7689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 }
7691}
7692
7693/*
7694 * ":pwd".
7695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007697ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
7699 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7700 {
7701#ifdef BACKSLASH_IN_FILENAME
7702 slash_adjust(NameBuff);
7703#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007704 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007707 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708}
7709
7710/*
7711 * ":=".
7712 */
7713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007714ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007716 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007717 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718}
7719
7720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007721ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007723 int n;
7724 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725
7726 if (cursor_valid())
7727 {
7728 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7729 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007730 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007732
7733 len = eap->line2;
7734 switch (*eap->arg)
7735 {
7736 case 'm': break;
7737 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007738 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007739 }
7740 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741}
7742
7743/*
7744 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7745 */
7746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007747do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748{
7749 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007750 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
7752 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007753 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007754 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007756 wait_now = msec - done > 1000L ? 1000L : msec - done;
7757#ifdef FEAT_TIMERS
7758 {
7759 long due_time = check_due_timer();
7760
7761 if (due_time > 0 && due_time < wait_now)
7762 wait_now = due_time;
7763 }
7764#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007765#ifdef FEAT_JOB_CHANNEL
7766 if (has_any_channel() && wait_now > 100L)
7767 wait_now = 100L;
7768#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007769 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007770#ifdef FEAT_JOB_CHANNEL
7771 if (has_any_channel())
7772 ui_breakcheck_force(TRUE);
7773 else
7774#endif
7775 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007776#ifdef MESSAGE_QUEUE
7777 /* Process the netbeans and clientserver messages that may have been
7778 * received in the call to ui_breakcheck() when the GUI is in use. This
7779 * may occur when running a test case. */
7780 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007783
7784 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7785 // input buffer, otherwise a following call to input() fails.
7786 if (got_int)
7787 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
7789
7790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007791do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 int mode;
7794 char_u *cmdp;
7795
7796 cmdp = eap->cmd;
7797 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7798
7799 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7800 eap->arg, mode, isabbrev))
7801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007802 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007804 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 break;
7806 }
7807}
7808
7809/*
7810 * ":winsize" command (obsolete).
7811 */
7812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007813ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814{
7815 int w, h;
7816 char_u *arg = eap->arg;
7817 char_u *p;
7818
7819 w = getdigits(&arg);
7820 arg = skipwhite(arg);
7821 p = arg;
7822 h = getdigits(&arg);
7823 if (*p != NUL && *arg == NUL)
7824 set_shellsize(w, h, TRUE);
7825 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007826 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827}
7828
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007830ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
7832 int xchar = NUL;
7833 char_u *p;
7834
7835 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7836 {
7837 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7838 if (eap->arg[1] == NUL)
7839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007840 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 return;
7842 }
7843 xchar = eap->arg[1];
7844 p = eap->arg + 2;
7845 }
7846 else
7847 p = eap->arg + 1;
7848
7849 eap->nextcmd = check_nextcmd(p);
7850 p = skipwhite(p);
7851 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007852 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007853 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {
7855 /* Pass flags on for ":vertical wincmd ]". */
7856 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007857 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7859 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007860 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 }
7862}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
Bram Moolenaar843ee412004-06-30 16:16:41 +00007864#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865/*
7866 * ":winpos".
7867 */
7868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007869ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
7871 int x, y;
7872 char_u *arg = eap->arg;
7873 char_u *p;
7874
7875 if (*arg == NUL)
7876 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007877# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007878# ifdef VIMDLL
7879 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7880 mch_get_winpos(&x, &y) != FAIL)
7881# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007883# else
7884 if (mch_get_winpos(&x, &y) != FAIL)
7885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007888 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 else
7891# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007892 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 }
7894 else
7895 {
7896 x = getdigits(&arg);
7897 arg = skipwhite(arg);
7898 p = arg;
7899 y = getdigits(&arg);
7900 if (*p == NUL || *arg != NUL)
7901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007902 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 return;
7904 }
7905# ifdef FEAT_GUI
7906 if (gui.in_use)
7907 gui_mch_set_winpos(x, y);
7908 else if (gui.starting)
7909 {
7910 /* Remember the coordinates for when the window is opened. */
7911 gui_win_x = x;
7912 gui_win_y = y;
7913 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007914# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 else
7916# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007917# endif
7918# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007919 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920# endif
7921# ifdef HAVE_TGETENT
7922 if (*T_CWP)
7923 term_set_winpos(x, y);
7924# endif
7925 }
7926}
7927#endif
7928
7929/*
7930 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7931 */
7932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007933ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 oparg_T oa;
7936
7937 clear_oparg(&oa);
7938 oa.regname = eap->regname;
7939 oa.start.lnum = eap->line1;
7940 oa.end.lnum = eap->line2;
7941 oa.line_count = eap->line2 - eap->line1 + 1;
7942 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7945 {
7946 setpcmark();
7947 curwin->w_cursor.lnum = eap->line1;
7948 beginline(BL_SOL | BL_FIX);
7949 }
7950
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007951 if (VIsual_active)
7952 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007953
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 switch (eap->cmdidx)
7955 {
7956 case CMD_delete:
7957 oa.op_type = OP_DELETE;
7958 op_delete(&oa);
7959 break;
7960
7961 case CMD_yank:
7962 oa.op_type = OP_YANK;
7963 (void)op_yank(&oa, FALSE, TRUE);
7964 break;
7965
7966 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007967 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007969 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7970#else
7971 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007973 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 oa.op_type = OP_RSHIFT;
7975 else
7976 oa.op_type = OP_LSHIFT;
7977 op_shift(&oa, FALSE, eap->amount);
7978 break;
7979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007981 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982}
7983
7984/*
7985 * ":put".
7986 */
7987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007988ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989{
7990 /* ":0put" works like ":1put!". */
7991 if (eap->line2 == 0)
7992 {
7993 eap->line2 = 1;
7994 eap->forceit = TRUE;
7995 }
7996 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007997 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7998 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999}
8000
8001/*
8002 * Handle ":copy" and ":move".
8003 */
8004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008005ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
8007 long n;
8008
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008009 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (eap->arg == NULL) /* error detected */
8011 {
8012 eap->nextcmd = NULL;
8013 return;
8014 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008015 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016
8017 /*
8018 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8019 */
8020 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008022 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 return;
8024 }
8025
8026 if (eap->cmdidx == CMD_move)
8027 {
8028 if (do_move(eap->line1, eap->line2, n) == FAIL)
8029 return;
8030 }
8031 else
8032 ex_copy(eap->line1, eap->line2, n);
8033 u_clearline();
8034 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008035 ex_may_print(eap);
8036}
8037
8038/*
8039 * Print the current line if flags were given to the Ex command.
8040 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008041 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008043{
8044 if (eap->flags != 0)
8045 {
8046 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8047 (eap->flags & EXFLAG_LIST));
8048 ex_no_reprint = TRUE;
8049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050}
8051
8052/*
8053 * ":smagic" and ":snomagic".
8054 */
8055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 int magic_save = p_magic;
8059
8060 p_magic = (eap->cmdidx == CMD_smagic);
8061 do_sub(eap);
8062 p_magic = magic_save;
8063}
8064
8065/*
8066 * ":join".
8067 */
8068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008069ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070{
8071 curwin->w_cursor.lnum = eap->line1;
8072 if (eap->line1 == eap->line2)
8073 {
8074 if (eap->addr_count >= 2) /* :2,2join does nothing */
8075 return;
8076 if (eap->line2 == curbuf->b_ml.ml_line_count)
8077 {
8078 beep_flush();
8079 return;
8080 }
8081 ++eap->line2;
8082 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008083 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008085 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086}
8087
8088/*
8089 * ":[addr]@r" or ":[addr]*r": execute register
8090 */
8091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008092ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
8094 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008095 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096
8097 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008098 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099
8100#ifdef USE_ON_FLY_SCROLL
8101 dont_scroll = TRUE; /* disallow scrolling here */
8102#endif
8103
8104 /* get the register name. No name means to use the previous one */
8105 c = *eap->arg;
8106 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8107 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008108 /* Put the register in the typeahead buffer with the "silent" flag. */
8109 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8110 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008111 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 else
8115 {
8116 int save_efr = exec_from_reg;
8117
8118 exec_from_reg = TRUE;
8119
8120 /*
8121 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008122 * Continue until the stuff buffer is empty and all added characters
8123 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008125 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8127
8128 exec_from_reg = save_efr;
8129 }
8130}
8131
8132/*
8133 * ":!".
8134 */
8135 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008136ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137{
8138 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8139}
8140
8141/*
8142 * ":undo".
8143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008145ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008147 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008148 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008149 else
8150 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151}
8152
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008153#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008155ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008156{
8157 char_u hash[UNDO_HASH_SIZE];
8158
8159 u_compute_hash(hash);
8160 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8161}
8162
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008164ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008165{
8166 char_u hash[UNDO_HASH_SIZE];
8167
8168 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008169 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008170}
8171#endif
8172
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173/*
8174 * ":redo".
8175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008177ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178{
8179 u_redo(1);
8180}
8181
8182/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008183 * ":earlier" and ":later".
8184 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008186ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008187{
8188 long count = 0;
8189 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008190 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008191 char_u *p = eap->arg;
8192
8193 if (*p == NUL)
8194 count = 1;
8195 else if (isdigit(*p))
8196 {
8197 count = getdigits(&p);
8198 switch (*p)
8199 {
8200 case 's': ++p; sec = TRUE; break;
8201 case 'm': ++p; sec = TRUE; count *= 60; break;
8202 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008203 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8204 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008205 }
8206 }
8207
8208 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008209 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008210 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008211 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8212 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008213}
8214
8215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 * ":redir": start/stop redirection.
8217 */
8218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008219ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220{
8221 char *mode;
8222 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008223 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
Bram Moolenaarba768492016-07-08 15:32:54 +02008225#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008226 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008227 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008228 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008229 return;
8230 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008231#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008232
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 if (STRICMP(eap->arg, "END") == 0)
8234 close_redir();
8235 else
8236 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008237 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008239 ++arg;
8240 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008242 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 mode = "a";
8244 }
8245 else
8246 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008247 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248
8249 close_redir();
8250
8251 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008252 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 if (fname == NULL)
8254 return;
8255#ifdef FEAT_BROWSE
8256 if (cmdmod.browse)
8257 {
8258 char_u *browseFile;
8259
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008260 browseFile = do_browse(BROWSE_SAVE,
8261 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008262 fname, NULL, NULL,
8263 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 if (browseFile == NULL)
8265 return; /* operation cancelled */
8266 vim_free(fname);
8267 fname = browseFile;
8268 eap->forceit = TRUE; /* since dialog already asked */
8269 }
8270#endif
8271
8272 redir_fd = open_exfile(fname, eap->forceit, mode);
8273 vim_free(fname);
8274 }
8275#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008276 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {
8278 /* redirect to a register a-z (resp. A-Z for appending) */
8279 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008280 ++arg;
8281 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008283 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008284 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008286 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008288 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008289 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008290 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008291 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008293 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008294 if (*arg == '>')
8295 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008296 /* Make register empty when not using @A-@Z and the
8297 * command is valid. */
8298 if (*arg == NUL && !isupper(redir_reg))
8299 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008301 }
8302 if (*arg != NUL)
8303 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008304 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008305 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008306 }
8307 }
8308 else if (*arg == '=' && arg[1] == '>')
8309 {
8310 int append;
8311
8312 /* redirect to a variable */
8313 close_redir();
8314 arg += 2;
8315
8316 if (*arg == '>')
8317 {
8318 ++arg;
8319 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
8321 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008322 append = FALSE;
8323
8324 if (var_redir_start(skipwhite(arg), append) == OK)
8325 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 }
8327#endif
8328
8329 /* TODO: redirect to a buffer */
8330
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008332 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008334
8335 /* Make sure redirection is not off. Can happen for cmdline completion
8336 * that indirectly invokes a command to catch its output. */
8337 if (redir_fd != NULL
8338#ifdef FEAT_EVAL
8339 || redir_reg || redir_vname
8340#endif
8341 )
8342 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343}
8344
8345/*
8346 * ":redraw": force redraw
8347 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008348 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008349ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350{
8351 int r = RedrawingDisabled;
8352 int p = p_lz;
8353
8354 RedrawingDisabled = 0;
8355 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008356 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008358 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359#ifdef FEAT_TITLE
8360 if (need_maketitle)
8361 maketitle();
8362#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008363#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8364# ifdef VIMDLL
8365 if (!gui.in_use)
8366# endif
8367 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 RedrawingDisabled = r;
8370 p_lz = p;
8371
8372 /* Reset msg_didout, so that a message that's there is overwritten. */
8373 msg_didout = FALSE;
8374 msg_col = 0;
8375
Bram Moolenaar56667a52013-07-17 11:54:28 +02008376 /* No need to wait after an intentional redraw. */
8377 need_wait_return = FALSE;
8378
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 out_flush();
8380}
8381
8382/*
8383 * ":redrawstatus": force redraw of status line(s)
8384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008386ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 int r = RedrawingDisabled;
8389 int p = p_lz;
8390
8391 RedrawingDisabled = 0;
8392 p_lz = FALSE;
8393 if (eap->forceit)
8394 status_redraw_all();
8395 else
8396 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008397 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 RedrawingDisabled = r;
8399 p_lz = p;
8400 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401}
8402
Bram Moolenaare12bab32019-01-08 22:02:56 +01008403/*
8404 * ":redrawtabline": force redraw of the tabline
8405 */
8406 static void
8407ex_redrawtabline(exarg_T *eap UNUSED)
8408{
8409 int r = RedrawingDisabled;
8410 int p = p_lz;
8411
8412 RedrawingDisabled = 0;
8413 p_lz = FALSE;
8414
8415 draw_tabline();
8416
8417 RedrawingDisabled = r;
8418 p_lz = p;
8419 out_flush();
8420}
8421
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008423close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424{
8425 if (redir_fd != NULL)
8426 {
8427 fclose(redir_fd);
8428 redir_fd = NULL;
8429 }
8430#ifdef FEAT_EVAL
8431 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008432 if (redir_vname)
8433 {
8434 var_redir_stop();
8435 redir_vname = 0;
8436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437#endif
8438}
8439
8440#if defined(FEAT_SESSION) && defined(USE_CRNL)
8441# define MKSESSION_NL
8442static int mksession_nl = FALSE; /* use NL only in put_eol() */
8443#endif
8444
8445/*
8446 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8447 */
8448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008449ex_mkrc(
8450 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451{
8452 FILE *fd;
8453 int failed = FALSE;
8454 char_u *fname;
8455#ifdef FEAT_BROWSE
8456 char_u *browseFile = NULL;
8457#endif
8458#ifdef FEAT_SESSION
8459 int view_session = FALSE;
8460 int using_vdir = FALSE; /* using 'viewdir'? */
8461 char_u *viewFile = NULL;
8462 unsigned *flagp;
8463#endif
8464
8465 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8466 {
8467#ifdef FEAT_SESSION
8468 view_session = TRUE;
8469#else
8470 ex_ni(eap);
8471 return;
8472#endif
8473 }
8474
8475#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008476 /* Use the short file name until ":lcd" is used. We also don't use the
8477 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008478 did_lcd = FALSE;
8479
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8481 if (eap->cmdidx == CMD_mkview
8482 && (*eap->arg == NUL
8483 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8484 {
8485 eap->forceit = TRUE;
8486 fname = get_view_file(*eap->arg);
8487 if (fname == NULL)
8488 return;
8489 viewFile = fname;
8490 using_vdir = TRUE;
8491 }
8492 else
8493#endif
8494 if (*eap->arg != NUL)
8495 fname = eap->arg;
8496 else if (eap->cmdidx == CMD_mkvimrc)
8497 fname = (char_u *)VIMRC_FILE;
8498#ifdef FEAT_SESSION
8499 else if (eap->cmdidx == CMD_mksession)
8500 fname = (char_u *)SESSION_FILE;
8501#endif
8502 else
8503 fname = (char_u *)EXRC_FILE;
8504
8505#ifdef FEAT_BROWSE
8506 if (cmdmod.browse)
8507 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008508 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509# ifdef FEAT_SESSION
8510 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8511 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8512# endif
8513 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008514 fname, (char_u *)"vim", NULL,
8515 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 if (browseFile == NULL)
8517 goto theend;
8518 fname = browseFile;
8519 eap->forceit = TRUE; /* since dialog already asked */
8520 }
8521#endif
8522
8523#if defined(FEAT_SESSION) && defined(vim_mkdir)
8524 /* When using 'viewdir' may have to create the directory. */
8525 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008526 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527#endif
8528
8529 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8530 if (fd != NULL)
8531 {
8532#ifdef FEAT_SESSION
8533 if (eap->cmdidx == CMD_mkview)
8534 flagp = &vop_flags;
8535 else
8536 flagp = &ssop_flags;
8537#endif
8538
8539#ifdef MKSESSION_NL
8540 /* "unix" in 'sessionoptions': use NL line separator */
8541 if (view_session && (*flagp & SSOP_UNIX))
8542 mksession_nl = TRUE;
8543#endif
8544
8545 /* Write the version command for :mkvimrc */
8546 if (eap->cmdidx == CMD_mkvimrc)
8547 (void)put_line(fd, "version 6.0");
8548
8549#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008550 if (eap->cmdidx == CMD_mksession)
8551 {
8552 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8553 failed = TRUE;
8554 }
8555
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 if (eap->cmdidx != CMD_mkview)
8557#endif
8558 {
8559 /* Write setting 'compatible' first, because it has side effects.
8560 * For that same reason only do it when needed. */
8561 if (p_cp)
8562 (void)put_line(fd, "if !&cp | set cp | endif");
8563 else
8564 (void)put_line(fd, "if &cp | set nocp | endif");
8565 }
8566
8567#ifdef FEAT_SESSION
8568 if (!view_session
8569 || (eap->cmdidx == CMD_mksession
8570 && (*flagp & SSOP_OPTIONS)))
8571#endif
8572 failed |= (makemap(fd, NULL) == FAIL
8573 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8574
8575#ifdef FEAT_SESSION
8576 if (!failed && view_session)
8577 {
8578 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8579 failed = TRUE;
8580 if (eap->cmdidx == CMD_mksession)
8581 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008582 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583
Bram Moolenaard9462e32011-04-11 21:35:11 +02008584 dirnow = alloc(MAXPATHL);
8585 if (dirnow == NULL)
8586 failed = TRUE;
8587 else
8588 {
8589 /*
8590 * Change to session file's dir.
8591 */
8592 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008594 *dirnow = NUL;
8595 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8596 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008597 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008598 shorten_fnames(TRUE);
8599 }
8600 else if (*dirnow != NUL
8601 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8602 {
8603 if (mch_chdir((char *)globaldir) == 0)
8604 shorten_fnames(TRUE);
8605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaard9462e32011-04-11 21:35:11 +02008607 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
Bram Moolenaard9462e32011-04-11 21:35:11 +02008609 /* restore original dir */
8610 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008612 {
8613 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008614 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008615 shorten_fnames(TRUE);
8616 }
8617 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 }
8619 }
8620 else
8621 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008622 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8623 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 }
8625 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8626 == FAIL)
8627 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008628# ifdef FEAT_SEARCH_EXTRA
8629 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8630 failed = TRUE;
8631# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008632 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8633 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008634 if (eap->cmdidx == CMD_mksession)
8635 {
8636 if (put_line(fd, "unlet SessionLoad") == FAIL)
8637 failed = TRUE;
8638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 }
8640#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008641 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8642 failed = TRUE;
8643
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 failed |= fclose(fd);
8645
8646 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008647 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8649 else if (eap->cmdidx == CMD_mksession)
8650 {
8651 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008652 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
Bram Moolenaard9462e32011-04-11 21:35:11 +02008654 tbuf = alloc(MAXPATHL);
8655 if (tbuf != NULL)
8656 {
8657 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8658 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8659 vim_free(tbuf);
8660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
8662#endif
8663#ifdef MKSESSION_NL
8664 mksession_nl = FALSE;
8665#endif
8666 }
8667
8668#ifdef FEAT_BROWSE
8669theend:
8670 vim_free(browseFile);
8671#endif
8672#ifdef FEAT_SESSION
8673 vim_free(viewFile);
8674#endif
8675}
8676
Bram Moolenaardf177f62005-02-22 08:39:57 +00008677#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8678 || defined(PROTO)
8679 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008680vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008681{
8682 if (vim_mkdir(name, prot) != 0)
8683 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008684 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008685 return FAIL;
8686 }
8687 return OK;
8688}
8689#endif
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691/*
8692 * Open a file for writing for an Ex command, with some checks.
8693 * Return file descriptor, or NULL on failure.
8694 */
8695 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008696open_exfile(
8697 char_u *fname,
8698 int forceit,
8699 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701 FILE *fd;
8702
8703#ifdef UNIX
8704 /* with Unix it is possible to open a directory */
8705 if (mch_isdir(fname))
8706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008707 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 return NULL;
8709 }
8710#endif
8711 if (!forceit && *mode != 'a' && vim_fexists(fname))
8712 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008713 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 return NULL;
8715 }
8716
8717 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008718 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720 return fd;
8721}
8722
8723/*
8724 * ":mark" and ":k".
8725 */
8726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008727ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729 pos_T pos;
8730
8731 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008732 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008734 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 else
8736 {
8737 pos = curwin->w_cursor; /* save curwin->w_cursor */
8738 curwin->w_cursor.lnum = eap->line2;
8739 beginline(BL_WHITE | BL_FIX);
8740 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008741 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8743 }
8744}
8745
8746/*
8747 * Update w_topline, w_leftcol and the cursor position.
8748 */
8749 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008750update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
8752 check_cursor(); /* put cursor on valid line */
8753 update_topline();
8754 if (!curwin->w_p_wrap)
8755 validate_cursor();
8756 update_curswant();
8757}
8758
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008760 * Save the current State and go to Normal mode.
8761 * Return TRUE if the typeahead could be saved.
8762 */
8763 int
8764save_current_state(save_state_T *sst)
8765{
8766 sst->save_msg_scroll = msg_scroll;
8767 sst->save_restart_edit = restart_edit;
8768 sst->save_msg_didout = msg_didout;
8769 sst->save_State = State;
8770 sst->save_insertmode = p_im;
8771 sst->save_finish_op = finish_op;
8772 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008773 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008774
8775 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8776 restart_edit = 0; /* don't go to Insert mode */
8777 p_im = FALSE; /* don't use 'insertmode' */
8778
8779 /*
8780 * Save the current typeahead. This is required to allow using ":normal"
8781 * from an event handler and makes sure we don't hang when the argument
8782 * ends with half a command.
8783 */
8784 save_typeahead(&sst->tabuf);
8785 return sst->tabuf.typebuf_valid;
8786}
8787
8788 void
8789restore_current_state(save_state_T *sst)
8790{
8791 /* Restore the previous typeahead. */
8792 restore_typeahead(&sst->tabuf);
8793
8794 msg_scroll = sst->save_msg_scroll;
8795 restart_edit = sst->save_restart_edit;
8796 p_im = sst->save_insertmode;
8797 finish_op = sst->save_finish_op;
8798 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008799 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008800 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8801
8802 /* Restore the state (needed when called from a function executed for
8803 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8804 State = sst->save_State;
8805#ifdef CURSOR_SHAPE
8806 ui_cursor_shape(); /* may show different cursor shape */
8807#endif
8808}
8809
8810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 * ":normal[!] {commands}": Execute normal mode commands.
8812 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008813 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008814ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008816 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 char_u *arg = NULL;
8818 int l;
8819 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008821 if (ex_normal_lock > 0)
8822 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008823 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008824 return;
8825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 if (ex_normal_busy >= p_mmd)
8827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008828 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 return;
8830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 /*
8833 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8834 * this for the K_SPECIAL leading byte, otherwise special keys will not
8835 * work.
8836 */
8837 if (has_mbyte)
8838 {
8839 int len = 0;
8840
8841 /* Count the number of characters to be escaped. */
8842 for (p = eap->arg; *p != NUL; ++p)
8843 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008844#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 if (*p == CSI) /* leadbyte CSI */
8846 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008847#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008848 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008850#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 )
8854 len += 2;
8855 }
8856 if (len > 0)
8857 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008858 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 if (arg != NULL)
8860 {
8861 len = 0;
8862 for (p = eap->arg; *p != NUL; ++p)
8863 {
8864 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008865#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 if (*p == CSI)
8867 {
8868 arg[len++] = KS_EXTRA;
8869 arg[len++] = (int)KE_CSI;
8870 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008871#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008872 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {
8874 arg[len++] = *++p;
8875 if (*p == K_SPECIAL)
8876 {
8877 arg[len++] = KS_SPECIAL;
8878 arg[len++] = KE_FILLER;
8879 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008880#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 else if (*p == CSI)
8882 {
8883 arg[len++] = KS_EXTRA;
8884 arg[len++] = (int)KE_CSI;
8885 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 }
8888 arg[len] = NUL;
8889 }
8890 }
8891 }
8892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008894 ++ex_normal_busy;
8895 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 {
8897 /*
8898 * Repeat the :normal command for each line in the range. When no
8899 * range given, execute it just once, without positioning the cursor
8900 * first.
8901 */
8902 do
8903 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 if (eap->addr_count != 0)
8905 {
8906 curwin->w_cursor.lnum = eap->line1++;
8907 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008908 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 }
8910
Bram Moolenaar13505972019-01-24 15:04:48 +01008911 exec_normal_cmd(arg != NULL
8912 ? arg
8913 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8916 }
8917
8918 /* Might not return to the main loop when in an event handler. */
8919 update_topline_cursor();
8920
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008921 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008923#ifdef FEAT_MOUSE
8924 setmouse();
8925#endif
8926#ifdef CURSOR_SHAPE
8927 ui_cursor_shape(); /* may show different cursor shape */
8928#endif
8929
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931}
8932
8933/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008934 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 */
8936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008937ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008939 if (eap->forceit)
8940 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008941 /* cursor line can be zero on startup */
8942 if (!curwin->w_cursor.lnum)
8943 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008944 coladvance((colnr_T)MAXCOL);
8945 curwin->w_curswant = MAXCOL;
8946 curwin->w_set_curswant = FALSE;
8947 }
8948
Bram Moolenaara40c5002005-01-09 21:16:21 +00008949 /* Ignore the command when already in Insert mode. Inserting an
8950 * expression register that invokes a function can do this. */
8951 if (State & INSERT)
8952 return;
8953
Bram Moolenaar64969662005-12-14 21:59:55 +00008954 if (eap->cmdidx == CMD_startinsert)
8955 restart_edit = 'a';
8956 else if (eap->cmdidx == CMD_startreplace)
8957 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008959 restart_edit = 'V';
8960
8961 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008963 if (eap->cmdidx == CMD_startinsert)
8964 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 curwin->w_curswant = 0; /* avoid MAXCOL */
8966 }
8967}
8968
8969/*
8970 * ":stopinsert"
8971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008973ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974{
8975 restart_edit = 0;
8976 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008977 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008980/*
8981 * Execute normal mode command "cmd".
8982 * "remap" can be REMAP_NONE or REMAP_YES.
8983 */
8984 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008985exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008986{
Bram Moolenaar25281632016-01-21 23:32:32 +01008987 /* Stuff the argument into the typeahead buffer. */
8988 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008989 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008990}
Bram Moolenaar25281632016-01-21 23:32:32 +01008991
Bram Moolenaar25281632016-01-21 23:32:32 +01008992/*
8993 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008994 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008995 */
8996 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008997exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008998{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008999 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009000 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009001
Bram Moolenaar905dd902019-04-07 14:21:47 +02009002 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9003 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009004 clear_oparg(&oa);
9005 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009006 while ((!stuff_empty()
9007 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009008 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009009 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009010 {
9011 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009012#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009013 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009014 && oa.op_type == OP_NOP && oa.regname == NUL
9015 && !VIsual_active)
9016 {
9017 /* If terminal_loop() returns OK we got a key that is handled
9018 * in Normal model. With FAIL we first need to position the
9019 * cursor and the screen needs to be redrawn. */
9020 if (terminal_loop(TRUE) == OK)
9021 normal_cmd(&oa, TRUE);
9022 }
9023 else
9024#endif
9025 /* execute a Normal mode cmd */
9026 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009027 }
9028}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009029
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030#ifdef FEAT_FIND_ID
9031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009032ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9035 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9036 (linenr_T)1, (linenr_T)MAXLNUM);
9037}
9038
Bram Moolenaar4033c552017-09-16 20:54:51 +02009039#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040/*
9041 * ":psearch"
9042 */
9043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009044ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
9046 g_do_tagpreview = p_pvh;
9047 ex_findpat(eap);
9048 g_do_tagpreview = 0;
9049}
9050#endif
9051
9052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009053ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
9055 int whole = TRUE;
9056 long n;
9057 char_u *p;
9058 int action;
9059
9060 switch (cmdnames[eap->cmdidx].cmd_name[2])
9061 {
9062 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9063 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9064 action = ACTION_GOTO;
9065 else
9066 action = ACTION_SHOW;
9067 break;
9068 case 'i': /* ":ilist" and ":dlist" */
9069 action = ACTION_SHOW_ALL;
9070 break;
9071 case 'u': /* ":ijump" and ":djump" */
9072 action = ACTION_GOTO;
9073 break;
9074 default: /* ":isplit" and ":dsplit" */
9075 action = ACTION_SPLIT;
9076 break;
9077 }
9078
9079 n = 1;
9080 if (vim_isdigit(*eap->arg)) /* get count */
9081 {
9082 n = getdigits(&eap->arg);
9083 eap->arg = skipwhite(eap->arg);
9084 }
9085 if (*eap->arg == '/') /* Match regexp, not just whole words */
9086 {
9087 whole = FALSE;
9088 ++eap->arg;
9089 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9090 if (*p)
9091 {
9092 *p++ = NUL;
9093 p = skipwhite(p);
9094
9095 /* Check for trailing illegal characters */
9096 if (!ends_excmd(*p))
9097 eap->errmsg = e_trailing;
9098 else
9099 eap->nextcmd = check_nextcmd(p);
9100 }
9101 }
9102 if (!eap->skip)
9103 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9104 whole, !eap->forceit,
9105 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9106 n, action, eap->line1, eap->line2);
9107}
9108#endif
9109
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110
Bram Moolenaar4033c552017-09-16 20:54:51 +02009111#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112/*
9113 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9114 */
9115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009116ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009118 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9120}
9121
9122/*
9123 * ":pedit"
9124 */
9125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009126ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127{
9128 win_T *curwin_save = curwin;
9129
9130 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009131 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009132 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 do_exedit(eap, NULL);
9134 keep_help_flag = FALSE;
9135 if (curwin != curwin_save && win_valid(curwin_save))
9136 {
9137 /* Return cursor to where we were */
9138 validate_cursor();
9139 redraw_later(VALID);
9140 win_enter(curwin_save, TRUE);
9141 }
9142 g_do_tagpreview = 0;
9143}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145
9146/*
9147 * ":stag", ":stselect" and ":stjump".
9148 */
9149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009150ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151{
9152 postponed_split = -1;
9153 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009154 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9156 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009157 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159
9160/*
9161 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9162 */
9163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009164ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
9166 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9167}
9168
9169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009170ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171{
9172 int cmd;
9173
9174 switch (name[1])
9175 {
9176 case 'j': cmd = DT_JUMP; /* ":tjump" */
9177 break;
9178 case 's': cmd = DT_SELECT; /* ":tselect" */
9179 break;
9180 case 'p': cmd = DT_PREV; /* ":tprevious" */
9181 break;
9182 case 'N': cmd = DT_PREV; /* ":tNext" */
9183 break;
9184 case 'n': cmd = DT_NEXT; /* ":tnext" */
9185 break;
9186 case 'o': cmd = DT_POP; /* ":pop" */
9187 break;
9188 case 'f': /* ":tfirst" */
9189 case 'r': cmd = DT_FIRST; /* ":trewind" */
9190 break;
9191 case 'l': cmd = DT_LAST; /* ":tlast" */
9192 break;
9193 default: /* ":tag" */
9194#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009195 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009197 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 return;
9199 }
9200#endif
9201 cmd = DT_TAG;
9202 break;
9203 }
9204
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009205 if (name[0] == 'l')
9206 {
9207#ifndef FEAT_QUICKFIX
9208 ex_ni(eap);
9209 return;
9210#else
9211 cmd = DT_LTAG;
9212#endif
9213 }
9214
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9216 eap->forceit, TRUE);
9217}
9218
9219/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009220 * Check "str" for starting with a special cmdline variable.
9221 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9222 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9223 */
9224 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009225find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009226{
9227 int len;
9228 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009229 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009230 "%",
9231#define SPEC_PERC 0
9232 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009233#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009234 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009235#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009236 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009237#define SPEC_CCWORD (SPEC_CWORD + 1)
9238 "<cexpr>", /* expr under cursor */
9239#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009240 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009241#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009242 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009243#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009244 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009245#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009246 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009247#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009248 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009249#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009250 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009251#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009252 "<sflnum>", /* script file line number */
9253#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009254#ifdef FEAT_CLIENTSERVER
9255 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009256# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009257#endif
9258 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009259
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009260 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009261 {
9262 len = (int)STRLEN(spec_str[i]);
9263 if (STRNCMP(src, spec_str[i], len) == 0)
9264 {
9265 *usedlen = len;
9266 return i;
9267 }
9268 }
9269 return -1;
9270}
9271
9272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 * Evaluate cmdline variables.
9274 *
9275 * change '%' to curbuf->b_ffname
9276 * '#' to curwin->w_altfile
9277 * '<cword>' to word under the cursor
9278 * '<cWORD>' to WORD under the cursor
9279 * '<cfile>' to path name under the cursor
9280 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009281 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 * '<afile>' to file name for autocommand
9283 * '<abuf>' to buffer number for autocommand
9284 * '<amatch>' to matching name for autocommand
9285 *
9286 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9287 * "" for error without a message) and NULL is returned.
9288 * Returns an allocated string if a valid match was found.
9289 * Returns NULL if no match was found. "usedlen" then still contains the
9290 * number of characters to skip.
9291 */
9292 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009293eval_vars(
9294 char_u *src, /* pointer into commandline */
9295 char_u *srcstart, /* beginning of valid memory for src */
9296 int *usedlen, /* characters after src that are used */
9297 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009298 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009299 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009300 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 int i;
9303 char_u *s;
9304 char_u *result;
9305 char_u *resultbuf = NULL;
9306 int resultlen;
9307 buf_T *buf;
9308 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9309 int spec_idx;
9310#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009311 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 int skip_mod = FALSE;
9313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315
9316 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009317 if (escaped != NULL)
9318 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319
9320 /*
9321 * Check if there is something to do.
9322 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009323 spec_idx = find_cmdline_var(src, usedlen);
9324 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 {
9326 *usedlen = 1;
9327 return NULL;
9328 }
9329
9330 /*
9331 * Skip when preceded with a backslash "\%" and "\#".
9332 * Note: In "\\%" the % is also not recognized!
9333 */
9334 if (src > srcstart && src[-1] == '\\')
9335 {
9336 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009337 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 return NULL;
9339 }
9340
9341 /*
9342 * word or WORD under cursor
9343 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009344 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9345 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009347 resultlen = find_ident_under_cursor(&result,
9348 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9349 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9350 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351 if (resultlen == 0)
9352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009353 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 return NULL;
9355 }
9356 }
9357
9358 /*
9359 * '#': Alternate file name
9360 * '%': Current file name
9361 * File name under the cursor
9362 * File name for autocommand
9363 * and following modifiers
9364 */
9365 else
9366 {
9367 switch (spec_idx)
9368 {
9369 case SPEC_PERC: /* '%': current file */
9370 if (curbuf->b_fname == NULL)
9371 {
9372 result = (char_u *)"";
9373 valid = 0; /* Must have ":p:h" to be valid */
9374 }
9375 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009376 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009377 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009378#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009379 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009380#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382 break;
9383
9384 case SPEC_HASH: /* '#' or "#99": alternate file */
9385 if (src[1] == '#') /* "##": the argument list */
9386 {
9387 result = arg_all();
9388 resultbuf = result;
9389 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009390 if (escaped != NULL)
9391 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392#ifdef FEAT_MODIFY_FNAME
9393 skip_mod = TRUE;
9394#endif
9395 break;
9396 }
9397 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009398 if (*s == '<') /* "#<99" uses v:oldfiles */
9399 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009401 if (s == src + 2 && src[1] == '-')
9402 /* just a minus sign, don't skip over it */
9403 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 *usedlen = (int)(s - src); /* length of what we expand */
9405
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009406 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009408 if (*usedlen < 2)
9409 {
9410 /* Should we give an error message for #<text? */
9411 *usedlen = 1;
9412 return NULL;
9413 }
9414#ifdef FEAT_EVAL
9415 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9416 (long)i);
9417 if (result == NULL)
9418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009419 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009420 return NULL;
9421 }
9422#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009423 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009428 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009429 if (i == 0 && src[1] == '<' && *usedlen > 1)
9430 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009431 buf = buflist_findnr(i);
9432 if (buf == NULL)
9433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009434 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009435 return NULL;
9436 }
9437 if (lnump != NULL)
9438 *lnump = ECMD_LAST;
9439 if (buf->b_fname == NULL)
9440 {
9441 result = (char_u *)"";
9442 valid = 0; /* Must have ":p:h" to be valid */
9443 }
9444 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009445 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009446 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009447#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009448 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009449#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009450 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 break;
9453
9454#ifdef FEAT_SEARCHPATH
9455 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009456 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 if (result == NULL)
9458 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009459 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 return NULL;
9461 }
9462 resultbuf = result; /* remember allocated string */
9463 break;
9464#endif
9465
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 case SPEC_AFILE: /* file name for autocommand */
9467 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009468 if (result != NULL && !autocmd_fname_full)
9469 {
9470 /* Still need to turn the fname into a full path. It is
9471 * postponed to avoid a delay when <afile> is not used. */
9472 autocmd_fname_full = TRUE;
9473 result = FullName_save(autocmd_fname, FALSE);
9474 vim_free(autocmd_fname);
9475 autocmd_fname = result;
9476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 if (result == NULL)
9478 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009479 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 return NULL;
9481 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009482 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 break;
9484
9485 case SPEC_ABUF: /* buffer number for autocommand */
9486 if (autocmd_bufnr <= 0)
9487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009488 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 return NULL;
9490 }
9491 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9492 result = strbuf;
9493 break;
9494
9495 case SPEC_AMATCH: /* match name for autocommand */
9496 result = autocmd_match;
9497 if (result == NULL)
9498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009499 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 return NULL;
9501 }
9502 break;
9503
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 case SPEC_SFILE: /* file name for ":so" command */
9505 result = sourcing_name;
9506 if (result == NULL)
9507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009508 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 return NULL;
9510 }
9511 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009512
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009513 case SPEC_SLNUM: /* line in file for ":so" command */
9514 if (sourcing_name == NULL || sourcing_lnum == 0)
9515 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009516 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009517 return NULL;
9518 }
9519 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9520 result = strbuf;
9521 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009522
9523#ifdef FEAT_EVAL
9524 case SPEC_SFLNUM: /* line in script file */
9525 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009527 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009528 return NULL;
9529 }
9530 sprintf((char *)strbuf, "%ld",
9531 (long)(current_sctx.sc_lnum + sourcing_lnum));
9532 result = strbuf;
9533 break;
9534#endif
9535
9536#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009538 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9539 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 result = strbuf;
9541 break;
9542#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009543
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009544 default:
9545 result = (char_u *)""; /* avoid gcc warning */
9546 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 }
9548
9549 resultlen = (int)STRLEN(result); /* length of new string */
9550 if (src[*usedlen] == '<') /* remove the file name extension */
9551 {
9552 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 resultlen = (int)(s - result);
9555 }
9556#ifdef FEAT_MODIFY_FNAME
9557 else if (!skip_mod)
9558 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009559 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 &resultlen);
9561 if (result == NULL)
9562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009563 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 return NULL;
9565 }
9566 }
9567#endif
9568 }
9569
9570 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9571 {
9572 if (valid != VALID_HEAD + VALID_PATH)
9573 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009574 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009576 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 result = NULL;
9578 }
9579 else
9580 result = vim_strnsave(result, resultlen);
9581 vim_free(resultbuf);
9582 return result;
9583}
9584
9585/*
9586 * Concatenate all files in the argument list, separated by spaces, and return
9587 * it in one allocated string.
9588 * Spaces and backslashes in the file names are escaped with a backslash.
9589 * Returns NULL when out of memory.
9590 */
9591 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009592arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 int len;
9595 int idx;
9596 char_u *retval = NULL;
9597 char_u *p;
9598
9599 /*
9600 * Do this loop two times:
9601 * first time: compute the total length
9602 * second time: concatenate the names
9603 */
9604 for (;;)
9605 {
9606 len = 0;
9607 for (idx = 0; idx < ARGCOUNT; ++idx)
9608 {
9609 p = alist_name(&ARGLIST[idx]);
9610 if (p != NULL)
9611 {
9612 if (len > 0)
9613 {
9614 /* insert a space in between names */
9615 if (retval != NULL)
9616 retval[len] = ' ';
9617 ++len;
9618 }
9619 for ( ; *p != NUL; ++p)
9620 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009621 if (*p == ' '
9622#ifndef BACKSLASH_IN_FILENAME
9623 || *p == '\\'
9624#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009625 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 {
9627 /* insert a backslash */
9628 if (retval != NULL)
9629 retval[len] = '\\';
9630 ++len;
9631 }
9632 if (retval != NULL)
9633 retval[len] = *p;
9634 ++len;
9635 }
9636 }
9637 }
9638
9639 /* second time: break here */
9640 if (retval != NULL)
9641 {
9642 retval[len] = NUL;
9643 break;
9644 }
9645
9646 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009647 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 if (retval == NULL)
9649 break;
9650 }
9651
9652 return retval;
9653}
9654
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655/*
9656 * Expand the <sfile> string in "arg".
9657 *
9658 * Returns an allocated string, or NULL for any error.
9659 */
9660 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009661expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009663 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 int len;
9665 char_u *result;
9666 char_u *newres;
9667 char_u *repl;
9668 int srclen;
9669 char_u *p;
9670
9671 result = vim_strsave(arg);
9672 if (result == NULL)
9673 return NULL;
9674
9675 for (p = result; *p; )
9676 {
9677 if (STRNCMP(p, "<sfile>", 7) != 0)
9678 ++p;
9679 else
9680 {
9681 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009682 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 if (errormsg != NULL)
9684 {
9685 if (*errormsg)
9686 emsg(errormsg);
9687 vim_free(result);
9688 return NULL;
9689 }
9690 if (repl == NULL) /* no match (cannot happen) */
9691 {
9692 p += srclen;
9693 continue;
9694 }
9695 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9696 newres = alloc(len);
9697 if (newres == NULL)
9698 {
9699 vim_free(repl);
9700 vim_free(result);
9701 return NULL;
9702 }
9703 mch_memmove(newres, result, (size_t)(p - result));
9704 STRCPY(newres + (p - result), repl);
9705 len = (int)STRLEN(newres);
9706 STRCAT(newres, p + srclen);
9707 vim_free(repl);
9708 vim_free(result);
9709 result = newres;
9710 p = newres + len; /* continue after the match */
9711 }
9712 }
9713
9714 return result;
9715}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716
9717#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009718static int ses_winsizes(FILE *fd, int restore_size,
9719 win_T *tab_firstwin);
9720static int ses_win_rec(FILE *fd, frame_T *fr);
9721static frame_T *ses_skipframe(frame_T *fr);
9722static int ses_do_frame(frame_T *fr);
9723static int ses_do_win(win_T *wp);
9724static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9725static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009726static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727
9728/*
9729 * Write openfile commands for the current buffers to an .exrc file.
9730 * Return FAIL on error, OK otherwise.
9731 */
9732 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009733makeopens(
9734 FILE *fd,
9735 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736{
9737 buf_T *buf;
9738 int only_save_windows = TRUE;
9739 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 int restore_size = TRUE;
9741 win_T *wp;
9742 char_u *sname;
9743 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009744 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009745 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009746 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009747 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009748 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009749 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750
9751 if (ssop_flags & SSOP_BUFFERS)
9752 only_save_windows = FALSE; /* Save ALL buffers */
9753
9754 /*
9755 * Begin by setting the this_session variable, and then other
9756 * sessionable variables.
9757 */
9758#ifdef FEAT_EVAL
9759 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9760 return FAIL;
9761 if (ssop_flags & SSOP_GLOBALS)
9762 if (store_session_globals(fd) == FAIL)
9763 return FAIL;
9764#endif
9765
9766 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009767 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 */
9769 if (put_line(fd, "silent only") == FAIL)
9770 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009771 if ((ssop_flags & SSOP_TABPAGES)
9772 && put_line(fd, "silent tabonly") == FAIL)
9773 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774
9775 /*
9776 * Now a :cd command to the session directory or the current directory
9777 */
9778 if (ssop_flags & SSOP_SESDIR)
9779 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009780 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9781 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 return FAIL;
9783 }
9784 else if (ssop_flags & SSOP_CURDIR)
9785 {
9786 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9787 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009788 || fputs("cd ", fd) < 0
9789 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9790 || put_eol(fd) == FAIL)
9791 {
9792 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 vim_free(sname);
9796 }
9797
9798 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009799 * If there is an empty, unnamed buffer we will wipe it out later.
9800 * Remember the buffer number.
9801 */
9802 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9803 return FAIL;
9804 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9805 return FAIL;
9806 if (put_line(fd, "endif") == FAIL)
9807 return FAIL;
9808
9809 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 * Now save the current files, current buffer first.
9811 */
9812 if (put_line(fd, "set shortmess=aoO") == FAIL)
9813 return FAIL;
9814
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009816 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9818 return FAIL;
9819
9820 if (ssop_flags & SSOP_RESIZE)
9821 {
9822 /* Note: after the restore we still check it worked!*/
9823 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9824 || put_eol(fd) == FAIL)
9825 return FAIL;
9826 }
9827
9828#ifdef FEAT_GUI
9829 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9830 {
9831 int x, y;
9832
9833 if (gui_mch_get_winpos(&x, &y) == OK)
9834 {
9835 /* Note: after the restore we still check it worked!*/
9836 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9837 return FAIL;
9838 }
9839 }
9840#endif
9841
9842 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009843 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9844 * will be displayed when creating the next tab. That resizes the windows
9845 * in the first tab, which may cause problems. Set 'showtabline' to 2
9846 * temporarily to avoid that.
9847 */
9848 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9849 {
9850 if (put_line(fd, "set stal=2") == FAIL)
9851 return FAIL;
9852 restore_stal = TRUE;
9853 }
9854
9855 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009856 * May repeat putting Windows for each tab, when "tabpages" is in
9857 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009858 * Don't use goto_tabpage(), it may change directory and trigger
9859 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009861 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009862 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009863 if ((ssop_flags & SSOP_TABPAGES))
9864 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009865 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009866
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009867 // Similar to ses_win_rec() below, populate the tab pages first so
9868 // later local options won't be copied to the new tabs.
9869 FOR_ALL_TABPAGES(tp)
9870 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009871 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009872 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009873 return FAIL;
9874 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009875 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009877 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009878 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009879 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009880
Bram Moolenaar18144c82006-04-12 21:52:12 +00009881 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009883 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009884
9885 if (tp == NULL)
9886 break; /* done all tab pages */
9887 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009888 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009889 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009890 tab_topframe = topframe;
9891 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009892 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009893 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009894 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009895 tab_topframe = tp->tp_topframe;
9896 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009897 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009898 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900
Bram Moolenaar18144c82006-04-12 21:52:12 +00009901 /*
9902 * Before creating the window layout, try loading one file. If this
9903 * is aborted we don't end up with a number of useless windows.
9904 * This may have side effects! (e.g., compressed or network file).
9905 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009906 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009907 {
9908 if (ses_do_win(wp)
9909 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009910 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009911#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02009912 && !bt_nofilename(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009913#endif
9914 )
9915 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009916 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9917 return FAIL;
9918 need_tabnext = FALSE;
9919
9920 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009921 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9922 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009923 return FAIL;
9924 if (!wp->w_arg_idx_invalid)
9925 edited_win = wp;
9926 break;
9927 }
9928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009930 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009931 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009932 return FAIL;
9933
Bram Moolenaar18144c82006-04-12 21:52:12 +00009934 /*
9935 * Save current window layout.
9936 */
9937 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009939 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009941 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9942 return FAIL;
9943 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9944 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
Bram Moolenaar18144c82006-04-12 21:52:12 +00009946 /*
9947 * Check if window sizes can be restored (no windows omitted).
9948 * Remember the window number of the current window after restoring.
9949 */
9950 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009951 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009952 {
9953 if (ses_do_win(wp))
9954 ++nr;
9955 else
9956 restore_size = FALSE;
9957 if (curwin == wp)
9958 cnr = nr;
9959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960
Bram Moolenaar18144c82006-04-12 21:52:12 +00009961 /* Go to the first window. */
9962 if (put_line(fd, "wincmd t") == FAIL)
9963 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009964
Bram Moolenaar18144c82006-04-12 21:52:12 +00009965 /*
9966 * If more than one window, see if sizes can be restored.
9967 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9968 * resized when moving between windows.
9969 * Do this before restoring the view, so that the topline and the
9970 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009971 * winminheight and winminwidth need to be set to avoid an error if the
9972 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009973 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009974 if (put_line(fd, "set winminheight=0") == FAIL
9975 || put_line(fd, "set winheight=1") == FAIL
9976 || put_line(fd, "set winminwidth=0") == FAIL
9977 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009978 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009979 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009980 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009982 // Restore the tab-local working directory if specified
9983 // Do this before the windows, so that the window-local directory can
9984 // override the tab-local directory.
9985 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9986 {
9987 if (fputs("tcd ", fd) < 0
9988 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9989 || put_eol(fd) == FAIL)
9990 return FAIL;
9991 did_lcd = TRUE;
9992 }
9993
Bram Moolenaar18144c82006-04-12 21:52:12 +00009994 /*
9995 * Restore the view of the window (options, file, cursor, etc.).
9996 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009997 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009998 {
9999 if (!ses_do_win(wp))
10000 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010001 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10002 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010003 return FAIL;
10004 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10005 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010006 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010007 }
10008
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010009 /* The argument index in the first tab page is zero, need to set it in
10010 * each window. For further tab pages it's the window where we do
10011 * "tabedit". */
10012 cur_arg_idx = next_arg_idx;
10013
Bram Moolenaar18144c82006-04-12 21:52:12 +000010014 /*
10015 * Restore cursor to the current window if it's not the first one.
10016 */
10017 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10018 || put_eol(fd) == FAIL))
10019 return FAIL;
10020
10021 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010022 * Restore window sizes again after jumping around in windows, because
10023 * the current window has a minimum size while others may not.
10024 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010025 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010026 return FAIL;
10027
Bram Moolenaar18144c82006-04-12 21:52:12 +000010028 /* Don't continue in another tab page when doing only the current one
10029 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010030 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010031 break;
10032 }
10033
10034 if (ssop_flags & SSOP_TABPAGES)
10035 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010036 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10037 || put_eol(fd) == FAIL)
10038 return FAIL;
10039 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010040 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10041 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010042
Bram Moolenaard39e2752019-01-26 20:07:38 +010010043 // Now put the remaining buffers into the buffer list.
10044 // This is near the end, so that when 'hidden' is set we don't create extra
10045 // buffers. If the buffer was already created with another command the
10046 // ":badd" will have no effect.
10047 FOR_ALL_BUFFERS(buf)
10048 {
10049 if (!(only_save_windows && buf->b_nwindows == 0)
10050 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10051#ifdef FEAT_TERMINAL
10052 // Skip terminal buffers: finished ones are not useful, others
10053 // will be resurrected and result in a new buffer.
10054 && !bt_terminal(buf)
10055#endif
10056 && buf->b_fname != NULL
10057 && buf->b_p_bl)
10058 {
10059 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10060 : buf->b_wininfo->wi_fpos.lnum) < 0
10061 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
10062 return FAIL;
10063 }
10064 }
10065
Bram Moolenaar9c102382006-05-03 21:26:49 +000010066 /*
10067 * Wipe out an empty unnamed buffer we started in.
10068 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020010069 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010070 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010071 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010072 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010073 return FAIL;
10074 if (put_line(fd, "endif") == FAIL)
10075 return FAIL;
10076 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10077 return FAIL;
10078
10079 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10080 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10081 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10082 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010010083 /* Re-apply 'winminheight' and 'winminwidth'. */
10084 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
10085 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
10086 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
10088 /*
10089 * Lastly, execute the x.vim file if it exists.
10090 */
10091 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10092 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010093 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 || put_line(fd, "endif") == FAIL)
10095 return FAIL;
10096
10097 return OK;
10098}
10099
10100 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010101ses_winsizes(
10102 FILE *fd,
10103 int restore_size,
10104 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
10106 int n = 0;
10107 win_T *wp;
10108
10109 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10110 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010111 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 {
10113 if (!ses_do_win(wp))
10114 continue;
10115 ++n;
10116
10117 /* restore height when not full height */
10118 if (wp->w_height + wp->w_status_height < topframe->fr_height
10119 && (fprintf(fd,
10120 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10121 n, (long)wp->w_height, Rows / 2, Rows) < 0
10122 || put_eol(fd) == FAIL))
10123 return FAIL;
10124
10125 /* restore width when not full width */
10126 if (wp->w_width < Columns && (fprintf(fd,
10127 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10128 n, (long)wp->w_width, Columns / 2, Columns) < 0
10129 || put_eol(fd) == FAIL))
10130 return FAIL;
10131 }
10132 }
10133 else
10134 {
10135 /* Just equalise window sizes */
10136 if (put_line(fd, "wincmd =") == FAIL)
10137 return FAIL;
10138 }
10139 return OK;
10140}
10141
10142/*
10143 * Write commands to "fd" to recursively create windows for frame "fr",
10144 * horizontally and vertically split.
10145 * After the commands the last window in the frame is the current window.
10146 * Returns FAIL when writing the commands to "fd" fails.
10147 */
10148 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010149ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150{
10151 frame_T *frc;
10152 int count = 0;
10153
10154 if (fr->fr_layout != FR_LEAF)
10155 {
10156 /* Find first frame that's not skipped and then create a window for
10157 * each following one (first frame is already there). */
10158 frc = ses_skipframe(fr->fr_child);
10159 if (frc != NULL)
10160 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10161 {
10162 /* Make window as big as possible so that we have lots of room
10163 * to split. */
10164 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10165 || put_line(fd, fr->fr_layout == FR_COL
10166 ? "split" : "vsplit") == FAIL)
10167 return FAIL;
10168 ++count;
10169 }
10170
10171 /* Go back to the first window. */
10172 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10173 ? "%dwincmd k" : "%dwincmd h", count) < 0
10174 || put_eol(fd) == FAIL))
10175 return FAIL;
10176
10177 /* Recursively create frames/windows in each window of this column or
10178 * row. */
10179 frc = ses_skipframe(fr->fr_child);
10180 while (frc != NULL)
10181 {
10182 ses_win_rec(fd, frc);
10183 frc = ses_skipframe(frc->fr_next);
10184 /* Go to next window. */
10185 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10186 return FAIL;
10187 }
10188 }
10189 return OK;
10190}
10191
10192/*
10193 * Skip frames that don't contain windows we want to save in the Session.
10194 * Returns NULL when there none.
10195 */
10196 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010197ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198{
10199 frame_T *frc;
10200
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010201 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 if (ses_do_frame(frc))
10203 break;
10204 return frc;
10205}
10206
10207/*
10208 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10209 * the Session.
10210 */
10211 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010212ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 frame_T *frc;
10215
10216 if (fr->fr_layout == FR_LEAF)
10217 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010218 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 if (ses_do_frame(frc))
10220 return TRUE;
10221 return FALSE;
10222}
10223
10224/*
10225 * Return non-zero if window "wp" is to be stored in the Session.
10226 */
10227 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010228ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010230#ifdef FEAT_TERMINAL
10231 if (bt_terminal(wp->w_buffer))
10232 return !term_is_finished(wp->w_buffer)
10233 && (ssop_flags & SSOP_TERMINAL)
10234 && term_should_restore(wp->w_buffer);
10235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 if (wp->w_buffer->b_fname == NULL
10237#ifdef FEAT_QUICKFIX
10238 /* When 'buftype' is "nofile" can't restore the window contents. */
Bram Moolenaar26910de2019-06-15 19:37:15 +020010239 || bt_nofilename(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240#endif
10241 )
10242 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010243 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 return (ssop_flags & SSOP_HELP);
10245 return TRUE;
10246}
10247
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010248 static int
10249put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10250{
10251 int r;
10252
10253 if (wp->w_curswant == MAXCOL)
10254 r = fprintf(fd, "%snormal! $", spaces);
10255 else
10256 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10257 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10258}
10259
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260/*
10261 * Write commands to "fd" to restore the view of a window.
10262 * Caller must make sure 'scrolloff' is zero.
10263 */
10264 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010265put_view(
10266 FILE *fd,
10267 win_T *wp,
10268 int add_edit, /* add ":edit" command to view */
10269 unsigned *flagp, /* vop_flags or ssop_flags */
10270 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010271 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272{
10273 win_T *save_curwin;
10274 int f;
10275 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010276 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277
10278 /* Always restore cursor position for ":mksession". For ":mkview" only
10279 * when 'viewoptions' contains "cursor". */
10280 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10281
10282 /*
10283 * Local argument list.
10284 */
10285 if (wp->w_alist == &global_alist)
10286 {
10287 if (put_line(fd, "argglobal") == FAIL)
10288 return FAIL;
10289 }
10290 else
10291 {
10292 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10293 flagp == &vop_flags
10294 || !(*flagp & SSOP_CURDIR)
10295 || wp->w_localdir != NULL, flagp) == FAIL)
10296 return FAIL;
10297 }
10298
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010299 /* Only when part of a session: restore the argument index. Some
10300 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010301 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010302 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010304 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 || put_eol(fd) == FAIL)
10306 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010307 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 }
10309
10310 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010311 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010313# ifdef FEAT_TERMINAL
10314 if (bt_terminal(wp->w_buffer))
10315 {
10316 if (term_write_session(fd, wp) == FAIL)
10317 return FAIL;
10318 }
10319 else
10320# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 /*
10322 * Load the file.
10323 */
10324 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010325# ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +020010326 && !bt_nofilename(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 )
10329 {
10330 /*
10331 * Editing a file in this buffer: use ":edit file".
10332 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010333 *
10334 * Note, if a buffer for that file already exists, use :badd to
10335 * edit that buffer, to not lose folding information (:edit resets
10336 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010338 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010339 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010340 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010341 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10342 || fputs(" | else | edit ", fd) < 0
10343 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10344 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010345 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 return FAIL;
10347 }
10348 else
10349 {
10350 /* No file in this buffer, just make it empty. */
10351 if (put_line(fd, "enew") == FAIL)
10352 return FAIL;
10353#ifdef FEAT_QUICKFIX
10354 if (wp->w_buffer->b_ffname != NULL)
10355 {
10356 /* The buffer does have a name, but it's not a file name. */
10357 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010358 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 return FAIL;
10360 }
10361#endif
10362 do_cursor = FALSE;
10363 }
10364 }
10365
10366 /*
10367 * Local mappings and abbreviations.
10368 */
10369 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10370 && makemap(fd, wp->w_buffer) == FAIL)
10371 return FAIL;
10372
10373 /*
10374 * Local options. Need to go to the window temporarily.
10375 * Store only local values when using ":mkview" and when ":mksession" is
10376 * used and 'sessionoptions' doesn't include "options".
10377 * Some folding options are always stored when "folds" is included,
10378 * otherwise the folds would not be restored correctly.
10379 */
10380 save_curwin = curwin;
10381 curwin = wp;
10382 curbuf = curwin->w_buffer;
10383 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10384 f = makeset(fd, OPT_LOCAL,
10385 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10386#ifdef FEAT_FOLDING
10387 else if (*flagp & SSOP_FOLDS)
10388 f = makefoldset(fd);
10389#endif
10390 else
10391 f = OK;
10392 curwin = save_curwin;
10393 curbuf = curwin->w_buffer;
10394 if (f == FAIL)
10395 return FAIL;
10396
10397#ifdef FEAT_FOLDING
10398 /*
10399 * Save Folds when 'buftype' is empty and for help files.
10400 */
10401 if ((*flagp & SSOP_FOLDS)
10402 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010403 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 {
10405 if (put_folds(fd, wp) == FAIL)
10406 return FAIL;
10407 }
10408#endif
10409
10410 /*
10411 * Set the cursor after creating folds, since that moves the cursor.
10412 */
10413 if (do_cursor)
10414 {
10415
10416 /* Restore the cursor line in the file and relatively in the
10417 * window. Don't use "G", it changes the jumplist. */
10418 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10419 (long)wp->w_cursor.lnum,
10420 (long)(wp->w_cursor.lnum - wp->w_topline),
10421 (long)wp->w_height / 2, (long)wp->w_height) < 0
10422 || put_eol(fd) == FAIL
10423 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10424 || put_line(fd, "exe s:l") == FAIL
10425 || put_line(fd, "normal! zt") == FAIL
10426 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10427 || put_eol(fd) == FAIL)
10428 return FAIL;
10429 /* Restore the cursor column and left offset when not wrapping. */
10430 if (wp->w_cursor.col == 0)
10431 {
10432 if (put_line(fd, "normal! 0") == FAIL)
10433 return FAIL;
10434 }
10435 else
10436 {
10437 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10438 {
10439 if (fprintf(fd,
10440 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010441 (long)wp->w_virtcol + 1,
10442 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 (long)wp->w_width / 2, (long)wp->w_width) < 0
10444 || put_eol(fd) == FAIL
10445 || put_line(fd, "if s:c > 0") == FAIL
10446 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010447 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10448 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 || put_eol(fd) == FAIL
10450 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010451 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 || put_line(fd, "endif") == FAIL)
10453 return FAIL;
10454 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010455 else if (put_view_curpos(fd, wp, "") == FAIL)
10456 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 }
10458 }
10459
10460 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010461 * Local directory, if the current flag is not view options or the "curdir"
10462 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010464 if (wp->w_localdir != NULL
10465 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 {
10467 if (fputs("lcd ", fd) < 0
10468 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10469 || put_eol(fd) == FAIL)
10470 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010471 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 }
10473
10474 return OK;
10475}
10476
10477/*
10478 * Write an argument list to the session file.
10479 * Returns FAIL if writing fails.
10480 */
10481 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010482ses_arglist(
10483 FILE *fd,
10484 char *cmd,
10485 garray_T *gap,
10486 int fullname, /* TRUE: use full path name */
10487 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488{
10489 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010490 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491 char_u *s;
10492
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010493 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10494 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010495 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496 return FAIL;
10497 for (i = 0; i < gap->ga_len; ++i)
10498 {
10499 /* NULL file names are skipped (only happens when out of memory). */
10500 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10501 if (s != NULL)
10502 {
10503 if (fullname)
10504 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010505 buf = alloc(MAXPATHL);
10506 if (buf != NULL)
10507 {
10508 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10509 s = buf;
10510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010512 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010513 || ses_put_fname(fd, s, flagp) == FAIL
10514 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010515 {
10516 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010518 }
10519 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 }
10521 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010522 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523}
10524
10525/*
10526 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010527 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 * Returns FAIL if writing fails.
10529 */
10530 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010531ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532{
10533 char_u *name;
10534
10535 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010536 * the session file will be sourced.
10537 * Don't do this for ":mkview", we don't know the current directory.
10538 * Don't do this after ":lcd", we don't keep track of what the current
10539 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540 if (buf->b_sfname != NULL
10541 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010542 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010543#ifdef FEAT_AUTOCHDIR
10544 && !p_acd
10545#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010546 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 name = buf->b_sfname;
10548 else
10549 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010550 if (ses_put_fname(fd, name, flagp) == FAIL
10551 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 return FAIL;
10553 return OK;
10554}
10555
10556/*
10557 * Write a file name to the session file.
10558 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10559 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010560 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 */
10562 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010563ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564{
10565 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010566 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568
10569 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010570 if (sname == NULL)
10571 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010573 if (*flagp & SSOP_SLASH)
10574 {
10575 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010576 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010577 if (*p == '\\')
10578 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010580
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010581 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010582 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010584 if (p == NULL)
10585 return FAIL;
10586
10587 /* write the result */
10588 if (fputs((char *)p, fd) < 0)
10589 retval = FAIL;
10590
10591 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592 return retval;
10593}
10594
10595/*
10596 * ":loadview [nr]"
10597 */
10598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010599ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
10601 char_u *fname;
10602
10603 fname = get_view_file(*eap->arg);
10604 if (fname != NULL)
10605 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010606 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 vim_free(fname);
10608 }
10609}
10610
10611/*
10612 * Get the name of the view file for the current buffer.
10613 */
10614 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010615get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616{
10617 int len = 0;
10618 char_u *p, *s;
10619 char_u *retval;
10620 char_u *sname;
10621
10622 if (curbuf->b_ffname == NULL)
10623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010624 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 return NULL;
10626 }
10627 sname = home_replace_save(NULL, curbuf->b_ffname);
10628 if (sname == NULL)
10629 return NULL;
10630
10631 /*
10632 * We want a file name without separators, because we're not going to make
10633 * a directory.
10634 * "normal" path separator -> "=+"
10635 * "=" -> "=="
10636 * ":" path separator -> "=-"
10637 */
10638 for (p = sname; *p; ++p)
10639 if (*p == '=' || vim_ispathsep(*p))
10640 ++len;
Bram Moolenaar964b3742019-05-24 18:54:09 +020010641 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 if (retval != NULL)
10643 {
10644 STRCPY(retval, p_vdir);
10645 add_pathsep(retval);
10646 s = retval + STRLEN(retval);
10647 for (p = sname; *p; ++p)
10648 {
10649 if (*p == '=')
10650 {
10651 *s++ = '=';
10652 *s++ = '=';
10653 }
10654 else if (vim_ispathsep(*p))
10655 {
10656 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010657#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 if (*p == ':')
10659 *s++ = '-';
10660 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010662 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663 }
10664 else
10665 *s++ = *p;
10666 }
10667 *s++ = '=';
10668 *s++ = c;
10669 STRCPY(s, ".vim");
10670 }
10671
10672 vim_free(sname);
10673 return retval;
10674}
10675
10676#endif /* FEAT_SESSION */
10677
10678/*
10679 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10680 * Return FAIL for a write error.
10681 */
10682 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010683put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684{
10685 if (
10686#ifdef USE_CRNL
10687 (
10688# ifdef MKSESSION_NL
10689 !mksession_nl &&
10690# endif
10691 (putc('\r', fd) < 0)) ||
10692#endif
10693 (putc('\n', fd) < 0))
10694 return FAIL;
10695 return OK;
10696}
10697
10698/*
10699 * Write a line to "fd".
10700 * Return FAIL for a write error.
10701 */
10702 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010703put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704{
10705 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10706 return FAIL;
10707 return OK;
10708}
10709
10710#ifdef FEAT_VIMINFO
10711/*
10712 * ":rviminfo" and ":wviminfo".
10713 */
10714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010715ex_viminfo(
10716 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718 char_u *save_viminfo;
10719
10720 save_viminfo = p_viminfo;
10721 if (*p_viminfo == NUL)
10722 p_viminfo = (char_u *)"'100";
10723 if (eap->cmdidx == CMD_rviminfo)
10724 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010725 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10726 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010727 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 }
10729 else
10730 write_viminfo(eap->arg, eap->forceit);
10731 p_viminfo = save_viminfo;
10732}
10733#endif
10734
10735#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010736/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010737 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010738 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010739 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010741dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 if (fname == NULL)
10744 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010745 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746}
10747#endif
10748
10749/*
10750 * ":behave {mswin,xterm}"
10751 */
10752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010753ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754{
10755 if (STRCMP(eap->arg, "mswin") == 0)
10756 {
10757 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10758 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10759 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10760 set_option_value((char_u *)"keymodel", 0L,
10761 (char_u *)"startsel,stopsel", 0);
10762 }
10763 else if (STRCMP(eap->arg, "xterm") == 0)
10764 {
10765 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10766 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10767 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10768 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10769 }
10770 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010771 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772}
10773
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010774#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10775/*
10776 * Function given to ExpandGeneric() to obtain the possible arguments of the
10777 * ":behave {mswin,xterm}" command.
10778 */
10779 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010780get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010781{
10782 if (idx == 0)
10783 return (char_u *)"mswin";
10784 if (idx == 1)
10785 return (char_u *)"xterm";
10786 return NULL;
10787}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010788
10789/*
10790 * Function given to ExpandGeneric() to obtain the possible arguments of the
10791 * ":messages {clear}" command.
10792 */
10793 char_u *
10794get_messages_arg(expand_T *xp UNUSED, int idx)
10795{
10796 if (idx == 0)
10797 return (char_u *)"clear";
10798 return NULL;
10799}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010800#endif
10801
Bram Moolenaar113e1072019-01-20 15:30:40 +010010802#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010803 char_u *
10804get_mapclear_arg(expand_T *xp UNUSED, int idx)
10805{
10806 if (idx == 0)
10807 return (char_u *)"<buffer>";
10808 return NULL;
10809}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010810#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010811
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812static int filetype_detect = FALSE;
10813static int filetype_plugin = FALSE;
10814static int filetype_indent = FALSE;
10815
10816/*
10817 * ":filetype [plugin] [indent] {on,off,detect}"
10818 * on: Load the filetype.vim file to install autocommands for file types.
10819 * off: Load the ftoff.vim file to remove all autocommands for file types.
10820 * plugin on: load filetype.vim and ftplugin.vim
10821 * plugin off: load ftplugof.vim
10822 * indent on: load filetype.vim and indent.vim
10823 * indent off: load indoff.vim
10824 */
10825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010826ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827{
10828 char_u *arg = eap->arg;
10829 int plugin = FALSE;
10830 int indent = FALSE;
10831
10832 if (*eap->arg == NUL)
10833 {
10834 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010835 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 filetype_detect ? "ON" : "OFF",
10837 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10838 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10839 return;
10840 }
10841
10842 /* Accept "plugin" and "indent" in any order. */
10843 for (;;)
10844 {
10845 if (STRNCMP(arg, "plugin", 6) == 0)
10846 {
10847 plugin = TRUE;
10848 arg = skipwhite(arg + 6);
10849 continue;
10850 }
10851 if (STRNCMP(arg, "indent", 6) == 0)
10852 {
10853 indent = TRUE;
10854 arg = skipwhite(arg + 6);
10855 continue;
10856 }
10857 break;
10858 }
10859 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10860 {
10861 if (*arg == 'o' || !filetype_detect)
10862 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010863 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 filetype_detect = TRUE;
10865 if (plugin)
10866 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010867 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 filetype_plugin = TRUE;
10869 }
10870 if (indent)
10871 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010872 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 filetype_indent = TRUE;
10874 }
10875 }
10876 if (*arg == 'd')
10877 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010878 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010879 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 }
10881 }
10882 else if (STRCMP(arg, "off") == 0)
10883 {
10884 if (plugin || indent)
10885 {
10886 if (plugin)
10887 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010888 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 filetype_plugin = FALSE;
10890 }
10891 if (indent)
10892 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010893 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 filetype_indent = FALSE;
10895 }
10896 }
10897 else
10898 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010899 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 filetype_detect = FALSE;
10901 }
10902 }
10903 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010904 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905}
10906
10907/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010908 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 */
10910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010911ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
10913 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010914 {
10915 char_u *arg = eap->arg;
10916
10917 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10918 arg += 9;
10919
10920 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10921 if (arg != eap->arg)
10922 did_filetype = FALSE;
10923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010927ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928{
10929#ifdef FEAT_DIGRAPHS
10930 if (*eap->arg != NUL)
10931 putdigraph(eap->arg);
10932 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010933 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010935 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936#endif
10937}
10938
10939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010940ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941{
10942 int flags = 0;
10943
10944 if (eap->cmdidx == CMD_setlocal)
10945 flags = OPT_LOCAL;
10946 else if (eap->cmdidx == CMD_setglobal)
10947 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010948#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 if (cmdmod.browse && flags == 0)
10950 ex_options(eap);
10951 else
10952#endif
10953 (void)do_set(eap->arg, flags);
10954}
10955
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010956#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10957 void
10958set_no_hlsearch(int flag)
10959{
10960 no_hlsearch = flag;
10961# ifdef FEAT_EVAL
10962 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10963# endif
10964}
10965
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966/*
10967 * ":nohlsearch"
10968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010970ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010972 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010973 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974}
10975
10976/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010977 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 * Sets nextcmd to the start of the next command, if any. Also called when
10979 * skipping commands to find the next command.
10980 */
10981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010982ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983{
10984 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010985 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 char_u *end;
10987 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010988 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010989
10990 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010991 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010992 else
10993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010994 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010995 return;
10996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997
10998 /* First clear any old pattern. */
10999 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011000 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001
11002 if (ends_excmd(*eap->arg))
11003 end = eap->arg;
11004 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010011005 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 end = eap->arg + 4;
11007 else
11008 {
11009 p = skiptowhite(eap->arg);
11010 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011011 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 p = skipwhite(p);
11013 if (*p == NUL)
11014 {
11015 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011016 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011017 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 return;
11019 }
11020 end = skip_regexp(p + 1, *p, TRUE, NULL);
11021 if (!eap->skip)
11022 {
11023 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11024 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011025 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 eap->errmsg = e_trailing;
11027 return;
11028 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011029 if (*end != *p)
11030 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011031 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011032 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011033 return;
11034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035
11036 c = *end;
11037 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011038 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011039 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011040 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 }
11042 }
11043 eap->nextcmd = find_nextcmd(end);
11044}
11045#endif
11046
11047#ifdef FEAT_CRYPT
11048/*
11049 * ":X": Get crypt key
11050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011052ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011054 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011055 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056}
11057#endif
11058
11059#ifdef FEAT_FOLDING
11060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011061ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062{
11063 if (foldManualAllowed(TRUE))
11064 foldCreate(eap->line1, eap->line2);
11065}
11066
11067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011068ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069{
11070 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11071 eap->forceit, FALSE);
11072}
11073
11074 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011075ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076{
11077 linenr_T lnum;
11078
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011079#ifdef FEAT_CLIPBOARD
11080 start_global_changes();
11081#endif
11082
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 /* First set the marks for all lines closed/open. */
11084 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11085 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11086 ml_setmarked(lnum);
11087
11088 /* Execute the command on the marked lines. */
11089 global_exe(eap->arg);
11090 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011091#ifdef FEAT_CLIPBOARD
11092 end_global_changes();
11093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011094}
11095#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011096
Bram Moolenaar39665952018-08-15 20:59:48 +020011097#ifdef FEAT_QUICKFIX
11098/*
11099 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11100 * instead of a quickfix command.
11101 */
11102 int
11103is_loclist_cmd(int cmdidx)
11104{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011105 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011106 return FALSE;
11107 return cmdnames[cmdidx].cmd_name[0] == 'l';
11108}
11109#endif
11110
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011111# if defined(FEAT_TIMERS) || defined(PROTO)
11112 int
11113get_pressedreturn(void)
11114{
11115 return ex_pressedreturn;
11116}
11117
11118 void
11119set_pressedreturn(int val)
11120{
11121 ex_pressedreturn = val;
11122}
11123#endif