blob: 1ddd28ffe59180f1e3832527fa6958a137b15ff6 [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 Moolenaare96a2492019-06-25 04:12:16 +020023static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, 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);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_startinsert(exarg_T *eap);
262static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ex_checkpath(exarg_T *eap);
265static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#else
267# define ex_findpat ex_ni
268# define ex_checkpath ex_ni
269#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200270#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#else
273# define ex_psearch ex_ni
274#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100275static void ex_tag(exarg_T *eap);
276static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_EVAL
278# define ex_scriptnames ex_ni
279# define ex_finish ex_ni
280# define ex_echo ex_ni
281# define ex_echohl ex_ni
282# define ex_execute ex_ni
283# define ex_call ex_ni
284# define ex_if ex_ni
285# define ex_endif ex_ni
286# define ex_else ex_ni
287# define ex_while ex_ni
288# define ex_continue ex_ni
289# define ex_break ex_ni
290# define ex_endwhile ex_ni
291# define ex_throw ex_ni
292# define ex_try ex_ni
293# define ex_catch ex_ni
294# define ex_finally ex_ni
295# define ex_endtry ex_ni
296# define ex_endfunction ex_ni
297# define ex_let ex_ni
Bram Moolenaar0abb4272019-06-15 16:06:00 +0200298# define ex_const ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000300# define ex_lockvar ex_ni
301# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# define ex_function ex_ni
303# define ex_delfunction ex_ni
304# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000305# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100307static char_u *arg_all(void);
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
311#ifndef FEAT_EVAL
312# define ex_compiler ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
329static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100330#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331# define ex_options ex_ni
332#endif
333#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#else
336# define ex_nohlsearch ex_ni
337# define ex_match ex_ni
338#endif
339#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_X ex_ni
343#endif
344#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100345static void ex_fold(exarg_T *eap);
346static void ex_foldopen(exarg_T *eap);
347static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#else
349# define ex_fold ex_ni
350# define ex_foldopen ex_ni
351# define ex_folddo ex_ni
352#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100353#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354# define ex_language ex_ni
355#endif
356#ifndef FEAT_SIGNS
357# define ex_sign ex_ni
358#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200360# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000361# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200362# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364
365#ifndef FEAT_EVAL
366# define ex_debug ex_ni
367# define ex_breakadd ex_ni
368# define ex_debuggreedy ex_ni
369# define ex_breakdel ex_ni
370# define ex_breaklist ex_ni
371#endif
372
373#ifndef FEAT_CMDHIST
374# define ex_history ex_ni
375#endif
376#ifndef FEAT_JUMPLIST
377# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200378# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379# define ex_changes ex_ni
380#endif
381
Bram Moolenaar05159a02005-02-26 23:04:13 +0000382#ifndef FEAT_PROFILE
383# define ex_profile ex_ni
384#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200385#ifndef FEAT_TERMINAL
386# define ex_terminal ex_ni
387#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200388#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
389# define ex_xrestore ex_ni
390#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200391#if !defined(FEAT_TEXT_PROP)
392# define ex_popupclear ex_ni
393#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000394
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395/*
396 * Declare cmdnames[].
397 */
398#define DO_DECLARE_EXCMD
399#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200400#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100401
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static char_u dollar_command[2] = {'$', 0};
403
404
405#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000406/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407typedef struct
408{
409 char_u *line; /* command line */
410 linenr_T lnum; /* sourcing_lnum of the line */
411} wcmd_T;
412
413/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000414 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000416 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000418struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419{
420 garray_T *lines_gap; /* growarray with line info */
421 int current_line; /* last read line from growarray */
422 int repeating; /* TRUE when looping a second time */
423 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200424 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 void *cookie;
426};
427
Bram Moolenaare96a2492019-06-25 04:12:16 +0200428static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100429static int store_loop_line(garray_T *gap, char_u *line);
430static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000431
432/* Struct to save a few things while debugging. Used in do_cmdline() only. */
433struct dbg_stuff
434{
435 int trylevel;
436 int force_abort;
437 except_T *caught_stack;
438 char_u *vv_exception;
439 char_u *vv_throwpoint;
440 int did_emsg;
441 int got_int;
442 int did_throw;
443 int need_rethrow;
444 int check_cstack;
445 except_T *current_exception;
446};
447
Bram Moolenaared203462004-06-16 11:19:22 +0000448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 dsp->trylevel = trylevel; trylevel = 0;
452 dsp->force_abort = force_abort; force_abort = FALSE;
453 dsp->caught_stack = caught_stack; caught_stack = NULL;
454 dsp->vv_exception = v_exception(NULL);
455 dsp->vv_throwpoint = v_throwpoint(NULL);
456
457 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
458 dsp->did_emsg = did_emsg; did_emsg = FALSE;
459 dsp->got_int = got_int; got_int = FALSE;
460 dsp->did_throw = did_throw; did_throw = FALSE;
461 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
462 dsp->check_cstack = check_cstack; check_cstack = FALSE;
463 dsp->current_exception = current_exception; current_exception = NULL;
464}
465
466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100467restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000468{
469 suppress_errthrow = FALSE;
470 trylevel = dsp->trylevel;
471 force_abort = dsp->force_abort;
472 caught_stack = dsp->caught_stack;
473 (void)v_exception(dsp->vv_exception);
474 (void)v_throwpoint(dsp->vv_throwpoint);
475 did_emsg = dsp->did_emsg;
476 got_int = dsp->got_int;
477 did_throw = dsp->did_throw;
478 need_rethrow = dsp->need_rethrow;
479 check_cstack = dsp->check_cstack;
480 current_exception = dsp->current_exception;
481}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482#endif
483
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484/*
485 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
486 * command is given.
487 */
488 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100489do_exmode(
490 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491{
492 int save_msg_scroll;
493 int prev_msg_row;
494 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100495 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000496
497 if (improved)
498 exmode_active = EXMODE_VIM;
499 else
500 exmode_active = EXMODE_NORMAL;
501 State = NORMAL;
502
503 /* When using ":global /pat/ visual" and then "Q" we return to continue
504 * the :global command. */
505 if (global_busy)
506 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507
508 save_msg_scroll = msg_scroll;
509 ++RedrawingDisabled; /* don't redisplay the window */
510 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511#ifdef FEAT_GUI
512 /* Ignore scrollbar and mouse events in Ex mode */
513 ++hold_gui_events;
514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
Bram Moolenaar32526b32019-01-19 17:43:09 +0100516 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 while (exmode_active)
518 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000519 /* Check for a ":normal" command and no more characters left. */
520 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
521 {
522 exmode_active = FALSE;
523 break;
524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 msg_scroll = TRUE;
526 need_wait_return = FALSE;
527 ex_pressedreturn = FALSE;
528 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100529 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 prev_msg_row = msg_row;
531 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 if (improved)
533 {
534 cmdline_row = msg_row;
535 do_cmdline(NULL, getexline, NULL, 0);
536 }
537 else
538 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
539 lines_left = Rows - 1;
540
Bram Moolenaardf177f62005-02-22 08:39:57 +0000541 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100542 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100545 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 if (ex_pressedreturn)
549 {
550 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000551 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 msg_row = prev_msg_row;
553 if (prev_msg_row == Rows - 1)
554 msg_row--;
555 }
556 msg_col = 0;
557 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
558 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000561 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
562 {
563 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100564 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000565 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100566 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
569
570#ifdef FEAT_GUI
571 --hold_gui_events;
572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 --RedrawingDisabled;
574 --no_wait_return;
575 update_screen(CLEAR);
576 need_wait_return = FALSE;
577 msg_scroll = save_msg_scroll;
578}
579
580/*
581 * Execute a simple command line. Used for translated commands like "*".
582 */
583 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100584do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 return do_cmdline(cmd, NULL, NULL,
587 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
588}
589
590/*
591 * do_cmdline(): execute one Ex command line
592 *
593 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100594 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 * 2. Split up in parts separated with '|'.
596 *
597 * This function can be called recursively!
598 *
599 * flags:
600 * DOCMD_VERBOSE - The command will be included in the error message.
601 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100602 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * DOCMD_KEYTYPED - Don't reset KeyTyped.
604 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
605 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
606 *
607 * return FAIL if cmdline could not be executed, OK otherwise
608 */
609 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100610do_cmdline(
611 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200612 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613 void *cookie, /* argument for fgetline() */
614 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
616 char_u *next_cmdline; /* next cmd to execute */
617 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100618 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 static int recursive = 0; /* recursive depth */
620 int msg_didout_before_start = 0;
621 int count = 0; /* line number count */
622 int did_inc = FALSE; /* incremented RedrawingDisabled */
623 int retval = OK;
624#ifdef FEAT_EVAL
625 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000626 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 int current_line = 0; /* active line in lines_ga */
628 char_u *fname = NULL; /* function or script name */
629 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
630 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000631 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 int initial_trylevel;
633 struct msglist **saved_msg_list = NULL;
634 struct msglist *private_msg_list;
635
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100636 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200637 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000639 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000641 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100643# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644# define cmd_cookie cookie
645#endif
646 static int call_depth = 0; /* recursiveness */
647
648#ifdef FEAT_EVAL
649 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
650 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000651 * This ensures that the do_errthrow() call in do_one_cmd() does not
652 * combine the messages stored by an earlier invocation of do_one_cmd()
653 * with the command name of the later one. This would happen when
654 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 saved_msg_list = msg_list;
656 msg_list = &private_msg_list;
657 private_msg_list = NULL;
658#endif
659
660 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100661 * here. The value of 200 allows nested function calls, ":source", etc.
662 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100663 if (call_depth >= 200
664#ifdef FEAT_EVAL
665 && call_depth >= p_mfd
666#endif
667 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100669 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#ifdef FEAT_EVAL
671 /* When converting to an exception, we do not include the command name
672 * since this is not an error of the specific command. */
673 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
674 msg_list = saved_msg_list;
675#endif
676 return FAIL;
677 }
678 ++call_depth;
679
680#ifdef FEAT_EVAL
681 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000682 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 cstack.cs_trylevel = 0;
684 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000685 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
687
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100688 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689
690 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100691 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000692 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 ++ex_nesting_level;
694
695 /* Get the function or script name and the address where the next breakpoint
696 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
699 fname = func_name(real_cookie);
700 breakpoint = func_breakpoint(real_cookie);
701 dbg_tick = func_dbg_tick(real_cookie);
702 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100703 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 fname = sourcing_name;
706 breakpoint = source_breakpoint(real_cookie);
707 dbg_tick = source_dbg_tick(real_cookie);
708 }
709
710 /*
711 * Initialize "force_abort" and "suppress_errthrow" at the top level.
712 */
713 if (!recursive)
714 {
715 force_abort = FALSE;
716 suppress_errthrow = FALSE;
717 }
718
719 /*
720 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000721 * exception handling (used when debugging). Otherwise clear it to avoid
722 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000724 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000725 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200727 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
729 initial_trylevel = trylevel;
730
731 /*
732 * "did_throw" will be set to TRUE when an exception is being thrown.
733 */
734 did_throw = FALSE;
735#endif
736 /*
737 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000738 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * If force_abort is set, we cancel everything.
740 */
741 did_emsg = FALSE;
742
743 /*
744 * KeyTyped is only set when calling vgetc(). Reset it here when not
745 * calling vgetc() (sourced command lines).
746 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 if (!(flags & DOCMD_KEYTYPED)
748 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 KeyTyped = FALSE;
750
751 /*
752 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000753 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * - for multiple commands on one line, separated with '|'
755 * - when repeating until there are no more lines (for ":source")
756 */
757 next_cmdline = cmdline;
758 do
759 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000760#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100761 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000762#endif
763
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000764 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (next_cmdline == NULL
766#ifdef FEAT_EVAL
767 && !force_abort
768 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770#endif
771 )
772 did_emsg = FALSE;
773
774 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000775 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100776 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 * 3. If a line is given: Make a copy, so we can mess with it.
778 */
779
780#ifdef FEAT_EVAL
781 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
784 /* Each '|' separated command is stored separately in lines_ga, to
785 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100786 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788 /* Check if a function has returned or, unless it has an unclosed
789 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000790 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000793 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794 func_line_end(real_cookie);
795# endif
796 if (func_has_ended(real_cookie))
797 {
798 retval = FAIL;
799 break;
800 }
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 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100804 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805 script_line_end();
806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807
808 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
811 retval = FAIL;
812 break;
813 }
814
815 /* If breakpoints have been added/deleted need to check for it. */
816 if (breakpoint != NULL && dbg_tick != NULL
817 && *dbg_tick != debug_tick)
818 {
819 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100820 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 fname, sourcing_lnum);
822 *dbg_tick = debug_tick;
823 }
824
825 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
826 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
827
828 /* Did we encounter a breakpoint? */
829 if (breakpoint != NULL && *breakpoint != 0
830 && *breakpoint <= sourcing_lnum)
831 {
832 dbg_breakpoint(fname, sourcing_lnum);
833 /* Find next breakpoint. */
834 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 fname, sourcing_lnum);
837 *dbg_tick = debug_tick;
838 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000839# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000840 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841 {
842 if (getline_is_func)
843 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 script_line_start();
846 }
847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 }
849
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000850 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000852 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100853 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 * below, so that it stores lines in or reads them from
855 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000856 * while/for loop. */
857 cmd_getline = get_loop_line;
858 cmd_cookie = (void *)&cmd_loop_cookie;
859 cmd_loop_cookie.lines_gap = &lines_ga;
860 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 cmd_loop_cookie.cookie = cookie;
863 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
865 else
866 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 cmd_cookie = cookie;
869 }
870#endif
871
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100872 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 if (next_cmdline == NULL)
874 {
875 /*
876 * Need to set msg_didout for the first line after an ":if",
877 * otherwise the ":if" will be overwritten.
878 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100879 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#ifdef FEAT_EVAL
883 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
884#else
885 0
886#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200887 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {
889 /* Don't call wait_return for aborted command line. The NULL
890 * returned for the end of a sourced file or executed function
891 * doesn't do this. */
892 if (KeyTyped && !(flags & DOCMD_REPEAT))
893 need_wait_return = FALSE;
894 retval = FAIL;
895 break;
896 }
897 used_getline = TRUE;
898
899 /*
900 * Keep the first typed line. Clear it when more lines are typed.
901 */
902 if (flags & DOCMD_KEEPLINE)
903 {
904 vim_free(repeat_cmdline);
905 if (count == 0)
906 repeat_cmdline = vim_strsave(next_cmdline);
907 else
908 repeat_cmdline = NULL;
909 }
910 }
911
912 /* 3. Make a copy of the command so we can mess with it. */
913 else if (cmdline_copy == NULL)
914 {
915 next_cmdline = vim_strsave(next_cmdline);
916 if (next_cmdline == NULL)
917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100918 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 retval = FAIL;
920 break;
921 }
922 }
923 cmdline_copy = next_cmdline;
924
925#ifdef FEAT_EVAL
926 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000927 * Save the current line when inside a ":while" or ":for", and when
928 * the command looks like a ":while" or ":for", because we may need it
929 * later. When there is a '|' and another command, it is stored
930 * separately, because we need to be able to jump back to it from an
931 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000933 if (current_line == lines_ga.ga_len
934 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 retval = FAIL;
939 break;
940 }
941 }
942 did_endif = FALSE;
943#endif
944
945 if (count++ == 0)
946 {
947 /*
948 * All output from the commands is put below each other, without
949 * waiting for a return. Don't do this when executing commands
950 * from a script or when being called recursive (e.g. for ":e
951 * +command file").
952 */
953 if (!(flags & DOCMD_NOWAIT) && !recursive)
954 {
955 msg_didout_before_start = msg_didout;
956 msg_didany = FALSE; /* no output yet */
957 msg_start();
958 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200959 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 ++RedrawingDisabled;
961 did_inc = TRUE;
962 }
963 }
964
965 if (p_verbose >= 15 && sourcing_name != NULL)
966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000968 verbose_enter_scroll();
969
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100970 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000972 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100973 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000974
975 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 --no_wait_return;
977 }
978
979 /*
980 * 2. Execute one '|' separated command.
981 * do_one_cmd() will return NULL if there is no trailing '|'.
982 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
983 */
984 ++recursive;
985 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
986#ifdef FEAT_EVAL
987 &cstack,
988#endif
989 cmd_getline, cmd_cookie);
990 --recursive;
991
992#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 if (cmd_cookie == (void *)&cmd_loop_cookie)
994 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000996 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#endif
998
999 if (next_cmdline == NULL)
1000 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001001 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002#ifdef FEAT_CMDHIST
1003 /*
1004 * If the command was typed, remember it for the ':' register.
1005 * Do this AFTER executing the command to make :@: work.
1006 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001007 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 && new_last_cmdline != NULL)
1009 {
1010 vim_free(last_cmdline);
1011 last_cmdline = new_last_cmdline;
1012 new_last_cmdline = NULL;
1013 }
1014#endif
1015 }
1016 else
1017 {
1018 /* need to copy the command after the '|' to cmdline_copy, for the
1019 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001020 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 next_cmdline = cmdline_copy;
1022 }
1023
1024
1025#ifdef FEAT_EVAL
1026 /* reset did_emsg for a function that is not aborted by an error */
1027 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001028 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 && !func_has_abort(real_cookie))
1030 did_emsg = FALSE;
1031
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 {
1034 ++current_line;
1035
1036 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 * An ":endwhile", ":endfor" and ":continue" is handled here.
1038 * If we were executing commands, jump back to the ":while" or
1039 * ":for".
1040 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 /* Jump back to the matching ":while" or ":for". Be careful
1047 * not to use a cs_line[] from an entry that isn't a ":while"
1048 * or ":for": It would make "current_line" invalid and can
1049 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (!did_emsg && !got_int && !did_throw
1051 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 && (cstack.cs_flags[cstack.cs_idx]
1053 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 && cstack.cs_line[cstack.cs_idx] >= 0
1055 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1056 {
1057 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 /* remember we jumped there */
1059 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 line_breakcheck(); /* check if CTRL-C typed */
1061
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 /* Check for the next breakpoint at or after the ":while"
1063 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 if (breakpoint != NULL)
1065 {
1066 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001067 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 fname,
1069 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1070 *dbg_tick = debug_tick;
1071 }
1072 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001077 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1078 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 }
1080 }
1081
1082 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1089 }
1090 }
1091
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001092 /* Check for the next breakpoint after a watchexpression */
1093 if (breakpoint != NULL && has_watchexpr())
1094 {
1095 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1096 *dbg_tick = debug_tick;
1097 }
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 /*
1100 * When not inside any ":while" loop, clear remembered lines.
1101 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 {
1104 if (lines_ga.ga_len > 0)
1105 {
1106 sourcing_lnum =
1107 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1108 free_cmdlines(&lines_ga);
1109 }
1110 current_line = 0;
1111 }
1112
1113 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1115 * being restored at the ":endtry". Reset them here and set the
1116 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1117 * This includes the case where a missing ":endif", ":endwhile" or
1118 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 cstack.cs_lflags &= ~CSL_HAD_FINA;
1123 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1124 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 did_throw ? (void *)current_exception : NULL);
1126 did_emsg = got_int = did_throw = FALSE;
1127 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1128 }
1129
1130 /* Update global "trylevel" for recursive calls to do_cmdline() from
1131 * within this loop. */
1132 trylevel = initial_trylevel + cstack.cs_trylevel;
1133
1134 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001135 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 * files) is aborted because of an error, an interrupt, or an uncaught
1137 * exception, cancel everything. If it is left normally, reset
1138 * force_abort to get the non-EH compatible abortion behavior for
1139 * the rest of the script.
1140 */
1141 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1142 force_abort = FALSE;
1143
1144 /* Convert an interrupt to an exception if appropriate. */
1145 (void)do_intthrow(&cstack);
1146#endif /* FEAT_EVAL */
1147
1148 }
1149 /*
1150 * Continue executing command lines when:
1151 * - no CTRL-C typed, no aborting error, no exception thrown or try
1152 * conditionals need to be checked for executing finally clauses or
1153 * catching an interrupt exception
1154 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001155 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * looping for ":source" command or function call.
1157 */
1158 while (!((got_int
1159#ifdef FEAT_EVAL
1160 || (did_emsg && force_abort) || did_throw
1161#endif
1162 )
1163#ifdef FEAT_EVAL
1164 && cstack.cs_trylevel == 0
1165#endif
1166 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001167 && !(did_emsg
1168#ifdef FEAT_EVAL
1169 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001170 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001171 * the :endtry to be missed. */
1172 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1173#endif
1174 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001175 && (getline_equal(fgetline, cookie, getexmodeline)
1176 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 && (next_cmdline != NULL
1178#ifdef FEAT_EVAL
1179 || cstack.cs_idx >= 0
1180#endif
1181 || (flags & DOCMD_REPEAT)));
1182
1183 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001184 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185#ifdef FEAT_EVAL
1186 free_cmdlines(&lines_ga);
1187 ga_clear(&lines_ga);
1188
1189 if (cstack.cs_idx >= 0)
1190 {
1191 /*
1192 * If a sourced file or executed function ran to its end, report the
1193 * unclosed conditional.
1194 */
1195 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001196 && ((getline_equal(fgetline, cookie, getsourceline)
1197 && !source_finished(fgetline, cookie))
1198 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 && !func_has_ended(real_cookie))))
1200 {
1201 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001205 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 }
1210
1211 /*
1212 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1213 * ":endtry" in a sourced file or executed function. If the try
1214 * conditional is in its finally clause, ignore anything pending.
1215 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 */
1218 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001219 {
1220 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1221
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001222 if (idx >= 0)
1223 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001224 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1225 &cstack.cs_looplevel);
1226 }
1227 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 trylevel = initial_trylevel;
1229 }
1230
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1232 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001234 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 ? (char_u *)"endfunction" : (char_u *)NULL);
1236
1237 if (trylevel == 0)
1238 {
1239 /*
1240 * When an exception is being thrown out of the outermost try
1241 * conditional, discard the uncaught exception, disable the conversion
1242 * of interrupts or errors to exceptions, and ensure that no more
1243 * commands are executed.
1244 */
1245 if (did_throw)
1246 {
1247 void *p = NULL;
1248 char_u *saved_sourcing_name;
1249 int saved_sourcing_lnum;
1250 struct msglist *messages = NULL, *next;
1251
1252 /*
1253 * If the uncaught exception is a user exception, report it as an
1254 * error. If it is an error exception, display the saved error
1255 * message now. For an interrupt exception, do nothing; the
1256 * interrupt message is given elsewhere.
1257 */
1258 switch (current_exception->type)
1259 {
1260 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001261 vim_snprintf((char *)IObuff, IOSIZE,
1262 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 current_exception->value);
1264 p = vim_strsave(IObuff);
1265 break;
1266 case ET_ERROR:
1267 messages = current_exception->messages;
1268 current_exception->messages = NULL;
1269 break;
1270 case ET_INTERRUPT:
1271 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273
1274 saved_sourcing_name = sourcing_name;
1275 saved_sourcing_lnum = sourcing_lnum;
1276 sourcing_name = current_exception->throw_name;
1277 sourcing_lnum = current_exception->throw_lnum;
1278 current_exception->throw_name = NULL;
1279
1280 discard_current_exception(); /* uses IObuff if 'verbose' */
1281 suppress_errthrow = TRUE;
1282 force_abort = TRUE;
1283
1284 if (messages != NULL)
1285 {
1286 do
1287 {
1288 next = messages->next;
1289 emsg(messages->msg);
1290 vim_free(messages->msg);
1291 vim_free(messages);
1292 messages = next;
1293 }
1294 while (messages != NULL);
1295 }
1296 else if (p != NULL)
1297 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001298 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 vim_free(p);
1300 }
1301 vim_free(sourcing_name);
1302 sourcing_name = saved_sourcing_name;
1303 sourcing_lnum = saved_sourcing_lnum;
1304 }
1305
1306 /*
1307 * On an interrupt or an aborting error not converted to an exception,
1308 * disable the conversion of errors to exceptions. (Interrupts are not
1309 * converted any more, here.) This enables also the interrupt message
1310 * when force_abort is set and did_emsg unset in case of an interrupt
1311 * from a finally clause after an error.
1312 */
1313 else if (got_int || (did_emsg && force_abort))
1314 suppress_errthrow = TRUE;
1315 }
1316
1317 /*
1318 * The current cstack will be freed when do_cmdline() returns. An uncaught
1319 * exception will have to be rethrown in the previous cstack. If a function
1320 * has just returned or a script file was just finished and the previous
1321 * cstack belongs to the same function or, respectively, script file, it
1322 * will have to be checked for finally clauses to be executed due to the
1323 * ":return" or ":finish". This is done in do_one_cmd().
1324 */
1325 if (did_throw)
1326 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ex_nesting_level > func_level(real_cookie) + 1))
1331 {
1332 if (!did_throw)
1333 check_cstack = TRUE;
1334 }
1335 else
1336 {
1337 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 --ex_nesting_level;
1340 /*
1341 * Go to debug mode when returning from a function in which we are
1342 * single-stepping.
1343 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001344 if ((getline_equal(fgetline, cookie, getsourceline)
1345 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001347 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 ? (char_u *)_("End of sourced file")
1349 : (char_u *)_("End of function"));
1350 }
1351
1352 /*
1353 * Restore the exception environment (done after returning from the
1354 * debugger).
1355 */
1356 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001357 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359 msg_list = saved_msg_list;
1360#endif /* FEAT_EVAL */
1361
1362 /*
1363 * If there was too much output to fit on the command line, ask the user to
1364 * hit return before redrawing the screen. With the ":global" command we do
1365 * this only once after the command is finished.
1366 */
1367 if (did_inc)
1368 {
1369 --RedrawingDisabled;
1370 --no_wait_return;
1371 msg_scroll = FALSE;
1372
1373 /*
1374 * When just finished an ":if"-":else" which was typed, no need to
1375 * wait for hit-return. Also for an error situation.
1376 */
1377 if (retval == FAIL
1378#ifdef FEAT_EVAL
1379 || (did_endif && KeyTyped && !did_emsg)
1380#endif
1381 )
1382 {
1383 need_wait_return = FALSE;
1384 msg_didany = FALSE; /* don't wait when restarting edit */
1385 }
1386 else if (need_wait_return)
1387 {
1388 /*
1389 * The msg_start() above clears msg_didout. The wait_return we do
1390 * here should not overwrite the command that may be shown before
1391 * doing that.
1392 */
1393 msg_didout |= msg_didout_before_start;
1394 wait_return(FALSE);
1395 }
1396 }
1397
Bram Moolenaar2a942252012-11-28 23:03:07 +01001398#ifdef FEAT_EVAL
1399 did_endif = FALSE; /* in case do_cmdline used recursively */
1400#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 /*
1402 * Reset if_level, in case a sourced script file contains more ":if" than
1403 * ":endif" (could be ":if x | foo | endif").
1404 */
1405 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001406#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 --call_depth;
1409 return retval;
1410}
1411
1412#ifdef FEAT_EVAL
1413/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001414 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 */
1416 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001417get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001419 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 wcmd_T *wp;
1421 char_u *line;
1422
1423 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1424 {
1425 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001426 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001428 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001430 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001432 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001433 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 ++cp->current_line;
1435
1436 return line;
1437 }
1438
1439 KeyTyped = FALSE;
1440 ++cp->current_line;
1441 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1442 sourcing_lnum = wp->lnum;
1443 return vim_strsave(wp->line);
1444}
1445
1446/*
1447 * Store a line in "gap" so that a ":while" loop can execute it again.
1448 */
1449 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001450store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 if (ga_grow(gap, 1) == FAIL)
1453 return FAIL;
1454 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1455 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1456 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 return OK;
1458}
1459
1460/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001461 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 */
1463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001464free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465{
1466 while (gap->ga_len > 0)
1467 {
1468 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1469 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 }
1471}
1472#endif
1473
1474/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001475 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1476 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001479getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001480 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001481 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001482 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001486 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487
Bram Moolenaar89d40322006-08-29 15:30:07 +00001488 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001489 * function that's originally used to obtain the lines. This may be
1490 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001491 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001492 cp = (struct loop_cookie *)cookie;
1493 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 {
1495 gp = cp->getline;
1496 cp = cp->cookie;
1497 }
1498 return gp == func;
1499#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001500 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501#endif
1502}
1503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001505 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 * getline function. Otherwise return "cookie".
1507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001509getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001510 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001511 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512{
Bram Moolenaar13505972019-01-24 15:04:48 +01001513#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001514 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001515 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
Bram Moolenaar89d40322006-08-29 15:30:07 +00001517 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001518 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001520 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001521 cp = (struct loop_cookie *)cookie;
1522 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
1524 gp = cp->getline;
1525 cp = cp->cookie;
1526 }
1527 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001528#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001531}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001533
1534/*
1535 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1536 * ":bwipeout", etc.
1537 * Returns the buffer number.
1538 */
1539 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001540compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001541{
1542 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001543 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001544 int count = offset;
1545
1546 buf = firstbuf;
1547 while (buf->b_next != NULL && buf->b_fnum < lnum)
1548 buf = buf->b_next;
1549 while (count != 0)
1550 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001551 count += (offset < 0) ? 1 : -1;
1552 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1553 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001554 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001555 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001556 if (addr_type == ADDR_LOADED_BUFFERS)
1557 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001558 while (buf->b_ml.ml_mfp == NULL)
1559 {
1560 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1561 if (nextbuf == NULL)
1562 break;
1563 buf = nextbuf;
1564 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001565 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001566 /* we might have gone too far, last buffer is not loadedd */
1567 if (addr_type == ADDR_LOADED_BUFFERS)
1568 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 return buf->b_fnum;
1576}
1577
Bram Moolenaarb7316892019-05-01 18:08:42 +02001578/*
1579 * Return the window number of "win".
1580 * When "win" is NULL return the number of windows.
1581 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001582 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001583current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001584{
1585 win_T *wp;
1586 int nr = 0;
1587
Bram Moolenaar29323592016-07-24 22:04:11 +02001588 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001589 {
1590 ++nr;
1591 if (wp == win)
1592 break;
1593 }
1594 return nr;
1595}
1596
1597 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001599{
1600 tabpage_T *tp;
1601 int nr = 0;
1602
Bram Moolenaar29323592016-07-24 22:04:11 +02001603 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001604 {
1605 ++nr;
1606 if (tp == tab)
1607 break;
1608 }
1609 return nr;
1610}
1611
1612# define CURRENT_WIN_NR current_win_nr(curwin)
1613# define LAST_WIN_NR current_win_nr(NULL)
1614# define CURRENT_TAB_NR current_tab_nr(curtab)
1615# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617/*
1618 * Execute one Ex command.
1619 *
1620 * If 'sourcing' is TRUE, the command will be included in the error message.
1621 *
1622 * 1. skip comment lines and leading space
1623 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001624 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001626 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001627 * 6. parse arguments
1628 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001630 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 *
1632 * This function may be called recursively!
1633 */
1634#if (_MSC_VER == 1200)
1635/*
Bram Moolenaared203462004-06-16 11:19:22 +00001636 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001638 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639#endif
1640 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001641do_one_cmd(
1642 char_u **cmdlinep,
1643 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001645 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001647 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001648 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649{
1650 char_u *p;
1651 linenr_T lnum;
1652 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001653 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001654 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001656 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001658 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001660 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
1662 vim_memset(&ea, 0, sizeof(ea));
1663 ea.line1 = 1;
1664 ea.line2 = 1;
1665#ifdef FEAT_EVAL
1666 ++ex_nesting_level;
1667#endif
1668
Bram Moolenaar21691f82012-12-05 19:13:18 +01001669 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 if (quitmore
1671#ifdef FEAT_EVAL
1672 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001673 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001674#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001675 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001676 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 --quitmore;
1678
1679 /*
1680 * Reset browse, confirm, etc.. They are restored when returning, for
1681 * recursive calls.
1682 */
1683 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001685 /* "#!anything" is handled like a comment. */
1686 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1687 goto doend;
1688
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001689/*
1690 * 1. Skip comment lines and leading white space and colons.
1691 * 2. Handle command modifiers.
1692 */
1693 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001695 ea.cmdlinep = cmdlinep;
1696 ea.getline = fgetline;
1697 ea.cookie = cookie;
1698#ifdef FEAT_EVAL
1699 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001701 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001702 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001704 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706#ifdef FEAT_EVAL
1707 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1708 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1709#else
1710 ea.skip = (if_level > 0);
1711#endif
1712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001714 * 3. Skip over the range to find the command. Let "p" point to after it.
1715 *
1716 * We need the command to know what kind of range it uses.
1717 */
1718 cmd = ea.cmd;
1719 ea.cmd = skip_range(ea.cmd, NULL);
1720 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1721 ea.cmd = skipwhite(ea.cmd + 1);
1722 p = find_command(&ea, NULL);
1723
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001724#ifdef FEAT_EVAL
1725# ifdef FEAT_PROFILE
1726 // Count this line for profiling if skip is TRUE.
1727 if (do_profiling == PROF_YES
1728 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1729 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1730 {
1731 int skip = did_emsg || got_int || did_throw;
1732
1733 if (ea.cmdidx == CMD_catch)
1734 skip = !skip && !(cstack->cs_idx >= 0
1735 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1736 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1737 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1738 skip = skip || !(cstack->cs_idx >= 0
1739 && !(cstack->cs_flags[cstack->cs_idx]
1740 & (CSF_ACTIVE | CSF_TRUE)));
1741 else if (ea.cmdidx == CMD_finally)
1742 skip = FALSE;
1743 else if (ea.cmdidx != CMD_endif
1744 && ea.cmdidx != CMD_endfor
1745 && ea.cmdidx != CMD_endtry
1746 && ea.cmdidx != CMD_endwhile)
1747 skip = ea.skip;
1748
1749 if (!skip)
1750 {
1751 if (getline_equal(fgetline, cookie, get_func_line))
1752 func_line_exec(getline_cookie(fgetline, cookie));
1753 else if (getline_equal(fgetline, cookie, getsourceline))
1754 script_line_exec();
1755 }
1756 }
1757# endif
1758
1759 /* May go to debug mode. If this happens and the ">quit" debug command is
1760 * used, throw an interrupt exception and skip the next command. */
1761 dbg_check_breakpoint(&ea);
1762 if (!ea.skip && got_int)
1763 {
1764 ea.skip = TRUE;
1765 (void)do_intthrow(cstack);
1766 }
1767#endif
1768
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001769/*
1770 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 *
1772 * where 'addr' is:
1773 *
1774 * % (entire file)
1775 * $ [+-NUM]
1776 * 'x [+-NUM] (where x denotes a currently defined mark)
1777 * . [+-NUM]
1778 * [+-NUM]..
1779 * NUM
1780 *
1781 * The ea.cmd pointer is updated to point to the first character following the
1782 * range spec. If an initial address is found, but no second, the upper bound
1783 * is equal to the lower.
1784 */
1785
Bram Moolenaar25190db2019-05-04 15:05:28 +02001786 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001787 if (!IS_USER_CMDIDX(ea.cmdidx))
1788 {
1789 if (ea.cmdidx != CMD_SIZE)
1790 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1791 else
1792 ea.addr_type = ADDR_LINES;
1793
Bram Moolenaar25190db2019-05-04 15:05:28 +02001794 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001795 if (ea.cmdidx == CMD_wincmd && p != NULL)
1796 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001797#ifdef FEAT_QUICKFIX
1798 // :.cc in quickfix window uses line number
1799 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1800 ea.addr_type = ADDR_OTHER;
1801#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001802 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001804 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001805 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001806 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001809 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 */
1811
1812 /*
1813 * Skip ':' and any white space
1814 */
1815 ea.cmd = skipwhite(ea.cmd);
1816 while (*ea.cmd == ':')
1817 ea.cmd = skipwhite(ea.cmd + 1);
1818
1819 /*
1820 * If we got a line, but no command, then go to the line.
1821 * If we find a '|' or '\n' we set ea.nextcmd.
1822 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001823 if (*ea.cmd == NUL || *ea.cmd == '"'
1824 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
1826 /*
1827 * strange vi behaviour:
1828 * ":3" jumps to line 3
1829 * ":3|..." prints line 3
1830 * ":|" prints current line
1831 */
1832 if (ea.skip) /* skip this if inside :if */
1833 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001834 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001837 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if ((errormsg = invalid_range(&ea)) == NULL)
1839 {
1840 correct_range(&ea);
1841 ex_print(&ea);
1842 }
1843 }
1844 else if (ea.addr_count != 0)
1845 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001846 if (ea.line2 > curbuf->b_ml.ml_line_count)
1847 {
1848 /* With '-' in 'cpoptions' a line number past the file is an
1849 * error, otherwise put it at the end of the file. */
1850 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1851 ea.line2 = -1;
1852 else
1853 ea.line2 = curbuf->b_ml.ml_line_count;
1854 }
1855
1856 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001857 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 else
1859 {
1860 if (ea.line2 == 0)
1861 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 else
1863 curwin->w_cursor.lnum = ea.line2;
1864 beginline(BL_SOL | BL_FIX);
1865 }
1866 }
1867 goto doend;
1868 }
1869
Bram Moolenaard5005162014-08-22 23:05:54 +02001870 /* If this looks like an undefined user command and there are CmdUndefined
1871 * autocommands defined, trigger the matching autocommands. */
1872 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1873 && ASCII_ISUPPER(*ea.cmd)
1874 && has_cmdundefined())
1875 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001876 int ret;
1877
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001878 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001879 while (ASCII_ISALNUM(*p))
1880 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001881 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001882 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1883 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001884 /* If the autocommands did something and didn't cause an error, try
1885 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001886 p = (ret
1887#ifdef FEAT_EVAL
1888 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001889#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001890 ) ? find_command(&ea, NULL) : ea.cmd;
1891 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (p == NULL)
1894 {
1895 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001896 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 goto doend;
1898 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001899 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001900 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1901 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 {
1903 errormsg = uc_fun_cmd();
1904 goto doend;
1905 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 if (ea.cmdidx == CMD_SIZE)
1908 {
1909 if (!ea.skip)
1910 {
1911 STRCPY(IObuff, _("E492: Not an editor command"));
1912 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001913 {
1914 /* If the modifier was parsed OK the error must be in the
1915 * following command */
1916 if (after_modifier != NULL)
1917 append_command(after_modifier);
1918 else
1919 append_command(*cmdlinep);
1920 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001921 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001922 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 }
1924 goto doend;
1925 }
1926
Bram Moolenaar958636c2014-10-21 20:01:58 +02001927 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1928 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001929#ifdef HAVE_EX_SCRIPT_NI
1930 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1931#endif
1932 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
1934#ifndef FEAT_EVAL
1935 /*
1936 * When the expression evaluation is disabled, recognize the ":if" and
1937 * ":endif" commands and ignore everything in between it.
1938 */
1939 if (ea.cmdidx == CMD_if)
1940 ++if_level;
1941 if (if_level)
1942 {
1943 if (ea.cmdidx == CMD_endif)
1944 --if_level;
1945 goto doend;
1946 }
1947
1948#endif
1949
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001950 /* forced commands */
1951 if (*p == '!' && ea.cmdidx != CMD_substitute
1952 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 {
1954 ++p;
1955 ea.forceit = TRUE;
1956 }
1957 else
1958 ea.forceit = FALSE;
1959
1960/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001961 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001963 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001964 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966 if (!ea.skip)
1967 {
1968#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001969 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001971 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001972 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 goto doend;
1974 }
1975#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001976 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001977 {
1978 errormsg = _("E981: Command not allowed in rvim");
1979 goto doend;
1980 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001981 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
1983 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001984 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 goto doend;
1986 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001987
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001988 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001989 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001991 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001992 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 goto doend;
1994 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001995
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001996 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001997 * Do allow ":checktime" (it is postponed).
1998 * Do allow ":edit" (check for an argument later).
1999 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002000 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002001 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002002 && ea.cmdidx != CMD_edit
2003 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002004 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002005 && curbuf_locked())
2006 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002008 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
2010 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto doend;
2013 }
2014 }
2015
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002016 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002018 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 goto doend;
2020 }
2021
2022 /*
2023 * Don't complain about the range if it is not used
2024 * (could happen if line_count is accidentally set to 0).
2025 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002026 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
2028 /*
2029 * If the range is backwards, ask for confirmation and, if given, swap
2030 * ea.line1 & ea.line2 so it's forwards again.
2031 * When global command is busy, don't ask, will fail below.
2032 */
2033 if (!global_busy && ea.line1 > ea.line2)
2034 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002035 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037 if (sourcing || exmode_active)
2038 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002039 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002040 goto doend;
2041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (ask_yesno((char_u *)
2043 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002044 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046 lnum = ea.line1;
2047 ea.line1 = ea.line2;
2048 ea.line2 = lnum;
2049 }
2050 if ((errormsg = invalid_range(&ea)) != NULL)
2051 goto doend;
2052 }
2053
Bram Moolenaarb7316892019-05-01 18:08:42 +02002054 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2055 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 ea.line2 = 1;
2057
2058 correct_range(&ea);
2059
2060#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002061 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002062 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
2064 /* Put the first line at the start of a closed fold, put the last line
2065 * at the end of a closed fold. */
2066 (void)hasFolding(ea.line1, &ea.line1, NULL);
2067 (void)hasFolding(ea.line2, NULL, &ea.line2);
2068 }
2069#endif
2070
2071#ifdef FEAT_QUICKFIX
2072 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002073 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 * option here, so things like % get expanded.
2075 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002076 p = replace_makeprg(&ea, p, cmdlinep);
2077 if (p == NULL)
2078 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079#endif
2080
2081 /*
2082 * Skip to start of argument.
2083 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2084 */
2085 if (ea.cmdidx == CMD_bang)
2086 ea.arg = p;
2087 else
2088 ea.arg = skipwhite(p);
2089
Bram Moolenaar379fb762018-08-30 15:58:28 +02002090 // ":file" cannot be run with an argument when "curbuf_lock" is set
2091 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2092 goto doend;
2093
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 /*
2095 * Check for "++opt=val" argument.
2096 * Must be first, allow ":w ++enc=utf8 !cmd"
2097 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002098 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2100 if (getargopt(&ea) == FAIL && !ni)
2101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002102 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 goto doend;
2104 }
2105
2106 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2107 {
2108 if (*ea.arg == '>') /* append */
2109 {
2110 if (*++ea.arg != '>') /* typed wrong */
2111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002112 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 goto doend;
2114 }
2115 ea.arg = skipwhite(ea.arg + 1);
2116 ea.append = TRUE;
2117 }
2118 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2119 {
2120 ++ea.arg;
2121 ea.usefilter = TRUE;
2122 }
2123 }
2124
2125 if (ea.cmdidx == CMD_read)
2126 {
2127 if (ea.forceit)
2128 {
2129 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2130 ea.forceit = FALSE;
2131 }
2132 else if (*ea.arg == '!') /* :r !filter */
2133 {
2134 ++ea.arg;
2135 ea.usefilter = TRUE;
2136 }
2137 }
2138
2139 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2140 {
2141 ea.amount = 1;
2142 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2143 {
2144 ++ea.arg;
2145 ++ea.amount;
2146 }
2147 ea.arg = skipwhite(ea.arg);
2148 }
2149
2150 /*
2151 * Check for "+command" argument, before checking for next command.
2152 * Don't do this for ":read !cmd" and ":write !cmd".
2153 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002154 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2156
2157 /*
2158 * Check for '|' to separate commands and '"' to start comments.
2159 * Don't do this for ":read !cmd" and ":write !cmd".
2160 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002161 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 separate_nextcmd(&ea);
2163
2164 /*
2165 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002166 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2167 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002169 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002170 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002171 || ea.cmdidx == CMD_global
2172 || ea.cmdidx == CMD_vglobal
2173 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
2175 for (p = ea.arg; *p; ++p)
2176 {
2177 /* Remove one backslash before a newline, so that it's possible to
2178 * pass a newline to the shell and also a newline that is preceded
2179 * with a backslash. This makes it impossible to end a shell
2180 * command in a backslash, but that doesn't appear useful.
2181 * Halving the number of backslashes is incompatible with previous
2182 * versions. */
2183 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002184 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 else if (*p == '\n')
2186 {
2187 ea.nextcmd = p + 1;
2188 *p = NUL;
2189 break;
2190 }
2191 }
2192 }
2193
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002194 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002196 buf_T *buf;
2197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002199 switch (ea.addr_type)
2200 {
2201 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002202 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002203 ea.line2 = curbuf->b_ml.ml_line_count;
2204 break;
2205 case ADDR_LOADED_BUFFERS:
2206 buf = firstbuf;
2207 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2208 buf = buf->b_next;
2209 ea.line1 = buf->b_fnum;
2210 buf = lastbuf;
2211 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2212 buf = buf->b_prev;
2213 ea.line2 = buf->b_fnum;
2214 break;
2215 case ADDR_BUFFERS:
2216 ea.line1 = firstbuf->b_fnum;
2217 ea.line2 = lastbuf->b_fnum;
2218 break;
2219 case ADDR_WINDOWS:
2220 ea.line2 = LAST_WIN_NR;
2221 break;
2222 case ADDR_TABS:
2223 ea.line2 = LAST_TAB_NR;
2224 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002225 case ADDR_TABS_RELATIVE:
2226 ea.line2 = 1;
2227 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002228 case ADDR_ARGUMENTS:
2229 if (ARGCOUNT == 0)
2230 ea.line1 = ea.line2 = 0;
2231 else
2232 ea.line2 = ARGCOUNT;
2233 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002234 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002235#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002236 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002237 if (ea.line2 == 0)
2238 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002239#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002240 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002241 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002242 case ADDR_UNSIGNED:
2243 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002244 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002245 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
2248
2249 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002250 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002252 /* Do not allow register = for user commands */
2253 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002254 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002256#ifndef FEAT_CLIPBOARD
2257 /* check these explicitly for a more specific error message */
2258 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002260 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002261 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002264 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2265 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002266 {
2267 ea.regname = *ea.arg++;
2268#ifdef FEAT_EVAL
2269 /* for '=' register: accept the rest of the line as an expression */
2270 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2271 {
2272 set_expr_line(vim_strsave(ea.arg));
2273 ea.arg += STRLEN(ea.arg);
2274 }
2275#endif
2276 ea.arg = skipwhite(ea.arg);
2277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279
2280 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002281 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 * count, it's a buffer name.
2283 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002284 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2285 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002286 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
2288 n = getdigits(&ea.arg);
2289 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002290 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002292 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 goto doend;
2294 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002295 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 ea.line2 = n;
2298 if (ea.addr_count == 0)
2299 ea.addr_count = 1;
2300 }
2301 else
2302 {
2303 ea.line1 = ea.line2;
2304 ea.line2 += n - 1;
2305 ++ea.addr_count;
2306 /*
2307 * Be vi compatible: no error message for out of range.
2308 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002309 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 ea.line2 = curbuf->b_ml.ml_line_count;
2311 }
2312 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002313
2314 /*
2315 * Check for flags: 'l', 'p' and '#'.
2316 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002318 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002319 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2320 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002322 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002323 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 goto doend;
2325 }
2326
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002327 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002329 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 goto doend;
2331 }
2332
2333#ifdef FEAT_EVAL
2334 /*
2335 * Skip the command when it's not going to be executed.
2336 * The commands like :if, :endif, etc. always need to be executed.
2337 * Also make an exception for commands that handle a trailing command
2338 * themselves.
2339 */
2340 if (ea.skip)
2341 {
2342 switch (ea.cmdidx)
2343 {
2344 /* commands that need evaluation */
2345 case CMD_while:
2346 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002347 case CMD_for:
2348 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 case CMD_if:
2350 case CMD_elseif:
2351 case CMD_else:
2352 case CMD_endif:
2353 case CMD_try:
2354 case CMD_catch:
2355 case CMD_finally:
2356 case CMD_endtry:
2357 case CMD_function:
2358 break;
2359
2360 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002361 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 * the list at ":help :bar". */
2363 case CMD_aboveleft:
2364 case CMD_and:
2365 case CMD_belowright:
2366 case CMD_botright:
2367 case CMD_browse:
2368 case CMD_call:
2369 case CMD_confirm:
2370 case CMD_delfunction:
2371 case CMD_djump:
2372 case CMD_dlist:
2373 case CMD_dsearch:
2374 case CMD_dsplit:
2375 case CMD_echo:
2376 case CMD_echoerr:
2377 case CMD_echomsg:
2378 case CMD_echon:
2379 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002380 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 case CMD_help:
2382 case CMD_hide:
2383 case CMD_ijump:
2384 case CMD_ilist:
2385 case CMD_isearch:
2386 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002387 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 case CMD_keepjumps:
2389 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002390 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_leftabove:
2392 case CMD_let:
2393 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002394 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002396 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002397 case CMD_noautocmd:
2398 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 case CMD_perl:
2400 case CMD_psearch:
2401 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002402 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002403 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_return:
2405 case CMD_rightbelow:
2406 case CMD_ruby:
2407 case CMD_silent:
2408 case CMD_smagic:
2409 case CMD_snomagic:
2410 case CMD_substitute:
2411 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002412 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 case CMD_tcl:
2414 case CMD_throw:
2415 case CMD_tilde:
2416 case CMD_topleft:
2417 case CMD_unlet:
2418 case CMD_verbose:
2419 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002420 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 break;
2422
2423 default: goto doend;
2424 }
2425 }
2426#endif
2427
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002428 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2431 goto doend;
2432 }
2433
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 /*
2435 * Accept buffer name. Cannot be used at the same time with a buffer
2436 * number. Don't do this for a user command.
2437 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002438 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002439 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
2441 /*
2442 * :bdelete, :bwipeout and :bunload take several arguments, separated
2443 * by spaces: find next space (skipping over escaped characters).
2444 * The others take one argument: ignore trailing spaces.
2445 */
2446 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2447 || ea.cmdidx == CMD_bunload)
2448 p = skiptowhite_esc(ea.arg);
2449 else
2450 {
2451 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002452 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 --p;
2454 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002455 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002456 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (ea.line2 < 0) /* failed */
2458 goto doend;
2459 ea.addr_count = 1;
2460 ea.arg = skipwhite(p);
2461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
Bram Moolenaar2be57332018-02-13 18:05:18 +01002463 /* The :try command saves the emsg_silent flag, reset it here when
2464 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002465 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002466 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002467 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002468 if (emsg_silent < 0)
2469 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002470 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002471 }
2472
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002474 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
Bram Moolenaar958636c2014-10-21 20:01:58 +02002477 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 /*
2480 * Execute a user-defined command.
2481 */
2482 do_ucmd(&ea);
2483 }
2484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002487 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 */
2489 ea.errmsg = NULL;
2490 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2491 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002492 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 }
2494
2495#ifdef FEAT_EVAL
2496 /*
2497 * If the command just executed called do_cmdline(), any throw or ":return"
2498 * or ":finish" encountered there must also check the cstack of the still
2499 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2500 * exception, or reanimate a returned function or finished script file and
2501 * return or finish it again.
2502 */
2503 if (need_rethrow)
2504 do_throw(cstack);
2505 else if (check_cstack)
2506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002509 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 && current_func_returned())
2511 do_return(&ea, TRUE, FALSE, NULL);
2512 }
2513 need_rethrow = check_cstack = FALSE;
2514#endif
2515
2516doend:
2517 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002518 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002520 curwin->w_cursor.col = 0;
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2524 {
2525 if (sourcing)
2526 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002527 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
2529 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002530 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002532 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
2534 emsg(errormsg);
2535 }
2536#ifdef FEAT_EVAL
2537 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002538 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2539 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540#endif
2541
Bram Moolenaareffed932018-08-14 13:38:17 +02002542 if (ea.verbose_save >= 0)
2543 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002544
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002545 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002547 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
Bram Moolenaareffed932018-08-14 13:38:17 +02002549 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
2551 /* messages could be enabled for a serious error, need to check if the
2552 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002553 if (!did_emsg || msg_silent > ea.save_msg_silent)
2554 msg_silent = ea.save_msg_silent;
2555 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 if (emsg_silent < 0)
2557 emsg_silent = 0;
2558 /* Restore msg_scroll, it's set by file I/O commands, even when no
2559 * message is actually displayed. */
2560 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002561
2562 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2563 * somewhere in the line. Put it back in the first column. */
2564 if (redirecting())
2565 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002568#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002569 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002570 --sandbox;
2571#endif
2572
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2574 ea.nextcmd = NULL;
2575
2576#ifdef FEAT_EVAL
2577 --ex_nesting_level;
2578#endif
2579
2580 return ea.nextcmd;
2581}
2582#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002583 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584#endif
2585
2586/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002587 * Parse and skip over command modifiers:
2588 * - update eap->cmd
2589 * - store flags in "cmdmod".
2590 * - Set ex_pressedreturn for an empty command line.
2591 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002592 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002593 * - set p_verbose for ":verbose"
2594 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002595 * When "skip_only" is TRUE the global variables are not changed, except for
2596 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002597 * Return FAIL when the command is not to be executed.
2598 * May set "errormsg" to an error message.
2599 */
2600 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002601parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002602{
2603 char_u *p;
2604
2605 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2606 eap->verbose_save = -1;
2607 eap->save_msg_silent = -1;
2608
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002609 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002610 for (;;)
2611 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002612 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2613 ++eap->cmd;
2614
2615 /* in ex mode, an empty line works like :+ */
2616 if (*eap->cmd == NUL && exmode_active
2617 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2618 || getline_equal(eap->getline, eap->cookie, getexline))
2619 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2620 {
2621 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002622 if (!skip_only)
2623 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002624 }
2625
2626 /* ignore comment and empty lines */
2627 if (*eap->cmd == '"')
2628 return FAIL;
2629 if (*eap->cmd == NUL)
2630 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002631 if (!skip_only)
2632 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 return FAIL;
2634 }
2635
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 p = skip_range(eap->cmd, NULL);
2637 switch (*p)
2638 {
2639 /* When adding an entry, also modify cmd_exists(). */
2640 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2641 break;
2642 cmdmod.split |= WSP_ABOVE;
2643 continue;
2644
2645 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2646 {
2647 cmdmod.split |= WSP_BELOW;
2648 continue;
2649 }
2650 if (checkforcmd(&eap->cmd, "browse", 3))
2651 {
2652#ifdef FEAT_BROWSE_CMD
2653 cmdmod.browse = TRUE;
2654#endif
2655 continue;
2656 }
2657 if (!checkforcmd(&eap->cmd, "botright", 2))
2658 break;
2659 cmdmod.split |= WSP_BOT;
2660 continue;
2661
2662 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2663 break;
2664#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2665 cmdmod.confirm = TRUE;
2666#endif
2667 continue;
2668
2669 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2670 {
2671 cmdmod.keepmarks = TRUE;
2672 continue;
2673 }
2674 if (checkforcmd(&eap->cmd, "keepalt", 5))
2675 {
2676 cmdmod.keepalt = TRUE;
2677 continue;
2678 }
2679 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2680 {
2681 cmdmod.keeppatterns = TRUE;
2682 continue;
2683 }
2684 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2685 break;
2686 cmdmod.keepjumps = TRUE;
2687 continue;
2688
2689 case 'f': /* only accept ":filter {pat} cmd" */
2690 {
2691 char_u *reg_pat;
2692
2693 if (!checkforcmd(&p, "filter", 4)
2694 || *p == NUL || ends_excmd(*p))
2695 break;
2696 if (*p == '!')
2697 {
2698 cmdmod.filter_force = TRUE;
2699 p = skipwhite(p + 1);
2700 if (*p == NUL || ends_excmd(*p))
2701 break;
2702 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002703 if (skip_only)
2704 p = skip_vimgrep_pat(p, NULL, NULL);
2705 else
2706 // NOTE: This puts a NUL after the pattern.
2707 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002708 if (p == NULL || *p == NUL)
2709 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002710 if (!skip_only)
2711 {
2712 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002713 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002714 if (cmdmod.filter_regmatch.regprog == NULL)
2715 break;
2716 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002717 eap->cmd = p;
2718 continue;
2719 }
2720
2721 /* ":hide" and ":hide | cmd" are not modifiers */
2722 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2723 || *p == NUL || ends_excmd(*p))
2724 break;
2725 eap->cmd = p;
2726 cmdmod.hide = TRUE;
2727 continue;
2728
2729 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2730 {
2731 cmdmod.lockmarks = TRUE;
2732 continue;
2733 }
2734
2735 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2736 break;
2737 cmdmod.split |= WSP_ABOVE;
2738 continue;
2739
2740 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2741 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002742 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 {
2744 /* Set 'eventignore' to "all". Restore the
2745 * existing option value later. */
2746 cmdmod.save_ei = vim_strsave(p_ei);
2747 set_string_option_direct((char_u *)"ei", -1,
2748 (char_u *)"all", OPT_FREE, SID_NONE);
2749 }
2750 continue;
2751 }
2752 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2753 break;
2754 cmdmod.noswapfile = TRUE;
2755 continue;
2756
2757 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2758 break;
2759 cmdmod.split |= WSP_BELOW;
2760 continue;
2761
2762 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2763 {
2764#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002765 if (!skip_only)
2766 {
2767 if (!eap->did_sandbox)
2768 ++sandbox;
2769 eap->did_sandbox = TRUE;
2770 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002771#endif
2772 continue;
2773 }
2774 if (!checkforcmd(&eap->cmd, "silent", 3))
2775 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (!skip_only)
2777 {
2778 if (eap->save_msg_silent == -1)
2779 eap->save_msg_silent = msg_silent;
2780 ++msg_silent;
2781 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002782 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2783 {
2784 /* ":silent!", but not "silent !cmd" */
2785 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002786 if (!skip_only)
2787 {
2788 ++emsg_silent;
2789 ++eap->did_esilent;
2790 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 }
2792 continue;
2793
2794 case 't': if (checkforcmd(&p, "tab", 3))
2795 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002796 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002798 long tabnr = get_address(eap, &eap->cmd,
2799 ADDR_TABS, eap->skip,
2800 skip_only, FALSE, 1);
2801 if (tabnr == MAXLNUM)
2802 cmdmod.tab = tabpage_index(curtab) + 1;
2803 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002805 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002807 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002808 return FAIL;
2809 }
2810 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002812 }
2813 eap->cmd = p;
2814 continue;
2815 }
2816 if (!checkforcmd(&eap->cmd, "topleft", 2))
2817 break;
2818 cmdmod.split |= WSP_TOP;
2819 continue;
2820
2821 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2822 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002823 if (!skip_only)
2824 {
2825 if (eap->save_msg_silent == -1)
2826 eap->save_msg_silent = msg_silent;
2827 msg_silent = 0;
2828 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002829 continue;
2830
2831 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2832 {
2833 cmdmod.split |= WSP_VERT;
2834 continue;
2835 }
2836 if (!checkforcmd(&p, "verbose", 4))
2837 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002838 if (!skip_only)
2839 {
2840 if (eap->verbose_save < 0)
2841 eap->verbose_save = p_verbose;
2842 if (vim_isdigit(*eap->cmd))
2843 p_verbose = atoi((char *)eap->cmd);
2844 else
2845 p_verbose = 1;
2846 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 eap->cmd = p;
2848 continue;
2849 }
2850 break;
2851 }
2852
2853 return OK;
2854}
2855
2856/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002857 * Free contents of "cmdmod".
2858 */
2859 static void
2860free_cmdmod(void)
2861{
2862 if (cmdmod.save_ei != NULL)
2863 {
2864 /* Restore 'eventignore' to the value before ":noautocmd". */
2865 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2866 OPT_FREE, SID_NONE);
2867 free_string_option(cmdmod.save_ei);
2868 }
2869
2870 if (cmdmod.filter_regmatch.regprog != NULL)
2871 vim_regfree(cmdmod.filter_regmatch.regprog);
2872}
2873
2874/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002875 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002876 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002877 * Return FAIL and set "errormsg" or return OK.
2878 */
2879 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002880parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002881{
2882 int address_count = 1;
2883 linenr_T lnum;
2884
2885 // Repeat for all ',' or ';' separated addresses.
2886 for (;;)
2887 {
2888 eap->line1 = eap->line2;
2889 switch (eap->addr_type)
2890 {
2891 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002892 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002893 // default is current line number
2894 eap->line2 = curwin->w_cursor.lnum;
2895 break;
2896 case ADDR_WINDOWS:
2897 eap->line2 = CURRENT_WIN_NR;
2898 break;
2899 case ADDR_ARGUMENTS:
2900 eap->line2 = curwin->w_arg_idx + 1;
2901 if (eap->line2 > ARGCOUNT)
2902 eap->line2 = ARGCOUNT;
2903 break;
2904 case ADDR_LOADED_BUFFERS:
2905 case ADDR_BUFFERS:
2906 eap->line2 = curbuf->b_fnum;
2907 break;
2908 case ADDR_TABS:
2909 eap->line2 = CURRENT_TAB_NR;
2910 break;
2911 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002912 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002913 eap->line2 = 1;
2914 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002916#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002917 eap->line2 = qf_get_cur_idx(eap);
2918#endif
2919 break;
2920 case ADDR_QUICKFIX_VALID:
2921#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002922 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002923#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002924 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002925 case ADDR_NONE:
2926 // Will give an error later if a range is found.
2927 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002928 }
2929 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002930 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002931 eap->addr_count == 0, address_count++);
2932 if (eap->cmd == NULL) // error detected
2933 return FAIL;
2934 if (lnum == MAXLNUM)
2935 {
2936 if (*eap->cmd == '%') // '%' - all lines
2937 {
2938 ++eap->cmd;
2939 switch (eap->addr_type)
2940 {
2941 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002942 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002943 eap->line1 = 1;
2944 eap->line2 = curbuf->b_ml.ml_line_count;
2945 break;
2946 case ADDR_LOADED_BUFFERS:
2947 {
2948 buf_T *buf = firstbuf;
2949
2950 while (buf->b_next != NULL
2951 && buf->b_ml.ml_mfp == NULL)
2952 buf = buf->b_next;
2953 eap->line1 = buf->b_fnum;
2954 buf = lastbuf;
2955 while (buf->b_prev != NULL
2956 && buf->b_ml.ml_mfp == NULL)
2957 buf = buf->b_prev;
2958 eap->line2 = buf->b_fnum;
2959 break;
2960 }
2961 case ADDR_BUFFERS:
2962 eap->line1 = firstbuf->b_fnum;
2963 eap->line2 = lastbuf->b_fnum;
2964 break;
2965 case ADDR_WINDOWS:
2966 case ADDR_TABS:
2967 if (IS_USER_CMDIDX(eap->cmdidx))
2968 {
2969 eap->line1 = 1;
2970 eap->line2 = eap->addr_type == ADDR_WINDOWS
2971 ? LAST_WIN_NR : LAST_TAB_NR;
2972 }
2973 else
2974 {
2975 // there is no Vim command which uses '%' and
2976 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002977 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002978 return FAIL;
2979 }
2980 break;
2981 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002982 case ADDR_UNSIGNED:
2983 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002984 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002985 return FAIL;
2986 case ADDR_ARGUMENTS:
2987 if (ARGCOUNT == 0)
2988 eap->line1 = eap->line2 = 0;
2989 else
2990 {
2991 eap->line1 = 1;
2992 eap->line2 = ARGCOUNT;
2993 }
2994 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002995 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002996#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002997 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002998 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002999 if (eap->line2 == 0)
3000 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003001#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003002 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003003 case ADDR_NONE:
3004 // Will give an error later if a range is found.
3005 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003006 }
3007 ++eap->addr_count;
3008 }
3009 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3010 {
3011 pos_T *fp;
3012
3013 // '*' - visual area
3014 if (eap->addr_type != ADDR_LINES)
3015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003016 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003017 return FAIL;
3018 }
3019
3020 ++eap->cmd;
3021 if (!eap->skip)
3022 {
3023 fp = getmark('<', FALSE);
3024 if (check_mark(fp) == FAIL)
3025 return FAIL;
3026 eap->line1 = fp->lnum;
3027 fp = getmark('>', FALSE);
3028 if (check_mark(fp) == FAIL)
3029 return FAIL;
3030 eap->line2 = fp->lnum;
3031 ++eap->addr_count;
3032 }
3033 }
3034 }
3035 else
3036 eap->line2 = lnum;
3037 eap->addr_count++;
3038
3039 if (*eap->cmd == ';')
3040 {
3041 if (!eap->skip)
3042 {
3043 curwin->w_cursor.lnum = eap->line2;
3044 // don't leave the cursor on an illegal line or column
3045 check_cursor();
3046 }
3047 }
3048 else if (*eap->cmd != ',')
3049 break;
3050 ++eap->cmd;
3051 }
3052
3053 // One address given: set start and end lines.
3054 if (eap->addr_count == 1)
3055 {
3056 eap->line1 = eap->line2;
3057 // ... but only implicit: really no address given
3058 if (lnum == MAXLNUM)
3059 eap->addr_count = 0;
3060 }
3061 return OK;
3062}
3063
3064/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003065 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 * If there is a match advance "pp" to the argument and return TRUE.
3067 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003068 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003069checkforcmd(
3070 char_u **pp, /* start of command */
3071 char *cmd, /* name of command */
3072 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073{
3074 int i;
3075
3076 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003077 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 break;
3079 if (i >= len && !isalpha((*pp)[i]))
3080 {
3081 *pp = skipwhite(*pp + i);
3082 return TRUE;
3083 }
3084 return FALSE;
3085}
3086
3087/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003088 * Append "cmd" to the error message in IObuff.
3089 * Takes care of limiting the length and handling 0xa0, which would be
3090 * invisible otherwise.
3091 */
3092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003093append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003094{
3095 char_u *s = cmd;
3096 char_u *d;
3097
3098 STRCAT(IObuff, ": ");
3099 d = IObuff + STRLEN(IObuff);
3100 while (*s != NUL && d - IObuff < IOSIZE - 7)
3101 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003102 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003103 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003104 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003105 STRCPY(d, "<a0>");
3106 d += 4;
3107 }
3108 else
3109 MB_COPY_CHAR(s, d);
3110 }
3111 *d = NUL;
3112}
3113
3114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003116 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003117 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003118 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 * Returns NULL for an ambiguous user command.
3120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003122find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 int len;
3125 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003126 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 /*
3129 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003130 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 * - the 'k' command can directly be followed by any character.
3132 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003133 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003135 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 */
3137 p = eap->cmd;
3138 if (*p == 'k')
3139 {
3140 eap->cmdidx = CMD_k;
3141 ++p;
3142 }
3143 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003144 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3145 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 || p[1] == 'g'
3147 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3148 || p[1] == 'I'
3149 || (p[1] == 'r' && p[2] != 'e')))
3150 {
3151 eap->cmdidx = CMD_substitute;
3152 ++p;
3153 }
3154 else
3155 {
3156 while (ASCII_ISALPHA(*p))
3157 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003158 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003159 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003160 while (ASCII_ISALNUM(*p))
3161 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003162
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 /* check for non-alpha command */
3164 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3165 ++p;
3166 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003167 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3168 {
3169 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3170 * :delete with the 'l' flag. Same for 'p'. */
3171 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003172 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003173 break;
3174 if (i == len - 1)
3175 {
3176 --len;
3177 if (p[-1] == 'l')
3178 eap->flags |= EXFLAG_LIST;
3179 else
3180 eap->flags |= EXFLAG_PRINT;
3181 }
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003184 if (ASCII_ISLOWER(eap->cmd[0]))
3185 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003186 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003187 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003188
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003189 if (command_count != (int)CMD_SIZE)
3190 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003191 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003192 getout(1);
3193 }
3194
3195 /* Use a precomputed index for fast look-up in cmdnames[]
3196 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003197 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3198 if (ASCII_ISLOWER(c2))
3199 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003202 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
3204 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3205 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3206 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3207 (size_t)len) == 0)
3208 {
3209#ifdef FEAT_EVAL
3210 if (full != NULL
3211 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3212 *full = TRUE;
3213#endif
3214 break;
3215 }
3216
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003217 // Look for a user defined command as a last resort. Let ":Print" be
3218 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003219 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3220 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003222 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 while (ASCII_ISALNUM(*p))
3224 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003225 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003227 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 eap->cmdidx = CMD_SIZE;
3229 }
3230
3231 return p;
3232}
3233
3234#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003235static struct cmdmod
3236{
3237 char *name;
3238 int minlen;
3239 int has_count; /* :123verbose :3tab */
3240} cmdmods[] = {
3241 {"aboveleft", 3, FALSE},
3242 {"belowright", 3, FALSE},
3243 {"botright", 2, FALSE},
3244 {"browse", 3, FALSE},
3245 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003246 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003247 {"hide", 3, FALSE},
3248 {"keepalt", 5, FALSE},
3249 {"keepjumps", 5, FALSE},
3250 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003251 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003252 {"leftabove", 5, FALSE},
3253 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003254 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003255 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003256 {"rightbelow", 6, FALSE},
3257 {"sandbox", 3, FALSE},
3258 {"silent", 3, FALSE},
3259 {"tab", 3, TRUE},
3260 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003261 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003262 {"verbose", 4, TRUE},
3263 {"vertical", 4, FALSE},
3264};
3265
3266/*
3267 * Return length of a command modifier (including optional count).
3268 * Return zero when it's not a modifier.
3269 */
3270 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003272{
3273 int i, j;
3274 char_u *p = cmd;
3275
3276 if (VIM_ISDIGIT(*cmd))
3277 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003278 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003279 {
3280 for (j = 0; p[j] != NUL; ++j)
3281 if (p[j] != cmdmods[i].name[j])
3282 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003283 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003284 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003285 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003286 }
3287 return 0;
3288}
3289
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290/*
3291 * Return > 0 if an Ex command "name" exists.
3292 * Return 2 if there is an exact match.
3293 * Return 3 if there is an ambiguous match.
3294 */
3295 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003296cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 exarg_T ea;
3299 int full = FALSE;
3300 int i;
3301 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003302 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303
3304 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003305 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
3307 for (j = 0; name[j] != NUL; ++j)
3308 if (name[j] != cmdmods[i].name[j])
3309 break;
3310 if (name[j] == NUL && j >= cmdmods[i].minlen)
3311 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3312 }
3313
Bram Moolenaara9587612006-05-04 21:47:50 +00003314 /* Check built-in commands and user defined commands.
3315 * For ":2match" and ":3match" we need to skip the number. */
3316 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003318 p = find_command(&ea, &full);
3319 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003321 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3322 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003323 if (*skipwhite(p) != NUL)
3324 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3326}
3327#endif
3328
3329/*
3330 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3331 * we don't need/want deleted. Maybe this could be done better if we didn't
3332 * repeat all this stuff. The only problem is that they may not stay
3333 * perfectly compatible with each other, but then the command line syntax
3334 * probably won't change that much -- webb.
3335 */
3336 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003337set_one_cmd_context(
3338 expand_T *xp,
3339 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 char_u *p;
3342 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003343 int len = 0;
3344 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003346 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 int delim;
3348#endif
3349 int forceit = FALSE;
3350 int usefilter = FALSE; /* filter instead of file name */
3351
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003352 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 xp->xp_pattern = buff;
3354 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003355 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
3357/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003358 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 */
3360 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3361 ;
3362 xp->xp_pattern = cmd;
3363
3364 if (*cmd == NUL)
3365 return NULL;
3366 if (*cmd == '"') /* ignore comment lines */
3367 {
3368 xp->xp_context = EXPAND_NOTHING;
3369 return NULL;
3370 }
3371
3372/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003373 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 */
3375 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 xp->xp_pattern = cmd;
3377 if (*cmd == NUL)
3378 return NULL;
3379 if (*cmd == '"')
3380 {
3381 xp->xp_context = EXPAND_NOTHING;
3382 return NULL;
3383 }
3384
3385 if (*cmd == '|' || *cmd == '\n')
3386 return cmd + 1; /* There's another command */
3387
3388 /*
3389 * Isolate the command and search for it in the command table.
3390 * Exceptions:
3391 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003392 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3394 */
3395 if (*cmd == 'k' && cmd[1] != 'e')
3396 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003397 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 p = cmd + 1;
3399 }
3400 else
3401 {
3402 p = cmd;
3403 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3404 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003405 /* a user command may contain digits */
3406 if (ASCII_ISUPPER(cmd[0]))
3407 while (ASCII_ISALNUM(*p) || *p == '*')
3408 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003409 /* for python 3.x: ":py3*" commands completion */
3410 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003411 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003412 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003413 while (ASCII_ISALPHA(*p) || *p == '*')
3414 ++p;
3415 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003416 /* check for non-alpha command */
3417 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3418 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003419 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003421 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
3423 xp->xp_context = EXPAND_UNSUCCESSFUL;
3424 return NULL;
3425 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003426 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003427 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3428 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3429 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 break;
3431
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003433 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436
3437 /*
3438 * If the cursor is touching the command, and it ends in an alpha-numeric
3439 * character, complete the command name.
3440 */
3441 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3442 return NULL;
3443
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003444 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 {
3446 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3447 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003448 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 p = cmd + 1;
3450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3452 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 ea.cmd = cmd;
3454 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003455#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003457#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003458 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003459#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003460 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003461 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003462 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003465 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 /* Not still touching the command and it was an illegal one */
3468 xp->xp_context = EXPAND_UNSUCCESSFUL;
3469 return NULL;
3470 }
3471
3472 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3473
3474 if (*p == '!') /* forced commands */
3475 {
3476 forceit = TRUE;
3477 ++p;
3478 }
3479
3480/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003481 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003483 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003484 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485
3486 arg = skipwhite(p);
3487
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003488 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
3490 if (*arg == '>') /* append */
3491 {
3492 if (*++arg == '>')
3493 ++arg;
3494 arg = skipwhite(arg);
3495 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003496 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 ++arg;
3499 usefilter = TRUE;
3500 }
3501 }
3502
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003503 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 usefilter = forceit; /* :r! filter if forced */
3506 if (*arg == '!') /* :r !filter */
3507 {
3508 ++arg;
3509 usefilter = TRUE;
3510 }
3511 }
3512
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003513 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 while (*arg == *cmd) /* allow any number of '>' or '<' */
3516 ++arg;
3517 arg = skipwhite(arg);
3518 }
3519
3520 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003521 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
3523 /* Check if we're in the +command */
3524 p = arg + 1;
3525 arg = skip_cmd_arg(arg, FALSE);
3526
3527 /* Still touching the command after '+'? */
3528 if (*arg == NUL)
3529 return p;
3530
3531 /* Skip space(s) after +command to get to the real argument */
3532 arg = skipwhite(arg);
3533 }
3534
3535 /*
3536 * Check for '|' to separate commands and '"' to start comments.
3537 * Don't do this for ":read !cmd" and ":write !cmd".
3538 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003539 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 {
3541 p = arg;
3542 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003543 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 p += 2;
3545 while (*p)
3546 {
3547 if (*p == Ctrl_V)
3548 {
3549 if (p[1] != NUL)
3550 ++p;
3551 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003552 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 || *p == '|' || *p == '\n')
3554 {
3555 if (*(p - 1) != '\\')
3556 {
3557 if (*p == '|' || *p == '\n')
3558 return p + 1;
3559 return NULL; /* It's a comment */
3560 }
3561 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003562 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564 }
3565
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003566 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3567 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3568 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 return NULL;
3570
3571 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003572 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003574 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003575 while (*p && p < buff + len)
3576 {
3577 if (*p == ' ' || *p == TAB)
3578 {
3579 /* argument starts after a space */
3580 xp->xp_pattern = ++p;
3581 }
3582 else
3583 {
3584 if (*p == '\\' && *(p + 1) != NUL)
3585 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003586 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003587 }
3588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003590 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003592 int c;
3593 int in_quote = FALSE;
3594 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 /*
3597 * Allow spaces within back-quotes to count as part of the argument
3598 * being expanded.
3599 */
3600 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003601 p = xp->xp_pattern;
3602 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003604 if (has_mbyte)
3605 c = mb_ptr2char(p);
3606 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003607 c = *p;
3608 if (c == '\\' && p[1] != NUL)
3609 ++p;
3610 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
3612 if (!in_quote)
3613 {
3614 xp->xp_pattern = p;
3615 bow = p + 1;
3616 }
3617 in_quote = !in_quote;
3618 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003619 /* An argument can contain just about everything, except
3620 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003621 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003622#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003623 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003624#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003625 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003626 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003627 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003628 while (*p != NUL)
3629 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003630 if (has_mbyte)
3631 c = mb_ptr2char(p);
3632 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003633 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003634 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003635 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003636 if (has_mbyte)
3637 len = (*mb_ptr2len)(p);
3638 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003639 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003640 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003641 }
3642 if (in_quote)
3643 bow = p;
3644 else
3645 xp->xp_pattern = p;
3646 p -= len;
3647 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003648 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 }
3650
3651 /*
3652 * If we are still inside the quotes, and we passed a space, just
3653 * expand from there.
3654 */
3655 if (bow != NULL && in_quote)
3656 xp->xp_pattern = bow;
3657 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003658
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003659 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003660 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003661 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003662#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003663 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003664#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003665 /* When still after the command name expand executables. */
3666 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003667 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 /* Check for environment variable */
3671 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003672#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 || *xp->xp_pattern == '%'
3674#endif
3675 )
3676 {
3677 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3678 if (!vim_isIDc(*p))
3679 break;
3680 if (*p == NUL)
3681 {
3682 xp->xp_context = EXPAND_ENV_VARS;
3683 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003684#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003685 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003686 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003687 compl = EXPAND_ENV_VARS;
3688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003691#if defined(FEAT_CMDL_COMPL)
3692 /* Check for user names */
3693 if (*xp->xp_pattern == '~')
3694 {
3695 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3696 ;
3697 /* Complete ~user only if it partially matches a user name.
3698 * A full match ~user<Tab> will be replaced by user's home
3699 * directory i.e. something like ~user<Tab> -> /home/user/ */
3700 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003701 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003702 {
3703 xp->xp_context = EXPAND_USER;
3704 ++xp->xp_pattern;
3705 }
3706 }
3707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709
3710/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003711 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003713 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003715 case CMD_find:
3716 case CMD_sfind:
3717 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003718 if (xp->xp_context == EXPAND_FILES)
3719 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003720 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 case CMD_cd:
3722 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003723 case CMD_tcd:
3724 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 case CMD_lcd:
3726 case CMD_lchdir:
3727 if (xp->xp_context == EXPAND_FILES)
3728 xp->xp_context = EXPAND_DIRECTORIES;
3729 break;
3730 case CMD_help:
3731 xp->xp_context = EXPAND_HELP;
3732 xp->xp_pattern = arg;
3733 break;
3734
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003735 /* Command modifiers: return the argument.
3736 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003738 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 case CMD_belowright:
3740 case CMD_botright:
3741 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003742 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003743 case CMD_cdo:
3744 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003746 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 case CMD_folddoclosed:
3748 case CMD_folddoopen:
3749 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003750 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 case CMD_keepjumps:
3752 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003753 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003754 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003756 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003758 case CMD_noautocmd:
3759 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003761 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003763 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003764 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 case CMD_topleft:
3766 case CMD_verbose:
3767 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003768 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 return arg;
3770
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003771 case CMD_filter:
3772 if (*arg != NUL)
3773 arg = skip_vimgrep_pat(arg, NULL, NULL);
3774 if (arg == NULL || *arg == NUL)
3775 {
3776 xp->xp_context = EXPAND_NOTHING;
3777 return NULL;
3778 }
3779 return skipwhite(arg);
3780
Bram Moolenaar4f688582007-07-24 12:34:30 +00003781#ifdef FEAT_CMDL_COMPL
3782# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 case CMD_match:
3784 if (*arg == NUL || !ends_excmd(*arg))
3785 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003786 /* also complete "None" */
3787 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 arg = skipwhite(skiptowhite(arg));
3789 if (*arg != NUL)
3790 {
3791 xp->xp_context = EXPAND_NOTHING;
3792 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3793 }
3794 }
3795 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798/*
3799 * All completion for the +cmdline_compl feature goes here.
3800 */
3801
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003803 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804
3805 case CMD_delcommand:
3806 xp->xp_context = EXPAND_USER_COMMANDS;
3807 xp->xp_pattern = arg;
3808 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 case CMD_global:
3811 case CMD_vglobal:
3812 delim = *arg; /* get the delimiter */
3813 if (delim)
3814 ++arg; /* skip delimiter if there is one */
3815
3816 while (arg[0] != NUL && arg[0] != delim)
3817 {
3818 if (arg[0] == '\\' && arg[1] != NUL)
3819 ++arg;
3820 ++arg;
3821 }
3822 if (arg[0] != NUL)
3823 return arg + 1;
3824 break;
3825 case CMD_and:
3826 case CMD_substitute:
3827 delim = *arg;
3828 if (delim)
3829 {
3830 /* skip "from" part */
3831 ++arg;
3832 arg = skip_regexp(arg, delim, p_magic, NULL);
3833 }
3834 /* skip "to" part */
3835 while (arg[0] != NUL && arg[0] != delim)
3836 {
3837 if (arg[0] == '\\' && arg[1] != NUL)
3838 ++arg;
3839 ++arg;
3840 }
3841 if (arg[0] != NUL) /* skip delimiter */
3842 ++arg;
3843 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3844 ++arg;
3845 if (arg[0] != NUL)
3846 return arg;
3847 break;
3848 case CMD_isearch:
3849 case CMD_dsearch:
3850 case CMD_ilist:
3851 case CMD_dlist:
3852 case CMD_ijump:
3853 case CMD_psearch:
3854 case CMD_djump:
3855 case CMD_isplit:
3856 case CMD_dsplit:
3857 arg = skipwhite(skipdigits(arg)); /* skip count */
3858 if (*arg == '/') /* Match regexp, not just whole words */
3859 {
3860 for (++arg; *arg && *arg != '/'; arg++)
3861 if (*arg == '\\' && arg[1] != NUL)
3862 arg++;
3863 if (*arg)
3864 {
3865 arg = skipwhite(arg + 1);
3866
3867 /* Check for trailing illegal characters */
3868 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3869 xp->xp_context = EXPAND_NOTHING;
3870 else
3871 return arg;
3872 }
3873 }
3874 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003875
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 case CMD_autocmd:
3877 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003879 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_set:
3882 set_context_in_set_cmd(xp, arg, 0);
3883 break;
3884 case CMD_setglobal:
3885 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3886 break;
3887 case CMD_setlocal:
3888 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3889 break;
3890 case CMD_tag:
3891 case CMD_stag:
3892 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003893 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 case CMD_tselect:
3895 case CMD_stselect:
3896 case CMD_ptselect:
3897 case CMD_tjump:
3898 case CMD_stjump:
3899 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003900 if (*p_wop != NUL)
3901 xp->xp_context = EXPAND_TAGS_LISTFILES;
3902 else
3903 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 xp->xp_pattern = arg;
3905 break;
3906 case CMD_augroup:
3907 xp->xp_context = EXPAND_AUGROUP;
3908 xp->xp_pattern = arg;
3909 break;
3910#ifdef FEAT_SYN_HL
3911 case CMD_syntax:
3912 set_context_in_syntax_cmd(xp, arg);
3913 break;
3914#endif
3915#ifdef FEAT_EVAL
3916 case CMD_let:
3917 case CMD_if:
3918 case CMD_elseif:
3919 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003920 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 case CMD_echo:
3922 case CMD_echon:
3923 case CMD_execute:
3924 case CMD_echomsg:
3925 case CMD_echoerr:
3926 case CMD_call:
3927 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003928 case CMD_cexpr:
3929 case CMD_caddexpr:
3930 case CMD_cgetexpr:
3931 case CMD_lexpr:
3932 case CMD_laddexpr:
3933 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003934 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
3936
3937 case CMD_unlet:
3938 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3939 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 xp->xp_context = EXPAND_USER_VARS;
3942 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003943
3944 if (*xp->xp_pattern == '$')
3945 {
3946 xp->xp_context = EXPAND_ENV_VARS;
3947 ++xp->xp_pattern;
3948 }
3949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 break;
3951
3952 case CMD_function:
3953 case CMD_delfunction:
3954 xp->xp_context = EXPAND_USER_FUNC;
3955 xp->xp_pattern = arg;
3956 break;
3957
3958 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003959 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 break;
3961#endif
3962 case CMD_highlight:
3963 set_context_in_highlight_cmd(xp, arg);
3964 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003965#ifdef FEAT_CSCOPE
3966 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003967 case CMD_lcscope:
3968 case CMD_scscope:
3969 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003970 break;
3971#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003972#ifdef FEAT_SIGNS
3973 case CMD_sign:
3974 set_context_in_sign_cmd(xp, arg);
3975 break;
3976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 case CMD_bdelete:
3978 case CMD_bwipeout:
3979 case CMD_bunload:
3980 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3981 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003982 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 case CMD_buffer:
3984 case CMD_sbuffer:
3985 case CMD_checktime:
3986 xp->xp_context = EXPAND_BUFFERS;
3987 xp->xp_pattern = arg;
3988 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 case CMD_USER:
3991 case CMD_USER_BUF:
3992 if (compl != EXPAND_NOTHING)
3993 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003994 // EX_XFILE: file names are handled above
3995 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003997#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (compl == EXPAND_MENUS)
3999 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 if (compl == EXPAND_COMMANDS)
4002 return arg;
4003 if (compl == EXPAND_MAPPINGS)
4004 return set_context_in_map_cmd(xp, (char_u *)"map",
4005 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004006 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004007 p = arg;
4008 while (*p)
4009 {
4010 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004011 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004012 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004013 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004014 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004015 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 xp->xp_pattern = arg;
4018 }
4019 xp->xp_context = compl;
4020 }
4021 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004022
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 case CMD_map: case CMD_noremap:
4024 case CMD_nmap: case CMD_nnoremap:
4025 case CMD_vmap: case CMD_vnoremap:
4026 case CMD_omap: case CMD_onoremap:
4027 case CMD_imap: case CMD_inoremap:
4028 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004029 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004030 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004031 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004032 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004034 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 case CMD_unmap:
4036 case CMD_nunmap:
4037 case CMD_vunmap:
4038 case CMD_ounmap:
4039 case CMD_iunmap:
4040 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004041 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004042 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004043 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004044 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004046 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004047 case CMD_mapclear:
4048 case CMD_nmapclear:
4049 case CMD_vmapclear:
4050 case CMD_omapclear:
4051 case CMD_imapclear:
4052 case CMD_cmapclear:
4053 case CMD_lmapclear:
4054 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004055 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004056 case CMD_xmapclear:
4057 xp->xp_context = EXPAND_MAPCLEAR;
4058 xp->xp_pattern = arg;
4059 break;
4060
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 case CMD_abbreviate: case CMD_noreabbrev:
4062 case CMD_cabbrev: case CMD_cnoreabbrev:
4063 case CMD_iabbrev: case CMD_inoreabbrev:
4064 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004065 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 case CMD_unabbreviate:
4067 case CMD_cunabbrev:
4068 case CMD_iunabbrev:
4069 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004070 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071#ifdef FEAT_MENU
4072 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4073 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4074 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4075 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4076 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4077 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4078 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004079 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 case CMD_tmenu: case CMD_tunmenu:
4081 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4082 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4083#endif
4084
4085 case CMD_colorscheme:
4086 xp->xp_context = EXPAND_COLORS;
4087 xp->xp_pattern = arg;
4088 break;
4089
4090 case CMD_compiler:
4091 xp->xp_context = EXPAND_COMPILER;
4092 xp->xp_pattern = arg;
4093 break;
4094
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004095 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004096 xp->xp_context = EXPAND_OWNSYNTAX;
4097 xp->xp_pattern = arg;
4098 break;
4099
4100 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004101 xp->xp_context = EXPAND_FILETYPE;
4102 xp->xp_pattern = arg;
4103 break;
4104
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004105 case CMD_packadd:
4106 xp->xp_context = EXPAND_PACKADD;
4107 xp->xp_pattern = arg;
4108 break;
4109
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004110#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004112 p = skiptowhite(arg);
4113 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 {
4115 xp->xp_context = EXPAND_LANGUAGE;
4116 xp->xp_pattern = arg;
4117 }
4118 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004119 {
4120 if ( STRNCMP(arg, "messages", p - arg) == 0
4121 || STRNCMP(arg, "ctype", p - arg) == 0
4122 || STRNCMP(arg, "time", p - arg) == 0)
4123 {
4124 xp->xp_context = EXPAND_LOCALES;
4125 xp->xp_pattern = skipwhite(p);
4126 }
4127 else
4128 xp->xp_context = EXPAND_NOTHING;
4129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 break;
4131#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004132#if defined(FEAT_PROFILE)
4133 case CMD_profile:
4134 set_context_in_profile_cmd(xp, arg);
4135 break;
4136#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004137 case CMD_behave:
4138 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004139 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004140 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004142 case CMD_messages:
4143 xp->xp_context = EXPAND_MESSAGES;
4144 xp->xp_pattern = arg;
4145 break;
4146
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004147#if defined(FEAT_CMDHIST)
4148 case CMD_history:
4149 xp->xp_context = EXPAND_HISTORY;
4150 xp->xp_pattern = arg;
4151 break;
4152#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004153#if defined(FEAT_PROFILE)
4154 case CMD_syntime:
4155 xp->xp_context = EXPAND_SYNTIME;
4156 xp->xp_pattern = arg;
4157 break;
4158#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004159
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004160 case CMD_argdelete:
4161 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4162 arg = xp->xp_pattern + 1;
4163 xp->xp_context = EXPAND_ARGLIST;
4164 xp->xp_pattern = arg;
4165 break;
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#endif /* FEAT_CMDL_COMPL */
4168
4169 default:
4170 break;
4171 }
4172 return NULL;
4173}
4174
4175/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004176 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 *
4178 * Backslashed delimiters after / or ? will be skipped, and commands will
4179 * not be expanded between /'s and ?'s or after "'".
4180 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004181 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 * Returns the "cmd" pointer advanced to beyond the range.
4183 */
4184 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004185skip_range(
4186 char_u *cmd,
4187 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004189 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004191 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004193 if (*cmd == '\\')
4194 {
4195 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4196 ++cmd;
4197 else
4198 break;
4199 }
4200 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
4202 if (*++cmd == NUL && ctx != NULL)
4203 *ctx = EXPAND_NOTHING;
4204 }
4205 else if (*cmd == '/' || *cmd == '?')
4206 {
4207 delim = *cmd++;
4208 while (*cmd != NUL && *cmd != delim)
4209 if (*cmd++ == '\\' && *cmd != NUL)
4210 ++cmd;
4211 if (*cmd == NUL && ctx != NULL)
4212 *ctx = EXPAND_NOTHING;
4213 }
4214 if (*cmd != NUL)
4215 ++cmd;
4216 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004217
4218 /* Skip ":" and white space. */
4219 while (*cmd == ':')
4220 cmd = skipwhite(cmd + 1);
4221
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 return cmd;
4223}
4224
4225/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004226 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 *
4228 * Set ptr to the next character after the part that was interpreted.
4229 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004230 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 *
4232 * Return MAXLNUM when no Ex address was found.
4233 */
4234 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004235get_address(
4236 exarg_T *eap UNUSED,
4237 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004238 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004239 int skip, // only skip the address, don't use it
4240 int silent, // no errors or side effects
4241 int to_other_file, // flag: may jump to other file
4242 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004244 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int c;
4246 int i;
4247 long n;
4248 char_u *cmd;
4249 pos_T pos;
4250 pos_T *fp;
4251 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004252 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
4254 cmd = skipwhite(*ptr);
4255 lnum = MAXLNUM;
4256 do
4257 {
4258 switch (*cmd)
4259 {
4260 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004261 ++cmd;
4262 switch (addr_type)
4263 {
4264 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004265 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 lnum = curwin->w_cursor.lnum;
4267 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004268 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004269 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004270 break;
4271 case ADDR_ARGUMENTS:
4272 lnum = curwin->w_arg_idx + 1;
4273 break;
4274 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004275 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004276 lnum = curbuf->b_fnum;
4277 break;
4278 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004279 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004280 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004281 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004282 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004283 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004284 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004285 cmd = NULL;
4286 goto error;
4287 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004288 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004289#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004290 lnum = qf_get_cur_idx(eap);
4291#endif
4292 break;
4293 case ADDR_QUICKFIX_VALID:
4294#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004295 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004296#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004297 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004298 }
4299 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
4301 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004302 ++cmd;
4303 switch (addr_type)
4304 {
4305 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004306 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 lnum = curbuf->b_ml.ml_line_count;
4308 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004309 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004310 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004311 break;
4312 case ADDR_ARGUMENTS:
4313 lnum = ARGCOUNT;
4314 break;
4315 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004316 buf = lastbuf;
4317 while (buf->b_ml.ml_mfp == NULL)
4318 {
4319 if (buf->b_prev == NULL)
4320 break;
4321 buf = buf->b_prev;
4322 }
4323 lnum = buf->b_fnum;
4324 break;
4325 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004326 lnum = lastbuf->b_fnum;
4327 break;
4328 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004329 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004330 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004331 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004332 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004333 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004334 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004335 cmd = NULL;
4336 goto error;
4337 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004338 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004339#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004340 lnum = qf_get_size(eap);
4341 if (lnum == 0)
4342 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004343#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004344 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004345 case ADDR_QUICKFIX_VALID:
4346#ifdef FEAT_QUICKFIX
4347 lnum = qf_get_valid_size(eap);
4348 if (lnum == 0)
4349 lnum = 1;
4350#endif
4351 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004352 }
4353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004356 if (*++cmd == NUL)
4357 {
4358 cmd = NULL;
4359 goto error;
4360 }
4361 if (addr_type != ADDR_LINES)
4362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004363 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004364 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 goto error;
4366 }
4367 if (skip)
4368 ++cmd;
4369 else
4370 {
4371 /* Only accept a mark in another file when it is
4372 * used by itself: ":'M". */
4373 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4374 ++cmd;
4375 if (fp == (pos_T *)-1)
4376 /* Jumped to another file. */
4377 lnum = curwin->w_cursor.lnum;
4378 else
4379 {
4380 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 {
4382 cmd = NULL;
4383 goto error;
4384 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004385 lnum = fp->lnum;
4386 }
4387 }
4388 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389
4390 case '/':
4391 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 c = *cmd++;
4393 if (addr_type != ADDR_LINES)
4394 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004395 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004396 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004397 goto error;
4398 }
4399 if (skip) /* skip "/pat/" */
4400 {
4401 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4402 if (*cmd == c)
4403 ++cmd;
4404 }
4405 else
4406 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004407 int flags;
4408
4409 pos = curwin->w_cursor; // save curwin->w_cursor
4410
4411 // When '/' or '?' follows another address, start from
4412 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004413 if (lnum != MAXLNUM)
4414 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004415
4416 // Start a forward search at the end of the line (unless
4417 // before the first line).
4418 // Start a backward search at the start of the line.
4419 // This makes sure we never match in the current
4420 // line, and can match anywhere in the
4421 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004422 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 curwin->w_cursor.col = MAXCOL;
4424 else
4425 curwin->w_cursor.col = 0;
4426 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004427 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4428 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 {
4430 curwin->w_cursor = pos;
4431 cmd = NULL;
4432 goto error;
4433 }
4434 lnum = curwin->w_cursor.lnum;
4435 curwin->w_cursor = pos;
4436 /* adjust command string pointer */
4437 cmd += searchcmdlen;
4438 }
4439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 ++cmd;
4443 if (addr_type != ADDR_LINES)
4444 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004445 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004446 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004447 goto error;
4448 }
4449 if (*cmd == '&')
4450 i = RE_SUBST;
4451 else if (*cmd == '?' || *cmd == '/')
4452 i = RE_SEARCH;
4453 else
4454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004455 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 cmd = NULL;
4457 goto error;
4458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 if (!skip)
4461 {
4462 /*
4463 * When search follows another address, start from
4464 * there.
4465 */
4466 if (lnum != MAXLNUM)
4467 pos.lnum = lnum;
4468 else
4469 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004471 /*
4472 * Start the search just like for the above
4473 * do_search().
4474 */
4475 if (*cmd != '?')
4476 pos.col = MAXCOL;
4477 else
4478 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004479 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004480 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 *cmd == '?' ? BACKWARD : FORWARD,
4482 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004483 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004484 lnum = pos.lnum;
4485 else
4486 {
4487 cmd = NULL;
4488 goto error;
4489 }
4490 }
4491 ++cmd;
4492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
4494 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4496 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498
4499 for (;;)
4500 {
4501 cmd = skipwhite(cmd);
4502 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4503 break;
4504
4505 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004506 {
4507 switch (addr_type)
4508 {
4509 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004510 case ADDR_OTHER:
4511 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004512 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 break;
4514 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004515 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 break;
4517 case ADDR_ARGUMENTS:
4518 lnum = curwin->w_arg_idx + 1;
4519 break;
4520 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004521 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 lnum = curbuf->b_fnum;
4523 break;
4524 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004525 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004527 case ADDR_TABS_RELATIVE:
4528 lnum = 1;
4529 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004530 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004531#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004532 lnum = qf_get_cur_idx(eap);
4533#endif
4534 break;
4535 case ADDR_QUICKFIX_VALID:
4536#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004537 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004538#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004539 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004540 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004541 case ADDR_UNSIGNED:
4542 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004543 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004544 }
4545 }
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (VIM_ISDIGIT(*cmd))
4548 i = '+'; /* "number" is same as "+number" */
4549 else
4550 i = *cmd++;
4551 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4552 n = 1;
4553 else
4554 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004555
4556 if (addr_type == ADDR_TABS_RELATIVE)
4557 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004558 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004559 cmd = NULL;
4560 goto error;
4561 }
4562 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004563 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004564 lnum = compute_buffer_local_count(
4565 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004567 {
4568#ifdef FEAT_FOLDING
4569 /* Relative line addressing, need to adjust for folded lines
4570 * now, but only do it after the first address. */
4571 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4572 && address_count >= 2)
4573 (void)hasFolding(lnum, NULL, &lnum);
4574#endif
4575 if (i == '-')
4576 lnum -= n;
4577 else
4578 lnum += n;
4579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 } while (*cmd == '/' || *cmd == '?');
4582
4583error:
4584 *ptr = cmd;
4585 return lnum;
4586}
4587
4588/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004589 * Get flags from an Ex command argument.
4590 */
4591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004592get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004593{
4594 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4595 {
4596 if (*eap->arg == 'l')
4597 eap->flags |= EXFLAG_LIST;
4598 else if (*eap->arg == 'p')
4599 eap->flags |= EXFLAG_PRINT;
4600 else
4601 eap->flags |= EXFLAG_NR;
4602 eap->arg = skipwhite(eap->arg + 1);
4603 }
4604}
4605
4606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 * Function called for command which is Not Implemented. NI!
4608 */
4609 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004610ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611{
4612 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004613 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614}
4615
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004616#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617/*
4618 * Function called for script command which is Not Implemented. NI!
4619 * Skips over ":perl <<EOF" constructs.
4620 */
4621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004622ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 if (!eap->skip)
4625 ex_ni(eap);
4626 else
4627 vim_free(script_get(eap, eap->arg));
4628}
4629#endif
4630
4631/*
4632 * Check range in Ex command for validity.
4633 * Return NULL when valid, error message when invalid.
4634 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004635 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004636invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004638 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004639
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 if ( eap->line1 < 0
4641 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004642 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004643 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004644
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004645 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004646 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004647 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004648 {
4649 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004650 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004651#ifdef FEAT_DIFF
4652 + (eap->cmdidx == CMD_diffget)
4653#endif
4654 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004655 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004656 break;
4657 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004658 /* add 1 if ARGCOUNT is 0 */
4659 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004660 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004661 break;
4662 case ADDR_BUFFERS:
4663 if (eap->line1 < firstbuf->b_fnum
4664 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004665 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004666 break;
4667 case ADDR_LOADED_BUFFERS:
4668 buf = firstbuf;
4669 while (buf->b_ml.ml_mfp == NULL)
4670 {
4671 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004672 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004673 buf = buf->b_next;
4674 }
4675 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004676 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004677 buf = lastbuf;
4678 while (buf->b_ml.ml_mfp == NULL)
4679 {
4680 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004681 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004682 buf = buf->b_prev;
4683 }
4684 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004685 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004686 break;
4687 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004688 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004689 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004690 break;
4691 case ADDR_TABS:
4692 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004693 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004694 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004695 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004696 case ADDR_OTHER:
4697 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004698 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004699 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004700#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004701 // No error for value that is too big, will use the last entry.
4702 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004703 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004704#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004705 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004706 case ADDR_QUICKFIX_VALID:
4707#ifdef FEAT_QUICKFIX
4708 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4709 || eap->line2 < 0)
4710 return _(e_invrange);
4711#endif
4712 break;
4713 case ADDR_UNSIGNED:
4714 if (eap->line2 < 0)
4715 return _(e_invrange);
4716 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004717 case ADDR_NONE:
4718 // Will give an error elsewhere.
4719 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004720 }
4721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 return NULL;
4723}
4724
4725/*
4726 * Correct the range for zero line number, if required.
4727 */
4728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004729correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004731 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 {
4733 if (eap->line1 == 0)
4734 eap->line1 = 1;
4735 if (eap->line2 == 0)
4736 eap->line2 = 1;
4737 }
4738}
4739
Bram Moolenaar748bf032005-02-02 23:04:36 +00004740#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004741/*
4742 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4743 * pattern. Otherwise return eap->arg.
4744 */
4745 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004746skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004747{
4748 char_u *p = eap->arg;
4749
Bram Moolenaara37420f2006-02-04 22:37:47 +00004750 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4751 || eap->cmdidx == CMD_vimgrepadd
4752 || eap->cmdidx == CMD_lvimgrepadd
4753 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004754 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004755 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004756 if (p == NULL)
4757 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004758 }
4759 return p;
4760}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004761
4762/*
4763 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4764 * in the command line, so that things like % get expanded.
4765 */
4766 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004767replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004768{
4769 char_u *new_cmdline;
4770 char_u *program;
4771 char_u *pos;
4772 char_u *ptr;
4773 int len;
4774 int i;
4775
4776 /*
4777 * Don't do it when ":vimgrep" is used for ":grep".
4778 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004779 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4780 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4781 || eap->cmdidx == CMD_grepadd
4782 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004783 && !grep_internal(eap->cmdidx))
4784 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004785 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4786 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004787 {
4788 if (*curbuf->b_p_gp == NUL)
4789 program = p_gp;
4790 else
4791 program = curbuf->b_p_gp;
4792 }
4793 else
4794 {
4795 if (*curbuf->b_p_mp == NUL)
4796 program = p_mp;
4797 else
4798 program = curbuf->b_p_mp;
4799 }
4800
4801 p = skipwhite(p);
4802
4803 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4804 {
4805 /* replace $* by given arguments */
4806 i = 1;
4807 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4808 ++i;
4809 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004810 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004811 if (new_cmdline == NULL)
4812 return NULL; /* out of memory */
4813 ptr = new_cmdline;
4814 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4815 {
4816 i = (int)(pos - program);
4817 STRNCPY(ptr, program, i);
4818 STRCPY(ptr += i, p);
4819 ptr += len;
4820 program = pos + 2;
4821 }
4822 STRCPY(ptr, program);
4823 }
4824 else
4825 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004826 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004827 if (new_cmdline == NULL)
4828 return NULL; /* out of memory */
4829 STRCPY(new_cmdline, program);
4830 STRCAT(new_cmdline, " ");
4831 STRCAT(new_cmdline, p);
4832 }
4833 msg_make(p);
4834
4835 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4836 vim_free(*cmdlinep);
4837 *cmdlinep = new_cmdline;
4838 p = new_cmdline;
4839 }
4840 return p;
4841}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004842#endif
4843
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844/*
4845 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004846 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 * Return FAIL for failure, OK otherwise.
4848 */
4849 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004850expand_filename(
4851 exarg_T *eap,
4852 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004853 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 int has_wildcards; /* need to expand wildcards */
4856 char_u *repl;
4857 int srclen;
4858 char_u *p;
4859 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004860 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
Bram Moolenaar748bf032005-02-02 23:04:36 +00004862#ifdef FEAT_QUICKFIX
4863 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4864 p = skip_grep_pat(eap);
4865#else
4866 p = eap->arg;
4867#endif
4868
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 /*
4870 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4871 * the file name contains a wildcard it should not cause expanding.
4872 * (it will be expanded anyway if there is a wildcard before replacing).
4873 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004874 has_wildcards = mch_has_wildcard(p);
4875 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004877#ifdef FEAT_EVAL
4878 /* Skip over `=expr`, wildcards in it are not expanded. */
4879 if (p[0] == '`' && p[1] == '=')
4880 {
4881 p += 2;
4882 (void)skip_expr(&p);
4883 if (*p == '`')
4884 ++p;
4885 continue;
4886 }
4887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 /*
4889 * Quick check if this cannot be the start of a special string.
4890 * Also removes backslash before '%', '#' and '<'.
4891 */
4892 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4893 {
4894 ++p;
4895 continue;
4896 }
4897
4898 /*
4899 * Try to find a match at this position.
4900 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004901 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4902 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (*errormsgp != NULL) /* error detected */
4904 return FAIL;
4905 if (repl == NULL) /* no match found */
4906 {
4907 p += srclen;
4908 continue;
4909 }
4910
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004911 /* Wildcards won't be expanded below, the replacement is taken
4912 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4913 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4914 {
4915 char_u *l = repl;
4916
4917 repl = expand_env_save(repl);
4918 vim_free(l);
4919 }
4920
Bram Moolenaar63b92542007-03-27 14:57:09 +00004921 /* Need to escape white space et al. with a backslash.
4922 * Don't do this for:
4923 * - replacement that already has been escaped: "##"
4924 * - shell commands (may have to use quotes instead).
4925 * - non-unix systems when there is a single argument (spaces don't
4926 * separate arguments then).
4927 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004929 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 && eap->cmdidx != CMD_grep
4932 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004933 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004934 && eap->cmdidx != CMD_lgrep
4935 && eap->cmdidx != CMD_lgrepadd
4936 && eap->cmdidx != CMD_lmake
4937 && eap->cmdidx != CMD_make
4938 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004940 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941#endif
4942 )
4943 {
4944 char_u *l;
4945#ifdef BACKSLASH_IN_FILENAME
4946 /* Don't escape a backslash here, because rem_backslash() doesn't
4947 * remove it later. */
4948 static char_u *nobslash = (char_u *)" \t\"|";
4949# define ESCAPE_CHARS nobslash
4950#else
4951# define ESCAPE_CHARS escape_chars
4952#endif
4953
4954 for (l = repl; *l; ++l)
4955 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4956 {
4957 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4958 if (l != NULL)
4959 {
4960 vim_free(repl);
4961 repl = l;
4962 }
4963 break;
4964 }
4965 }
4966
4967 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004968 if ((eap->usefilter || eap->cmdidx == CMD_bang
4969 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004970 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 char_u *l;
4973
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004974 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (l != NULL)
4976 {
4977 vim_free(repl);
4978 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 }
4980 }
4981
4982 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4983 vim_free(repl);
4984 if (p == NULL)
4985 return FAIL;
4986 }
4987
4988 /*
4989 * One file argument: Expand wildcards.
4990 * Don't do this with ":r !command" or ":w !command".
4991 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004992 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
4994 /*
4995 * May do this twice:
4996 * 1. Replace environment variables.
4997 * 2. Replace any other wildcards, remove backslashes.
4998 */
4999 for (n = 1; n <= 2; ++n)
5000 {
5001 if (n == 2)
5002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 /*
5004 * Halve the number of backslashes (this is Vi compatible).
5005 * For Unix and OS/2, when wildcards are expanded, this is
5006 * done by ExpandOne() below.
5007 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005008#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 if (!has_wildcards)
5010#endif
5011 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013
5014 if (has_wildcards)
5015 {
5016 if (n == 1)
5017 {
5018 /*
5019 * First loop: May expand environment variables. This
5020 * can be done much faster with expand_env() than with
5021 * something else (e.g., calling a shell).
5022 * After expanding environment variables, check again
5023 * if there are still wildcards present.
5024 */
5025 if (vim_strchr(eap->arg, '$') != NULL
5026 || vim_strchr(eap->arg, '~') != NULL)
5027 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005028 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005029 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 has_wildcards = mch_has_wildcard(NameBuff);
5031 p = NameBuff;
5032 }
5033 else
5034 p = NULL;
5035 }
5036 else /* n == 2 */
5037 {
5038 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005039 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
5041 ExpandInit(&xpc);
5042 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005043 if (p_wic)
5044 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005046 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (p == NULL)
5048 return FAIL;
5049 }
5050 if (p != NULL)
5051 {
5052 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5053 p, cmdlinep);
5054 if (n == 2) /* p came from ExpandOne() */
5055 vim_free(p);
5056 }
5057 }
5058 }
5059 }
5060 return OK;
5061}
5062
5063/*
5064 * Replace part of the command line, keeping eap->cmd, eap->arg and
5065 * eap->nextcmd correct.
5066 * "src" points to the part that is to be replaced, of length "srclen".
5067 * "repl" is the replacement string.
5068 * Returns a pointer to the character after the replaced string.
5069 * Returns NULL for failure.
5070 */
5071 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005072repl_cmdline(
5073 exarg_T *eap,
5074 char_u *src,
5075 int srclen,
5076 char_u *repl,
5077 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 int len;
5080 int i;
5081 char_u *new_cmdline;
5082
5083 /*
5084 * The new command line is build in new_cmdline[].
5085 * First allocate it.
5086 * Careful: a "+cmd" argument may have been NUL terminated.
5087 */
5088 len = (int)STRLEN(repl);
5089 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005090 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005092 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 return NULL; /* out of memory! */
5094
5095 /*
5096 * Copy the stuff before the expanded part.
5097 * Copy the expanded stuff.
5098 * Copy what came after the expanded part.
5099 * Copy the next commands, if there are any.
5100 */
5101 i = (int)(src - *cmdlinep); /* length of part before match */
5102 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 mch_memmove(new_cmdline + i, repl, (size_t)len);
5105 i += len; /* remember the end of the string */
5106 STRCPY(new_cmdline + i, src + srclen);
5107 src = new_cmdline + i; /* remember where to continue */
5108
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005109 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 i = (int)STRLEN(new_cmdline) + 1;
5112 STRCPY(new_cmdline + i, eap->nextcmd);
5113 eap->nextcmd = new_cmdline + i;
5114 }
5115 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5116 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5117 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5118 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5119 vim_free(*cmdlinep);
5120 *cmdlinep = new_cmdline;
5121
5122 return src;
5123}
5124
5125/*
5126 * Check for '|' to separate commands and '"' to start comments.
5127 */
5128 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005129separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
5131 char_u *p;
5132
Bram Moolenaar86b68352004-12-27 21:59:20 +00005133#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005134 p = skip_grep_pat(eap);
5135#else
5136 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005137#endif
5138
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005139 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
5141 if (*p == Ctrl_V)
5142 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005143 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 ++p; /* skip CTRL-V and next char */
5145 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005146 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005147 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 if (*p == NUL) /* stop at NUL after CTRL-V */
5149 break;
5150 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005151
5152#ifdef FEAT_EVAL
5153 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005154 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005155 {
5156 p += 2;
5157 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005158 }
5159#endif
5160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 /* Check for '"': start of comment or '|': next command */
5162 /* :@" and :*" do not start a comment!
5163 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005164 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5166 || p != eap->arg)
5167 && (eap->cmdidx != CMD_redir
5168 || p != eap->arg + 1 || p[-1] != '@'))
5169 || *p == '|' || *p == '\n')
5170 {
5171 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005172 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 * AND 'b' is present in 'cpoptions'.
5174 */
5175 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005176 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005178 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 --p;
5180 }
5181 else
5182 {
5183 eap->nextcmd = check_nextcmd(p);
5184 *p = NUL;
5185 break;
5186 }
5187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005189
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005190 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 del_trailing_spaces(eap->arg);
5192}
5193
5194/*
5195 * get + command from ex argument
5196 */
5197 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005198getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 char_u *arg = *argp;
5201 char_u *command = NULL;
5202
5203 if (*arg == '+') /* +[command] */
5204 {
5205 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005206 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 command = dollar_command;
5208 else
5209 {
5210 command = arg;
5211 arg = skip_cmd_arg(command, TRUE);
5212 if (*arg != NUL)
5213 *arg++ = NUL; /* terminate command with NUL */
5214 }
5215
5216 arg = skipwhite(arg); /* skip over spaces */
5217 *argp = arg;
5218 }
5219 return command;
5220}
5221
5222/*
5223 * Find end of "+command" argument. Skip over "\ " and "\\".
5224 */
5225 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005226skip_cmd_arg(
5227 char_u *p,
5228 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229{
5230 while (*p && !vim_isspace(*p))
5231 {
5232 if (*p == '\\' && p[1] != NUL)
5233 {
5234 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005235 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 else
5237 ++p;
5238 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005239 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241 return p;
5242}
5243
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005244 int
5245get_bad_opt(char_u *p, exarg_T *eap)
5246{
5247 if (STRICMP(p, "keep") == 0)
5248 eap->bad_char = BAD_KEEP;
5249 else if (STRICMP(p, "drop") == 0)
5250 eap->bad_char = BAD_DROP;
5251 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5252 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005253 else
5254 return FAIL;
5255 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005256}
5257
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258/*
5259 * Get "++opt=arg" argument.
5260 * Return FAIL or OK.
5261 */
5262 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005263getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264{
5265 char_u *arg = eap->arg + 2;
5266 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005267 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 /* ":edit ++[no]bin[ary] file" */
5271 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5272 {
5273 if (*arg == 'n')
5274 {
5275 arg += 2;
5276 eap->force_bin = FORCE_NOBIN;
5277 }
5278 else
5279 eap->force_bin = FORCE_BIN;
5280 if (!checkforcmd(&arg, "binary", 3))
5281 return FAIL;
5282 eap->arg = skipwhite(arg);
5283 return OK;
5284 }
5285
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005286 /* ":read ++edit file" */
5287 if (STRNCMP(arg, "edit", 4) == 0)
5288 {
5289 eap->read_edit = TRUE;
5290 eap->arg = skipwhite(arg + 4);
5291 return OK;
5292 }
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (STRNCMP(arg, "ff", 2) == 0)
5295 {
5296 arg += 2;
5297 pp = &eap->force_ff;
5298 }
5299 else if (STRNCMP(arg, "fileformat", 10) == 0)
5300 {
5301 arg += 10;
5302 pp = &eap->force_ff;
5303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 else if (STRNCMP(arg, "enc", 3) == 0)
5305 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005306 if (STRNCMP(arg, "encoding", 8) == 0)
5307 arg += 8;
5308 else
5309 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 pp = &eap->force_enc;
5311 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005312 else if (STRNCMP(arg, "bad", 3) == 0)
5313 {
5314 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005315 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317
5318 if (pp == NULL || *arg != '=')
5319 return FAIL;
5320
5321 ++arg;
5322 *pp = (int)(arg - eap->cmd);
5323 arg = skip_cmd_arg(arg, FALSE);
5324 eap->arg = skipwhite(arg);
5325 *arg = NUL;
5326
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 if (pp == &eap->force_ff)
5328 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5330 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005331 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005333 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 {
5335 /* Make 'fileencoding' lower case. */
5336 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5337 *p = TOLOWER_ASC(*p);
5338 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005339 else
5340 {
5341 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5342 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005343 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005344 return FAIL;
5345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347 return OK;
5348}
5349
5350/*
5351 * ":abbreviate" and friends.
5352 */
5353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005354ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 do_exmap(eap, TRUE); /* almost the same as mapping */
5357}
5358
5359/*
5360 * ":map" and friends.
5361 */
5362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005363ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
5365 /*
5366 * If we are sourcing .exrc or .vimrc in current directory we
5367 * print the mappings for security reasons.
5368 */
5369 if (secure)
5370 {
5371 secure = 2;
5372 msg_outtrans(eap->cmd);
5373 msg_putchar('\n');
5374 }
5375 do_exmap(eap, FALSE);
5376}
5377
5378/*
5379 * ":unmap" and friends.
5380 */
5381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005382ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383{
5384 do_exmap(eap, FALSE);
5385}
5386
5387/*
5388 * ":mapclear" and friends.
5389 */
5390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005391ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392{
5393 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5394}
5395
5396/*
5397 * ":abclear" and friends.
5398 */
5399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005400ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5403}
5404
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005406ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
5408 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005409 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 * directory for security reasons.
5411 */
5412 if (secure)
5413 {
5414 secure = 2;
5415 eap->errmsg = e_curdir;
5416 }
5417 else if (eap->cmdidx == CMD_autocmd)
5418 do_autocmd(eap->arg, eap->forceit);
5419 else
5420 do_augroup(eap->arg, eap->forceit);
5421}
5422
5423/*
5424 * ":doautocmd": Apply the automatic commands to the current buffer.
5425 */
5426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005427ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005429 char_u *arg = eap->arg;
5430 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005431 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005432
Bram Moolenaar1610d052016-06-09 22:53:01 +02005433 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5434 /* Only when there is no <nomodeline>. */
5435 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005436 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439/*
5440 * :[N]bunload[!] [N] [bufname] unload buffer
5441 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5442 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5443 */
5444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005445ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005447 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005448 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 eap->errmsg = do_bufdel(
5450 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5451 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5452 : DOBUF_UNLOAD, eap->arg,
5453 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5454}
5455
5456/*
5457 * :[N]buffer [N] to buffer N
5458 * :[N]sbuffer [N] to buffer N
5459 */
5460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005463 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005464 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 if (*eap->arg)
5466 eap->errmsg = e_trailing;
5467 else
5468 {
5469 if (eap->addr_count == 0) /* default is current buffer */
5470 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5471 else
5472 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005473 if (eap->do_ecmd_cmd != NULL)
5474 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476}
5477
5478/*
5479 * :[N]bmodified [N] to next mod. buffer
5480 * :[N]sbmodified [N] to next mod. buffer
5481 */
5482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005486 if (eap->do_ecmd_cmd != NULL)
5487 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488}
5489
5490/*
5491 * :[N]bnext [N] to next buffer
5492 * :[N]sbnext [N] split and to next buffer
5493 */
5494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005495ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005498 if (eap->do_ecmd_cmd != NULL)
5499 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500}
5501
5502/*
5503 * :[N]bNext [N] to previous buffer
5504 * :[N]bprevious [N] to previous buffer
5505 * :[N]sbNext [N] split and to previous buffer
5506 * :[N]sbprevious [N] split and to previous buffer
5507 */
5508 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005509ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510{
5511 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005512 if (eap->do_ecmd_cmd != NULL)
5513 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514}
5515
5516/*
5517 * :brewind to first buffer
5518 * :bfirst to first buffer
5519 * :sbrewind split and to first buffer
5520 * :sbfirst split and to first buffer
5521 */
5522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005523ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005526 if (eap->do_ecmd_cmd != NULL)
5527 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528}
5529
5530/*
5531 * :blast to last buffer
5532 * :sblast split and to last buffer
5533 */
5534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005535ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005538 if (eap->do_ecmd_cmd != NULL)
5539 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
5542 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 return (c == NUL || c == '|' || c == '"' || c == '\n');
5546}
5547
5548#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5549 || defined(PROTO)
5550/*
5551 * Return the next command, after the first '|' or '\n'.
5552 * Return NULL if not found.
5553 */
5554 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
5557 while (*p != '|' && *p != '\n')
5558 {
5559 if (*p == NUL)
5560 return NULL;
5561 ++p;
5562 }
5563 return (p + 1);
5564}
5565#endif
5566
5567/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005568 * Check if *p is a separator between Ex commands, skipping over white space.
5569 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 */
5571 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005574 char_u *s = skipwhite(p);
5575
5576 if (*s == '|' || *s == '\n')
5577 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 else
5579 return NULL;
5580}
5581
5582/*
5583 * - if there are more files to edit
5584 * - and this is the last window
5585 * - and forceit not used
5586 * - and not repeated twice on a row
5587 * return FAIL and give error message if 'message' TRUE
5588 * return OK otherwise
5589 */
5590 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005591check_more(
5592 int message, /* when FALSE check only, no messages */
5593 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594{
5595 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5596
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005597 if (!forceit && only_one_window()
5598 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
5600 if (message)
5601 {
5602#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5603 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5604 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005605 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005607 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5608 NGETTEXT("%d more file to edit. Quit anyway?",
5609 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5611 return OK;
5612 return FAIL;
5613 }
5614#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005615 semsg(NGETTEXT("E173: %d more file to edit",
5616 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 quitmore = 2; /* next try to quit is allowed */
5618 }
5619 return FAIL;
5620 }
5621 return OK;
5622}
5623
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005624#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625/*
5626 * Function given to ExpandGeneric() to obtain the list of command names.
5627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
5631 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 return cmdnames[idx].cmd_name;
5634}
5635#endif
5636
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
Bram Moolenaare6850792010-05-14 15:28:44 +02005640 if (*eap->arg == NUL)
5641 {
5642#ifdef FEAT_EVAL
5643 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5644 char_u *p = NULL;
5645
5646 if (expr != NULL)
5647 {
5648 ++emsg_off;
5649 p = eval_to_string(expr, NULL, FALSE);
5650 --emsg_off;
5651 vim_free(expr);
5652 }
5653 if (p != NULL)
5654 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005655 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005656 vim_free(p);
5657 }
5658 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005659 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005660#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005661 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005662#endif
5663 }
5664 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005665 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005666
5667#ifdef FEAT_VTP
5668 else if (has_vtp_working())
5669 {
5670 // background color change requires clear + redraw
5671 update_screen(CLEAR);
5672 redrawcmd();
5673 }
5674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675}
5676
5677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005681 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 do_highlight(eap->arg, eap->forceit, FALSE);
5683}
5684
5685
5686/*
5687 * Call this function if we thought we were going to exit, but we won't
5688 * (because of an error). May need to restore the terminal mode.
5689 */
5690 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005691not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692{
5693 exiting = FALSE;
5694 settmode(TMODE_RAW);
5695}
5696
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005697 static int
5698before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5699{
5700 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5701
5702 /* Bail out when autocommands closed the window.
5703 * Refuse to quit when the buffer in the last window is being closed (can
5704 * only happen in autocommands). */
5705 if (!win_valid(wp)
5706 || curbuf_locked()
5707 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5708 return TRUE;
5709
5710 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5711 {
5712 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5713 /* Refuse to quit when locked or when the buffer in the last window is
5714 * being closed (can only happen in autocommands). */
5715 if (curbuf_locked()
5716 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5717 return TRUE;
5718 }
5719
5720 return FALSE;
5721}
5722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005724 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005725 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 */
5727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005728ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005730 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732#ifdef FEAT_CMDWIN
5733 if (cmdwin_type != 0)
5734 {
5735 cmdwin_result = Ctrl_C;
5736 return;
5737 }
5738#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005739 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005740 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005741 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005742 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005743 return;
5744 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005745 if (eap->addr_count > 0)
5746 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005747 int wnr = eap->line2;
5748
5749 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5750 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005751 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005752 }
5753 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005754 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005755
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005756 /* Refuse to quit when locked. */
5757 if (curbuf_locked())
5758 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005759
5760 /* Trigger QuitPre and maybe ExitPre */
5761 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005762 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
5764#ifdef FEAT_NETBEANS_INTG
5765 netbeansForcedQuit = eap->forceit;
5766#endif
5767
5768 /*
5769 * If there are more files or windows we won't exit.
5770 */
5771 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5772 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005773 if ((!buf_hide(wp->w_buffer)
5774 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005775 | (eap->forceit ? CCGD_FORCEIT : 0)
5776 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005778 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
5780 not_exiting();
5781 }
5782 else
5783 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005784 /* quit last window
5785 * Note: only_one_window() returns true, even so a help window is
5786 * still open. In that case only quit, if no address has been
5787 * specified. Example:
5788 * :h|wincmd w|1q - don't quit
5789 * :h|wincmd w|q - quit
5790 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005791 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005793 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005794#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005798 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 }
5800}
5801
5802/*
5803 * ":cquit".
5804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005806ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
5808 getout(1); /* this does not always pass on the exit code to the Manx
5809 compiler. why? */
5810}
5811
5812/*
5813 * ":qall": try to quit all windows
5814 */
5815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005816ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818# ifdef FEAT_CMDWIN
5819 if (cmdwin_type != 0)
5820 {
5821 if (eap->forceit)
5822 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5823 else
5824 cmdwin_result = K_XF2;
5825 return;
5826 }
5827# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005828
5829 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005830 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005831 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005832 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005833 return;
5834 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005835
5836 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005837 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005840 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 getout(0);
5842 not_exiting();
5843}
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845/*
5846 * ":close": close current window, unless it is the last one
5847 */
5848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005849ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005851 win_T *win;
5852 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005853#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005855 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005857#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005858 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005859 {
5860 if (eap->addr_count == 0)
5861 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005862 else
5863 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005864 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005865 {
5866 winnr++;
5867 if (winnr == eap->line2)
5868 break;
5869 }
5870 if (win == NULL)
5871 win = lastwin;
5872 ex_win_close(eap->forceit, win, NULL);
5873 }
5874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875}
5876
Bram Moolenaar4033c552017-09-16 20:54:51 +02005877#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005878/*
5879 * ":pclose": Close any preview window.
5880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005882ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005883{
5884 win_T *win;
5885
Bram Moolenaar79648732019-07-18 21:43:07 +02005886 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005887 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005888 if (win->w_p_pvw)
5889 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005890 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005891 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005892 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005893# ifdef FEAT_TEXT_PROP
5894 // Also when 'previewpopup' is empty, it might have been cleared.
5895 popup_close_preview();
5896# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005897}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005898#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005899
Bram Moolenaarf740b292006-02-16 22:11:02 +00005900/*
5901 * Close window "win" and take care of handling closing the last window for a
5902 * modified buffer.
5903 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005905ex_win_close(
5906 int forceit,
5907 win_T *win,
5908 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 int need_hide;
5911 buf_T *buf = win->w_buffer;
5912
5913 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005914 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005916#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 if ((p_confirm || cmdmod.confirm) && p_write)
5918 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005919 bufref_T bufref;
5920
5921 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005923 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 return;
5925 need_hide = FALSE;
5926 }
5927 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005928#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005930 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 return;
5932 }
5933 }
5934
Bram Moolenaar4033c552017-09-16 20:54:51 +02005935#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005937#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005938
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005940 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005941 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005942 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005943 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944}
5945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005947 * Handle the argument for a tabpage related ex command.
5948 * Returns a tabpage number.
5949 * When an error is encountered then eap->errmsg is set.
5950 */
5951 static int
5952get_tabpage_arg(exarg_T *eap)
5953{
5954 int tab_number;
5955 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5956
5957 if (eap->arg && *eap->arg != NUL)
5958 {
5959 char_u *p = eap->arg;
5960 char_u *p_save;
5961 int relative = 0; /* argument +N/-N means: go to N places to the
5962 * right/left relative to the current position. */
5963
5964 if (*p == '-')
5965 {
5966 relative = -1;
5967 p++;
5968 }
5969 else if (*p == '+')
5970 {
5971 relative = 1;
5972 p++;
5973 }
5974
5975 p_save = p;
5976 tab_number = getdigits(&p);
5977
5978 if (relative == 0)
5979 {
5980 if (STRCMP(p, "$") == 0)
5981 tab_number = LAST_TAB_NR;
5982 else if (p == p_save || *p_save == '-' || *p != NUL
5983 || tab_number > LAST_TAB_NR)
5984 {
5985 /* No numbers as argument. */
5986 eap->errmsg = e_invarg;
5987 goto theend;
5988 }
5989 }
5990 else
5991 {
5992 if (*p_save == NUL)
5993 tab_number = 1;
5994 else if (p == p_save || *p_save == '-' || *p != NUL
5995 || tab_number == 0)
5996 {
5997 /* No numbers as argument. */
5998 eap->errmsg = e_invarg;
5999 goto theend;
6000 }
6001 tab_number = tab_number * relative + tabpage_index(curtab);
6002 if (!unaccept_arg0 && relative == -1)
6003 --tab_number;
6004 }
6005 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6006 eap->errmsg = e_invarg;
6007 }
6008 else if (eap->addr_count > 0)
6009 {
6010 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006011 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006012 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006013 tab_number = 0;
6014 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006015 else
6016 {
6017 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006018 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006019 {
6020 --tab_number;
6021 if (tab_number < unaccept_arg0)
6022 eap->errmsg = e_invarg;
6023 }
6024 }
6025 }
6026 else
6027 {
6028 switch (eap->cmdidx)
6029 {
6030 case CMD_tabnext:
6031 tab_number = tabpage_index(curtab) + 1;
6032 if (tab_number > LAST_TAB_NR)
6033 tab_number = 1;
6034 break;
6035 case CMD_tabmove:
6036 tab_number = LAST_TAB_NR;
6037 break;
6038 default:
6039 tab_number = tabpage_index(curtab);
6040 }
6041 }
6042
6043theend:
6044 return tab_number;
6045}
6046
6047/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006048 * ":tabclose": close current tab page, unless it is the last one.
6049 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 */
6051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006052ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006054 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006055 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006056
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006057# ifdef FEAT_CMDWIN
6058 if (cmdwin_type != 0)
6059 cmdwin_result = K_IGNORE;
6060 else
6061# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006062 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006063 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006066 tab_number = get_tabpage_arg(eap);
6067 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006068 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006069 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006070 if (tp == NULL)
6071 {
6072 beep_flush();
6073 return;
6074 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006075 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006076 {
6077 tabpage_close_other(tp, eap->forceit);
6078 return;
6079 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006080 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006081 tabpage_close(eap->forceit);
6082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006084}
6085
6086/*
6087 * ":tabonly": close all tab pages except the current one
6088 */
6089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006090ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006091{
6092 tabpage_T *tp;
6093 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006094 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006095
6096# ifdef FEAT_CMDWIN
6097 if (cmdwin_type != 0)
6098 cmdwin_result = K_IGNORE;
6099 else
6100# endif
6101 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006102 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006103 else
6104 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006105 tab_number = get_tabpage_arg(eap);
6106 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006107 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006108 goto_tabpage(tab_number);
6109 /* Repeat this up to a 1000 times, because autocommands may
6110 * mess up the lists. */
6111 for (done = 0; done < 1000; ++done)
6112 {
6113 FOR_ALL_TABPAGES(tp)
6114 if (tp->tp_topframe != topframe)
6115 {
6116 tabpage_close_other(tp, eap->forceit);
6117 /* if we failed to close it quit */
6118 if (valid_tabpage(tp))
6119 done = 1000;
6120 /* start over, "tp" is now invalid */
6121 break;
6122 }
6123 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006124 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006125 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006126 }
6127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006131 * Close the current tab page.
6132 */
6133 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006134tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006135{
6136 /* First close all the windows but the current one. If that worked then
6137 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006138 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006139 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006140 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006141 ex_win_close(forceit, curwin, NULL);
6142# ifdef FEAT_GUI
6143 need_mouse_correct = TRUE;
6144# endif
6145}
6146
6147/*
6148 * Close tab page "tp", which is not the current tab page.
6149 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006150 * Also takes care of the tab pages line disappearing when closing the
6151 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006152 */
6153 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006154tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006155{
6156 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006157 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006158 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006159
6160 /* Limit to 1000 windows, autocommands may add a window while we close
6161 * one. OK, so I'm paranoid... */
6162 while (++done < 1000)
6163 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006164 wp = tp->tp_firstwin;
6165 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006166
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006167 /* Autocommands may delete the tab page under our fingers and we may
6168 * fail to close a window with a modified buffer. */
6169 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006170 break;
6171 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006172
Bram Moolenaar29323592016-07-24 22:04:11 +02006173 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006174
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006175 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006176 if (h != tabline_height())
6177 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006178}
6179
6180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 * ":only".
6182 */
6183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006184ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006186 win_T *wp;
6187 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188# ifdef FEAT_GUI
6189 need_mouse_correct = TRUE;
6190# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006191 if (eap->addr_count > 0)
6192 {
6193 wnr = eap->line2;
6194 for (wp = firstwin; --wnr > 0; )
6195 {
6196 if (wp->w_next == NULL)
6197 break;
6198 else
6199 wp = wp->w_next;
6200 }
6201 win_goto(wp);
6202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 close_others(TRUE, eap->forceit);
6204}
6205
6206/*
6207 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006208 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006210 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006211ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 if (eap->addr_count == 0)
6214 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006215 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006219ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006221 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006222 if (!eap->skip)
6223 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006224#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006225 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006226#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006227 if (eap->addr_count == 0)
6228 win_close(curwin, FALSE); /* don't free buffer */
6229 else
6230 {
6231 int winnr = 0;
6232 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006233
Bram Moolenaar2256c992016-11-15 21:17:07 +01006234 FOR_ALL_WINDOWS(win)
6235 {
6236 winnr++;
6237 if (winnr == eap->line2)
6238 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006239 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006240 if (win == NULL)
6241 win = lastwin;
6242 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
6245}
6246
6247/*
6248 * ":stop" and ":suspend": Suspend Vim.
6249 */
6250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006251ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 /*
6254 * Disallow suspending for "rvim".
6255 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006256 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
6258 if (!eap->forceit)
6259 autowrite_all();
6260 windgoto((int)Rows - 1, 0);
6261 out_char('\n');
6262 out_flush();
6263 stoptermcap();
6264 out_flush(); /* needed for SUN to restore xterm buffer */
6265#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006266 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267#endif
6268 ui_suspend(); /* call machine specific function */
6269#ifdef FEAT_TITLE
6270 maketitle();
6271 resettitle(); /* force updating the title */
6272#endif
6273 starttermcap();
6274 scroll_start(); /* scroll screen before redrawing */
6275 redraw_later_clear();
6276 shell_resized(); /* may have resized window */
6277 }
6278}
6279
6280/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006281 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 */
6283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006284ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286#ifdef FEAT_CMDWIN
6287 if (cmdwin_type != 0)
6288 {
6289 cmdwin_result = Ctrl_C;
6290 return;
6291 }
6292#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006293 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006294 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006295 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006296 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006297 return;
6298 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006299
6300 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006301 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302
6303 /*
6304 * if more files or windows we won't exit
6305 */
6306 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6307 exiting = TRUE;
6308 if ( ((eap->cmdidx == CMD_wq
6309 || curbufIsChanged())
6310 && do_write(eap) == FAIL)
6311 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006312 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
6314 not_exiting();
6315 }
6316 else
6317 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006320 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321# ifdef FEAT_GUI
6322 need_mouse_correct = TRUE;
6323# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006324 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006325 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
6327}
6328
6329/*
6330 * ":print", ":list", ":number".
6331 */
6332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006333ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006335 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006336 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006337 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006339 for ( ;!got_int; ui_breakcheck())
6340 {
6341 print_line(eap->line1,
6342 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6343 || (eap->flags & EXFLAG_NR)),
6344 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6345 if (++eap->line1 > eap->line2)
6346 break;
6347 out_flush(); /* show one line at a time */
6348 }
6349 setpcmark();
6350 /* put cursor at last line */
6351 curwin->w_cursor.lnum = eap->line2;
6352 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 }
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356}
6357
6358#ifdef FEAT_BYTEOFF
6359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006360ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361{
6362 goto_byte(eap->line2);
6363}
6364#endif
6365
6366/*
6367 * ":shell".
6368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006370ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 do_shell(NULL, 0);
6373}
6374
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006375#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006377static int drop_busy = FALSE;
6378static int drop_filec;
6379static char_u **drop_filev = NULL;
6380static int drop_split;
6381static void (*drop_callback)(void *);
6382static void *drop_cookie;
6383
6384 static void
6385handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 exarg_T ea;
6388 int save_msg_scroll = msg_scroll;
6389
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006390 // Setting the argument list may cause screen updates and being called
6391 // recursively. Avoid that by setting drop_busy.
6392 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393
6394 /* Check whether the current buffer is changed. If so, we will need
6395 * to split the current window or data could be lost.
6396 * We don't need to check if the 'hidden' option is set, as in this
6397 * case the buffer won't be lost.
6398 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006399 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
6401 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006402 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 --emsg_off;
6404 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006405 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if (win_split(0, 0) == FAIL)
6408 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006409 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410
6411 /* When splitting the window, create a new alist. Otherwise the
6412 * existing one is overwritten. */
6413 alist_unlink(curwin->w_alist);
6414 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 }
6416
6417 /*
6418 * Set up the new argument list.
6419 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006420 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 /*
6423 * Move to the first file.
6424 */
6425 /* Fake up a minimal "next" command for do_argfile() */
6426 vim_memset(&ea, 0, sizeof(ea));
6427 ea.cmd = (char_u *)"next";
6428 do_argfile(&ea, 0);
6429
6430 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6431 * mode that is not needed here. */
6432 need_start_insertmode = FALSE;
6433
6434 /* Restore msg_scroll, otherwise a following command may cause scrolling
6435 * unexpectedly. The screen will be redrawn by the caller, thus
6436 * msg_scroll being set by displaying a message is irrelevant. */
6437 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006438
6439 if (drop_callback != NULL)
6440 drop_callback(drop_cookie);
6441
6442 drop_filev = NULL;
6443 drop_busy = FALSE;
6444}
6445
6446/*
6447 * Handle a file drop. The code is here because a drop is *nearly* like an
6448 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006449 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006450 * code is very similar to :args and hence needs access to a lot of the static
6451 * functions in this file.
6452 *
6453 * The "filev" list must have been allocated using alloc(), as should each item
6454 * in the list. This function takes over responsibility for freeing the "filev"
6455 * list.
6456 */
6457 void
6458handle_drop(
6459 int filec, // the number of files dropped
6460 char_u **filev, // the list of files dropped
6461 int split, // force splitting the window
6462 void (*callback)(void *), // to be called after setting the argument
6463 // list
6464 void *cookie) // argument for "callback" (allocated)
6465{
6466 // Cannot handle recursive drops, finish the pending one.
6467 if (drop_busy)
6468 {
6469 FreeWild(filec, filev);
6470 vim_free(cookie);
6471 return;
6472 }
6473
6474 // When calling handle_drop() more than once in a row we only use the last
6475 // one.
6476 if (drop_filev != NULL)
6477 {
6478 FreeWild(drop_filec, drop_filev);
6479 vim_free(drop_cookie);
6480 }
6481
6482 drop_filec = filec;
6483 drop_filev = filev;
6484 drop_split = split;
6485 drop_callback = callback;
6486 drop_cookie = cookie;
6487
6488 // Postpone this when:
6489 // - editing the command line
6490 // - not possible to change the current buffer
6491 // - updating the screen
6492 // As it may change buffers and window structures that are in use and cause
6493 // freed memory to be used.
6494 if (text_locked() || curbuf_locked() || updating_screen)
6495 return;
6496
6497 handle_drop_internal();
6498}
6499
6500/*
6501 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6502 * reset: Handle a postponed drop.
6503 */
6504 void
6505handle_any_postponed_drop(void)
6506{
6507 if (!drop_busy && drop_filev != NULL
6508 && !text_locked() && !curbuf_locked() && !updating_screen)
6509 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510}
6511#endif
6512
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513/*
6514 * Clear an argument list: free all file names and reset it to zero entries.
6515 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006516 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006517alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 while (--al->al_ga.ga_len >= 0)
6520 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6521 ga_clear(&al->al_ga);
6522}
6523
6524/*
6525 * Init an argument list.
6526 */
6527 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006528alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529{
6530 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6531}
6532
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533/*
6534 * Remove a reference from an argument list.
6535 * Ignored when the argument list is the global one.
6536 * If the argument list is no longer used by any window, free it.
6537 */
6538 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006539alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 if (al != &global_alist && --al->al_refcount <= 0)
6542 {
6543 alist_clear(al);
6544 vim_free(al);
6545 }
6546}
6547
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548/*
6549 * Create a new argument list and use it for the current window.
6550 */
6551 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006552alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006554 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (curwin->w_alist == NULL)
6556 {
6557 curwin->w_alist = &global_alist;
6558 ++global_alist.al_refcount;
6559 }
6560 else
6561 {
6562 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006563 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 alist_init(curwin->w_alist);
6565 }
6566}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006568#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569/*
6570 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006571 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6572 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 */
6574 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006575alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006578 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 char_u **new_arg_files;
6580 int new_arg_file_count;
6581 char_u *save_p_su = p_su;
6582 int i;
6583
6584 /* Don't use 'suffixes' here. This should work like the shell did the
6585 * expansion. Also, the vimrc file isn't read yet, thus the user
6586 * can't set the options. */
6587 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006588 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (old_arg_files != NULL)
6590 {
6591 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006592 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6593 old_arg_count = GARGCOUNT;
6594 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006596 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 && new_arg_file_count > 0)
6598 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006599 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6600 TRUE, fnum_list, fnum_len);
6601 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 }
6604 p_su = save_p_su;
6605}
6606#endif
6607
6608/*
6609 * Set the argument list for the current window.
6610 * Takes over the allocated files[] and the allocated fnames in it.
6611 */
6612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006613alist_set(
6614 alist_T *al,
6615 int count,
6616 char_u **files,
6617 int use_curbuf,
6618 int *fnum_list,
6619 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620{
6621 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006622 static int recursive = 0;
6623
6624 if (recursive)
6625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006626 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006627 return;
6628 }
6629 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630
6631 alist_clear(al);
6632 if (ga_grow(&al->al_ga, count) == OK)
6633 {
6634 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006635 {
6636 if (got_int)
6637 {
6638 /* When adding many buffers this can take a long time. Allow
6639 * interrupting here. */
6640 while (i < count)
6641 vim_free(files[i++]);
6642 break;
6643 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006644
6645 /* May set buffer name of a buffer previously used for the
6646 * argument list, so that it's re-used by alist_add. */
6647 if (fnum_list != NULL && i < fnum_len)
6648 buf_set_name(fnum_list[i], files[i]);
6649
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006651 ui_breakcheck();
6652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 vim_free(files);
6654 }
6655 else
6656 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006659
6660 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661}
6662
6663/*
6664 * Add file "fname" to argument list "al".
6665 * "fname" must have been allocated and "al" must have been checked for room.
6666 */
6667 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006668alist_add(
6669 alist_T *al,
6670 char_u *fname,
6671 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 if (fname == NULL) /* don't add NULL file names */
6674 return;
6675#ifdef BACKSLASH_IN_FILENAME
6676 slash_adjust(fname);
6677#endif
6678 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6679 if (set_fnum > 0)
6680 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6681 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6682 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683}
6684
6685#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6686/*
6687 * Adjust slashes in file names. Called after 'shellslash' was set.
6688 */
6689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006690alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006694 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695
6696 for (i = 0; i < GARGCOUNT; ++i)
6697 if (GARGLIST[i].ae_fname != NULL)
6698 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006699 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (wp->w_alist != &global_alist)
6701 for (i = 0; i < WARGCOUNT(wp); ++i)
6702 if (WARGLIST(wp)[i].ae_fname != NULL)
6703 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704}
6705#endif
6706
6707/*
6708 * ":preserve".
6709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006711ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006713 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 ml_preserve(curbuf, TRUE);
6715}
6716
6717/*
6718 * ":recover".
6719 */
6720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006721ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6724 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006725 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6726 | CCGD_MULTWIN
6727 | (eap->forceit ? CCGD_FORCEIT : 0)
6728 | CCGD_EXCMD)
6729
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 && (*eap->arg == NUL
6731 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006732 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 recoverymode = FALSE;
6734}
6735
6736/*
6737 * Command modifier used in a wrong way.
6738 */
6739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006740ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 eap->errmsg = e_invcmd;
6743}
6744
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745/*
6746 * :sview [+command] file split window with new file, read-only
6747 * :split [[+command] file] split window with current or new file
6748 * :vsplit [[+command] file] split window vertically with current or new file
6749 * :new [[+command] file] split window with no or new file
6750 * :vnew [[+command] file] split vertically window with no or new file
6751 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006752 *
6753 * :tabedit open new Tab page with empty window
6754 * :tabedit [+command] file open new Tab page and edit "file"
6755 * :tabnew [[+command] file] just like :tabedit
6756 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 */
6758 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006759ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006761 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006762#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006764#endif
6765#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006767#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006768 int use_tab = eap->cmdidx == CMD_tabedit
6769 || eap->cmdidx == CMD_tabfind
6770 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006772 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006773 return;
6774
Bram Moolenaar4033c552017-09-16 20:54:51 +02006775#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
Bram Moolenaar4033c552017-09-16 20:54:51 +02006779#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006781 * quickfix windows. But it's OK when doing ":tab split". */
6782 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 {
6784 if (eap->cmdidx == CMD_split)
6785 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 if (eap->cmdidx == CMD_vsplit)
6787 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
Bram Moolenaar4033c552017-09-16 20:54:51 +02006791#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006792 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6795 FNAME_MESS, TRUE, curbuf->b_ffname);
6796 if (fname == NULL)
6797 goto theend;
6798 eap->arg = fname;
6799 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006800# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006801 else
6802# endif
6803#endif
6804#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 && eap->cmdidx != CMD_new)
6808 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006809 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006810# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006811 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006812# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006813 au_has_group((char_u *)"FileExplorer"))
6814 {
6815 /* No browsing supported but we do have the file explorer:
6816 * Edit the directory. */
6817 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6818 eap->arg = (char_u *)".";
6819 }
6820 else
6821 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006822 fname = do_browse(0, (char_u *)(use_tab
6823 ? _("Edit File in new tab page")
6824 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006826 if (fname == NULL)
6827 goto theend;
6828 eap->arg = fname;
6829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 }
6831 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006834 /*
6835 * Either open new tab page or split the window.
6836 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006837 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006838 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006839 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6840 : eap->addr_count == 0 ? 0
6841 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006842 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006843 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006844
6845 /* set the alternate buffer for the window we came from */
6846 if (curwin != old_curwin
6847 && win_valid(old_curwin)
6848 && old_curwin->w_buffer != curbuf
6849 && !cmdmod.keepalt)
6850 old_curwin->w_alt_fnum = curbuf->b_fnum;
6851 }
6852 }
6853 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 /* Reset 'scrollbind' when editing another file, but keep it when
6857 * doing ":split" without arguments. */
6858 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006859# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006863 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 else
6865 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 do_exedit(eap, old_curwin);
6867 }
6868
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006869# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006873# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874theend:
6875 vim_free(fname);
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
6879/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006880 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006881 */
6882 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006883tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006884{
6885 exarg_T ea;
6886
6887 vim_memset(&ea, 0, sizeof(ea));
6888 ea.cmdidx = CMD_tabnew;
6889 ea.cmd = (char_u *)"tabn";
6890 ea.arg = (char_u *)"";
6891 ex_splitview(&ea);
6892}
6893
6894/*
6895 * :tabnext command
6896 */
6897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006898ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006899{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006900 int tab_number;
6901
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006902 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006903 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006904 switch (eap->cmdidx)
6905 {
6906 case CMD_tabfirst:
6907 case CMD_tabrewind:
6908 goto_tabpage(1);
6909 break;
6910 case CMD_tablast:
6911 goto_tabpage(9999);
6912 break;
6913 case CMD_tabprevious:
6914 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006915 if (eap->arg && *eap->arg != NUL)
6916 {
6917 char_u *p = eap->arg;
6918 char_u *p_save = p;
6919
6920 tab_number = getdigits(&p);
6921 if (p == p_save || *p_save == '-' || *p != NUL
6922 || tab_number == 0)
6923 {
6924 /* No numbers as argument. */
6925 eap->errmsg = e_invarg;
6926 return;
6927 }
6928 }
6929 else
6930 {
6931 if (eap->addr_count == 0)
6932 tab_number = 1;
6933 else
6934 {
6935 tab_number = eap->line2;
6936 if (tab_number < 1)
6937 {
6938 eap->errmsg = e_invrange;
6939 return;
6940 }
6941 }
6942 }
6943 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006944 break;
6945 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006946 tab_number = get_tabpage_arg(eap);
6947 if (eap->errmsg == NULL)
6948 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006949 break;
6950 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006951}
6952
6953/*
6954 * :tabmove command
6955 */
6956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006957ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006958{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006959 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006960
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006961 tab_number = get_tabpage_arg(eap);
6962 if (eap->errmsg == NULL)
6963 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006964}
6965
6966/*
6967 * :tabs command: List tabs and their contents.
6968 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006970ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006971{
6972 tabpage_T *tp;
6973 win_T *wp;
6974 int tabcount = 1;
6975
6976 msg_start();
6977 msg_scroll = TRUE;
6978 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6979 {
6980 msg_putchar('\n');
6981 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006982 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006983 out_flush(); /* output one line at a time */
6984 ui_breakcheck();
6985
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006986 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006987 wp = firstwin;
6988 else
6989 wp = tp->tp_firstwin;
6990 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6991 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006992 msg_putchar('\n');
6993 msg_putchar(wp == curwin ? '>' : ' ');
6994 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006995 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6996 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006997 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006998 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006999 else
7000 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7001 IObuff, IOSIZE, TRUE);
7002 msg_outtrans(IObuff);
7003 out_flush(); /* output one line at a time */
7004 ui_breakcheck();
7005 }
7006 }
7007}
7008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009/*
7010 * ":mode": Set screen mode.
7011 * If no argument given, just get the screen size and redraw.
7012 */
7013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007014ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015{
7016 if (*eap->arg == NUL)
7017 shell_resized();
7018 else
7019 mch_screenmode(eap->arg);
7020}
7021
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022/*
7023 * ":resize".
7024 * set, increment or decrement current window height
7025 */
7026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007027ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
7029 int n;
7030 win_T *wp = curwin;
7031
7032 if (eap->addr_count > 0)
7033 {
7034 n = eap->line2;
7035 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7036 ;
7037 }
7038
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007039# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007041# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 if (cmdmod.split & WSP_VERT)
7044 {
7045 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007046 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7048 n = 9999;
7049 win_setwidth_win((int)n, wp);
7050 }
7051 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 {
7053 if (*eap->arg == '-' || *eap->arg == '+')
7054 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007055 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 n = 9999;
7057 win_setheight_win((int)n, wp);
7058 }
7059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
7061/*
7062 * ":find [+command] <file>" command.
7063 */
7064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007065ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
7067#ifdef FEAT_SEARCHPATH
7068 char_u *fname;
7069 int count;
7070
7071 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7072 TRUE, curbuf->b_ffname);
7073 if (eap->addr_count > 0)
7074 {
7075 /* Repeat finding the file "count" times. This matters when it
7076 * appears several times in the path. */
7077 count = eap->line2;
7078 while (fname != NULL && --count > 0)
7079 {
7080 vim_free(fname);
7081 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7082 FALSE, curbuf->b_ffname);
7083 }
7084 }
7085
7086 if (fname != NULL)
7087 {
7088 eap->arg = fname;
7089#endif
7090 do_exedit(eap, NULL);
7091#ifdef FEAT_SEARCHPATH
7092 vim_free(fname);
7093 }
7094#endif
7095}
7096
7097/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007098 * ":open" simulation: for now just work like ":visual".
7099 */
7100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007101ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007102{
7103 regmatch_T regmatch;
7104 char_u *p;
7105
7106 curwin->w_cursor.lnum = eap->line2;
7107 beginline(BL_SOL | BL_FIX);
7108 if (*eap->arg == '/')
7109 {
7110 /* ":open /pattern/": put cursor in column found with pattern */
7111 ++eap->arg;
7112 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7113 *p = NUL;
7114 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7115 if (regmatch.regprog != NULL)
7116 {
7117 regmatch.rm_ic = p_ic;
7118 p = ml_get_curline();
7119 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007120 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007121 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007122 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007123 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007124 }
7125 /* Move to the NUL, ignore any other arguments. */
7126 eap->arg += STRLEN(eap->arg);
7127 }
7128 check_cursor();
7129
7130 eap->cmdidx = CMD_visual;
7131 do_exedit(eap, NULL);
7132}
7133
7134/*
7135 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 */
7137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007138ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 do_exedit(eap, NULL);
7141}
7142
7143/*
7144 * ":edit <file>" command and alikes.
7145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007147do_exedit(
7148 exarg_T *eap,
7149 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007153 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007155 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007156 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 /*
7158 * ":vi" command ends Ex mode.
7159 */
7160 if (exmode_active && (eap->cmdidx == CMD_visual
7161 || eap->cmdidx == CMD_view))
7162 {
7163 exmode_active = FALSE;
7164 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007165 {
7166 /* Special case: ":global/pat/visual\NLvi-commands" */
7167 if (global_busy)
7168 {
7169 int rd = RedrawingDisabled;
7170 int nwr = no_wait_return;
7171 int ms = msg_scroll;
7172#ifdef FEAT_GUI
7173 int he = hold_gui_events;
7174#endif
7175
7176 if (eap->nextcmd != NULL)
7177 {
7178 stuffReadbuff(eap->nextcmd);
7179 eap->nextcmd = NULL;
7180 }
7181
7182 if (exmode_was != EXMODE_VIM)
7183 settmode(TMODE_RAW);
7184 RedrawingDisabled = 0;
7185 no_wait_return = 0;
7186 need_wait_return = FALSE;
7187 msg_scroll = 0;
7188#ifdef FEAT_GUI
7189 hold_gui_events = 0;
7190#endif
7191 must_redraw = CLEAR;
7192
7193 main_loop(FALSE, TRUE);
7194
7195 RedrawingDisabled = rd;
7196 no_wait_return = nwr;
7197 msg_scroll = ms;
7198#ifdef FEAT_GUI
7199 hold_gui_events = he;
7200#endif
7201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
7205
7206 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007207 || eap->cmdidx == CMD_tabnew
7208 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007209 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007211 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 setpcmark();
7213 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007214 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7215 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007217 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 || *eap->arg != NUL
7219#ifdef FEAT_BROWSE
7220 || cmdmod.browse
7221#endif
7222 )
7223 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007224 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7225 * can bring us here, others are stopped earlier. */
7226 if (*eap->arg != NUL && curbuf_locked())
7227 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007228
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 n = readonlymode;
7230 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7231 readonlymode = TRUE;
7232 else if (eap->cmdidx == CMD_enew)
7233 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7234 empty buffer */
7235 setpcmark();
7236 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7237 NULL, eap,
7238 /* ":edit" goes to first line if Vi compatible */
7239 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7240 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7241 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007242 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007244 /* after a split we can use an existing buffer */
7245 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007247 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
7249 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 if (old_curwin != NULL)
7251 {
7252 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007253 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007255#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007256 cleanup_T cs;
7257
7258 /* Reset the error/interrupt/exception state here so that
7259 * aborting() returns FALSE when closing a window. */
7260 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007261#endif
7262#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007264#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007265 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007266
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007267#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007268 /* Restore the error/interrupt/exception state if not
7269 * discarded by a new aborting error, interrupt, or
7270 * uncaught exception. */
7271 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 }
7274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 }
7276 else if (readonlymode && curbuf->b_nwindows == 1)
7277 {
7278 /* When editing an already visited buffer, 'readonly' won't be set
7279 * but the previous value is kept. With ":view" and ":sview" we
7280 * want the file to be readonly, except when another window is
7281 * editing the same buffer. */
7282 curbuf->b_p_ro = TRUE;
7283 }
7284 readonlymode = n;
7285 }
7286 else
7287 {
7288 if (eap->do_ecmd_cmd != NULL)
7289 do_cmdline_cmd(eap->do_ecmd_cmd);
7290#ifdef FEAT_TITLE
7291 n = curwin->w_arg_idx_invalid;
7292#endif
7293 check_arg_idx(curwin);
7294#ifdef FEAT_TITLE
7295 if (n != curwin->w_arg_idx_invalid)
7296 maketitle();
7297#endif
7298 }
7299
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 /*
7301 * if ":split file" worked, set alternate file name in old window to new
7302 * file
7303 */
7304 if (old_curwin != NULL
7305 && *eap->arg != NUL
7306 && curwin != old_curwin
7307 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007308 && old_curwin->w_buffer != curbuf
7309 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312 ex_no_reprint = TRUE;
7313}
7314
7315#ifndef FEAT_GUI
7316/*
7317 * ":gui" and ":gvim" when there is no GUI.
7318 */
7319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007320ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 eap->errmsg = e_nogvim;
7323}
7324#endif
7325
Bram Moolenaar4f974752019-02-17 17:44:42 +01007326#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007328ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 gui_make_tearoff(eap->arg);
7331}
7332#endif
7333
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007334#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7335 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007337ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007339# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7340 if (gui.in_use)
7341 gui_make_popup(eap->arg, eap->forceit);
7342# ifdef FEAT_TERM_POPUP_MENU
7343 else
7344# endif
7345# endif
7346# ifdef FEAT_TERM_POPUP_MENU
7347 pum_make_popup(eap->arg, eap->forceit);
7348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349}
7350#endif
7351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007353ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354{
7355 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007356 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007358 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360
7361/*
7362 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7363 * offset.
7364 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007367ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007370 win_T *save_curwin = curwin;
7371 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 long topline;
7373 long y;
7374 linenr_T old_linenr = curwin->w_cursor.lnum;
7375
7376 setpcmark();
7377
7378 /*
7379 * determine max topline
7380 */
7381 if (curwin->w_p_scb)
7382 {
7383 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007384 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
7386 if (wp->w_p_scb && wp->w_buffer)
7387 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007388 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 if (topline > y)
7390 topline = y;
7391 }
7392 }
7393 if (topline < 1)
7394 topline = 1;
7395 }
7396 else
7397 {
7398 topline = 1;
7399 }
7400
7401
7402 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007403 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007405 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 {
7407 if (curwin->w_p_scb)
7408 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007409 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 y = topline - curwin->w_topline;
7411 if (y > 0)
7412 scrollup(y, TRUE);
7413 else
7414 scrolldown(-y, TRUE);
7415 curwin->w_scbind_pos = topline;
7416 redraw_later(VALID);
7417 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 }
7420 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007421 curwin = save_curwin;
7422 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 if (curwin->w_p_scb)
7424 {
7425 did_syncbind = TRUE;
7426 checkpcmark();
7427 if (old_linenr != curwin->w_cursor.lnum)
7428 {
7429 char_u ctrl_o[2];
7430
7431 ctrl_o[0] = Ctrl_O;
7432 ctrl_o[1] = 0;
7433 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7434 }
7435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436}
7437
7438
7439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007440ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007442 int i;
7443 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7444 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445
7446 if (eap->usefilter) /* :r!cmd */
7447 do_bang(1, eap, FALSE, FALSE, TRUE);
7448 else
7449 {
7450 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7451 return;
7452
7453#ifdef FEAT_BROWSE
7454 if (cmdmod.browse)
7455 {
7456 char_u *browseFile;
7457
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007458 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 NULL, NULL, NULL, curbuf);
7460 if (browseFile != NULL)
7461 {
7462 i = readfile(browseFile, NULL,
7463 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7464 vim_free(browseFile);
7465 }
7466 else
7467 i = OK;
7468 }
7469 else
7470#endif
7471 if (*eap->arg == NUL)
7472 {
7473 if (check_fname() == FAIL) /* check for no file name */
7474 return;
7475 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7476 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7477 }
7478 else
7479 {
7480 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7481 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7482 i = readfile(eap->arg, NULL,
7483 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7484
7485 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007486 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007488#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 if (!aborting())
7490#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007491 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 }
7493 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007494 {
7495 if (empty && exmode_active)
7496 {
7497 /* Delete the empty line that remains. Historically ex does
7498 * this but vi doesn't. */
7499 if (eap->line2 == 0)
7500 lnum = curbuf->b_ml.ml_line_count;
7501 else
7502 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007503 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007504 {
7505 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007506 if (curwin->w_cursor.lnum > 1
7507 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007508 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007509 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007510 }
7511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 }
7515}
7516
Bram Moolenaarea408852005-06-25 22:49:46 +00007517static char_u *prev_dir = NULL;
7518
7519#if defined(EXITFREE) || defined(PROTO)
7520 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007521free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007522{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007523 VIM_CLEAR(prev_dir);
7524 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007525}
7526#endif
7527
Bram Moolenaarf4258302013-06-02 18:20:17 +02007528/*
7529 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007530 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7531 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007532 */
7533 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007534post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007535{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007536 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007537 // Clear tab local directory for both :cd and :tcd
7538 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007539 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007540 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007541 {
7542 /* If still in global directory, need to remember current
7543 * directory as global directory. */
7544 if (globaldir == NULL && prev_dir != NULL)
7545 globaldir = vim_strsave(prev_dir);
7546 /* Remember this local directory for the window. */
7547 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007548 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007549 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007550 curtab->tp_localdir = vim_strsave(NameBuff);
7551 else
7552 curwin->w_localdir = vim_strsave(NameBuff);
7553 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007554 }
7555 else
7556 {
7557 /* We are now in the global directory, no need to remember its
7558 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007559 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007560 }
7561
7562 shorten_fnames(TRUE);
7563}
7564
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007565/*
7566 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7567 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7568 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7569 * Otherwise changes the global directory.
7570 * Returns TRUE if the directory is successfully changed.
7571 */
7572 int
7573changedir_func(
7574 char_u *new_dir,
7575 int forceit,
7576 cdscope_T scope)
7577{
7578 char_u *tofree;
7579 int dir_differs;
7580 int retval = FALSE;
7581
7582 if (allbuf_locked())
7583 return FALSE;
7584
7585 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7586 {
7587 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7588 return FALSE;
7589 }
7590
7591 // ":cd -": Change to previous directory
7592 if (STRCMP(new_dir, "-") == 0)
7593 {
7594 if (prev_dir == NULL)
7595 {
7596 emsg(_("E186: No previous directory"));
7597 return FALSE;
7598 }
7599 new_dir = prev_dir;
7600 }
7601
7602 // Save current directory for next ":cd -"
7603 tofree = prev_dir;
7604 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7605 prev_dir = vim_strsave(NameBuff);
7606 else
7607 prev_dir = NULL;
7608
7609#if defined(UNIX) || defined(VMS)
7610 // for UNIX ":cd" means: go to home directory
7611 if (*new_dir == NUL)
7612 {
7613 // use NameBuff for home directory name
7614# ifdef VMS
7615 char_u *p;
7616
7617 p = mch_getenv((char_u *)"SYS$LOGIN");
7618 if (p == NULL || *p == NUL) // empty is the same as not set
7619 NameBuff[0] = NUL;
7620 else
7621 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7622# else
7623 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7624# endif
7625 new_dir = NameBuff;
7626 }
7627#endif
7628 dir_differs = new_dir == NULL || prev_dir == NULL
7629 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7630 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7631 emsg(_(e_failed));
7632 else
7633 {
7634 char_u *acmd_fname;
7635
7636 post_chdir(scope);
7637
7638 if (dir_differs)
7639 {
7640 if (scope == CDSCOPE_WINDOW)
7641 acmd_fname = (char_u *)"window";
7642 else if (scope == CDSCOPE_TABPAGE)
7643 acmd_fname = (char_u *)"tabpage";
7644 else
7645 acmd_fname = (char_u *)"global";
7646 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7647 curbuf);
7648 }
7649 retval = TRUE;
7650 }
7651 vim_free(tofree);
7652
7653 return retval;
7654}
Bram Moolenaarea408852005-06-25 22:49:46 +00007655
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007657 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007659 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007660ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007662 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663
7664 new_dir = eap->arg;
7665#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007666 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (*new_dir == NUL)
7668 ex_pwd(NULL);
7669 else
7670#endif
7671 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007672 cdscope_T scope = CDSCOPE_GLOBAL;
7673
7674 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7675 scope = CDSCOPE_WINDOW;
7676 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7677 scope = CDSCOPE_TABPAGE;
7678
7679 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007680 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007681 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007682 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 ex_pwd(eap);
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686}
7687
7688/*
7689 * ":pwd".
7690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007692ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7695 {
7696#ifdef BACKSLASH_IN_FILENAME
7697 slash_adjust(NameBuff);
7698#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007699 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007702 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703}
7704
7705/*
7706 * ":=".
7707 */
7708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007709ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007711 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007712 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713}
7714
7715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007716ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007718 int n;
7719 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
7721 if (cursor_valid())
7722 {
7723 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7724 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007725 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007727
7728 len = eap->line2;
7729 switch (*eap->arg)
7730 {
7731 case 'm': break;
7732 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007733 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007734 }
7735 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736}
7737
7738/*
7739 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7740 */
7741 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007742do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007745 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
7747 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007748 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007749 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007751 wait_now = msec - done > 1000L ? 1000L : msec - done;
7752#ifdef FEAT_TIMERS
7753 {
7754 long due_time = check_due_timer();
7755
7756 if (due_time > 0 && due_time < wait_now)
7757 wait_now = due_time;
7758 }
7759#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007760#ifdef FEAT_JOB_CHANNEL
7761 if (has_any_channel() && wait_now > 100L)
7762 wait_now = 100L;
7763#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007764 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007765#ifdef FEAT_JOB_CHANNEL
7766 if (has_any_channel())
7767 ui_breakcheck_force(TRUE);
7768 else
7769#endif
7770 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007771#ifdef MESSAGE_QUEUE
7772 /* Process the netbeans and clientserver messages that may have been
7773 * received in the call to ui_breakcheck() when the GUI is in use. This
7774 * may occur when running a test case. */
7775 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007778
7779 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7780 // input buffer, otherwise a following call to input() fails.
7781 if (got_int)
7782 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
7784
7785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007786do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787{
7788 int mode;
7789 char_u *cmdp;
7790
7791 cmdp = eap->cmd;
7792 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7793
7794 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7795 eap->arg, mode, isabbrev))
7796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007797 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007799 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 break;
7801 }
7802}
7803
7804/*
7805 * ":winsize" command (obsolete).
7806 */
7807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007808ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809{
7810 int w, h;
7811 char_u *arg = eap->arg;
7812 char_u *p;
7813
7814 w = getdigits(&arg);
7815 arg = skipwhite(arg);
7816 p = arg;
7817 h = getdigits(&arg);
7818 if (*p != NUL && *arg == NUL)
7819 set_shellsize(w, h, TRUE);
7820 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007821 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007825ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 int xchar = NUL;
7828 char_u *p;
7829
7830 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7831 {
7832 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7833 if (eap->arg[1] == NUL)
7834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007835 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 return;
7837 }
7838 xchar = eap->arg[1];
7839 p = eap->arg + 2;
7840 }
7841 else
7842 p = eap->arg + 1;
7843
7844 eap->nextcmd = check_nextcmd(p);
7845 p = skipwhite(p);
7846 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007847 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007848 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 {
7850 /* Pass flags on for ":vertical wincmd ]". */
7851 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007852 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7854 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007855 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 }
7857}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858
Bram Moolenaar843ee412004-06-30 16:16:41 +00007859#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860/*
7861 * ":winpos".
7862 */
7863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007864ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865{
7866 int x, y;
7867 char_u *arg = eap->arg;
7868 char_u *p;
7869
7870 if (*arg == NUL)
7871 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007872# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007873# ifdef VIMDLL
7874 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7875 mch_get_winpos(&x, &y) != FAIL)
7876# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007878# else
7879 if (mch_get_winpos(&x, &y) != FAIL)
7880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
7882 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007883 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 }
7885 else
7886# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007887 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 }
7889 else
7890 {
7891 x = getdigits(&arg);
7892 arg = skipwhite(arg);
7893 p = arg;
7894 y = getdigits(&arg);
7895 if (*p == NUL || *arg != NUL)
7896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007897 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 return;
7899 }
7900# ifdef FEAT_GUI
7901 if (gui.in_use)
7902 gui_mch_set_winpos(x, y);
7903 else if (gui.starting)
7904 {
7905 /* Remember the coordinates for when the window is opened. */
7906 gui_win_x = x;
7907 gui_win_y = y;
7908 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007909# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 else
7911# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007912# endif
7913# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007914 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915# endif
7916# ifdef HAVE_TGETENT
7917 if (*T_CWP)
7918 term_set_winpos(x, y);
7919# endif
7920 }
7921}
7922#endif
7923
7924/*
7925 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7926 */
7927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007928ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 oparg_T oa;
7931
7932 clear_oparg(&oa);
7933 oa.regname = eap->regname;
7934 oa.start.lnum = eap->line1;
7935 oa.end.lnum = eap->line2;
7936 oa.line_count = eap->line2 - eap->line1 + 1;
7937 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7940 {
7941 setpcmark();
7942 curwin->w_cursor.lnum = eap->line1;
7943 beginline(BL_SOL | BL_FIX);
7944 }
7945
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007946 if (VIsual_active)
7947 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007948
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 switch (eap->cmdidx)
7950 {
7951 case CMD_delete:
7952 oa.op_type = OP_DELETE;
7953 op_delete(&oa);
7954 break;
7955
7956 case CMD_yank:
7957 oa.op_type = OP_YANK;
7958 (void)op_yank(&oa, FALSE, TRUE);
7959 break;
7960
7961 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007962 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007964 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7965#else
7966 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007968 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 oa.op_type = OP_RSHIFT;
7970 else
7971 oa.op_type = OP_LSHIFT;
7972 op_shift(&oa, FALSE, eap->amount);
7973 break;
7974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007976 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977}
7978
7979/*
7980 * ":put".
7981 */
7982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007983ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984{
7985 /* ":0put" works like ":1put!". */
7986 if (eap->line2 == 0)
7987 {
7988 eap->line2 = 1;
7989 eap->forceit = TRUE;
7990 }
7991 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007992 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7993 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994}
7995
7996/*
7997 * Handle ":copy" and ":move".
7998 */
7999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008000ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001{
8002 long n;
8003
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008004 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 if (eap->arg == NULL) /* error detected */
8006 {
8007 eap->nextcmd = NULL;
8008 return;
8009 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008010 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011
8012 /*
8013 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8014 */
8015 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008017 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 return;
8019 }
8020
8021 if (eap->cmdidx == CMD_move)
8022 {
8023 if (do_move(eap->line1, eap->line2, n) == FAIL)
8024 return;
8025 }
8026 else
8027 ex_copy(eap->line1, eap->line2, n);
8028 u_clearline();
8029 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008030 ex_may_print(eap);
8031}
8032
8033/*
8034 * Print the current line if flags were given to the Ex command.
8035 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008036 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008037ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008038{
8039 if (eap->flags != 0)
8040 {
8041 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8042 (eap->flags & EXFLAG_LIST));
8043 ex_no_reprint = TRUE;
8044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045}
8046
8047/*
8048 * ":smagic" and ":snomagic".
8049 */
8050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008051ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053 int magic_save = p_magic;
8054
8055 p_magic = (eap->cmdidx == CMD_smagic);
8056 do_sub(eap);
8057 p_magic = magic_save;
8058}
8059
8060/*
8061 * ":join".
8062 */
8063 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008064ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065{
8066 curwin->w_cursor.lnum = eap->line1;
8067 if (eap->line1 == eap->line2)
8068 {
8069 if (eap->addr_count >= 2) /* :2,2join does nothing */
8070 return;
8071 if (eap->line2 == curbuf->b_ml.ml_line_count)
8072 {
8073 beep_flush();
8074 return;
8075 }
8076 ++eap->line2;
8077 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008078 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008080 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081}
8082
8083/*
8084 * ":[addr]@r" or ":[addr]*r": execute register
8085 */
8086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008087ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088{
8089 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008090 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091
8092 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008093 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094
8095#ifdef USE_ON_FLY_SCROLL
8096 dont_scroll = TRUE; /* disallow scrolling here */
8097#endif
8098
8099 /* get the register name. No name means to use the previous one */
8100 c = *eap->arg;
8101 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8102 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008103 /* Put the register in the typeahead buffer with the "silent" flag. */
8104 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8105 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 else
8110 {
8111 int save_efr = exec_from_reg;
8112
8113 exec_from_reg = TRUE;
8114
8115 /*
8116 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008117 * Continue until the stuff buffer is empty and all added characters
8118 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008120 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8122
8123 exec_from_reg = save_efr;
8124 }
8125}
8126
8127/*
8128 * ":!".
8129 */
8130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008131ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
8133 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8134}
8135
8136/*
8137 * ":undo".
8138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008140ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008142 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008143 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008144 else
8145 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146}
8147
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008148#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008150ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008151{
8152 char_u hash[UNDO_HASH_SIZE];
8153
8154 u_compute_hash(hash);
8155 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8156}
8157
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008159ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008160{
8161 char_u hash[UNDO_HASH_SIZE];
8162
8163 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008164 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008165}
8166#endif
8167
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168/*
8169 * ":redo".
8170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008172ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173{
8174 u_redo(1);
8175}
8176
8177/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008178 * ":earlier" and ":later".
8179 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008182{
8183 long count = 0;
8184 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008185 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008186 char_u *p = eap->arg;
8187
8188 if (*p == NUL)
8189 count = 1;
8190 else if (isdigit(*p))
8191 {
8192 count = getdigits(&p);
8193 switch (*p)
8194 {
8195 case 's': ++p; sec = TRUE; break;
8196 case 'm': ++p; sec = TRUE; count *= 60; break;
8197 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008198 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8199 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008200 }
8201 }
8202
8203 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008204 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008205 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008206 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8207 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008208}
8209
8210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 * ":redir": start/stop redirection.
8212 */
8213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008214ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215{
8216 char *mode;
8217 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008218 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219
Bram Moolenaarba768492016-07-08 15:32:54 +02008220#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008221 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008223 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008224 return;
8225 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008226#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 if (STRICMP(eap->arg, "END") == 0)
8229 close_redir();
8230 else
8231 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008232 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008234 ++arg;
8235 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008237 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 mode = "a";
8239 }
8240 else
8241 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008242 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243
8244 close_redir();
8245
8246 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008247 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 if (fname == NULL)
8249 return;
8250#ifdef FEAT_BROWSE
8251 if (cmdmod.browse)
8252 {
8253 char_u *browseFile;
8254
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008255 browseFile = do_browse(BROWSE_SAVE,
8256 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008257 fname, NULL, NULL,
8258 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 if (browseFile == NULL)
8260 return; /* operation cancelled */
8261 vim_free(fname);
8262 fname = browseFile;
8263 eap->forceit = TRUE; /* since dialog already asked */
8264 }
8265#endif
8266
8267 redir_fd = open_exfile(fname, eap->forceit, mode);
8268 vim_free(fname);
8269 }
8270#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008271 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 {
8273 /* redirect to a register a-z (resp. A-Z for appending) */
8274 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008275 ++arg;
8276 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008278 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008279 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008281 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008283 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008284 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008285 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008286 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008288 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008289 if (*arg == '>')
8290 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008291 /* Make register empty when not using @A-@Z and the
8292 * command is valid. */
8293 if (*arg == NUL && !isupper(redir_reg))
8294 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008296 }
8297 if (*arg != NUL)
8298 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008299 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008300 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008301 }
8302 }
8303 else if (*arg == '=' && arg[1] == '>')
8304 {
8305 int append;
8306
8307 /* redirect to a variable */
8308 close_redir();
8309 arg += 2;
8310
8311 if (*arg == '>')
8312 {
8313 ++arg;
8314 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 }
8316 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008317 append = FALSE;
8318
8319 if (var_redir_start(skipwhite(arg), append) == OK)
8320 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
8322#endif
8323
8324 /* TODO: redirect to a buffer */
8325
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008327 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008329
8330 /* Make sure redirection is not off. Can happen for cmdline completion
8331 * that indirectly invokes a command to catch its output. */
8332 if (redir_fd != NULL
8333#ifdef FEAT_EVAL
8334 || redir_reg || redir_vname
8335#endif
8336 )
8337 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338}
8339
8340/*
8341 * ":redraw": force redraw
8342 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008343 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008344ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
8346 int r = RedrawingDisabled;
8347 int p = p_lz;
8348
8349 RedrawingDisabled = 0;
8350 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008351 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008353 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354#ifdef FEAT_TITLE
8355 if (need_maketitle)
8356 maketitle();
8357#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008358#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8359# ifdef VIMDLL
8360 if (!gui.in_use)
8361# endif
8362 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 RedrawingDisabled = r;
8365 p_lz = p;
8366
8367 /* Reset msg_didout, so that a message that's there is overwritten. */
8368 msg_didout = FALSE;
8369 msg_col = 0;
8370
Bram Moolenaar56667a52013-07-17 11:54:28 +02008371 /* No need to wait after an intentional redraw. */
8372 need_wait_return = FALSE;
8373
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 out_flush();
8375}
8376
8377/*
8378 * ":redrawstatus": force redraw of status line(s)
8379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008381ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 int r = RedrawingDisabled;
8384 int p = p_lz;
8385
8386 RedrawingDisabled = 0;
8387 p_lz = FALSE;
8388 if (eap->forceit)
8389 status_redraw_all();
8390 else
8391 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008392 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 RedrawingDisabled = r;
8394 p_lz = p;
8395 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396}
8397
Bram Moolenaare12bab32019-01-08 22:02:56 +01008398/*
8399 * ":redrawtabline": force redraw of the tabline
8400 */
8401 static void
8402ex_redrawtabline(exarg_T *eap UNUSED)
8403{
8404 int r = RedrawingDisabled;
8405 int p = p_lz;
8406
8407 RedrawingDisabled = 0;
8408 p_lz = FALSE;
8409
8410 draw_tabline();
8411
8412 RedrawingDisabled = r;
8413 p_lz = p;
8414 out_flush();
8415}
8416
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008418close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 if (redir_fd != NULL)
8421 {
8422 fclose(redir_fd);
8423 redir_fd = NULL;
8424 }
8425#ifdef FEAT_EVAL
8426 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008427 if (redir_vname)
8428 {
8429 var_redir_stop();
8430 redir_vname = 0;
8431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432#endif
8433}
8434
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008435#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008436 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008437vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008438{
8439 if (vim_mkdir(name, prot) != 0)
8440 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008441 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008442 return FAIL;
8443 }
8444 return OK;
8445}
8446#endif
8447
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448/*
8449 * Open a file for writing for an Ex command, with some checks.
8450 * Return file descriptor, or NULL on failure.
8451 */
8452 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453open_exfile(
8454 char_u *fname,
8455 int forceit,
8456 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457{
8458 FILE *fd;
8459
8460#ifdef UNIX
8461 /* with Unix it is possible to open a directory */
8462 if (mch_isdir(fname))
8463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008464 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 return NULL;
8466 }
8467#endif
8468 if (!forceit && *mode != 'a' && vim_fexists(fname))
8469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008470 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 return NULL;
8472 }
8473
8474 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008475 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476
8477 return fd;
8478}
8479
8480/*
8481 * ":mark" and ":k".
8482 */
8483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008484ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485{
8486 pos_T pos;
8487
8488 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008489 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008491 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 else
8493 {
8494 pos = curwin->w_cursor; /* save curwin->w_cursor */
8495 curwin->w_cursor.lnum = eap->line2;
8496 beginline(BL_WHITE | BL_FIX);
8497 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008498 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8500 }
8501}
8502
8503/*
8504 * Update w_topline, w_leftcol and the cursor position.
8505 */
8506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008507update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
8509 check_cursor(); /* put cursor on valid line */
8510 update_topline();
8511 if (!curwin->w_p_wrap)
8512 validate_cursor();
8513 update_curswant();
8514}
8515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008517 * Save the current State and go to Normal mode.
8518 * Return TRUE if the typeahead could be saved.
8519 */
8520 int
8521save_current_state(save_state_T *sst)
8522{
8523 sst->save_msg_scroll = msg_scroll;
8524 sst->save_restart_edit = restart_edit;
8525 sst->save_msg_didout = msg_didout;
8526 sst->save_State = State;
8527 sst->save_insertmode = p_im;
8528 sst->save_finish_op = finish_op;
8529 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008530 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008531
8532 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8533 restart_edit = 0; /* don't go to Insert mode */
8534 p_im = FALSE; /* don't use 'insertmode' */
8535
8536 /*
8537 * Save the current typeahead. This is required to allow using ":normal"
8538 * from an event handler and makes sure we don't hang when the argument
8539 * ends with half a command.
8540 */
8541 save_typeahead(&sst->tabuf);
8542 return sst->tabuf.typebuf_valid;
8543}
8544
8545 void
8546restore_current_state(save_state_T *sst)
8547{
8548 /* Restore the previous typeahead. */
8549 restore_typeahead(&sst->tabuf);
8550
8551 msg_scroll = sst->save_msg_scroll;
8552 restart_edit = sst->save_restart_edit;
8553 p_im = sst->save_insertmode;
8554 finish_op = sst->save_finish_op;
8555 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008556 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008557 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8558
8559 /* Restore the state (needed when called from a function executed for
8560 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8561 State = sst->save_State;
8562#ifdef CURSOR_SHAPE
8563 ui_cursor_shape(); /* may show different cursor shape */
8564#endif
8565}
8566
8567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 * ":normal[!] {commands}": Execute normal mode commands.
8569 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008571ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008573 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 char_u *arg = NULL;
8575 int l;
8576 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008578 if (ex_normal_lock > 0)
8579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008580 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008581 return;
8582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 if (ex_normal_busy >= p_mmd)
8584 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008585 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 return;
8587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 /*
8590 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8591 * this for the K_SPECIAL leading byte, otherwise special keys will not
8592 * work.
8593 */
8594 if (has_mbyte)
8595 {
8596 int len = 0;
8597
8598 /* Count the number of characters to be escaped. */
8599 for (p = eap->arg; *p != NUL; ++p)
8600 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008601#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 if (*p == CSI) /* leadbyte CSI */
8603 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008604#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008605 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008607#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 )
8611 len += 2;
8612 }
8613 if (len > 0)
8614 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008615 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 if (arg != NULL)
8617 {
8618 len = 0;
8619 for (p = eap->arg; *p != NUL; ++p)
8620 {
8621 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008622#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 if (*p == CSI)
8624 {
8625 arg[len++] = KS_EXTRA;
8626 arg[len++] = (int)KE_CSI;
8627 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008628#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008629 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {
8631 arg[len++] = *++p;
8632 if (*p == K_SPECIAL)
8633 {
8634 arg[len++] = KS_SPECIAL;
8635 arg[len++] = KE_FILLER;
8636 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008637#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 else if (*p == CSI)
8639 {
8640 arg[len++] = KS_EXTRA;
8641 arg[len++] = (int)KE_CSI;
8642 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645 arg[len] = NUL;
8646 }
8647 }
8648 }
8649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008651 ++ex_normal_busy;
8652 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 {
8654 /*
8655 * Repeat the :normal command for each line in the range. When no
8656 * range given, execute it just once, without positioning the cursor
8657 * first.
8658 */
8659 do
8660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 if (eap->addr_count != 0)
8662 {
8663 curwin->w_cursor.lnum = eap->line1++;
8664 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008665 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 }
8667
Bram Moolenaar13505972019-01-24 15:04:48 +01008668 exec_normal_cmd(arg != NULL
8669 ? arg
8670 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 }
8672 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8673 }
8674
8675 /* Might not return to the main loop when in an event handler. */
8676 update_topline_cursor();
8677
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008678 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008680#ifdef FEAT_MOUSE
8681 setmouse();
8682#endif
8683#ifdef CURSOR_SHAPE
8684 ui_cursor_shape(); /* may show different cursor shape */
8685#endif
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688}
8689
8690/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008691 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 */
8693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008694ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008696 if (eap->forceit)
8697 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008698 /* cursor line can be zero on startup */
8699 if (!curwin->w_cursor.lnum)
8700 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008701 coladvance((colnr_T)MAXCOL);
8702 curwin->w_curswant = MAXCOL;
8703 curwin->w_set_curswant = FALSE;
8704 }
8705
Bram Moolenaara40c5002005-01-09 21:16:21 +00008706 /* Ignore the command when already in Insert mode. Inserting an
8707 * expression register that invokes a function can do this. */
8708 if (State & INSERT)
8709 return;
8710
Bram Moolenaar64969662005-12-14 21:59:55 +00008711 if (eap->cmdidx == CMD_startinsert)
8712 restart_edit = 'a';
8713 else if (eap->cmdidx == CMD_startreplace)
8714 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008716 restart_edit = 'V';
8717
8718 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008720 if (eap->cmdidx == CMD_startinsert)
8721 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 curwin->w_curswant = 0; /* avoid MAXCOL */
8723 }
8724}
8725
8726/*
8727 * ":stopinsert"
8728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008730ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
8732 restart_edit = 0;
8733 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008734 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008737/*
8738 * Execute normal mode command "cmd".
8739 * "remap" can be REMAP_NONE or REMAP_YES.
8740 */
8741 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008742exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008743{
Bram Moolenaar25281632016-01-21 23:32:32 +01008744 /* Stuff the argument into the typeahead buffer. */
8745 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008746 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008747}
Bram Moolenaar25281632016-01-21 23:32:32 +01008748
Bram Moolenaar25281632016-01-21 23:32:32 +01008749/*
8750 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008751 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008752 */
8753 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008754exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008755{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008756 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008757 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008758
Bram Moolenaar905dd902019-04-07 14:21:47 +02008759 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8760 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008761 clear_oparg(&oa);
8762 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008763 while ((!stuff_empty()
8764 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008765 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008766 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008767 {
8768 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008769#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008770 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008771 && oa.op_type == OP_NOP && oa.regname == NUL
8772 && !VIsual_active)
8773 {
8774 /* If terminal_loop() returns OK we got a key that is handled
8775 * in Normal model. With FAIL we first need to position the
8776 * cursor and the screen needs to be redrawn. */
8777 if (terminal_loop(TRUE) == OK)
8778 normal_cmd(&oa, TRUE);
8779 }
8780 else
8781#endif
8782 /* execute a Normal mode cmd */
8783 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008784 }
8785}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008786
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787#ifdef FEAT_FIND_ID
8788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008789ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790{
8791 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8792 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8793 (linenr_T)1, (linenr_T)MAXLNUM);
8794}
8795
Bram Moolenaar4033c552017-09-16 20:54:51 +02008796#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797/*
8798 * ":psearch"
8799 */
8800 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008801ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
8803 g_do_tagpreview = p_pvh;
8804 ex_findpat(eap);
8805 g_do_tagpreview = 0;
8806}
8807#endif
8808
8809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008810ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
8812 int whole = TRUE;
8813 long n;
8814 char_u *p;
8815 int action;
8816
8817 switch (cmdnames[eap->cmdidx].cmd_name[2])
8818 {
8819 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8820 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8821 action = ACTION_GOTO;
8822 else
8823 action = ACTION_SHOW;
8824 break;
8825 case 'i': /* ":ilist" and ":dlist" */
8826 action = ACTION_SHOW_ALL;
8827 break;
8828 case 'u': /* ":ijump" and ":djump" */
8829 action = ACTION_GOTO;
8830 break;
8831 default: /* ":isplit" and ":dsplit" */
8832 action = ACTION_SPLIT;
8833 break;
8834 }
8835
8836 n = 1;
8837 if (vim_isdigit(*eap->arg)) /* get count */
8838 {
8839 n = getdigits(&eap->arg);
8840 eap->arg = skipwhite(eap->arg);
8841 }
8842 if (*eap->arg == '/') /* Match regexp, not just whole words */
8843 {
8844 whole = FALSE;
8845 ++eap->arg;
8846 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8847 if (*p)
8848 {
8849 *p++ = NUL;
8850 p = skipwhite(p);
8851
8852 /* Check for trailing illegal characters */
8853 if (!ends_excmd(*p))
8854 eap->errmsg = e_trailing;
8855 else
8856 eap->nextcmd = check_nextcmd(p);
8857 }
8858 }
8859 if (!eap->skip)
8860 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8861 whole, !eap->forceit,
8862 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8863 n, action, eap->line1, eap->line2);
8864}
8865#endif
8866
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867
Bram Moolenaar4033c552017-09-16 20:54:51 +02008868#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869/*
8870 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8871 */
8872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008873ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008875 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8877}
8878
8879/*
8880 * ":pedit"
8881 */
8882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008883ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884{
8885 win_T *curwin_save = curwin;
8886
8887 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00008888 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02008889 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 do_exedit(eap, NULL);
8891 keep_help_flag = FALSE;
8892 if (curwin != curwin_save && win_valid(curwin_save))
8893 {
8894 /* Return cursor to where we were */
8895 validate_cursor();
8896 redraw_later(VALID);
8897 win_enter(curwin_save, TRUE);
8898 }
8899 g_do_tagpreview = 0;
8900}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902
8903/*
8904 * ":stag", ":stselect" and ":stjump".
8905 */
8906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008907ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
8909 postponed_split = -1;
8910 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008911 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8913 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008914 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
8917/*
8918 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8919 */
8920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008921ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8924}
8925
8926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008927ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928{
8929 int cmd;
8930
8931 switch (name[1])
8932 {
8933 case 'j': cmd = DT_JUMP; /* ":tjump" */
8934 break;
8935 case 's': cmd = DT_SELECT; /* ":tselect" */
8936 break;
8937 case 'p': cmd = DT_PREV; /* ":tprevious" */
8938 break;
8939 case 'N': cmd = DT_PREV; /* ":tNext" */
8940 break;
8941 case 'n': cmd = DT_NEXT; /* ":tnext" */
8942 break;
8943 case 'o': cmd = DT_POP; /* ":pop" */
8944 break;
8945 case 'f': /* ":tfirst" */
8946 case 'r': cmd = DT_FIRST; /* ":trewind" */
8947 break;
8948 case 'l': cmd = DT_LAST; /* ":tlast" */
8949 break;
8950 default: /* ":tag" */
8951#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008952 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008954 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 return;
8956 }
8957#endif
8958 cmd = DT_TAG;
8959 break;
8960 }
8961
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008962 if (name[0] == 'l')
8963 {
8964#ifndef FEAT_QUICKFIX
8965 ex_ni(eap);
8966 return;
8967#else
8968 cmd = DT_LTAG;
8969#endif
8970 }
8971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8973 eap->forceit, TRUE);
8974}
8975
8976/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008977 * Check "str" for starting with a special cmdline variable.
8978 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8979 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8980 */
8981 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008982find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008983{
8984 int len;
8985 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008986 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008987 "%",
8988#define SPEC_PERC 0
8989 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008990#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008991 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008992#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008993 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008994#define SPEC_CCWORD (SPEC_CWORD + 1)
8995 "<cexpr>", /* expr under cursor */
8996#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008997 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008998#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008999 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009000#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009001 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009002#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009003 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009004#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009005 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009006#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009007 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009008#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009009 "<sflnum>", /* script file line number */
9010#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009011#ifdef FEAT_CLIENTSERVER
9012 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009013# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009014#endif
9015 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009016
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009017 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009018 {
9019 len = (int)STRLEN(spec_str[i]);
9020 if (STRNCMP(src, spec_str[i], len) == 0)
9021 {
9022 *usedlen = len;
9023 return i;
9024 }
9025 }
9026 return -1;
9027}
9028
9029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 * Evaluate cmdline variables.
9031 *
9032 * change '%' to curbuf->b_ffname
9033 * '#' to curwin->w_altfile
9034 * '<cword>' to word under the cursor
9035 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02009036 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 * '<cfile>' to path name under the cursor
9038 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009039 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 * '<afile>' to file name for autocommand
9041 * '<abuf>' to buffer number for autocommand
9042 * '<amatch>' to matching name for autocommand
9043 *
9044 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9045 * "" for error without a message) and NULL is returned.
9046 * Returns an allocated string if a valid match was found.
9047 * Returns NULL if no match was found. "usedlen" then still contains the
9048 * number of characters to skip.
9049 */
9050 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009051eval_vars(
9052 char_u *src, /* pointer into commandline */
9053 char_u *srcstart, /* beginning of valid memory for src */
9054 int *usedlen, /* characters after src that are used */
9055 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009056 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009057 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009058 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 int i;
9061 char_u *s;
9062 char_u *result;
9063 char_u *resultbuf = NULL;
9064 int resultlen;
9065 buf_T *buf;
9066 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9067 int spec_idx;
9068#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009069 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 int skip_mod = FALSE;
9071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073
9074 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009075 if (escaped != NULL)
9076 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077
9078 /*
9079 * Check if there is something to do.
9080 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009081 spec_idx = find_cmdline_var(src, usedlen);
9082 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 {
9084 *usedlen = 1;
9085 return NULL;
9086 }
9087
9088 /*
9089 * Skip when preceded with a backslash "\%" and "\#".
9090 * Note: In "\\%" the % is also not recognized!
9091 */
9092 if (src > srcstart && src[-1] == '\\')
9093 {
9094 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009095 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 return NULL;
9097 }
9098
9099 /*
9100 * word or WORD under cursor
9101 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009102 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9103 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009105 resultlen = find_ident_under_cursor(&result,
9106 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9107 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9108 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 if (resultlen == 0)
9110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009111 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 return NULL;
9113 }
9114 }
9115
9116 /*
9117 * '#': Alternate file name
9118 * '%': Current file name
9119 * File name under the cursor
9120 * File name for autocommand
9121 * and following modifiers
9122 */
9123 else
9124 {
9125 switch (spec_idx)
9126 {
9127 case SPEC_PERC: /* '%': current file */
9128 if (curbuf->b_fname == NULL)
9129 {
9130 result = (char_u *)"";
9131 valid = 0; /* Must have ":p:h" to be valid */
9132 }
9133 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009136#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009137 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009138#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 break;
9141
9142 case SPEC_HASH: /* '#' or "#99": alternate file */
9143 if (src[1] == '#') /* "##": the argument list */
9144 {
9145 result = arg_all();
9146 resultbuf = result;
9147 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009148 if (escaped != NULL)
9149 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#ifdef FEAT_MODIFY_FNAME
9151 skip_mod = TRUE;
9152#endif
9153 break;
9154 }
9155 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009156 if (*s == '<') /* "#<99" uses v:oldfiles */
9157 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009159 if (s == src + 2 && src[1] == '-')
9160 /* just a minus sign, don't skip over it */
9161 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 *usedlen = (int)(s - src); /* length of what we expand */
9163
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009164 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009166 if (*usedlen < 2)
9167 {
9168 /* Should we give an error message for #<text? */
9169 *usedlen = 1;
9170 return NULL;
9171 }
9172#ifdef FEAT_EVAL
9173 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9174 (long)i);
9175 if (result == NULL)
9176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009177 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009178 return NULL;
9179 }
9180#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009181 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
9185 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009186 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009187 if (i == 0 && src[1] == '<' && *usedlen > 1)
9188 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009189 buf = buflist_findnr(i);
9190 if (buf == NULL)
9191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009192 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009193 return NULL;
9194 }
9195 if (lnump != NULL)
9196 *lnump = ECMD_LAST;
9197 if (buf->b_fname == NULL)
9198 {
9199 result = (char_u *)"";
9200 valid = 0; /* Must have ":p:h" to be valid */
9201 }
9202 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009203 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009204 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009205#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009206 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009207#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009208 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 break;
9211
9212#ifdef FEAT_SEARCHPATH
9213 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009214 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 if (result == NULL)
9216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009217 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 return NULL;
9219 }
9220 resultbuf = result; /* remember allocated string */
9221 break;
9222#endif
9223
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 case SPEC_AFILE: /* file name for autocommand */
9225 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009226 if (result != NULL && !autocmd_fname_full)
9227 {
9228 /* Still need to turn the fname into a full path. It is
9229 * postponed to avoid a delay when <afile> is not used. */
9230 autocmd_fname_full = TRUE;
9231 result = FullName_save(autocmd_fname, FALSE);
9232 vim_free(autocmd_fname);
9233 autocmd_fname = result;
9234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 if (result == NULL)
9236 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009237 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 return NULL;
9239 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009240 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 break;
9242
9243 case SPEC_ABUF: /* buffer number for autocommand */
9244 if (autocmd_bufnr <= 0)
9245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009246 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 return NULL;
9248 }
9249 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9250 result = strbuf;
9251 break;
9252
9253 case SPEC_AMATCH: /* match name for autocommand */
9254 result = autocmd_match;
9255 if (result == NULL)
9256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009257 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 return NULL;
9259 }
9260 break;
9261
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 case SPEC_SFILE: /* file name for ":so" command */
9263 result = sourcing_name;
9264 if (result == NULL)
9265 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009266 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 return NULL;
9268 }
9269 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009270
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009271 case SPEC_SLNUM: /* line in file for ":so" command */
9272 if (sourcing_name == NULL || sourcing_lnum == 0)
9273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009274 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009275 return NULL;
9276 }
9277 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9278 result = strbuf;
9279 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009280
9281#ifdef FEAT_EVAL
9282 case SPEC_SFLNUM: /* line in script file */
9283 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009285 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009286 return NULL;
9287 }
9288 sprintf((char *)strbuf, "%ld",
9289 (long)(current_sctx.sc_lnum + sourcing_lnum));
9290 result = strbuf;
9291 break;
9292#endif
9293
9294#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009296 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9297 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 result = strbuf;
9299 break;
9300#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009301
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009302 default:
9303 result = (char_u *)""; /* avoid gcc warning */
9304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 }
9306
9307 resultlen = (int)STRLEN(result); /* length of new string */
9308 if (src[*usedlen] == '<') /* remove the file name extension */
9309 {
9310 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 resultlen = (int)(s - result);
9313 }
9314#ifdef FEAT_MODIFY_FNAME
9315 else if (!skip_mod)
9316 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009317 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 &resultlen);
9319 if (result == NULL)
9320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009321 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 return NULL;
9323 }
9324 }
9325#endif
9326 }
9327
9328 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9329 {
9330 if (valid != VALID_HEAD + VALID_PATH)
9331 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009332 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009334 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 result = NULL;
9336 }
9337 else
9338 result = vim_strnsave(result, resultlen);
9339 vim_free(resultbuf);
9340 return result;
9341}
9342
9343/*
9344 * Concatenate all files in the argument list, separated by spaces, and return
9345 * it in one allocated string.
9346 * Spaces and backslashes in the file names are escaped with a backslash.
9347 * Returns NULL when out of memory.
9348 */
9349 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009350arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351{
9352 int len;
9353 int idx;
9354 char_u *retval = NULL;
9355 char_u *p;
9356
9357 /*
9358 * Do this loop two times:
9359 * first time: compute the total length
9360 * second time: concatenate the names
9361 */
9362 for (;;)
9363 {
9364 len = 0;
9365 for (idx = 0; idx < ARGCOUNT; ++idx)
9366 {
9367 p = alist_name(&ARGLIST[idx]);
9368 if (p != NULL)
9369 {
9370 if (len > 0)
9371 {
9372 /* insert a space in between names */
9373 if (retval != NULL)
9374 retval[len] = ' ';
9375 ++len;
9376 }
9377 for ( ; *p != NUL; ++p)
9378 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009379 if (*p == ' '
9380#ifndef BACKSLASH_IN_FILENAME
9381 || *p == '\\'
9382#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009383 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 {
9385 /* insert a backslash */
9386 if (retval != NULL)
9387 retval[len] = '\\';
9388 ++len;
9389 }
9390 if (retval != NULL)
9391 retval[len] = *p;
9392 ++len;
9393 }
9394 }
9395 }
9396
9397 /* second time: break here */
9398 if (retval != NULL)
9399 {
9400 retval[len] = NUL;
9401 break;
9402 }
9403
9404 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009405 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 if (retval == NULL)
9407 break;
9408 }
9409
9410 return retval;
9411}
9412
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413/*
9414 * Expand the <sfile> string in "arg".
9415 *
9416 * Returns an allocated string, or NULL for any error.
9417 */
9418 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009419expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009421 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 int len;
9423 char_u *result;
9424 char_u *newres;
9425 char_u *repl;
9426 int srclen;
9427 char_u *p;
9428
9429 result = vim_strsave(arg);
9430 if (result == NULL)
9431 return NULL;
9432
9433 for (p = result; *p; )
9434 {
9435 if (STRNCMP(p, "<sfile>", 7) != 0)
9436 ++p;
9437 else
9438 {
9439 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009440 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 if (errormsg != NULL)
9442 {
9443 if (*errormsg)
9444 emsg(errormsg);
9445 vim_free(result);
9446 return NULL;
9447 }
9448 if (repl == NULL) /* no match (cannot happen) */
9449 {
9450 p += srclen;
9451 continue;
9452 }
9453 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9454 newres = alloc(len);
9455 if (newres == NULL)
9456 {
9457 vim_free(repl);
9458 vim_free(result);
9459 return NULL;
9460 }
9461 mch_memmove(newres, result, (size_t)(p - result));
9462 STRCPY(newres + (p - result), repl);
9463 len = (int)STRLEN(newres);
9464 STRCAT(newres, p + srclen);
9465 vim_free(repl);
9466 vim_free(result);
9467 result = newres;
9468 p = newres + len; /* continue after the match */
9469 }
9470 }
9471
9472 return result;
9473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009476/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009477 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009478 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009481dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 if (fname == NULL)
9484 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009485 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486}
9487#endif
9488
9489/*
9490 * ":behave {mswin,xterm}"
9491 */
9492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009493ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494{
9495 if (STRCMP(eap->arg, "mswin") == 0)
9496 {
9497 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9498 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9499 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9500 set_option_value((char_u *)"keymodel", 0L,
9501 (char_u *)"startsel,stopsel", 0);
9502 }
9503 else if (STRCMP(eap->arg, "xterm") == 0)
9504 {
9505 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9506 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9507 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9508 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9509 }
9510 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009511 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512}
9513
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009514#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9515/*
9516 * Function given to ExpandGeneric() to obtain the possible arguments of the
9517 * ":behave {mswin,xterm}" command.
9518 */
9519 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009520get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009521{
9522 if (idx == 0)
9523 return (char_u *)"mswin";
9524 if (idx == 1)
9525 return (char_u *)"xterm";
9526 return NULL;
9527}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02009528
9529/*
9530 * Function given to ExpandGeneric() to obtain the possible arguments of the
9531 * ":messages {clear}" command.
9532 */
9533 char_u *
9534get_messages_arg(expand_T *xp UNUSED, int idx)
9535{
9536 if (idx == 0)
9537 return (char_u *)"clear";
9538 return NULL;
9539}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009540#endif
9541
Bram Moolenaar113e1072019-01-20 15:30:40 +01009542#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009543 char_u *
9544get_mapclear_arg(expand_T *xp UNUSED, int idx)
9545{
9546 if (idx == 0)
9547 return (char_u *)"<buffer>";
9548 return NULL;
9549}
Bram Moolenaar113e1072019-01-20 15:30:40 +01009550#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009551
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552static int filetype_detect = FALSE;
9553static int filetype_plugin = FALSE;
9554static int filetype_indent = FALSE;
9555
9556/*
9557 * ":filetype [plugin] [indent] {on,off,detect}"
9558 * on: Load the filetype.vim file to install autocommands for file types.
9559 * off: Load the ftoff.vim file to remove all autocommands for file types.
9560 * plugin on: load filetype.vim and ftplugin.vim
9561 * plugin off: load ftplugof.vim
9562 * indent on: load filetype.vim and indent.vim
9563 * indent off: load indoff.vim
9564 */
9565 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009566ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
9568 char_u *arg = eap->arg;
9569 int plugin = FALSE;
9570 int indent = FALSE;
9571
9572 if (*eap->arg == NUL)
9573 {
9574 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009575 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576 filetype_detect ? "ON" : "OFF",
9577 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9578 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9579 return;
9580 }
9581
9582 /* Accept "plugin" and "indent" in any order. */
9583 for (;;)
9584 {
9585 if (STRNCMP(arg, "plugin", 6) == 0)
9586 {
9587 plugin = TRUE;
9588 arg = skipwhite(arg + 6);
9589 continue;
9590 }
9591 if (STRNCMP(arg, "indent", 6) == 0)
9592 {
9593 indent = TRUE;
9594 arg = skipwhite(arg + 6);
9595 continue;
9596 }
9597 break;
9598 }
9599 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9600 {
9601 if (*arg == 'o' || !filetype_detect)
9602 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009603 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 filetype_detect = TRUE;
9605 if (plugin)
9606 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009607 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608 filetype_plugin = TRUE;
9609 }
9610 if (indent)
9611 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009612 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 filetype_indent = TRUE;
9614 }
9615 }
9616 if (*arg == 'd')
9617 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009618 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009619 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 }
9621 }
9622 else if (STRCMP(arg, "off") == 0)
9623 {
9624 if (plugin || indent)
9625 {
9626 if (plugin)
9627 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009628 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 filetype_plugin = FALSE;
9630 }
9631 if (indent)
9632 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009633 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 filetype_indent = FALSE;
9635 }
9636 }
9637 else
9638 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009639 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640 filetype_detect = FALSE;
9641 }
9642 }
9643 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009644 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645}
9646
9647/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009648 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 */
9650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009651ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009654 {
9655 char_u *arg = eap->arg;
9656
9657 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9658 arg += 9;
9659
9660 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9661 if (arg != eap->arg)
9662 did_filetype = FALSE;
9663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009667ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
9669#ifdef FEAT_DIGRAPHS
9670 if (*eap->arg != NUL)
9671 putdigraph(eap->arg);
9672 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009673 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009675 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676#endif
9677}
9678
9679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009680ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681{
9682 int flags = 0;
9683
9684 if (eap->cmdidx == CMD_setlocal)
9685 flags = OPT_LOCAL;
9686 else if (eap->cmdidx == CMD_setglobal)
9687 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009688#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 if (cmdmod.browse && flags == 0)
9690 ex_options(eap);
9691 else
9692#endif
9693 (void)do_set(eap->arg, flags);
9694}
9695
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009696#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9697 void
9698set_no_hlsearch(int flag)
9699{
9700 no_hlsearch = flag;
9701# ifdef FEAT_EVAL
9702 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9703# endif
9704}
9705
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706/*
9707 * ":nohlsearch"
9708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009710ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009712 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009713 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715#endif
9716
9717#ifdef FEAT_CRYPT
9718/*
9719 * ":X": Get crypt key
9720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009722ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009724 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009725 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
9727#endif
9728
9729#ifdef FEAT_FOLDING
9730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009731ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
9733 if (foldManualAllowed(TRUE))
9734 foldCreate(eap->line1, eap->line2);
9735}
9736
9737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009738ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9741 eap->forceit, FALSE);
9742}
9743
9744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009745ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746{
9747 linenr_T lnum;
9748
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009749#ifdef FEAT_CLIPBOARD
9750 start_global_changes();
9751#endif
9752
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 /* First set the marks for all lines closed/open. */
9754 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9755 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9756 ml_setmarked(lnum);
9757
9758 /* Execute the command on the marked lines. */
9759 global_exe(eap->arg);
9760 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009761#ifdef FEAT_CLIPBOARD
9762 end_global_changes();
9763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764}
9765#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009766
Bram Moolenaar39665952018-08-15 20:59:48 +02009767#ifdef FEAT_QUICKFIX
9768/*
9769 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9770 * instead of a quickfix command.
9771 */
9772 int
9773is_loclist_cmd(int cmdidx)
9774{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009775 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009776 return FALSE;
9777 return cmdnames[cmdidx].cmd_name[0] == 'l';
9778}
9779#endif
9780
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009781# if defined(FEAT_TIMERS) || defined(PROTO)
9782 int
9783get_pressedreturn(void)
9784{
9785 return ex_pressedreturn;
9786}
9787
9788 void
9789set_pressedreturn(int val)
9790{
9791 ex_pressedreturn = val;
9792}
9793#endif