blob: 601c51bbab1e156f00e9dd70b83cebfdf3028d76 [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
Bram Moolenaar84538072019-07-28 14:15:42 +0200310# define ex_mkrc ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
312#ifndef FEAT_EVAL
313# define ex_compiler ex_ni
314#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200315#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_viminfo ex_ni
317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_filetype(exarg_T *eap);
320static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000322# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323# define ex_diffpatch ex_ni
324# define ex_diffgetput ex_ni
325# define ex_diffsplit ex_ni
326# define ex_diffthis ex_ni
327# define ex_diffupdate ex_ni
328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_digraphs(exarg_T *eap);
330static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100331#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332# define ex_options ex_ni
333#endif
334#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_nohlsearch ex_ni
338# define ex_match ex_ni
339#endif
340#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#else
343# define ex_X ex_ni
344#endif
345#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100346static void ex_fold(exarg_T *eap);
347static void ex_foldopen(exarg_T *eap);
348static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#else
350# define ex_fold ex_ni
351# define ex_foldopen ex_ni
352# define ex_folddo ex_ni
353#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100354#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355# define ex_language ex_ni
356#endif
357#ifndef FEAT_SIGNS
358# define ex_sign ex_ni
359#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200361# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000362# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200363# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365
366#ifndef FEAT_EVAL
367# define ex_debug ex_ni
368# define ex_breakadd ex_ni
369# define ex_debuggreedy ex_ni
370# define ex_breakdel ex_ni
371# define ex_breaklist ex_ni
372#endif
373
374#ifndef FEAT_CMDHIST
375# define ex_history ex_ni
376#endif
377#ifndef FEAT_JUMPLIST
378# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200379# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380# define ex_changes ex_ni
381#endif
382
Bram Moolenaar05159a02005-02-26 23:04:13 +0000383#ifndef FEAT_PROFILE
384# define ex_profile ex_ni
385#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200386#ifndef FEAT_TERMINAL
387# define ex_terminal ex_ni
388#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200389#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
390# define ex_xrestore ex_ni
391#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200392#if !defined(FEAT_TEXT_PROP)
393# define ex_popupclear ex_ni
394#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000395
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396/*
397 * Declare cmdnames[].
398 */
399#define DO_DECLARE_EXCMD
400#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200401#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403static char_u dollar_command[2] = {'$', 0};
404
405
406#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000407/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408typedef struct
409{
410 char_u *line; /* command line */
411 linenr_T lnum; /* sourcing_lnum of the line */
412} wcmd_T;
413
414/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000415 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000417 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000419struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420{
421 garray_T *lines_gap; /* growarray with line info */
422 int current_line; /* last read line from growarray */
423 int repeating; /* TRUE when looping a second time */
424 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200425 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 void *cookie;
427};
428
Bram Moolenaare96a2492019-06-25 04:12:16 +0200429static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100430static int store_loop_line(garray_T *gap, char_u *line);
431static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000432
433/* Struct to save a few things while debugging. Used in do_cmdline() only. */
434struct dbg_stuff
435{
436 int trylevel;
437 int force_abort;
438 except_T *caught_stack;
439 char_u *vv_exception;
440 char_u *vv_throwpoint;
441 int did_emsg;
442 int got_int;
443 int did_throw;
444 int need_rethrow;
445 int check_cstack;
446 except_T *current_exception;
447};
448
Bram Moolenaared203462004-06-16 11:19:22 +0000449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100450save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000451{
452 dsp->trylevel = trylevel; trylevel = 0;
453 dsp->force_abort = force_abort; force_abort = FALSE;
454 dsp->caught_stack = caught_stack; caught_stack = NULL;
455 dsp->vv_exception = v_exception(NULL);
456 dsp->vv_throwpoint = v_throwpoint(NULL);
457
458 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
459 dsp->did_emsg = did_emsg; did_emsg = FALSE;
460 dsp->got_int = got_int; got_int = FALSE;
461 dsp->did_throw = did_throw; did_throw = FALSE;
462 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
463 dsp->check_cstack = check_cstack; check_cstack = FALSE;
464 dsp->current_exception = current_exception; current_exception = NULL;
465}
466
467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100468restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000469{
470 suppress_errthrow = FALSE;
471 trylevel = dsp->trylevel;
472 force_abort = dsp->force_abort;
473 caught_stack = dsp->caught_stack;
474 (void)v_exception(dsp->vv_exception);
475 (void)v_throwpoint(dsp->vv_throwpoint);
476 did_emsg = dsp->did_emsg;
477 got_int = dsp->got_int;
478 did_throw = dsp->did_throw;
479 need_rethrow = dsp->need_rethrow;
480 check_cstack = dsp->check_cstack;
481 current_exception = dsp->current_exception;
482}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483#endif
484
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485/*
486 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
487 * command is given.
488 */
489 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100490do_exmode(
491 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492{
493 int save_msg_scroll;
494 int prev_msg_row;
495 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100496 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000497
498 if (improved)
499 exmode_active = EXMODE_VIM;
500 else
501 exmode_active = EXMODE_NORMAL;
502 State = NORMAL;
503
504 /* When using ":global /pat/ visual" and then "Q" we return to continue
505 * the :global command. */
506 if (global_busy)
507 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508
509 save_msg_scroll = msg_scroll;
510 ++RedrawingDisabled; /* don't redisplay the window */
511 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512#ifdef FEAT_GUI
513 /* Ignore scrollbar and mouse events in Ex mode */
514 ++hold_gui_events;
515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
Bram Moolenaar32526b32019-01-19 17:43:09 +0100517 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 while (exmode_active)
519 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000520 /* Check for a ":normal" command and no more characters left. */
521 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
522 {
523 exmode_active = FALSE;
524 break;
525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 msg_scroll = TRUE;
527 need_wait_return = FALSE;
528 ex_pressedreturn = FALSE;
529 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100530 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 prev_msg_row = msg_row;
532 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 if (improved)
534 {
535 cmdline_row = msg_row;
536 do_cmdline(NULL, getexline, NULL, 0);
537 }
538 else
539 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
540 lines_left = Rows - 1;
541
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100543 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 if (ex_pressedreturn)
550 {
551 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000552 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553 msg_row = prev_msg_row;
554 if (prev_msg_row == Rows - 1)
555 msg_row--;
556 }
557 msg_col = 0;
558 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
559 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000562 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
563 {
564 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100565 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000566 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100567 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
570
571#ifdef FEAT_GUI
572 --hold_gui_events;
573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 --RedrawingDisabled;
575 --no_wait_return;
576 update_screen(CLEAR);
577 need_wait_return = FALSE;
578 msg_scroll = save_msg_scroll;
579}
580
581/*
582 * Execute a simple command line. Used for translated commands like "*".
583 */
584 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100585do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 return do_cmdline(cmd, NULL, NULL,
588 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
589}
590
591/*
592 * do_cmdline(): execute one Ex command line
593 *
594 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100595 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 * 2. Split up in parts separated with '|'.
597 *
598 * This function can be called recursively!
599 *
600 * flags:
601 * DOCMD_VERBOSE - The command will be included in the error message.
602 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100603 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * DOCMD_KEYTYPED - Don't reset KeyTyped.
605 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
606 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
607 *
608 * return FAIL if cmdline could not be executed, OK otherwise
609 */
610 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100611do_cmdline(
612 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200613 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100614 void *cookie, /* argument for fgetline() */
615 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616{
617 char_u *next_cmdline; /* next cmd to execute */
618 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100619 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 static int recursive = 0; /* recursive depth */
621 int msg_didout_before_start = 0;
622 int count = 0; /* line number count */
623 int did_inc = FALSE; /* incremented RedrawingDisabled */
624 int retval = OK;
625#ifdef FEAT_EVAL
626 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000627 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 int current_line = 0; /* active line in lines_ga */
629 char_u *fname = NULL; /* function or script name */
630 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
631 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000632 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 int initial_trylevel;
634 struct msglist **saved_msg_list = NULL;
635 struct msglist *private_msg_list;
636
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100637 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200638 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000640 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000642 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100644# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645# define cmd_cookie cookie
646#endif
647 static int call_depth = 0; /* recursiveness */
648
649#ifdef FEAT_EVAL
650 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
651 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000652 * This ensures that the do_errthrow() call in do_one_cmd() does not
653 * combine the messages stored by an earlier invocation of do_one_cmd()
654 * with the command name of the later one. This would happen when
655 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 saved_msg_list = msg_list;
657 msg_list = &private_msg_list;
658 private_msg_list = NULL;
659#endif
660
661 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100662 * here. The value of 200 allows nested function calls, ":source", etc.
663 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100664 if (call_depth >= 200
665#ifdef FEAT_EVAL
666 && call_depth >= p_mfd
667#endif
668 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100670 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671#ifdef FEAT_EVAL
672 /* When converting to an exception, we do not include the command name
673 * since this is not an error of the specific command. */
674 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
675 msg_list = saved_msg_list;
676#endif
677 return FAIL;
678 }
679 ++call_depth;
680
681#ifdef FEAT_EVAL
682 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000683 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 cstack.cs_trylevel = 0;
685 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000686 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
688
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100689 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100692 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000693 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ++ex_nesting_level;
695
696 /* Get the function or script name and the address where the next breakpoint
697 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000698 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
700 fname = func_name(real_cookie);
701 breakpoint = func_breakpoint(real_cookie);
702 dbg_tick = func_dbg_tick(real_cookie);
703 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100704 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = sourcing_name;
707 breakpoint = source_breakpoint(real_cookie);
708 dbg_tick = source_dbg_tick(real_cookie);
709 }
710
711 /*
712 * Initialize "force_abort" and "suppress_errthrow" at the top level.
713 */
714 if (!recursive)
715 {
716 force_abort = FALSE;
717 suppress_errthrow = FALSE;
718 }
719
720 /*
721 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000722 * exception handling (used when debugging). Otherwise clear it to avoid
723 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000725 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000726 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000727 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200728 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730 initial_trylevel = trylevel;
731
732 /*
733 * "did_throw" will be set to TRUE when an exception is being thrown.
734 */
735 did_throw = FALSE;
736#endif
737 /*
738 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000739 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * If force_abort is set, we cancel everything.
741 */
742 did_emsg = FALSE;
743
744 /*
745 * KeyTyped is only set when calling vgetc(). Reset it here when not
746 * calling vgetc() (sourced command lines).
747 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100748 if (!(flags & DOCMD_KEYTYPED)
749 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 KeyTyped = FALSE;
751
752 /*
753 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * - for multiple commands on one line, separated with '|'
756 * - when repeating until there are no more lines (for ":source")
757 */
758 next_cmdline = cmdline;
759 do
760 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000761#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100762 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000763#endif
764
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000765 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (next_cmdline == NULL
767#ifdef FEAT_EVAL
768 && !force_abort
769 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772 )
773 did_emsg = FALSE;
774
775 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000776 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 * 3. If a line is given: Make a copy, so we can mess with it.
779 */
780
781#ifdef FEAT_EVAL
782 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000783 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {
785 /* Each '|' separated command is stored separately in lines_ga, to
786 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100787 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 /* Check if a function has returned or, unless it has an unclosed
790 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000793# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000794 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 func_line_end(real_cookie);
796# endif
797 if (func_has_ended(real_cookie))
798 {
799 retval = FAIL;
800 break;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000804 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100805 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806 script_line_end();
807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {
812 retval = FAIL;
813 break;
814 }
815
816 /* If breakpoints have been added/deleted need to check for it. */
817 if (breakpoint != NULL && dbg_tick != NULL
818 && *dbg_tick != debug_tick)
819 {
820 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 fname, sourcing_lnum);
823 *dbg_tick = debug_tick;
824 }
825
826 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
827 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
828
829 /* Did we encounter a breakpoint? */
830 if (breakpoint != NULL && *breakpoint != 0
831 && *breakpoint <= sourcing_lnum)
832 {
833 dbg_breakpoint(fname, sourcing_lnum);
834 /* Find next breakpoint. */
835 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100836 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 fname, sourcing_lnum);
838 *dbg_tick = debug_tick;
839 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000841 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842 {
843 if (getline_is_func)
844 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100845 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 script_line_start();
847 }
848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000851 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000853 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100854 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 * below, so that it stores lines in or reads them from
856 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 * while/for loop. */
858 cmd_getline = get_loop_line;
859 cmd_cookie = (void *)&cmd_loop_cookie;
860 cmd_loop_cookie.lines_gap = &lines_ga;
861 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100862 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000863 cmd_loop_cookie.cookie = cookie;
864 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 else
867 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 cmd_cookie = cookie;
870 }
871#endif
872
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL)
875 {
876 /*
877 * Need to set msg_didout for the first line after an ":if",
878 * otherwise the ":if" will be overwritten.
879 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100880 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#ifdef FEAT_EVAL
884 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
885#else
886 0
887#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200888 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
890 /* Don't call wait_return for aborted command line. The NULL
891 * returned for the end of a sourced file or executed function
892 * doesn't do this. */
893 if (KeyTyped && !(flags & DOCMD_REPEAT))
894 need_wait_return = FALSE;
895 retval = FAIL;
896 break;
897 }
898 used_getline = TRUE;
899
900 /*
901 * Keep the first typed line. Clear it when more lines are typed.
902 */
903 if (flags & DOCMD_KEEPLINE)
904 {
905 vim_free(repeat_cmdline);
906 if (count == 0)
907 repeat_cmdline = vim_strsave(next_cmdline);
908 else
909 repeat_cmdline = NULL;
910 }
911 }
912
913 /* 3. Make a copy of the command so we can mess with it. */
914 else if (cmdline_copy == NULL)
915 {
916 next_cmdline = vim_strsave(next_cmdline);
917 if (next_cmdline == NULL)
918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100919 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 retval = FAIL;
921 break;
922 }
923 }
924 cmdline_copy = next_cmdline;
925
926#ifdef FEAT_EVAL
927 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 * Save the current line when inside a ":while" or ":for", and when
929 * the command looks like a ":while" or ":for", because we may need it
930 * later. When there is a '|' and another command, it is stored
931 * separately, because we need to be able to jump back to it from an
932 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000934 if (current_line == lines_ga.ga_len
935 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
939 retval = FAIL;
940 break;
941 }
942 }
943 did_endif = FALSE;
944#endif
945
946 if (count++ == 0)
947 {
948 /*
949 * All output from the commands is put below each other, without
950 * waiting for a return. Don't do this when executing commands
951 * from a script or when being called recursive (e.g. for ":e
952 * +command file").
953 */
954 if (!(flags & DOCMD_NOWAIT) && !recursive)
955 {
956 msg_didout_before_start = msg_didout;
957 msg_didany = FALSE; /* no output yet */
958 msg_start();
959 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200960 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 ++RedrawingDisabled;
962 did_inc = TRUE;
963 }
964 }
965
966 if (p_verbose >= 15 && sourcing_name != NULL)
967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000969 verbose_enter_scroll();
970
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100971 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000973 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100974 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000975
976 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 --no_wait_return;
978 }
979
980 /*
981 * 2. Execute one '|' separated command.
982 * do_one_cmd() will return NULL if there is no trailing '|'.
983 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
984 */
985 ++recursive;
986 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
987#ifdef FEAT_EVAL
988 &cstack,
989#endif
990 cmd_getline, cmd_cookie);
991 --recursive;
992
993#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 if (cmd_cookie == (void *)&cmd_loop_cookie)
995 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#endif
999
1000 if (next_cmdline == NULL)
1001 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001002 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#ifdef FEAT_CMDHIST
1004 /*
1005 * If the command was typed, remember it for the ':' register.
1006 * Do this AFTER executing the command to make :@: work.
1007 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001008 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 && new_last_cmdline != NULL)
1010 {
1011 vim_free(last_cmdline);
1012 last_cmdline = new_last_cmdline;
1013 new_last_cmdline = NULL;
1014 }
1015#endif
1016 }
1017 else
1018 {
1019 /* need to copy the command after the '|' to cmdline_copy, for the
1020 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001021 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 next_cmdline = cmdline_copy;
1023 }
1024
1025
1026#ifdef FEAT_EVAL
1027 /* reset did_emsg for a function that is not aborted by an error */
1028 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001029 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 && !func_has_abort(real_cookie))
1031 did_emsg = FALSE;
1032
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001033 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 {
1035 ++current_line;
1036
1037 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 * An ":endwhile", ":endfor" and ":continue" is handled here.
1039 * If we were executing commands, jump back to the ":while" or
1040 * ":for".
1041 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 /* Jump back to the matching ":while" or ":for". Be careful
1048 * not to use a cs_line[] from an entry that isn't a ":while"
1049 * or ":for": It would make "current_line" invalid and can
1050 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 if (!did_emsg && !got_int && !did_throw
1052 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 && (cstack.cs_flags[cstack.cs_idx]
1054 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 && cstack.cs_line[cstack.cs_idx] >= 0
1056 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1057 {
1058 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 /* remember we jumped there */
1060 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 line_breakcheck(); /* check if CTRL-C typed */
1062
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 /* Check for the next breakpoint at or after the ":while"
1064 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (breakpoint != NULL)
1066 {
1067 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001068 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 fname,
1070 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1071 *dbg_tick = debug_tick;
1072 }
1073 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001078 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1079 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
1081 }
1082
1083 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001086 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001088 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1090 }
1091 }
1092
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001093 /* Check for the next breakpoint after a watchexpression */
1094 if (breakpoint != NULL && has_watchexpr())
1095 {
1096 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1097 *dbg_tick = debug_tick;
1098 }
1099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 /*
1101 * When not inside any ":while" loop, clear remembered lines.
1102 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 {
1105 if (lines_ga.ga_len > 0)
1106 {
1107 sourcing_lnum =
1108 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1109 free_cmdlines(&lines_ga);
1110 }
1111 current_line = 0;
1112 }
1113
1114 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1116 * being restored at the ":endtry". Reset them here and set the
1117 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1118 * This includes the case where a missing ":endif", ":endwhile" or
1119 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 cstack.cs_lflags &= ~CSL_HAD_FINA;
1124 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1125 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 did_throw ? (void *)current_exception : NULL);
1127 did_emsg = got_int = did_throw = FALSE;
1128 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1129 }
1130
1131 /* Update global "trylevel" for recursive calls to do_cmdline() from
1132 * within this loop. */
1133 trylevel = initial_trylevel + cstack.cs_trylevel;
1134
1135 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001136 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 * files) is aborted because of an error, an interrupt, or an uncaught
1138 * exception, cancel everything. If it is left normally, reset
1139 * force_abort to get the non-EH compatible abortion behavior for
1140 * the rest of the script.
1141 */
1142 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1143 force_abort = FALSE;
1144
1145 /* Convert an interrupt to an exception if appropriate. */
1146 (void)do_intthrow(&cstack);
1147#endif /* FEAT_EVAL */
1148
1149 }
1150 /*
1151 * Continue executing command lines when:
1152 * - no CTRL-C typed, no aborting error, no exception thrown or try
1153 * conditionals need to be checked for executing finally clauses or
1154 * catching an interrupt exception
1155 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001156 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 * looping for ":source" command or function call.
1158 */
1159 while (!((got_int
1160#ifdef FEAT_EVAL
1161 || (did_emsg && force_abort) || did_throw
1162#endif
1163 )
1164#ifdef FEAT_EVAL
1165 && cstack.cs_trylevel == 0
1166#endif
1167 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001168 && !(did_emsg
1169#ifdef FEAT_EVAL
1170 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001171 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001172 * the :endtry to be missed. */
1173 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1174#endif
1175 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001176 && (getline_equal(fgetline, cookie, getexmodeline)
1177 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 && (next_cmdline != NULL
1179#ifdef FEAT_EVAL
1180 || cstack.cs_idx >= 0
1181#endif
1182 || (flags & DOCMD_REPEAT)));
1183
1184 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001185 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186#ifdef FEAT_EVAL
1187 free_cmdlines(&lines_ga);
1188 ga_clear(&lines_ga);
1189
1190 if (cstack.cs_idx >= 0)
1191 {
1192 /*
1193 * If a sourced file or executed function ran to its end, report the
1194 * unclosed conditional.
1195 */
1196 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001197 && ((getline_equal(fgetline, cookie, getsourceline)
1198 && !source_finished(fgetline, cookie))
1199 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 && !func_has_ended(real_cookie))))
1201 {
1202 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001207 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001209 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 }
1211
1212 /*
1213 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1214 * ":endtry" in a sourced file or executed function. If the try
1215 * conditional is in its finally clause, ignore anything pending.
1216 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001217 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 */
1219 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001220 {
1221 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1222
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001223 if (idx >= 0)
1224 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001225 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1226 &cstack.cs_looplevel);
1227 }
1228 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 trylevel = initial_trylevel;
1230 }
1231
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001232 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1233 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001235 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 ? (char_u *)"endfunction" : (char_u *)NULL);
1237
1238 if (trylevel == 0)
1239 {
1240 /*
1241 * When an exception is being thrown out of the outermost try
1242 * conditional, discard the uncaught exception, disable the conversion
1243 * of interrupts or errors to exceptions, and ensure that no more
1244 * commands are executed.
1245 */
1246 if (did_throw)
1247 {
1248 void *p = NULL;
1249 char_u *saved_sourcing_name;
1250 int saved_sourcing_lnum;
1251 struct msglist *messages = NULL, *next;
1252
1253 /*
1254 * If the uncaught exception is a user exception, report it as an
1255 * error. If it is an error exception, display the saved error
1256 * message now. For an interrupt exception, do nothing; the
1257 * interrupt message is given elsewhere.
1258 */
1259 switch (current_exception->type)
1260 {
1261 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001262 vim_snprintf((char *)IObuff, IOSIZE,
1263 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 current_exception->value);
1265 p = vim_strsave(IObuff);
1266 break;
1267 case ET_ERROR:
1268 messages = current_exception->messages;
1269 current_exception->messages = NULL;
1270 break;
1271 case ET_INTERRUPT:
1272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274
1275 saved_sourcing_name = sourcing_name;
1276 saved_sourcing_lnum = sourcing_lnum;
1277 sourcing_name = current_exception->throw_name;
1278 sourcing_lnum = current_exception->throw_lnum;
1279 current_exception->throw_name = NULL;
1280
1281 discard_current_exception(); /* uses IObuff if 'verbose' */
1282 suppress_errthrow = TRUE;
1283 force_abort = TRUE;
1284
1285 if (messages != NULL)
1286 {
1287 do
1288 {
1289 next = messages->next;
1290 emsg(messages->msg);
1291 vim_free(messages->msg);
1292 vim_free(messages);
1293 messages = next;
1294 }
1295 while (messages != NULL);
1296 }
1297 else if (p != NULL)
1298 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001299 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 vim_free(p);
1301 }
1302 vim_free(sourcing_name);
1303 sourcing_name = saved_sourcing_name;
1304 sourcing_lnum = saved_sourcing_lnum;
1305 }
1306
1307 /*
1308 * On an interrupt or an aborting error not converted to an exception,
1309 * disable the conversion of errors to exceptions. (Interrupts are not
1310 * converted any more, here.) This enables also the interrupt message
1311 * when force_abort is set and did_emsg unset in case of an interrupt
1312 * from a finally clause after an error.
1313 */
1314 else if (got_int || (did_emsg && force_abort))
1315 suppress_errthrow = TRUE;
1316 }
1317
1318 /*
1319 * The current cstack will be freed when do_cmdline() returns. An uncaught
1320 * exception will have to be rethrown in the previous cstack. If a function
1321 * has just returned or a script file was just finished and the previous
1322 * cstack belongs to the same function or, respectively, script file, it
1323 * will have to be checked for finally clauses to be executed due to the
1324 * ":return" or ":finish". This is done in do_one_cmd().
1325 */
1326 if (did_throw)
1327 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 && ex_nesting_level > func_level(real_cookie) + 1))
1332 {
1333 if (!did_throw)
1334 check_cstack = TRUE;
1335 }
1336 else
1337 {
1338 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 --ex_nesting_level;
1341 /*
1342 * Go to debug mode when returning from a function in which we are
1343 * single-stepping.
1344 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001345 if ((getline_equal(fgetline, cookie, getsourceline)
1346 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 ? (char_u *)_("End of sourced file")
1350 : (char_u *)_("End of function"));
1351 }
1352
1353 /*
1354 * Restore the exception environment (done after returning from the
1355 * debugger).
1356 */
1357 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001358 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360 msg_list = saved_msg_list;
1361#endif /* FEAT_EVAL */
1362
1363 /*
1364 * If there was too much output to fit on the command line, ask the user to
1365 * hit return before redrawing the screen. With the ":global" command we do
1366 * this only once after the command is finished.
1367 */
1368 if (did_inc)
1369 {
1370 --RedrawingDisabled;
1371 --no_wait_return;
1372 msg_scroll = FALSE;
1373
1374 /*
1375 * When just finished an ":if"-":else" which was typed, no need to
1376 * wait for hit-return. Also for an error situation.
1377 */
1378 if (retval == FAIL
1379#ifdef FEAT_EVAL
1380 || (did_endif && KeyTyped && !did_emsg)
1381#endif
1382 )
1383 {
1384 need_wait_return = FALSE;
1385 msg_didany = FALSE; /* don't wait when restarting edit */
1386 }
1387 else if (need_wait_return)
1388 {
1389 /*
1390 * The msg_start() above clears msg_didout. The wait_return we do
1391 * here should not overwrite the command that may be shown before
1392 * doing that.
1393 */
1394 msg_didout |= msg_didout_before_start;
1395 wait_return(FALSE);
1396 }
1397 }
1398
Bram Moolenaar2a942252012-11-28 23:03:07 +01001399#ifdef FEAT_EVAL
1400 did_endif = FALSE; /* in case do_cmdline used recursively */
1401#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 /*
1403 * Reset if_level, in case a sourced script file contains more ":if" than
1404 * ":endif" (could be ":if x | foo | endif").
1405 */
1406 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001407#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 --call_depth;
1410 return retval;
1411}
1412
1413#ifdef FEAT_EVAL
1414/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001415 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 */
1417 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001418get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001420 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 wcmd_T *wp;
1422 char_u *line;
1423
1424 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1425 {
1426 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001427 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001429 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001431 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001433 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001434 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 ++cp->current_line;
1436
1437 return line;
1438 }
1439
1440 KeyTyped = FALSE;
1441 ++cp->current_line;
1442 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1443 sourcing_lnum = wp->lnum;
1444 return vim_strsave(wp->line);
1445}
1446
1447/*
1448 * Store a line in "gap" so that a ":while" loop can execute it again.
1449 */
1450 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001451store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 if (ga_grow(gap, 1) == FAIL)
1454 return FAIL;
1455 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1456 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1457 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 return OK;
1459}
1460
1461/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001462 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
1464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001465free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 while (gap->ga_len > 0)
1468 {
1469 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1470 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 }
1472}
1473#endif
1474
1475/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001476 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1477 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001480getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001481 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001482 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001483 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001486 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001487 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Bram Moolenaar89d40322006-08-29 15:30:07 +00001489 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001490 * function that's originally used to obtain the lines. This may be
1491 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001492 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001493 cp = (struct loop_cookie *)cookie;
1494 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 gp = cp->getline;
1497 cp = cp->cookie;
1498 }
1499 return gp == func;
1500#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001501 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502#endif
1503}
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * getline function. Otherwise return "cookie".
1508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001510getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001511 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513{
Bram Moolenaar13505972019-01-24 15:04:48 +01001514#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001515 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001516 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517
Bram Moolenaar89d40322006-08-29 15:30:07 +00001518 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001519 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001521 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001522 cp = (struct loop_cookie *)cookie;
1523 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 {
1525 gp = cp->getline;
1526 cp = cp->cookie;
1527 }
1528 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001529#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001532}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001534
1535/*
1536 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1537 * ":bwipeout", etc.
1538 * Returns the buffer number.
1539 */
1540 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001541compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001542{
1543 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001545 int count = offset;
1546
1547 buf = firstbuf;
1548 while (buf->b_next != NULL && buf->b_fnum < lnum)
1549 buf = buf->b_next;
1550 while (count != 0)
1551 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001552 count += (offset < 0) ? 1 : -1;
1553 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1554 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001555 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001556 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001557 if (addr_type == ADDR_LOADED_BUFFERS)
1558 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001559 while (buf->b_ml.ml_mfp == NULL)
1560 {
1561 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1562 if (nextbuf == NULL)
1563 break;
1564 buf = nextbuf;
1565 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001566 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001567 /* we might have gone too far, last buffer is not loadedd */
1568 if (addr_type == ADDR_LOADED_BUFFERS)
1569 while (buf->b_ml.ml_mfp == NULL)
1570 {
1571 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1572 if (nextbuf == NULL)
1573 break;
1574 buf = nextbuf;
1575 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001576 return buf->b_fnum;
1577}
1578
Bram Moolenaarb7316892019-05-01 18:08:42 +02001579/*
1580 * Return the window number of "win".
1581 * When "win" is NULL return the number of windows.
1582 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001584current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585{
1586 win_T *wp;
1587 int nr = 0;
1588
Bram Moolenaar29323592016-07-24 22:04:11 +02001589 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001590 {
1591 ++nr;
1592 if (wp == win)
1593 break;
1594 }
1595 return nr;
1596}
1597
1598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001599current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001600{
1601 tabpage_T *tp;
1602 int nr = 0;
1603
Bram Moolenaar29323592016-07-24 22:04:11 +02001604 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001605 {
1606 ++nr;
1607 if (tp == tab)
1608 break;
1609 }
1610 return nr;
1611}
1612
1613# define CURRENT_WIN_NR current_win_nr(curwin)
1614# define LAST_WIN_NR current_win_nr(NULL)
1615# define CURRENT_TAB_NR current_tab_nr(curtab)
1616# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618/*
1619 * Execute one Ex command.
1620 *
1621 * If 'sourcing' is TRUE, the command will be included in the error message.
1622 *
1623 * 1. skip comment lines and leading space
1624 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001625 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001627 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 * 6. parse arguments
1629 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001631 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *
1633 * This function may be called recursively!
1634 */
1635#if (_MSC_VER == 1200)
1636/*
Bram Moolenaared203462004-06-16 11:19:22 +00001637 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001639 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
1641 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001642do_one_cmd(
1643 char_u **cmdlinep,
1644 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001646 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001648 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001649 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
1651 char_u *p;
1652 linenr_T lnum;
1653 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001654 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001655 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001657 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001659 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001661 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 vim_memset(&ea, 0, sizeof(ea));
1664 ea.line1 = 1;
1665 ea.line2 = 1;
1666#ifdef FEAT_EVAL
1667 ++ex_nesting_level;
1668#endif
1669
Bram Moolenaar21691f82012-12-05 19:13:18 +01001670 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (quitmore
1672#ifdef FEAT_EVAL
1673 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001674 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001675#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001676 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001677 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 --quitmore;
1679
1680 /*
1681 * Reset browse, confirm, etc.. They are restored when returning, for
1682 * recursive calls.
1683 */
1684 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001686 /* "#!anything" is handled like a comment. */
1687 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1688 goto doend;
1689
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001690/*
1691 * 1. Skip comment lines and leading white space and colons.
1692 * 2. Handle command modifiers.
1693 */
1694 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001696 ea.cmdlinep = cmdlinep;
1697 ea.getline = fgetline;
1698 ea.cookie = cookie;
1699#ifdef FEAT_EVAL
1700 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001702 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001703 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001705 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
1707#ifdef FEAT_EVAL
1708 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1709 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1710#else
1711 ea.skip = (if_level > 0);
1712#endif
1713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001715 * 3. Skip over the range to find the command. Let "p" point to after it.
1716 *
1717 * We need the command to know what kind of range it uses.
1718 */
1719 cmd = ea.cmd;
1720 ea.cmd = skip_range(ea.cmd, NULL);
1721 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1722 ea.cmd = skipwhite(ea.cmd + 1);
1723 p = find_command(&ea, NULL);
1724
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001725#ifdef FEAT_EVAL
1726# ifdef FEAT_PROFILE
1727 // Count this line for profiling if skip is TRUE.
1728 if (do_profiling == PROF_YES
1729 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1730 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1731 {
1732 int skip = did_emsg || got_int || did_throw;
1733
1734 if (ea.cmdidx == CMD_catch)
1735 skip = !skip && !(cstack->cs_idx >= 0
1736 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1737 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1738 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1739 skip = skip || !(cstack->cs_idx >= 0
1740 && !(cstack->cs_flags[cstack->cs_idx]
1741 & (CSF_ACTIVE | CSF_TRUE)));
1742 else if (ea.cmdidx == CMD_finally)
1743 skip = FALSE;
1744 else if (ea.cmdidx != CMD_endif
1745 && ea.cmdidx != CMD_endfor
1746 && ea.cmdidx != CMD_endtry
1747 && ea.cmdidx != CMD_endwhile)
1748 skip = ea.skip;
1749
1750 if (!skip)
1751 {
1752 if (getline_equal(fgetline, cookie, get_func_line))
1753 func_line_exec(getline_cookie(fgetline, cookie));
1754 else if (getline_equal(fgetline, cookie, getsourceline))
1755 script_line_exec();
1756 }
1757 }
1758# endif
1759
1760 /* May go to debug mode. If this happens and the ">quit" debug command is
1761 * used, throw an interrupt exception and skip the next command. */
1762 dbg_check_breakpoint(&ea);
1763 if (!ea.skip && got_int)
1764 {
1765 ea.skip = TRUE;
1766 (void)do_intthrow(cstack);
1767 }
1768#endif
1769
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001770/*
1771 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 *
1773 * where 'addr' is:
1774 *
1775 * % (entire file)
1776 * $ [+-NUM]
1777 * 'x [+-NUM] (where x denotes a currently defined mark)
1778 * . [+-NUM]
1779 * [+-NUM]..
1780 * NUM
1781 *
1782 * The ea.cmd pointer is updated to point to the first character following the
1783 * range spec. If an initial address is found, but no second, the upper bound
1784 * is equal to the lower.
1785 */
1786
Bram Moolenaar25190db2019-05-04 15:05:28 +02001787 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001788 if (!IS_USER_CMDIDX(ea.cmdidx))
1789 {
1790 if (ea.cmdidx != CMD_SIZE)
1791 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1792 else
1793 ea.addr_type = ADDR_LINES;
1794
Bram Moolenaar25190db2019-05-04 15:05:28 +02001795 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001796 if (ea.cmdidx == CMD_wincmd && p != NULL)
1797 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001798#ifdef FEAT_QUICKFIX
1799 // :.cc in quickfix window uses line number
1800 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1801 ea.addr_type = ADDR_OTHER;
1802#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001803 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001804
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001805 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001806 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001807 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001810 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
1812
1813 /*
1814 * Skip ':' and any white space
1815 */
1816 ea.cmd = skipwhite(ea.cmd);
1817 while (*ea.cmd == ':')
1818 ea.cmd = skipwhite(ea.cmd + 1);
1819
1820 /*
1821 * If we got a line, but no command, then go to the line.
1822 * If we find a '|' or '\n' we set ea.nextcmd.
1823 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001824 if (*ea.cmd == NUL || *ea.cmd == '"'
1825 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
1827 /*
1828 * strange vi behaviour:
1829 * ":3" jumps to line 3
1830 * ":3|..." prints line 3
1831 * ":|" prints current line
1832 */
1833 if (ea.skip) /* skip this if inside :if */
1834 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001835 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001838 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 if ((errormsg = invalid_range(&ea)) == NULL)
1840 {
1841 correct_range(&ea);
1842 ex_print(&ea);
1843 }
1844 }
1845 else if (ea.addr_count != 0)
1846 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001847 if (ea.line2 > curbuf->b_ml.ml_line_count)
1848 {
1849 /* With '-' in 'cpoptions' a line number past the file is an
1850 * error, otherwise put it at the end of the file. */
1851 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1852 ea.line2 = -1;
1853 else
1854 ea.line2 = curbuf->b_ml.ml_line_count;
1855 }
1856
1857 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001858 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 else
1860 {
1861 if (ea.line2 == 0)
1862 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 else
1864 curwin->w_cursor.lnum = ea.line2;
1865 beginline(BL_SOL | BL_FIX);
1866 }
1867 }
1868 goto doend;
1869 }
1870
Bram Moolenaard5005162014-08-22 23:05:54 +02001871 /* If this looks like an undefined user command and there are CmdUndefined
1872 * autocommands defined, trigger the matching autocommands. */
1873 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1874 && ASCII_ISUPPER(*ea.cmd)
1875 && has_cmdundefined())
1876 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001877 int ret;
1878
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001879 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001880 while (ASCII_ISALNUM(*p))
1881 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001882 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001883 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1884 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001885 /* If the autocommands did something and didn't cause an error, try
1886 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001887 p = (ret
1888#ifdef FEAT_EVAL
1889 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001890#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001891 ) ? find_command(&ea, NULL) : ea.cmd;
1892 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (p == NULL)
1895 {
1896 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001897 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 goto doend;
1899 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001900 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001901 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1902 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 {
1904 errormsg = uc_fun_cmd();
1905 goto doend;
1906 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 if (ea.cmdidx == CMD_SIZE)
1909 {
1910 if (!ea.skip)
1911 {
1912 STRCPY(IObuff, _("E492: Not an editor command"));
1913 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001914 {
1915 /* If the modifier was parsed OK the error must be in the
1916 * following command */
1917 if (after_modifier != NULL)
1918 append_command(after_modifier);
1919 else
1920 append_command(*cmdlinep);
1921 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001922 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001923 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 goto doend;
1926 }
1927
Bram Moolenaar958636c2014-10-21 20:01:58 +02001928 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1929 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001930#ifdef HAVE_EX_SCRIPT_NI
1931 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1932#endif
1933 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
1935#ifndef FEAT_EVAL
1936 /*
1937 * When the expression evaluation is disabled, recognize the ":if" and
1938 * ":endif" commands and ignore everything in between it.
1939 */
1940 if (ea.cmdidx == CMD_if)
1941 ++if_level;
1942 if (if_level)
1943 {
1944 if (ea.cmdidx == CMD_endif)
1945 --if_level;
1946 goto doend;
1947 }
1948
1949#endif
1950
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001951 /* forced commands */
1952 if (*p == '!' && ea.cmdidx != CMD_substitute
1953 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 ++p;
1956 ea.forceit = TRUE;
1957 }
1958 else
1959 ea.forceit = FALSE;
1960
1961/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001962 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001964 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001965 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
1967 if (!ea.skip)
1968 {
1969#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001970 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001972 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001973 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 goto doend;
1975 }
1976#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001977 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001978 {
1979 errormsg = _("E981: Command not allowed in rvim");
1980 goto doend;
1981 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001982 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001985 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 goto doend;
1987 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001988
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001989 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001990 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001992 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001993 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 goto doend;
1995 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001996
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001997 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001998 * Do allow ":checktime" (it is postponed).
1999 * Do allow ":edit" (check for an argument later).
2000 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002001 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002002 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002003 && ea.cmdidx != CMD_edit
2004 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002005 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002006 && curbuf_locked())
2007 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002009 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
2011 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002012 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 goto doend;
2014 }
2015 }
2016
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002017 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002019 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 goto doend;
2021 }
2022
2023 /*
2024 * Don't complain about the range if it is not used
2025 * (could happen if line_count is accidentally set to 0).
2026 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002027 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
2029 /*
2030 * If the range is backwards, ask for confirmation and, if given, swap
2031 * ea.line1 & ea.line2 so it's forwards again.
2032 * When global command is busy, don't ask, will fail below.
2033 */
2034 if (!global_busy && ea.line1 > ea.line2)
2035 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002036 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002038 if (sourcing || exmode_active)
2039 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002040 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002041 goto doend;
2042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 if (ask_yesno((char_u *)
2044 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002045 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 }
2047 lnum = ea.line1;
2048 ea.line1 = ea.line2;
2049 ea.line2 = lnum;
2050 }
2051 if ((errormsg = invalid_range(&ea)) != NULL)
2052 goto doend;
2053 }
2054
Bram Moolenaarb7316892019-05-01 18:08:42 +02002055 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2056 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 ea.line2 = 1;
2058
2059 correct_range(&ea);
2060
2061#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002062 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002063 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 /* Put the first line at the start of a closed fold, put the last line
2066 * at the end of a closed fold. */
2067 (void)hasFolding(ea.line1, &ea.line1, NULL);
2068 (void)hasFolding(ea.line2, NULL, &ea.line2);
2069 }
2070#endif
2071
2072#ifdef FEAT_QUICKFIX
2073 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002074 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 * option here, so things like % get expanded.
2076 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002077 p = replace_makeprg(&ea, p, cmdlinep);
2078 if (p == NULL)
2079 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080#endif
2081
2082 /*
2083 * Skip to start of argument.
2084 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2085 */
2086 if (ea.cmdidx == CMD_bang)
2087 ea.arg = p;
2088 else
2089 ea.arg = skipwhite(p);
2090
Bram Moolenaar379fb762018-08-30 15:58:28 +02002091 // ":file" cannot be run with an argument when "curbuf_lock" is set
2092 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2093 goto doend;
2094
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 /*
2096 * Check for "++opt=val" argument.
2097 * Must be first, allow ":w ++enc=utf8 !cmd"
2098 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002099 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2101 if (getargopt(&ea) == FAIL && !ni)
2102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 goto doend;
2105 }
2106
2107 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2108 {
2109 if (*ea.arg == '>') /* append */
2110 {
2111 if (*++ea.arg != '>') /* typed wrong */
2112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002113 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 goto doend;
2115 }
2116 ea.arg = skipwhite(ea.arg + 1);
2117 ea.append = TRUE;
2118 }
2119 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2120 {
2121 ++ea.arg;
2122 ea.usefilter = TRUE;
2123 }
2124 }
2125
2126 if (ea.cmdidx == CMD_read)
2127 {
2128 if (ea.forceit)
2129 {
2130 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2131 ea.forceit = FALSE;
2132 }
2133 else if (*ea.arg == '!') /* :r !filter */
2134 {
2135 ++ea.arg;
2136 ea.usefilter = TRUE;
2137 }
2138 }
2139
2140 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2141 {
2142 ea.amount = 1;
2143 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2144 {
2145 ++ea.arg;
2146 ++ea.amount;
2147 }
2148 ea.arg = skipwhite(ea.arg);
2149 }
2150
2151 /*
2152 * Check for "+command" argument, before checking for next command.
2153 * Don't do this for ":read !cmd" and ":write !cmd".
2154 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002155 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2157
2158 /*
2159 * Check for '|' to separate commands and '"' to start comments.
2160 * Don't do this for ":read !cmd" and ":write !cmd".
2161 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002162 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 separate_nextcmd(&ea);
2164
2165 /*
2166 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002167 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2168 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002170 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002171 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002172 || ea.cmdidx == CMD_global
2173 || ea.cmdidx == CMD_vglobal
2174 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
2176 for (p = ea.arg; *p; ++p)
2177 {
2178 /* Remove one backslash before a newline, so that it's possible to
2179 * pass a newline to the shell and also a newline that is preceded
2180 * with a backslash. This makes it impossible to end a shell
2181 * command in a backslash, but that doesn't appear useful.
2182 * Halving the number of backslashes is incompatible with previous
2183 * versions. */
2184 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002185 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 else if (*p == '\n')
2187 {
2188 ea.nextcmd = p + 1;
2189 *p = NUL;
2190 break;
2191 }
2192 }
2193 }
2194
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002195 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002197 buf_T *buf;
2198
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002200 switch (ea.addr_type)
2201 {
2202 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002203 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002204 ea.line2 = curbuf->b_ml.ml_line_count;
2205 break;
2206 case ADDR_LOADED_BUFFERS:
2207 buf = firstbuf;
2208 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2209 buf = buf->b_next;
2210 ea.line1 = buf->b_fnum;
2211 buf = lastbuf;
2212 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2213 buf = buf->b_prev;
2214 ea.line2 = buf->b_fnum;
2215 break;
2216 case ADDR_BUFFERS:
2217 ea.line1 = firstbuf->b_fnum;
2218 ea.line2 = lastbuf->b_fnum;
2219 break;
2220 case ADDR_WINDOWS:
2221 ea.line2 = LAST_WIN_NR;
2222 break;
2223 case ADDR_TABS:
2224 ea.line2 = LAST_TAB_NR;
2225 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002226 case ADDR_TABS_RELATIVE:
2227 ea.line2 = 1;
2228 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002229 case ADDR_ARGUMENTS:
2230 if (ARGCOUNT == 0)
2231 ea.line1 = ea.line2 = 0;
2232 else
2233 ea.line2 = ARGCOUNT;
2234 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002235 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002236#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002237 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002238 if (ea.line2 == 0)
2239 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002240#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002241 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002242 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002243 case ADDR_UNSIGNED:
2244 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002245 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002246 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249
2250 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002251 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002253 /* Do not allow register = for user commands */
2254 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002255 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002257#ifndef FEAT_CLIPBOARD
2258 /* check these explicitly for a more specific error message */
2259 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002261 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002262 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002265 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2266 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002267 {
2268 ea.regname = *ea.arg++;
2269#ifdef FEAT_EVAL
2270 /* for '=' register: accept the rest of the line as an expression */
2271 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2272 {
2273 set_expr_line(vim_strsave(ea.arg));
2274 ea.arg += STRLEN(ea.arg);
2275 }
2276#endif
2277 ea.arg = skipwhite(ea.arg);
2278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
2281 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002282 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 * count, it's a buffer name.
2284 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002285 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2286 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002287 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 n = getdigits(&ea.arg);
2290 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002291 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002293 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 goto doend;
2295 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002296 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 ea.line2 = n;
2299 if (ea.addr_count == 0)
2300 ea.addr_count = 1;
2301 }
2302 else
2303 {
2304 ea.line1 = ea.line2;
2305 ea.line2 += n - 1;
2306 ++ea.addr_count;
2307 /*
2308 * Be vi compatible: no error message for out of range.
2309 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002310 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 ea.line2 = curbuf->b_ml.ml_line_count;
2312 }
2313 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002314
2315 /*
2316 * Check for flags: 'l', 'p' and '#'.
2317 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002318 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002319 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2321 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002323 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002324 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 goto doend;
2326 }
2327
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002328 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002330 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 goto doend;
2332 }
2333
2334#ifdef FEAT_EVAL
2335 /*
2336 * Skip the command when it's not going to be executed.
2337 * The commands like :if, :endif, etc. always need to be executed.
2338 * Also make an exception for commands that handle a trailing command
2339 * themselves.
2340 */
2341 if (ea.skip)
2342 {
2343 switch (ea.cmdidx)
2344 {
2345 /* commands that need evaluation */
2346 case CMD_while:
2347 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002348 case CMD_for:
2349 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 case CMD_if:
2351 case CMD_elseif:
2352 case CMD_else:
2353 case CMD_endif:
2354 case CMD_try:
2355 case CMD_catch:
2356 case CMD_finally:
2357 case CMD_endtry:
2358 case CMD_function:
2359 break;
2360
2361 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 * the list at ":help :bar". */
2364 case CMD_aboveleft:
2365 case CMD_and:
2366 case CMD_belowright:
2367 case CMD_botright:
2368 case CMD_browse:
2369 case CMD_call:
2370 case CMD_confirm:
2371 case CMD_delfunction:
2372 case CMD_djump:
2373 case CMD_dlist:
2374 case CMD_dsearch:
2375 case CMD_dsplit:
2376 case CMD_echo:
2377 case CMD_echoerr:
2378 case CMD_echomsg:
2379 case CMD_echon:
2380 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002381 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_help:
2383 case CMD_hide:
2384 case CMD_ijump:
2385 case CMD_ilist:
2386 case CMD_isearch:
2387 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002388 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 case CMD_keepjumps:
2390 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002391 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 case CMD_leftabove:
2393 case CMD_let:
2394 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002395 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002397 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002398 case CMD_noautocmd:
2399 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_perl:
2401 case CMD_psearch:
2402 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002403 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002404 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_return:
2406 case CMD_rightbelow:
2407 case CMD_ruby:
2408 case CMD_silent:
2409 case CMD_smagic:
2410 case CMD_snomagic:
2411 case CMD_substitute:
2412 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002413 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_tcl:
2415 case CMD_throw:
2416 case CMD_tilde:
2417 case CMD_topleft:
2418 case CMD_unlet:
2419 case CMD_verbose:
2420 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002421 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 break;
2423
2424 default: goto doend;
2425 }
2426 }
2427#endif
2428
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002429 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
2431 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2432 goto doend;
2433 }
2434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 /*
2436 * Accept buffer name. Cannot be used at the same time with a buffer
2437 * number. Don't do this for a user command.
2438 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002439 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002440 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 {
2442 /*
2443 * :bdelete, :bwipeout and :bunload take several arguments, separated
2444 * by spaces: find next space (skipping over escaped characters).
2445 * The others take one argument: ignore trailing spaces.
2446 */
2447 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2448 || ea.cmdidx == CMD_bunload)
2449 p = skiptowhite_esc(ea.arg);
2450 else
2451 {
2452 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002453 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 --p;
2455 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002456 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002457 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (ea.line2 < 0) /* failed */
2459 goto doend;
2460 ea.addr_count = 1;
2461 ea.arg = skipwhite(p);
2462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaar2be57332018-02-13 18:05:18 +01002464 /* The :try command saves the emsg_silent flag, reset it here when
2465 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002466 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002467 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002468 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002469 if (emsg_silent < 0)
2470 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002471 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002472 }
2473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002475 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
Bram Moolenaar958636c2014-10-21 20:01:58 +02002478 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 /*
2481 * Execute a user-defined command.
2482 */
2483 do_ucmd(&ea);
2484 }
2485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002488 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 */
2490 ea.errmsg = NULL;
2491 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2492 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002493 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496#ifdef FEAT_EVAL
2497 /*
2498 * If the command just executed called do_cmdline(), any throw or ":return"
2499 * or ":finish" encountered there must also check the cstack of the still
2500 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2501 * exception, or reanimate a returned function or finished script file and
2502 * return or finish it again.
2503 */
2504 if (need_rethrow)
2505 do_throw(cstack);
2506 else if (check_cstack)
2507 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002508 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002510 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 && current_func_returned())
2512 do_return(&ea, TRUE, FALSE, NULL);
2513 }
2514 need_rethrow = check_cstack = FALSE;
2515#endif
2516
2517doend:
2518 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002521 curwin->w_cursor.col = 0;
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
2524 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2525 {
2526 if (sourcing)
2527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002528 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002531 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002533 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
2535 emsg(errormsg);
2536 }
2537#ifdef FEAT_EVAL
2538 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002539 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2540 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541#endif
2542
Bram Moolenaareffed932018-08-14 13:38:17 +02002543 if (ea.verbose_save >= 0)
2544 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002545
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002546 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002548 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
Bram Moolenaareffed932018-08-14 13:38:17 +02002550 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
2552 /* messages could be enabled for a serious error, need to check if the
2553 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002554 if (!did_emsg || msg_silent > ea.save_msg_silent)
2555 msg_silent = ea.save_msg_silent;
2556 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 if (emsg_silent < 0)
2558 emsg_silent = 0;
2559 /* Restore msg_scroll, it's set by file I/O commands, even when no
2560 * message is actually displayed. */
2561 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002562
2563 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2564 * somewhere in the line. Put it back in the first column. */
2565 if (redirecting())
2566 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002569#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002570 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002571 --sandbox;
2572#endif
2573
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2575 ea.nextcmd = NULL;
2576
2577#ifdef FEAT_EVAL
2578 --ex_nesting_level;
2579#endif
2580
2581 return ea.nextcmd;
2582}
2583#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002584 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585#endif
2586
2587/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002588 * Parse and skip over command modifiers:
2589 * - update eap->cmd
2590 * - store flags in "cmdmod".
2591 * - Set ex_pressedreturn for an empty command line.
2592 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002593 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002594 * - set p_verbose for ":verbose"
2595 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002596 * When "skip_only" is TRUE the global variables are not changed, except for
2597 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002598 * Return FAIL when the command is not to be executed.
2599 * May set "errormsg" to an error message.
2600 */
2601 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002602parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002603{
2604 char_u *p;
2605
2606 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2607 eap->verbose_save = -1;
2608 eap->save_msg_silent = -1;
2609
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002610 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002611 for (;;)
2612 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002613 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2614 ++eap->cmd;
2615
2616 /* in ex mode, an empty line works like :+ */
2617 if (*eap->cmd == NUL && exmode_active
2618 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2619 || getline_equal(eap->getline, eap->cookie, getexline))
2620 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2621 {
2622 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002623 if (!skip_only)
2624 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002625 }
2626
2627 /* ignore comment and empty lines */
2628 if (*eap->cmd == '"')
2629 return FAIL;
2630 if (*eap->cmd == NUL)
2631 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002632 if (!skip_only)
2633 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002634 return FAIL;
2635 }
2636
Bram Moolenaareffed932018-08-14 13:38:17 +02002637 p = skip_range(eap->cmd, NULL);
2638 switch (*p)
2639 {
2640 /* When adding an entry, also modify cmd_exists(). */
2641 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2642 break;
2643 cmdmod.split |= WSP_ABOVE;
2644 continue;
2645
2646 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2647 {
2648 cmdmod.split |= WSP_BELOW;
2649 continue;
2650 }
2651 if (checkforcmd(&eap->cmd, "browse", 3))
2652 {
2653#ifdef FEAT_BROWSE_CMD
2654 cmdmod.browse = TRUE;
2655#endif
2656 continue;
2657 }
2658 if (!checkforcmd(&eap->cmd, "botright", 2))
2659 break;
2660 cmdmod.split |= WSP_BOT;
2661 continue;
2662
2663 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2664 break;
2665#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2666 cmdmod.confirm = TRUE;
2667#endif
2668 continue;
2669
2670 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2671 {
2672 cmdmod.keepmarks = TRUE;
2673 continue;
2674 }
2675 if (checkforcmd(&eap->cmd, "keepalt", 5))
2676 {
2677 cmdmod.keepalt = TRUE;
2678 continue;
2679 }
2680 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2681 {
2682 cmdmod.keeppatterns = TRUE;
2683 continue;
2684 }
2685 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2686 break;
2687 cmdmod.keepjumps = TRUE;
2688 continue;
2689
2690 case 'f': /* only accept ":filter {pat} cmd" */
2691 {
2692 char_u *reg_pat;
2693
2694 if (!checkforcmd(&p, "filter", 4)
2695 || *p == NUL || ends_excmd(*p))
2696 break;
2697 if (*p == '!')
2698 {
2699 cmdmod.filter_force = TRUE;
2700 p = skipwhite(p + 1);
2701 if (*p == NUL || ends_excmd(*p))
2702 break;
2703 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002704 if (skip_only)
2705 p = skip_vimgrep_pat(p, NULL, NULL);
2706 else
2707 // NOTE: This puts a NUL after the pattern.
2708 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002709 if (p == NULL || *p == NUL)
2710 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002711 if (!skip_only)
2712 {
2713 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002714 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002715 if (cmdmod.filter_regmatch.regprog == NULL)
2716 break;
2717 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002718 eap->cmd = p;
2719 continue;
2720 }
2721
2722 /* ":hide" and ":hide | cmd" are not modifiers */
2723 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2724 || *p == NUL || ends_excmd(*p))
2725 break;
2726 eap->cmd = p;
2727 cmdmod.hide = TRUE;
2728 continue;
2729
2730 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2731 {
2732 cmdmod.lockmarks = TRUE;
2733 continue;
2734 }
2735
2736 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2737 break;
2738 cmdmod.split |= WSP_ABOVE;
2739 continue;
2740
2741 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2742 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002743 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002744 {
2745 /* Set 'eventignore' to "all". Restore the
2746 * existing option value later. */
2747 cmdmod.save_ei = vim_strsave(p_ei);
2748 set_string_option_direct((char_u *)"ei", -1,
2749 (char_u *)"all", OPT_FREE, SID_NONE);
2750 }
2751 continue;
2752 }
2753 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2754 break;
2755 cmdmod.noswapfile = TRUE;
2756 continue;
2757
2758 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2759 break;
2760 cmdmod.split |= WSP_BELOW;
2761 continue;
2762
2763 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2764 {
2765#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002766 if (!skip_only)
2767 {
2768 if (!eap->did_sandbox)
2769 ++sandbox;
2770 eap->did_sandbox = TRUE;
2771 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002772#endif
2773 continue;
2774 }
2775 if (!checkforcmd(&eap->cmd, "silent", 3))
2776 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002777 if (!skip_only)
2778 {
2779 if (eap->save_msg_silent == -1)
2780 eap->save_msg_silent = msg_silent;
2781 ++msg_silent;
2782 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2784 {
2785 /* ":silent!", but not "silent !cmd" */
2786 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002787 if (!skip_only)
2788 {
2789 ++emsg_silent;
2790 ++eap->did_esilent;
2791 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 }
2793 continue;
2794
2795 case 't': if (checkforcmd(&p, "tab", 3))
2796 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002797 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002799 long tabnr = get_address(eap, &eap->cmd,
2800 ADDR_TABS, eap->skip,
2801 skip_only, FALSE, 1);
2802 if (tabnr == MAXLNUM)
2803 cmdmod.tab = tabpage_index(curtab) + 1;
2804 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002806 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2807 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002808 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002809 return FAIL;
2810 }
2811 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002812 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002813 }
2814 eap->cmd = p;
2815 continue;
2816 }
2817 if (!checkforcmd(&eap->cmd, "topleft", 2))
2818 break;
2819 cmdmod.split |= WSP_TOP;
2820 continue;
2821
2822 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2823 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002824 if (!skip_only)
2825 {
2826 if (eap->save_msg_silent == -1)
2827 eap->save_msg_silent = msg_silent;
2828 msg_silent = 0;
2829 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002830 continue;
2831
2832 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2833 {
2834 cmdmod.split |= WSP_VERT;
2835 continue;
2836 }
2837 if (!checkforcmd(&p, "verbose", 4))
2838 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002839 if (!skip_only)
2840 {
2841 if (eap->verbose_save < 0)
2842 eap->verbose_save = p_verbose;
2843 if (vim_isdigit(*eap->cmd))
2844 p_verbose = atoi((char *)eap->cmd);
2845 else
2846 p_verbose = 1;
2847 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002848 eap->cmd = p;
2849 continue;
2850 }
2851 break;
2852 }
2853
2854 return OK;
2855}
2856
2857/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002858 * Free contents of "cmdmod".
2859 */
2860 static void
2861free_cmdmod(void)
2862{
2863 if (cmdmod.save_ei != NULL)
2864 {
2865 /* Restore 'eventignore' to the value before ":noautocmd". */
2866 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2867 OPT_FREE, SID_NONE);
2868 free_string_option(cmdmod.save_ei);
2869 }
2870
2871 if (cmdmod.filter_regmatch.regprog != NULL)
2872 vim_regfree(cmdmod.filter_regmatch.regprog);
2873}
2874
2875/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002876 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002877 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002878 * Return FAIL and set "errormsg" or return OK.
2879 */
2880 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002881parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002882{
2883 int address_count = 1;
2884 linenr_T lnum;
2885
2886 // Repeat for all ',' or ';' separated addresses.
2887 for (;;)
2888 {
2889 eap->line1 = eap->line2;
2890 switch (eap->addr_type)
2891 {
2892 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002893 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002894 // default is current line number
2895 eap->line2 = curwin->w_cursor.lnum;
2896 break;
2897 case ADDR_WINDOWS:
2898 eap->line2 = CURRENT_WIN_NR;
2899 break;
2900 case ADDR_ARGUMENTS:
2901 eap->line2 = curwin->w_arg_idx + 1;
2902 if (eap->line2 > ARGCOUNT)
2903 eap->line2 = ARGCOUNT;
2904 break;
2905 case ADDR_LOADED_BUFFERS:
2906 case ADDR_BUFFERS:
2907 eap->line2 = curbuf->b_fnum;
2908 break;
2909 case ADDR_TABS:
2910 eap->line2 = CURRENT_TAB_NR;
2911 break;
2912 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002913 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002914 eap->line2 = 1;
2915 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002916 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002917#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002918 eap->line2 = qf_get_cur_idx(eap);
2919#endif
2920 break;
2921 case ADDR_QUICKFIX_VALID:
2922#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002923 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002924#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002925 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002926 case ADDR_NONE:
2927 // Will give an error later if a range is found.
2928 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002929 }
2930 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002931 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002932 eap->addr_count == 0, address_count++);
2933 if (eap->cmd == NULL) // error detected
2934 return FAIL;
2935 if (lnum == MAXLNUM)
2936 {
2937 if (*eap->cmd == '%') // '%' - all lines
2938 {
2939 ++eap->cmd;
2940 switch (eap->addr_type)
2941 {
2942 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002943 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002944 eap->line1 = 1;
2945 eap->line2 = curbuf->b_ml.ml_line_count;
2946 break;
2947 case ADDR_LOADED_BUFFERS:
2948 {
2949 buf_T *buf = firstbuf;
2950
2951 while (buf->b_next != NULL
2952 && buf->b_ml.ml_mfp == NULL)
2953 buf = buf->b_next;
2954 eap->line1 = buf->b_fnum;
2955 buf = lastbuf;
2956 while (buf->b_prev != NULL
2957 && buf->b_ml.ml_mfp == NULL)
2958 buf = buf->b_prev;
2959 eap->line2 = buf->b_fnum;
2960 break;
2961 }
2962 case ADDR_BUFFERS:
2963 eap->line1 = firstbuf->b_fnum;
2964 eap->line2 = lastbuf->b_fnum;
2965 break;
2966 case ADDR_WINDOWS:
2967 case ADDR_TABS:
2968 if (IS_USER_CMDIDX(eap->cmdidx))
2969 {
2970 eap->line1 = 1;
2971 eap->line2 = eap->addr_type == ADDR_WINDOWS
2972 ? LAST_WIN_NR : LAST_TAB_NR;
2973 }
2974 else
2975 {
2976 // there is no Vim command which uses '%' and
2977 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002978 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002979 return FAIL;
2980 }
2981 break;
2982 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002983 case ADDR_UNSIGNED:
2984 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002985 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002986 return FAIL;
2987 case ADDR_ARGUMENTS:
2988 if (ARGCOUNT == 0)
2989 eap->line1 = eap->line2 = 0;
2990 else
2991 {
2992 eap->line1 = 1;
2993 eap->line2 = ARGCOUNT;
2994 }
2995 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002996 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002997#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002998 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002999 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003000 if (eap->line2 == 0)
3001 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003002#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003003 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003004 case ADDR_NONE:
3005 // Will give an error later if a range is found.
3006 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 }
3008 ++eap->addr_count;
3009 }
3010 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3011 {
3012 pos_T *fp;
3013
3014 // '*' - visual area
3015 if (eap->addr_type != ADDR_LINES)
3016 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003017 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003018 return FAIL;
3019 }
3020
3021 ++eap->cmd;
3022 if (!eap->skip)
3023 {
3024 fp = getmark('<', FALSE);
3025 if (check_mark(fp) == FAIL)
3026 return FAIL;
3027 eap->line1 = fp->lnum;
3028 fp = getmark('>', FALSE);
3029 if (check_mark(fp) == FAIL)
3030 return FAIL;
3031 eap->line2 = fp->lnum;
3032 ++eap->addr_count;
3033 }
3034 }
3035 }
3036 else
3037 eap->line2 = lnum;
3038 eap->addr_count++;
3039
3040 if (*eap->cmd == ';')
3041 {
3042 if (!eap->skip)
3043 {
3044 curwin->w_cursor.lnum = eap->line2;
3045 // don't leave the cursor on an illegal line or column
3046 check_cursor();
3047 }
3048 }
3049 else if (*eap->cmd != ',')
3050 break;
3051 ++eap->cmd;
3052 }
3053
3054 // One address given: set start and end lines.
3055 if (eap->addr_count == 1)
3056 {
3057 eap->line1 = eap->line2;
3058 // ... but only implicit: really no address given
3059 if (lnum == MAXLNUM)
3060 eap->addr_count = 0;
3061 }
3062 return OK;
3063}
3064
3065/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003066 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 * If there is a match advance "pp" to the argument and return TRUE.
3068 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003069 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003070checkforcmd(
3071 char_u **pp, /* start of command */
3072 char *cmd, /* name of command */
3073 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
3075 int i;
3076
3077 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003078 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 break;
3080 if (i >= len && !isalpha((*pp)[i]))
3081 {
3082 *pp = skipwhite(*pp + i);
3083 return TRUE;
3084 }
3085 return FALSE;
3086}
3087
3088/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003089 * Append "cmd" to the error message in IObuff.
3090 * Takes care of limiting the length and handling 0xa0, which would be
3091 * invisible otherwise.
3092 */
3093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003094append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003095{
3096 char_u *s = cmd;
3097 char_u *d;
3098
3099 STRCAT(IObuff, ": ");
3100 d = IObuff + STRLEN(IObuff);
3101 while (*s != NUL && d - IObuff < IOSIZE - 7)
3102 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003103 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003104 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003105 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003106 STRCPY(d, "<a0>");
3107 d += 4;
3108 }
3109 else
3110 MB_COPY_CHAR(s, d);
3111 }
3112 *d = NUL;
3113}
3114
3115/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003117 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003118 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003119 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 * Returns NULL for an ambiguous user command.
3121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003123find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124{
3125 int len;
3126 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003127 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
3129 /*
3130 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003131 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 * - the 'k' command can directly be followed by any character.
3133 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003134 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003136 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 */
3138 p = eap->cmd;
3139 if (*p == 'k')
3140 {
3141 eap->cmdidx = CMD_k;
3142 ++p;
3143 }
3144 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003145 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3146 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 || p[1] == 'g'
3148 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3149 || p[1] == 'I'
3150 || (p[1] == 'r' && p[2] != 'e')))
3151 {
3152 eap->cmdidx = CMD_substitute;
3153 ++p;
3154 }
3155 else
3156 {
3157 while (ASCII_ISALPHA(*p))
3158 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003159 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003160 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003161 while (ASCII_ISALNUM(*p))
3162 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003163
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 /* check for non-alpha command */
3165 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3166 ++p;
3167 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003168 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3169 {
3170 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3171 * :delete with the 'l' flag. Same for 'p'. */
3172 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003173 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003174 break;
3175 if (i == len - 1)
3176 {
3177 --len;
3178 if (p[-1] == 'l')
3179 eap->flags |= EXFLAG_LIST;
3180 else
3181 eap->flags |= EXFLAG_PRINT;
3182 }
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003185 if (ASCII_ISLOWER(eap->cmd[0]))
3186 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003187 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003188 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003189
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003190 if (command_count != (int)CMD_SIZE)
3191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003192 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003193 getout(1);
3194 }
3195
3196 /* Use a precomputed index for fast look-up in cmdnames[]
3197 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003198 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3199 if (ASCII_ISLOWER(c2))
3200 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003203 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3206 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3207 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3208 (size_t)len) == 0)
3209 {
3210#ifdef FEAT_EVAL
3211 if (full != NULL
3212 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3213 *full = TRUE;
3214#endif
3215 break;
3216 }
3217
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003218 // Look for a user defined command as a last resort. Let ":Print" be
3219 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003220 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3221 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003223 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 while (ASCII_ISALNUM(*p))
3225 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003226 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003228 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 eap->cmdidx = CMD_SIZE;
3230 }
3231
3232 return p;
3233}
3234
3235#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003236static struct cmdmod
3237{
3238 char *name;
3239 int minlen;
3240 int has_count; /* :123verbose :3tab */
3241} cmdmods[] = {
3242 {"aboveleft", 3, FALSE},
3243 {"belowright", 3, FALSE},
3244 {"botright", 2, FALSE},
3245 {"browse", 3, FALSE},
3246 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003247 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003248 {"hide", 3, FALSE},
3249 {"keepalt", 5, FALSE},
3250 {"keepjumps", 5, FALSE},
3251 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003252 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003253 {"leftabove", 5, FALSE},
3254 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003255 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003256 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003257 {"rightbelow", 6, FALSE},
3258 {"sandbox", 3, FALSE},
3259 {"silent", 3, FALSE},
3260 {"tab", 3, TRUE},
3261 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003262 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003263 {"verbose", 4, TRUE},
3264 {"vertical", 4, FALSE},
3265};
3266
3267/*
3268 * Return length of a command modifier (including optional count).
3269 * Return zero when it's not a modifier.
3270 */
3271 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003272modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003273{
3274 int i, j;
3275 char_u *p = cmd;
3276
3277 if (VIM_ISDIGIT(*cmd))
3278 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003279 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003280 {
3281 for (j = 0; p[j] != NUL; ++j)
3282 if (p[j] != cmdmods[i].name[j])
3283 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003284 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003285 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003286 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003287 }
3288 return 0;
3289}
3290
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291/*
3292 * Return > 0 if an Ex command "name" exists.
3293 * Return 2 if there is an exact match.
3294 * Return 3 if there is an ambiguous match.
3295 */
3296 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003297cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 exarg_T ea;
3300 int full = FALSE;
3301 int i;
3302 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003303 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003306 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 {
3308 for (j = 0; name[j] != NUL; ++j)
3309 if (name[j] != cmdmods[i].name[j])
3310 break;
3311 if (name[j] == NUL && j >= cmdmods[i].minlen)
3312 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3313 }
3314
Bram Moolenaara9587612006-05-04 21:47:50 +00003315 /* Check built-in commands and user defined commands.
3316 * For ":2match" and ":3match" we need to skip the number. */
3317 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003319 p = find_command(&ea, &full);
3320 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003322 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3323 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003324 if (*skipwhite(p) != NUL)
3325 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3327}
3328#endif
3329
3330/*
3331 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3332 * we don't need/want deleted. Maybe this could be done better if we didn't
3333 * repeat all this stuff. The only problem is that they may not stay
3334 * perfectly compatible with each other, but then the command line syntax
3335 * probably won't change that much -- webb.
3336 */
3337 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003338set_one_cmd_context(
3339 expand_T *xp,
3340 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
3342 char_u *p;
3343 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003344 int len = 0;
3345 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003347 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 int delim;
3349#endif
3350 int forceit = FALSE;
3351 int usefilter = FALSE; /* filter instead of file name */
3352
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003353 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 xp->xp_pattern = buff;
3355 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003356 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
3358/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003359 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 */
3361 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3362 ;
3363 xp->xp_pattern = cmd;
3364
3365 if (*cmd == NUL)
3366 return NULL;
3367 if (*cmd == '"') /* ignore comment lines */
3368 {
3369 xp->xp_context = EXPAND_NOTHING;
3370 return NULL;
3371 }
3372
3373/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003374 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 */
3376 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 xp->xp_pattern = cmd;
3378 if (*cmd == NUL)
3379 return NULL;
3380 if (*cmd == '"')
3381 {
3382 xp->xp_context = EXPAND_NOTHING;
3383 return NULL;
3384 }
3385
3386 if (*cmd == '|' || *cmd == '\n')
3387 return cmd + 1; /* There's another command */
3388
3389 /*
3390 * Isolate the command and search for it in the command table.
3391 * Exceptions:
3392 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003393 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3395 */
3396 if (*cmd == 'k' && cmd[1] != 'e')
3397 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003398 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 p = cmd + 1;
3400 }
3401 else
3402 {
3403 p = cmd;
3404 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3405 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003406 /* a user command may contain digits */
3407 if (ASCII_ISUPPER(cmd[0]))
3408 while (ASCII_ISALNUM(*p) || *p == '*')
3409 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003410 /* for python 3.x: ":py3*" commands completion */
3411 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003412 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003413 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003414 while (ASCII_ISALPHA(*p) || *p == '*')
3415 ++p;
3416 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003417 /* check for non-alpha command */
3418 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3419 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003422 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
3424 xp->xp_context = EXPAND_UNSUCCESSFUL;
3425 return NULL;
3426 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003428 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3429 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3430 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 break;
3432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003434 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
3437
3438 /*
3439 * If the cursor is touching the command, and it ends in an alpha-numeric
3440 * character, complete the command name.
3441 */
3442 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3443 return NULL;
3444
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003445 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
3447 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3448 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003449 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 p = cmd + 1;
3451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3453 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003454 ea.cmd = cmd;
3455 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003456#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003457 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003458#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003459 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003460#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003461 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003462 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003463 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
3468 /* Not still touching the command and it was an illegal one */
3469 xp->xp_context = EXPAND_UNSUCCESSFUL;
3470 return NULL;
3471 }
3472
3473 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3474
3475 if (*p == '!') /* forced commands */
3476 {
3477 forceit = TRUE;
3478 ++p;
3479 }
3480
3481/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003482 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003484 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003485 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
3487 arg = skipwhite(p);
3488
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003489 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
3491 if (*arg == '>') /* append */
3492 {
3493 if (*++arg == '>')
3494 ++arg;
3495 arg = skipwhite(arg);
3496 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 ++arg;
3500 usefilter = TRUE;
3501 }
3502 }
3503
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 usefilter = forceit; /* :r! filter if forced */
3507 if (*arg == '!') /* :r !filter */
3508 {
3509 ++arg;
3510 usefilter = TRUE;
3511 }
3512 }
3513
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003514 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 {
3516 while (*arg == *cmd) /* allow any number of '>' or '<' */
3517 ++arg;
3518 arg = skipwhite(arg);
3519 }
3520
3521 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003522 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 /* Check if we're in the +command */
3525 p = arg + 1;
3526 arg = skip_cmd_arg(arg, FALSE);
3527
3528 /* Still touching the command after '+'? */
3529 if (*arg == NUL)
3530 return p;
3531
3532 /* Skip space(s) after +command to get to the real argument */
3533 arg = skipwhite(arg);
3534 }
3535
3536 /*
3537 * Check for '|' to separate commands and '"' to start comments.
3538 * Don't do this for ":read !cmd" and ":write !cmd".
3539 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003540 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 p = arg;
3543 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003544 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 p += 2;
3546 while (*p)
3547 {
3548 if (*p == Ctrl_V)
3549 {
3550 if (p[1] != NUL)
3551 ++p;
3552 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003553 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 || *p == '|' || *p == '\n')
3555 {
3556 if (*(p - 1) != '\\')
3557 {
3558 if (*p == '|' || *p == '\n')
3559 return p + 1;
3560 return NULL; /* It's a comment */
3561 }
3562 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003563 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 }
3566
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003567 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3568 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3569 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 return NULL;
3571
3572 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003573 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003575 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003576 while (*p && p < buff + len)
3577 {
3578 if (*p == ' ' || *p == TAB)
3579 {
3580 /* argument starts after a space */
3581 xp->xp_pattern = ++p;
3582 }
3583 else
3584 {
3585 if (*p == '\\' && *(p + 1) != NUL)
3586 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003587 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003588 }
3589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003591 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003593 int c;
3594 int in_quote = FALSE;
3595 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597 /*
3598 * Allow spaces within back-quotes to count as part of the argument
3599 * being expanded.
3600 */
3601 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003602 p = xp->xp_pattern;
3603 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003605 if (has_mbyte)
3606 c = mb_ptr2char(p);
3607 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003608 c = *p;
3609 if (c == '\\' && p[1] != NUL)
3610 ++p;
3611 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
3613 if (!in_quote)
3614 {
3615 xp->xp_pattern = p;
3616 bow = p + 1;
3617 }
3618 in_quote = !in_quote;
3619 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003620 /* An argument can contain just about everything, except
3621 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003622 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003623#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003624 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003625#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003626 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003627 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003628 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003629 while (*p != NUL)
3630 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003631 if (has_mbyte)
3632 c = mb_ptr2char(p);
3633 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003634 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003635 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003636 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003637 if (has_mbyte)
3638 len = (*mb_ptr2len)(p);
3639 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003640 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003641 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003642 }
3643 if (in_quote)
3644 bow = p;
3645 else
3646 xp->xp_pattern = p;
3647 p -= len;
3648 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003649 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 }
3651
3652 /*
3653 * If we are still inside the quotes, and we passed a space, just
3654 * expand from there.
3655 */
3656 if (bow != NULL && in_quote)
3657 xp->xp_pattern = bow;
3658 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003659
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003660 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003661 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003662 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003663#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003664 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003665#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003666 /* When still after the command name expand executables. */
3667 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003668 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
3671 /* Check for environment variable */
3672 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003673#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 || *xp->xp_pattern == '%'
3675#endif
3676 )
3677 {
3678 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3679 if (!vim_isIDc(*p))
3680 break;
3681 if (*p == NUL)
3682 {
3683 xp->xp_context = EXPAND_ENV_VARS;
3684 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003685#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003686 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003687 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003688 compl = EXPAND_ENV_VARS;
3689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 }
3691 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003692#if defined(FEAT_CMDL_COMPL)
3693 /* Check for user names */
3694 if (*xp->xp_pattern == '~')
3695 {
3696 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3697 ;
3698 /* Complete ~user only if it partially matches a user name.
3699 * A full match ~user<Tab> will be replaced by user's home
3700 * directory i.e. something like ~user<Tab> -> /home/user/ */
3701 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003702 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003703 {
3704 xp->xp_context = EXPAND_USER;
3705 ++xp->xp_pattern;
3706 }
3707 }
3708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 }
3710
3711/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003712 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003714 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003716 case CMD_find:
3717 case CMD_sfind:
3718 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003719 if (xp->xp_context == EXPAND_FILES)
3720 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003721 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 case CMD_cd:
3723 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003724 case CMD_tcd:
3725 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 case CMD_lcd:
3727 case CMD_lchdir:
3728 if (xp->xp_context == EXPAND_FILES)
3729 xp->xp_context = EXPAND_DIRECTORIES;
3730 break;
3731 case CMD_help:
3732 xp->xp_context = EXPAND_HELP;
3733 xp->xp_pattern = arg;
3734 break;
3735
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003736 /* Command modifiers: return the argument.
3737 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003739 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 case CMD_belowright:
3741 case CMD_botright:
3742 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003743 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003744 case CMD_cdo:
3745 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003747 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 case CMD_folddoclosed:
3749 case CMD_folddoopen:
3750 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003751 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 case CMD_keepjumps:
3753 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003754 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003755 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003757 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003759 case CMD_noautocmd:
3760 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003762 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003764 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003765 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 case CMD_topleft:
3767 case CMD_verbose:
3768 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003769 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 return arg;
3771
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003772 case CMD_filter:
3773 if (*arg != NUL)
3774 arg = skip_vimgrep_pat(arg, NULL, NULL);
3775 if (arg == NULL || *arg == NUL)
3776 {
3777 xp->xp_context = EXPAND_NOTHING;
3778 return NULL;
3779 }
3780 return skipwhite(arg);
3781
Bram Moolenaar4f688582007-07-24 12:34:30 +00003782#ifdef FEAT_CMDL_COMPL
3783# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 case CMD_match:
3785 if (*arg == NUL || !ends_excmd(*arg))
3786 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003787 /* also complete "None" */
3788 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 arg = skipwhite(skiptowhite(arg));
3790 if (*arg != NUL)
3791 {
3792 xp->xp_context = EXPAND_NOTHING;
3793 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3794 }
3795 }
3796 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799/*
3800 * All completion for the +cmdline_compl feature goes here.
3801 */
3802
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003804 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805
3806 case CMD_delcommand:
3807 xp->xp_context = EXPAND_USER_COMMANDS;
3808 xp->xp_pattern = arg;
3809 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811 case CMD_global:
3812 case CMD_vglobal:
3813 delim = *arg; /* get the delimiter */
3814 if (delim)
3815 ++arg; /* skip delimiter if there is one */
3816
3817 while (arg[0] != NUL && arg[0] != delim)
3818 {
3819 if (arg[0] == '\\' && arg[1] != NUL)
3820 ++arg;
3821 ++arg;
3822 }
3823 if (arg[0] != NUL)
3824 return arg + 1;
3825 break;
3826 case CMD_and:
3827 case CMD_substitute:
3828 delim = *arg;
3829 if (delim)
3830 {
3831 /* skip "from" part */
3832 ++arg;
3833 arg = skip_regexp(arg, delim, p_magic, NULL);
3834 }
3835 /* skip "to" part */
3836 while (arg[0] != NUL && arg[0] != delim)
3837 {
3838 if (arg[0] == '\\' && arg[1] != NUL)
3839 ++arg;
3840 ++arg;
3841 }
3842 if (arg[0] != NUL) /* skip delimiter */
3843 ++arg;
3844 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3845 ++arg;
3846 if (arg[0] != NUL)
3847 return arg;
3848 break;
3849 case CMD_isearch:
3850 case CMD_dsearch:
3851 case CMD_ilist:
3852 case CMD_dlist:
3853 case CMD_ijump:
3854 case CMD_psearch:
3855 case CMD_djump:
3856 case CMD_isplit:
3857 case CMD_dsplit:
3858 arg = skipwhite(skipdigits(arg)); /* skip count */
3859 if (*arg == '/') /* Match regexp, not just whole words */
3860 {
3861 for (++arg; *arg && *arg != '/'; arg++)
3862 if (*arg == '\\' && arg[1] != NUL)
3863 arg++;
3864 if (*arg)
3865 {
3866 arg = skipwhite(arg + 1);
3867
3868 /* Check for trailing illegal characters */
3869 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3870 xp->xp_context = EXPAND_NOTHING;
3871 else
3872 return arg;
3873 }
3874 }
3875 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 case CMD_autocmd:
3878 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003880 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case CMD_set:
3883 set_context_in_set_cmd(xp, arg, 0);
3884 break;
3885 case CMD_setglobal:
3886 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3887 break;
3888 case CMD_setlocal:
3889 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3890 break;
3891 case CMD_tag:
3892 case CMD_stag:
3893 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003894 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 case CMD_tselect:
3896 case CMD_stselect:
3897 case CMD_ptselect:
3898 case CMD_tjump:
3899 case CMD_stjump:
3900 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003901 if (*p_wop != NUL)
3902 xp->xp_context = EXPAND_TAGS_LISTFILES;
3903 else
3904 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 xp->xp_pattern = arg;
3906 break;
3907 case CMD_augroup:
3908 xp->xp_context = EXPAND_AUGROUP;
3909 xp->xp_pattern = arg;
3910 break;
3911#ifdef FEAT_SYN_HL
3912 case CMD_syntax:
3913 set_context_in_syntax_cmd(xp, arg);
3914 break;
3915#endif
3916#ifdef FEAT_EVAL
3917 case CMD_let:
3918 case CMD_if:
3919 case CMD_elseif:
3920 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003921 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 case CMD_echo:
3923 case CMD_echon:
3924 case CMD_execute:
3925 case CMD_echomsg:
3926 case CMD_echoerr:
3927 case CMD_call:
3928 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003929 case CMD_cexpr:
3930 case CMD_caddexpr:
3931 case CMD_cgetexpr:
3932 case CMD_lexpr:
3933 case CMD_laddexpr:
3934 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003935 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 break;
3937
3938 case CMD_unlet:
3939 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3940 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 xp->xp_context = EXPAND_USER_VARS;
3943 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003944
3945 if (*xp->xp_pattern == '$')
3946 {
3947 xp->xp_context = EXPAND_ENV_VARS;
3948 ++xp->xp_pattern;
3949 }
3950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 break;
3952
3953 case CMD_function:
3954 case CMD_delfunction:
3955 xp->xp_context = EXPAND_USER_FUNC;
3956 xp->xp_pattern = arg;
3957 break;
3958
3959 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003960 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 break;
3962#endif
3963 case CMD_highlight:
3964 set_context_in_highlight_cmd(xp, arg);
3965 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003966#ifdef FEAT_CSCOPE
3967 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003968 case CMD_lcscope:
3969 case CMD_scscope:
3970 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003971 break;
3972#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003973#ifdef FEAT_SIGNS
3974 case CMD_sign:
3975 set_context_in_sign_cmd(xp, arg);
3976 break;
3977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 case CMD_bdelete:
3979 case CMD_bwipeout:
3980 case CMD_bunload:
3981 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3982 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003983 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 case CMD_buffer:
3985 case CMD_sbuffer:
3986 case CMD_checktime:
3987 xp->xp_context = EXPAND_BUFFERS;
3988 xp->xp_pattern = arg;
3989 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 case CMD_USER:
3992 case CMD_USER_BUF:
3993 if (compl != EXPAND_NOTHING)
3994 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003995 // EX_XFILE: file names are handled above
3996 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003998#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 if (compl == EXPAND_MENUS)
4000 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 if (compl == EXPAND_COMMANDS)
4003 return arg;
4004 if (compl == EXPAND_MAPPINGS)
4005 return set_context_in_map_cmd(xp, (char_u *)"map",
4006 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004007 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004008 p = arg;
4009 while (*p)
4010 {
4011 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004012 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004013 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004014 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004015 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004016 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 xp->xp_pattern = arg;
4019 }
4020 xp->xp_context = compl;
4021 }
4022 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004023
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 case CMD_map: case CMD_noremap:
4025 case CMD_nmap: case CMD_nnoremap:
4026 case CMD_vmap: case CMD_vnoremap:
4027 case CMD_omap: case CMD_onoremap:
4028 case CMD_imap: case CMD_inoremap:
4029 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004030 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004031 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004032 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004033 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004035 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 case CMD_unmap:
4037 case CMD_nunmap:
4038 case CMD_vunmap:
4039 case CMD_ounmap:
4040 case CMD_iunmap:
4041 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004042 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004043 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004044 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004045 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004047 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004048 case CMD_mapclear:
4049 case CMD_nmapclear:
4050 case CMD_vmapclear:
4051 case CMD_omapclear:
4052 case CMD_imapclear:
4053 case CMD_cmapclear:
4054 case CMD_lmapclear:
4055 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004056 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004057 case CMD_xmapclear:
4058 xp->xp_context = EXPAND_MAPCLEAR;
4059 xp->xp_pattern = arg;
4060 break;
4061
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 case CMD_abbreviate: case CMD_noreabbrev:
4063 case CMD_cabbrev: case CMD_cnoreabbrev:
4064 case CMD_iabbrev: case CMD_inoreabbrev:
4065 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004066 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 case CMD_unabbreviate:
4068 case CMD_cunabbrev:
4069 case CMD_iunabbrev:
4070 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004071 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072#ifdef FEAT_MENU
4073 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4074 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4075 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4076 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4077 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4078 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4079 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004080 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 case CMD_tmenu: case CMD_tunmenu:
4082 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4083 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4084#endif
4085
4086 case CMD_colorscheme:
4087 xp->xp_context = EXPAND_COLORS;
4088 xp->xp_pattern = arg;
4089 break;
4090
4091 case CMD_compiler:
4092 xp->xp_context = EXPAND_COMPILER;
4093 xp->xp_pattern = arg;
4094 break;
4095
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004096 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004097 xp->xp_context = EXPAND_OWNSYNTAX;
4098 xp->xp_pattern = arg;
4099 break;
4100
4101 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004102 xp->xp_context = EXPAND_FILETYPE;
4103 xp->xp_pattern = arg;
4104 break;
4105
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004106 case CMD_packadd:
4107 xp->xp_context = EXPAND_PACKADD;
4108 xp->xp_pattern = arg;
4109 break;
4110
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004111#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004113 p = skiptowhite(arg);
4114 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 {
4116 xp->xp_context = EXPAND_LANGUAGE;
4117 xp->xp_pattern = arg;
4118 }
4119 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004120 {
4121 if ( STRNCMP(arg, "messages", p - arg) == 0
4122 || STRNCMP(arg, "ctype", p - arg) == 0
4123 || STRNCMP(arg, "time", p - arg) == 0)
4124 {
4125 xp->xp_context = EXPAND_LOCALES;
4126 xp->xp_pattern = skipwhite(p);
4127 }
4128 else
4129 xp->xp_context = EXPAND_NOTHING;
4130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 break;
4132#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004133#if defined(FEAT_PROFILE)
4134 case CMD_profile:
4135 set_context_in_profile_cmd(xp, arg);
4136 break;
4137#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004138 case CMD_behave:
4139 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004140 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004141 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004143 case CMD_messages:
4144 xp->xp_context = EXPAND_MESSAGES;
4145 xp->xp_pattern = arg;
4146 break;
4147
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004148#if defined(FEAT_CMDHIST)
4149 case CMD_history:
4150 xp->xp_context = EXPAND_HISTORY;
4151 xp->xp_pattern = arg;
4152 break;
4153#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004154#if defined(FEAT_PROFILE)
4155 case CMD_syntime:
4156 xp->xp_context = EXPAND_SYNTIME;
4157 xp->xp_pattern = arg;
4158 break;
4159#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004160
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004161 case CMD_argdelete:
4162 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4163 arg = xp->xp_pattern + 1;
4164 xp->xp_context = EXPAND_ARGLIST;
4165 xp->xp_pattern = arg;
4166 break;
4167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168#endif /* FEAT_CMDL_COMPL */
4169
4170 default:
4171 break;
4172 }
4173 return NULL;
4174}
4175
4176/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004177 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 *
4179 * Backslashed delimiters after / or ? will be skipped, and commands will
4180 * not be expanded between /'s and ?'s or after "'".
4181 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004182 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 * Returns the "cmd" pointer advanced to beyond the range.
4184 */
4185 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004186skip_range(
4187 char_u *cmd,
4188 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004190 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004192 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004194 if (*cmd == '\\')
4195 {
4196 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4197 ++cmd;
4198 else
4199 break;
4200 }
4201 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 if (*++cmd == NUL && ctx != NULL)
4204 *ctx = EXPAND_NOTHING;
4205 }
4206 else if (*cmd == '/' || *cmd == '?')
4207 {
4208 delim = *cmd++;
4209 while (*cmd != NUL && *cmd != delim)
4210 if (*cmd++ == '\\' && *cmd != NUL)
4211 ++cmd;
4212 if (*cmd == NUL && ctx != NULL)
4213 *ctx = EXPAND_NOTHING;
4214 }
4215 if (*cmd != NUL)
4216 ++cmd;
4217 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004218
4219 /* Skip ":" and white space. */
4220 while (*cmd == ':')
4221 cmd = skipwhite(cmd + 1);
4222
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return cmd;
4224}
4225
4226/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004227 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 *
4229 * Set ptr to the next character after the part that was interpreted.
4230 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004231 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 *
4233 * Return MAXLNUM when no Ex address was found.
4234 */
4235 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004236get_address(
4237 exarg_T *eap UNUSED,
4238 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004239 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004240 int skip, // only skip the address, don't use it
4241 int silent, // no errors or side effects
4242 int to_other_file, // flag: may jump to other file
4243 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004245 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 int c;
4247 int i;
4248 long n;
4249 char_u *cmd;
4250 pos_T pos;
4251 pos_T *fp;
4252 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004253 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254
4255 cmd = skipwhite(*ptr);
4256 lnum = MAXLNUM;
4257 do
4258 {
4259 switch (*cmd)
4260 {
4261 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004262 ++cmd;
4263 switch (addr_type)
4264 {
4265 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004266 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 lnum = curwin->w_cursor.lnum;
4268 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004269 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004270 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 break;
4272 case ADDR_ARGUMENTS:
4273 lnum = curwin->w_arg_idx + 1;
4274 break;
4275 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004276 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004277 lnum = curbuf->b_fnum;
4278 break;
4279 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004280 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004281 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004282 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004283 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004284 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004285 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004286 cmd = NULL;
4287 goto error;
4288 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004289 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004290#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004291 lnum = qf_get_cur_idx(eap);
4292#endif
4293 break;
4294 case ADDR_QUICKFIX_VALID:
4295#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004296 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004297#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004298 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004299 }
4300 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301
4302 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004303 ++cmd;
4304 switch (addr_type)
4305 {
4306 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004307 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 lnum = curbuf->b_ml.ml_line_count;
4309 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004310 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004311 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 break;
4313 case ADDR_ARGUMENTS:
4314 lnum = ARGCOUNT;
4315 break;
4316 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004317 buf = lastbuf;
4318 while (buf->b_ml.ml_mfp == NULL)
4319 {
4320 if (buf->b_prev == NULL)
4321 break;
4322 buf = buf->b_prev;
4323 }
4324 lnum = buf->b_fnum;
4325 break;
4326 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004327 lnum = lastbuf->b_fnum;
4328 break;
4329 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004330 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004331 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004332 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004333 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004334 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004335 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004336 cmd = NULL;
4337 goto error;
4338 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004339 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004340#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004341 lnum = qf_get_size(eap);
4342 if (lnum == 0)
4343 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004344#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004345 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004346 case ADDR_QUICKFIX_VALID:
4347#ifdef FEAT_QUICKFIX
4348 lnum = qf_get_valid_size(eap);
4349 if (lnum == 0)
4350 lnum = 1;
4351#endif
4352 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004353 }
4354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355
4356 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004357 if (*++cmd == NUL)
4358 {
4359 cmd = NULL;
4360 goto error;
4361 }
4362 if (addr_type != ADDR_LINES)
4363 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004364 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004365 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 goto error;
4367 }
4368 if (skip)
4369 ++cmd;
4370 else
4371 {
4372 /* Only accept a mark in another file when it is
4373 * used by itself: ":'M". */
4374 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4375 ++cmd;
4376 if (fp == (pos_T *)-1)
4377 /* Jumped to another file. */
4378 lnum = curwin->w_cursor.lnum;
4379 else
4380 {
4381 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
4383 cmd = NULL;
4384 goto error;
4385 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 lnum = fp->lnum;
4387 }
4388 }
4389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390
4391 case '/':
4392 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 c = *cmd++;
4394 if (addr_type != ADDR_LINES)
4395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004396 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004397 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 goto error;
4399 }
4400 if (skip) /* skip "/pat/" */
4401 {
4402 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4403 if (*cmd == c)
4404 ++cmd;
4405 }
4406 else
4407 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004408 int flags;
4409
4410 pos = curwin->w_cursor; // save curwin->w_cursor
4411
4412 // When '/' or '?' follows another address, start from
4413 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004414 if (lnum != MAXLNUM)
4415 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004416
4417 // Start a forward search at the end of the line (unless
4418 // before the first line).
4419 // Start a backward search at the start of the line.
4420 // This makes sure we never match in the current
4421 // line, and can match anywhere in the
4422 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004423 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004424 curwin->w_cursor.col = MAXCOL;
4425 else
4426 curwin->w_cursor.col = 0;
4427 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004428 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4429 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 {
4431 curwin->w_cursor = pos;
4432 cmd = NULL;
4433 goto error;
4434 }
4435 lnum = curwin->w_cursor.lnum;
4436 curwin->w_cursor = pos;
4437 /* adjust command string pointer */
4438 cmd += searchcmdlen;
4439 }
4440 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004443 ++cmd;
4444 if (addr_type != ADDR_LINES)
4445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004446 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004447 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 goto error;
4449 }
4450 if (*cmd == '&')
4451 i = RE_SUBST;
4452 else if (*cmd == '?' || *cmd == '/')
4453 i = RE_SEARCH;
4454 else
4455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004456 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 cmd = NULL;
4458 goto error;
4459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004461 if (!skip)
4462 {
4463 /*
4464 * When search follows another address, start from
4465 * there.
4466 */
4467 if (lnum != MAXLNUM)
4468 pos.lnum = lnum;
4469 else
4470 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 /*
4473 * Start the search just like for the above
4474 * do_search().
4475 */
4476 if (*cmd != '?')
4477 pos.col = MAXCOL;
4478 else
4479 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004480 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004481 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004482 *cmd == '?' ? BACKWARD : FORWARD,
4483 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004484 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 lnum = pos.lnum;
4486 else
4487 {
4488 cmd = NULL;
4489 goto error;
4490 }
4491 }
4492 ++cmd;
4493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494
4495 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004496 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4497 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499
4500 for (;;)
4501 {
4502 cmd = skipwhite(cmd);
4503 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4504 break;
4505
4506 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004507 {
4508 switch (addr_type)
4509 {
4510 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004511 case ADDR_OTHER:
4512 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004513 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 break;
4515 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004516 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 break;
4518 case ADDR_ARGUMENTS:
4519 lnum = curwin->w_arg_idx + 1;
4520 break;
4521 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004522 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 lnum = curbuf->b_fnum;
4524 break;
4525 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004526 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004527 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004528 case ADDR_TABS_RELATIVE:
4529 lnum = 1;
4530 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004531 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004532#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004533 lnum = qf_get_cur_idx(eap);
4534#endif
4535 break;
4536 case ADDR_QUICKFIX_VALID:
4537#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004538 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004539#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004540 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004541 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004542 case ADDR_UNSIGNED:
4543 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004544 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004545 }
4546 }
4547
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (VIM_ISDIGIT(*cmd))
4549 i = '+'; /* "number" is same as "+number" */
4550 else
4551 i = *cmd++;
4552 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4553 n = 1;
4554 else
4555 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004556
4557 if (addr_type == ADDR_TABS_RELATIVE)
4558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004559 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004560 cmd = NULL;
4561 goto error;
4562 }
4563 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004564 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004565 lnum = compute_buffer_local_count(
4566 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004568 {
4569#ifdef FEAT_FOLDING
4570 /* Relative line addressing, need to adjust for folded lines
4571 * now, but only do it after the first address. */
4572 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4573 && address_count >= 2)
4574 (void)hasFolding(lnum, NULL, &lnum);
4575#endif
4576 if (i == '-')
4577 lnum -= n;
4578 else
4579 lnum += n;
4580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 } while (*cmd == '/' || *cmd == '?');
4583
4584error:
4585 *ptr = cmd;
4586 return lnum;
4587}
4588
4589/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004590 * Get flags from an Ex command argument.
4591 */
4592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004593get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004594{
4595 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4596 {
4597 if (*eap->arg == 'l')
4598 eap->flags |= EXFLAG_LIST;
4599 else if (*eap->arg == 'p')
4600 eap->flags |= EXFLAG_PRINT;
4601 else
4602 eap->flags |= EXFLAG_NR;
4603 eap->arg = skipwhite(eap->arg + 1);
4604 }
4605}
4606
4607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 * Function called for command which is Not Implemented. NI!
4609 */
4610 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004611ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612{
4613 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004614 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615}
4616
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004617#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618/*
4619 * Function called for script command which is Not Implemented. NI!
4620 * Skips over ":perl <<EOF" constructs.
4621 */
4622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004623ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624{
4625 if (!eap->skip)
4626 ex_ni(eap);
4627 else
4628 vim_free(script_get(eap, eap->arg));
4629}
4630#endif
4631
4632/*
4633 * Check range in Ex command for validity.
4634 * Return NULL when valid, error message when invalid.
4635 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004636 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004637invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004639 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if ( eap->line1 < 0
4642 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004643 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004644 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004645
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004646 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004647 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004648 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004649 {
4650 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004651 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004652#ifdef FEAT_DIFF
4653 + (eap->cmdidx == CMD_diffget)
4654#endif
4655 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004656 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004657 break;
4658 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004659 /* add 1 if ARGCOUNT is 0 */
4660 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004661 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004662 break;
4663 case ADDR_BUFFERS:
4664 if (eap->line1 < firstbuf->b_fnum
4665 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004666 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004667 break;
4668 case ADDR_LOADED_BUFFERS:
4669 buf = firstbuf;
4670 while (buf->b_ml.ml_mfp == NULL)
4671 {
4672 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004673 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004674 buf = buf->b_next;
4675 }
4676 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004677 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004678 buf = lastbuf;
4679 while (buf->b_ml.ml_mfp == NULL)
4680 {
4681 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004682 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004683 buf = buf->b_prev;
4684 }
4685 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004686 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004687 break;
4688 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004689 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004690 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004691 break;
4692 case ADDR_TABS:
4693 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004694 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004695 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004696 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004697 case ADDR_OTHER:
4698 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004699 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004700 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004701#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004702 // No error for value that is too big, will use the last entry.
4703 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004704 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004705#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004706 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004707 case ADDR_QUICKFIX_VALID:
4708#ifdef FEAT_QUICKFIX
4709 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4710 || eap->line2 < 0)
4711 return _(e_invrange);
4712#endif
4713 break;
4714 case ADDR_UNSIGNED:
4715 if (eap->line2 < 0)
4716 return _(e_invrange);
4717 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004718 case ADDR_NONE:
4719 // Will give an error elsewhere.
4720 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004721 }
4722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 return NULL;
4724}
4725
4726/*
4727 * Correct the range for zero line number, if required.
4728 */
4729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004730correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004732 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 {
4734 if (eap->line1 == 0)
4735 eap->line1 = 1;
4736 if (eap->line2 == 0)
4737 eap->line2 = 1;
4738 }
4739}
4740
Bram Moolenaar748bf032005-02-02 23:04:36 +00004741#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004742/*
4743 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4744 * pattern. Otherwise return eap->arg.
4745 */
4746 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004747skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004748{
4749 char_u *p = eap->arg;
4750
Bram Moolenaara37420f2006-02-04 22:37:47 +00004751 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4752 || eap->cmdidx == CMD_vimgrepadd
4753 || eap->cmdidx == CMD_lvimgrepadd
4754 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004755 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004756 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004757 if (p == NULL)
4758 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004759 }
4760 return p;
4761}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004762
4763/*
4764 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4765 * in the command line, so that things like % get expanded.
4766 */
4767 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004768replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004769{
4770 char_u *new_cmdline;
4771 char_u *program;
4772 char_u *pos;
4773 char_u *ptr;
4774 int len;
4775 int i;
4776
4777 /*
4778 * Don't do it when ":vimgrep" is used for ":grep".
4779 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004780 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4781 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4782 || eap->cmdidx == CMD_grepadd
4783 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004784 && !grep_internal(eap->cmdidx))
4785 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004786 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4787 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004788 {
4789 if (*curbuf->b_p_gp == NUL)
4790 program = p_gp;
4791 else
4792 program = curbuf->b_p_gp;
4793 }
4794 else
4795 {
4796 if (*curbuf->b_p_mp == NUL)
4797 program = p_mp;
4798 else
4799 program = curbuf->b_p_mp;
4800 }
4801
4802 p = skipwhite(p);
4803
4804 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4805 {
4806 /* replace $* by given arguments */
4807 i = 1;
4808 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4809 ++i;
4810 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004811 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004812 if (new_cmdline == NULL)
4813 return NULL; /* out of memory */
4814 ptr = new_cmdline;
4815 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4816 {
4817 i = (int)(pos - program);
4818 STRNCPY(ptr, program, i);
4819 STRCPY(ptr += i, p);
4820 ptr += len;
4821 program = pos + 2;
4822 }
4823 STRCPY(ptr, program);
4824 }
4825 else
4826 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004827 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004828 if (new_cmdline == NULL)
4829 return NULL; /* out of memory */
4830 STRCPY(new_cmdline, program);
4831 STRCAT(new_cmdline, " ");
4832 STRCAT(new_cmdline, p);
4833 }
4834 msg_make(p);
4835
4836 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4837 vim_free(*cmdlinep);
4838 *cmdlinep = new_cmdline;
4839 p = new_cmdline;
4840 }
4841 return p;
4842}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004843#endif
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845/*
4846 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004847 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 * Return FAIL for failure, OK otherwise.
4849 */
4850 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004851expand_filename(
4852 exarg_T *eap,
4853 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004854 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 int has_wildcards; /* need to expand wildcards */
4857 char_u *repl;
4858 int srclen;
4859 char_u *p;
4860 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004861 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
Bram Moolenaar748bf032005-02-02 23:04:36 +00004863#ifdef FEAT_QUICKFIX
4864 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4865 p = skip_grep_pat(eap);
4866#else
4867 p = eap->arg;
4868#endif
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 /*
4871 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4872 * the file name contains a wildcard it should not cause expanding.
4873 * (it will be expanded anyway if there is a wildcard before replacing).
4874 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004875 has_wildcards = mch_has_wildcard(p);
4876 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004878#ifdef FEAT_EVAL
4879 /* Skip over `=expr`, wildcards in it are not expanded. */
4880 if (p[0] == '`' && p[1] == '=')
4881 {
4882 p += 2;
4883 (void)skip_expr(&p);
4884 if (*p == '`')
4885 ++p;
4886 continue;
4887 }
4888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 /*
4890 * Quick check if this cannot be the start of a special string.
4891 * Also removes backslash before '%', '#' and '<'.
4892 */
4893 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4894 {
4895 ++p;
4896 continue;
4897 }
4898
4899 /*
4900 * Try to find a match at this position.
4901 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004902 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4903 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 if (*errormsgp != NULL) /* error detected */
4905 return FAIL;
4906 if (repl == NULL) /* no match found */
4907 {
4908 p += srclen;
4909 continue;
4910 }
4911
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004912 /* Wildcards won't be expanded below, the replacement is taken
4913 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4914 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4915 {
4916 char_u *l = repl;
4917
4918 repl = expand_env_save(repl);
4919 vim_free(l);
4920 }
4921
Bram Moolenaar63b92542007-03-27 14:57:09 +00004922 /* Need to escape white space et al. with a backslash.
4923 * Don't do this for:
4924 * - replacement that already has been escaped: "##"
4925 * - shell commands (may have to use quotes instead).
4926 * - non-unix systems when there is a single argument (spaces don't
4927 * separate arguments then).
4928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004930 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 && eap->cmdidx != CMD_grep
4933 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004934 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004935 && eap->cmdidx != CMD_lgrep
4936 && eap->cmdidx != CMD_lgrepadd
4937 && eap->cmdidx != CMD_lmake
4938 && eap->cmdidx != CMD_make
4939 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004941 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#endif
4943 )
4944 {
4945 char_u *l;
4946#ifdef BACKSLASH_IN_FILENAME
4947 /* Don't escape a backslash here, because rem_backslash() doesn't
4948 * remove it later. */
4949 static char_u *nobslash = (char_u *)" \t\"|";
4950# define ESCAPE_CHARS nobslash
4951#else
4952# define ESCAPE_CHARS escape_chars
4953#endif
4954
4955 for (l = repl; *l; ++l)
4956 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4957 {
4958 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4959 if (l != NULL)
4960 {
4961 vim_free(repl);
4962 repl = l;
4963 }
4964 break;
4965 }
4966 }
4967
4968 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004969 if ((eap->usefilter || eap->cmdidx == CMD_bang
4970 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004971 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 char_u *l;
4974
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004975 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 if (l != NULL)
4977 {
4978 vim_free(repl);
4979 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981 }
4982
4983 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4984 vim_free(repl);
4985 if (p == NULL)
4986 return FAIL;
4987 }
4988
4989 /*
4990 * One file argument: Expand wildcards.
4991 * Don't do this with ":r !command" or ":w !command".
4992 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004993 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
4995 /*
4996 * May do this twice:
4997 * 1. Replace environment variables.
4998 * 2. Replace any other wildcards, remove backslashes.
4999 */
5000 for (n = 1; n <= 2; ++n)
5001 {
5002 if (n == 2)
5003 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 /*
5005 * Halve the number of backslashes (this is Vi compatible).
5006 * For Unix and OS/2, when wildcards are expanded, this is
5007 * done by ExpandOne() below.
5008 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005009#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (!has_wildcards)
5011#endif
5012 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014
5015 if (has_wildcards)
5016 {
5017 if (n == 1)
5018 {
5019 /*
5020 * First loop: May expand environment variables. This
5021 * can be done much faster with expand_env() than with
5022 * something else (e.g., calling a shell).
5023 * After expanding environment variables, check again
5024 * if there are still wildcards present.
5025 */
5026 if (vim_strchr(eap->arg, '$') != NULL
5027 || vim_strchr(eap->arg, '~') != NULL)
5028 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005029 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005030 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 has_wildcards = mch_has_wildcard(NameBuff);
5032 p = NameBuff;
5033 }
5034 else
5035 p = NULL;
5036 }
5037 else /* n == 2 */
5038 {
5039 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005040 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
5042 ExpandInit(&xpc);
5043 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005044 if (p_wic)
5045 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005047 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (p == NULL)
5049 return FAIL;
5050 }
5051 if (p != NULL)
5052 {
5053 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5054 p, cmdlinep);
5055 if (n == 2) /* p came from ExpandOne() */
5056 vim_free(p);
5057 }
5058 }
5059 }
5060 }
5061 return OK;
5062}
5063
5064/*
5065 * Replace part of the command line, keeping eap->cmd, eap->arg and
5066 * eap->nextcmd correct.
5067 * "src" points to the part that is to be replaced, of length "srclen".
5068 * "repl" is the replacement string.
5069 * Returns a pointer to the character after the replaced string.
5070 * Returns NULL for failure.
5071 */
5072 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005073repl_cmdline(
5074 exarg_T *eap,
5075 char_u *src,
5076 int srclen,
5077 char_u *repl,
5078 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
5080 int len;
5081 int i;
5082 char_u *new_cmdline;
5083
5084 /*
5085 * The new command line is build in new_cmdline[].
5086 * First allocate it.
5087 * Careful: a "+cmd" argument may have been NUL terminated.
5088 */
5089 len = (int)STRLEN(repl);
5090 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005091 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005093 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 return NULL; /* out of memory! */
5095
5096 /*
5097 * Copy the stuff before the expanded part.
5098 * Copy the expanded stuff.
5099 * Copy what came after the expanded part.
5100 * Copy the next commands, if there are any.
5101 */
5102 i = (int)(src - *cmdlinep); /* length of part before match */
5103 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 mch_memmove(new_cmdline + i, repl, (size_t)len);
5106 i += len; /* remember the end of the string */
5107 STRCPY(new_cmdline + i, src + srclen);
5108 src = new_cmdline + i; /* remember where to continue */
5109
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005110 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 i = (int)STRLEN(new_cmdline) + 1;
5113 STRCPY(new_cmdline + i, eap->nextcmd);
5114 eap->nextcmd = new_cmdline + i;
5115 }
5116 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5117 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5118 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5119 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5120 vim_free(*cmdlinep);
5121 *cmdlinep = new_cmdline;
5122
5123 return src;
5124}
5125
5126/*
5127 * Check for '|' to separate commands and '"' to start comments.
5128 */
5129 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005130separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131{
5132 char_u *p;
5133
Bram Moolenaar86b68352004-12-27 21:59:20 +00005134#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005135 p = skip_grep_pat(eap);
5136#else
5137 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005138#endif
5139
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005140 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142 if (*p == Ctrl_V)
5143 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005144 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 ++p; /* skip CTRL-V and next char */
5146 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005147 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005148 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 if (*p == NUL) /* stop at NUL after CTRL-V */
5150 break;
5151 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005152
5153#ifdef FEAT_EVAL
5154 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005155 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005156 {
5157 p += 2;
5158 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005159 }
5160#endif
5161
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 /* Check for '"': start of comment or '|': next command */
5163 /* :@" and :*" do not start a comment!
5164 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005165 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5167 || p != eap->arg)
5168 && (eap->cmdidx != CMD_redir
5169 || p != eap->arg + 1 || p[-1] != '@'))
5170 || *p == '|' || *p == '\n')
5171 {
5172 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005173 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 * AND 'b' is present in 'cpoptions'.
5175 */
5176 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005177 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005179 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 --p;
5181 }
5182 else
5183 {
5184 eap->nextcmd = check_nextcmd(p);
5185 *p = NUL;
5186 break;
5187 }
5188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005190
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005191 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 del_trailing_spaces(eap->arg);
5193}
5194
5195/*
5196 * get + command from ex argument
5197 */
5198 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005199getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200{
5201 char_u *arg = *argp;
5202 char_u *command = NULL;
5203
5204 if (*arg == '+') /* +[command] */
5205 {
5206 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005207 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 command = dollar_command;
5209 else
5210 {
5211 command = arg;
5212 arg = skip_cmd_arg(command, TRUE);
5213 if (*arg != NUL)
5214 *arg++ = NUL; /* terminate command with NUL */
5215 }
5216
5217 arg = skipwhite(arg); /* skip over spaces */
5218 *argp = arg;
5219 }
5220 return command;
5221}
5222
5223/*
5224 * Find end of "+command" argument. Skip over "\ " and "\\".
5225 */
5226 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005227skip_cmd_arg(
5228 char_u *p,
5229 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
5231 while (*p && !vim_isspace(*p))
5232 {
5233 if (*p == '\\' && p[1] != NUL)
5234 {
5235 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005236 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 else
5238 ++p;
5239 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005240 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 return p;
5243}
5244
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005245 int
5246get_bad_opt(char_u *p, exarg_T *eap)
5247{
5248 if (STRICMP(p, "keep") == 0)
5249 eap->bad_char = BAD_KEEP;
5250 else if (STRICMP(p, "drop") == 0)
5251 eap->bad_char = BAD_DROP;
5252 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5253 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005254 else
5255 return FAIL;
5256 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005257}
5258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259/*
5260 * Get "++opt=arg" argument.
5261 * Return FAIL or OK.
5262 */
5263 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005264getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265{
5266 char_u *arg = eap->arg + 2;
5267 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005268 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 /* ":edit ++[no]bin[ary] file" */
5272 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5273 {
5274 if (*arg == 'n')
5275 {
5276 arg += 2;
5277 eap->force_bin = FORCE_NOBIN;
5278 }
5279 else
5280 eap->force_bin = FORCE_BIN;
5281 if (!checkforcmd(&arg, "binary", 3))
5282 return FAIL;
5283 eap->arg = skipwhite(arg);
5284 return OK;
5285 }
5286
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005287 /* ":read ++edit file" */
5288 if (STRNCMP(arg, "edit", 4) == 0)
5289 {
5290 eap->read_edit = TRUE;
5291 eap->arg = skipwhite(arg + 4);
5292 return OK;
5293 }
5294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 if (STRNCMP(arg, "ff", 2) == 0)
5296 {
5297 arg += 2;
5298 pp = &eap->force_ff;
5299 }
5300 else if (STRNCMP(arg, "fileformat", 10) == 0)
5301 {
5302 arg += 10;
5303 pp = &eap->force_ff;
5304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 else if (STRNCMP(arg, "enc", 3) == 0)
5306 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005307 if (STRNCMP(arg, "encoding", 8) == 0)
5308 arg += 8;
5309 else
5310 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 pp = &eap->force_enc;
5312 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005313 else if (STRNCMP(arg, "bad", 3) == 0)
5314 {
5315 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005316 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318
5319 if (pp == NULL || *arg != '=')
5320 return FAIL;
5321
5322 ++arg;
5323 *pp = (int)(arg - eap->cmd);
5324 arg = skip_cmd_arg(arg, FALSE);
5325 eap->arg = skipwhite(arg);
5326 *arg = NUL;
5327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (pp == &eap->force_ff)
5329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5331 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005332 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005334 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
5336 /* Make 'fileencoding' lower case. */
5337 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5338 *p = TOLOWER_ASC(*p);
5339 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005340 else
5341 {
5342 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5343 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005344 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005345 return FAIL;
5346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
5348 return OK;
5349}
5350
5351/*
5352 * ":abbreviate" and friends.
5353 */
5354 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005355ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
5357 do_exmap(eap, TRUE); /* almost the same as mapping */
5358}
5359
5360/*
5361 * ":map" and friends.
5362 */
5363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005364ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 /*
5367 * If we are sourcing .exrc or .vimrc in current directory we
5368 * print the mappings for security reasons.
5369 */
5370 if (secure)
5371 {
5372 secure = 2;
5373 msg_outtrans(eap->cmd);
5374 msg_putchar('\n');
5375 }
5376 do_exmap(eap, FALSE);
5377}
5378
5379/*
5380 * ":unmap" and friends.
5381 */
5382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005383ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
5385 do_exmap(eap, FALSE);
5386}
5387
5388/*
5389 * ":mapclear" and friends.
5390 */
5391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005392ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5395}
5396
5397/*
5398 * ":abclear" and friends.
5399 */
5400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5404}
5405
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005407ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005410 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 * directory for security reasons.
5412 */
5413 if (secure)
5414 {
5415 secure = 2;
5416 eap->errmsg = e_curdir;
5417 }
5418 else if (eap->cmdidx == CMD_autocmd)
5419 do_autocmd(eap->arg, eap->forceit);
5420 else
5421 do_augroup(eap->arg, eap->forceit);
5422}
5423
5424/*
5425 * ":doautocmd": Apply the automatic commands to the current buffer.
5426 */
5427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005428ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005430 char_u *arg = eap->arg;
5431 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005432 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005433
Bram Moolenaar1610d052016-06-09 22:53:01 +02005434 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5435 /* Only when there is no <nomodeline>. */
5436 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005437 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440/*
5441 * :[N]bunload[!] [N] [bufname] unload buffer
5442 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5443 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5444 */
5445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005446ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005448 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005449 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 eap->errmsg = do_bufdel(
5451 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5452 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5453 : DOBUF_UNLOAD, eap->arg,
5454 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5455}
5456
5457/*
5458 * :[N]buffer [N] to buffer N
5459 * :[N]sbuffer [N] to buffer N
5460 */
5461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005464 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005465 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 if (*eap->arg)
5467 eap->errmsg = e_trailing;
5468 else
5469 {
5470 if (eap->addr_count == 0) /* default is current buffer */
5471 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5472 else
5473 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005474 if (eap->do_ecmd_cmd != NULL)
5475 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 }
5477}
5478
5479/*
5480 * :[N]bmodified [N] to next mod. buffer
5481 * :[N]sbmodified [N] to next mod. buffer
5482 */
5483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
5486 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005487 if (eap->do_ecmd_cmd != NULL)
5488 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489}
5490
5491/*
5492 * :[N]bnext [N] to next buffer
5493 * :[N]sbnext [N] split and to next buffer
5494 */
5495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005496ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497{
5498 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005499 if (eap->do_ecmd_cmd != NULL)
5500 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501}
5502
5503/*
5504 * :[N]bNext [N] to previous buffer
5505 * :[N]bprevious [N] to previous buffer
5506 * :[N]sbNext [N] split and to previous buffer
5507 * :[N]sbprevious [N] split and to previous buffer
5508 */
5509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005513 if (eap->do_ecmd_cmd != NULL)
5514 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515}
5516
5517/*
5518 * :brewind to first buffer
5519 * :bfirst to first buffer
5520 * :sbrewind split and to first buffer
5521 * :sbfirst split and to first buffer
5522 */
5523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005524ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005527 if (eap->do_ecmd_cmd != NULL)
5528 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529}
5530
5531/*
5532 * :blast to last buffer
5533 * :sblast split and to last buffer
5534 */
5535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005536ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005539 if (eap->do_ecmd_cmd != NULL)
5540 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
5543 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 return (c == NUL || c == '|' || c == '"' || c == '\n');
5547}
5548
5549#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5550 || defined(PROTO)
5551/*
5552 * Return the next command, after the first '|' or '\n'.
5553 * Return NULL if not found.
5554 */
5555 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005556find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 while (*p != '|' && *p != '\n')
5559 {
5560 if (*p == NUL)
5561 return NULL;
5562 ++p;
5563 }
5564 return (p + 1);
5565}
5566#endif
5567
5568/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005569 * Check if *p is a separator between Ex commands, skipping over white space.
5570 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 */
5572 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005573check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005575 char_u *s = skipwhite(p);
5576
5577 if (*s == '|' || *s == '\n')
5578 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 else
5580 return NULL;
5581}
5582
5583/*
5584 * - if there are more files to edit
5585 * - and this is the last window
5586 * - and forceit not used
5587 * - and not repeated twice on a row
5588 * return FAIL and give error message if 'message' TRUE
5589 * return OK otherwise
5590 */
5591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005592check_more(
5593 int message, /* when FALSE check only, no messages */
5594 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5597
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005598 if (!forceit && only_one_window()
5599 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
5601 if (message)
5602 {
5603#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5604 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5605 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005606 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005608 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5609 NGETTEXT("%d more file to edit. Quit anyway?",
5610 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5612 return OK;
5613 return FAIL;
5614 }
5615#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005616 semsg(NGETTEXT("E173: %d more file to edit",
5617 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 quitmore = 2; /* next try to quit is allowed */
5619 }
5620 return FAIL;
5621 }
5622 return OK;
5623}
5624
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005625#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626/*
5627 * Function given to ExpandGeneric() to obtain the list of command names.
5628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 return cmdnames[idx].cmd_name;
5635}
5636#endif
5637
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
Bram Moolenaare6850792010-05-14 15:28:44 +02005641 if (*eap->arg == NUL)
5642 {
5643#ifdef FEAT_EVAL
5644 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5645 char_u *p = NULL;
5646
5647 if (expr != NULL)
5648 {
5649 ++emsg_off;
5650 p = eval_to_string(expr, NULL, FALSE);
5651 --emsg_off;
5652 vim_free(expr);
5653 }
5654 if (p != NULL)
5655 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005656 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005657 vim_free(p);
5658 }
5659 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005660 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005661#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005662 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005663#endif
5664 }
5665 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005666 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005667
5668#ifdef FEAT_VTP
5669 else if (has_vtp_working())
5670 {
5671 // background color change requires clear + redraw
5672 update_screen(CLEAR);
5673 redrawcmd();
5674 }
5675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676}
5677
5678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005679ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
5681 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005682 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 do_highlight(eap->arg, eap->forceit, FALSE);
5684}
5685
5686
5687/*
5688 * Call this function if we thought we were going to exit, but we won't
5689 * (because of an error). May need to restore the terminal mode.
5690 */
5691 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005692not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693{
5694 exiting = FALSE;
5695 settmode(TMODE_RAW);
5696}
5697
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005698 static int
5699before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5700{
5701 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5702
5703 /* Bail out when autocommands closed the window.
5704 * Refuse to quit when the buffer in the last window is being closed (can
5705 * only happen in autocommands). */
5706 if (!win_valid(wp)
5707 || curbuf_locked()
5708 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5709 return TRUE;
5710
5711 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5712 {
5713 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5714 /* Refuse to quit when locked or when the buffer in the last window is
5715 * being closed (can only happen in autocommands). */
5716 if (curbuf_locked()
5717 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5718 return TRUE;
5719 }
5720
5721 return FALSE;
5722}
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005725 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005726 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 */
5728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005729ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005731 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733#ifdef FEAT_CMDWIN
5734 if (cmdwin_type != 0)
5735 {
5736 cmdwin_result = Ctrl_C;
5737 return;
5738 }
5739#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005740 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005741 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005742 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005743 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005744 return;
5745 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005746 if (eap->addr_count > 0)
5747 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005748 int wnr = eap->line2;
5749
5750 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5751 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005752 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005753 }
5754 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005755 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005756
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005757 /* Refuse to quit when locked. */
5758 if (curbuf_locked())
5759 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005760
5761 /* Trigger QuitPre and maybe ExitPre */
5762 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005763 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765#ifdef FEAT_NETBEANS_INTG
5766 netbeansForcedQuit = eap->forceit;
5767#endif
5768
5769 /*
5770 * If there are more files or windows we won't exit.
5771 */
5772 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5773 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005774 if ((!buf_hide(wp->w_buffer)
5775 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005776 | (eap->forceit ? CCGD_FORCEIT : 0)
5777 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005779 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 {
5781 not_exiting();
5782 }
5783 else
5784 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005785 /* quit last window
5786 * Note: only_one_window() returns true, even so a help window is
5787 * still open. In that case only quit, if no address has been
5788 * specified. Example:
5789 * :h|wincmd w|1q - don't quit
5790 * :h|wincmd w|q - quit
5791 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005792 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005794 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005795#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005799 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 }
5801}
5802
5803/*
5804 * ":cquit".
5805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005807ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
5809 getout(1); /* this does not always pass on the exit code to the Manx
5810 compiler. why? */
5811}
5812
5813/*
5814 * ":qall": try to quit all windows
5815 */
5816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005817ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818{
5819# ifdef FEAT_CMDWIN
5820 if (cmdwin_type != 0)
5821 {
5822 if (eap->forceit)
5823 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5824 else
5825 cmdwin_result = K_XF2;
5826 return;
5827 }
5828# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005829
5830 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005831 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005832 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005833 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005834 return;
5835 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005836
5837 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005838 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005839
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005841 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 getout(0);
5843 not_exiting();
5844}
5845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846/*
5847 * ":close": close current window, unless it is the last one
5848 */
5849 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005850ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005852 win_T *win;
5853 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005854#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005856 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005858#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005859 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005860 {
5861 if (eap->addr_count == 0)
5862 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005863 else
5864 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005865 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005866 {
5867 winnr++;
5868 if (winnr == eap->line2)
5869 break;
5870 }
5871 if (win == NULL)
5872 win = lastwin;
5873 ex_win_close(eap->forceit, win, NULL);
5874 }
5875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876}
5877
Bram Moolenaar4033c552017-09-16 20:54:51 +02005878#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005879/*
5880 * ":pclose": Close any preview window.
5881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005883ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005884{
5885 win_T *win;
5886
Bram Moolenaar79648732019-07-18 21:43:07 +02005887 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005888 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005889 if (win->w_p_pvw)
5890 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005891 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005892 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005894# ifdef FEAT_TEXT_PROP
5895 // Also when 'previewpopup' is empty, it might have been cleared.
5896 popup_close_preview();
5897# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005898}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005899#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005900
Bram Moolenaarf740b292006-02-16 22:11:02 +00005901/*
5902 * Close window "win" and take care of handling closing the last window for a
5903 * modified buffer.
5904 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005906ex_win_close(
5907 int forceit,
5908 win_T *win,
5909 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
5911 int need_hide;
5912 buf_T *buf = win->w_buffer;
5913
5914 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005915 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005917#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 if ((p_confirm || cmdmod.confirm) && p_write)
5919 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005920 bufref_T bufref;
5921
5922 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005924 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 return;
5926 need_hide = FALSE;
5927 }
5928 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005931 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 return;
5933 }
5934 }
5935
Bram Moolenaar4033c552017-09-16 20:54:51 +02005936#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005938#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005941 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005942 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005943 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005944 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945}
5946
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005948 * Handle the argument for a tabpage related ex command.
5949 * Returns a tabpage number.
5950 * When an error is encountered then eap->errmsg is set.
5951 */
5952 static int
5953get_tabpage_arg(exarg_T *eap)
5954{
5955 int tab_number;
5956 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5957
5958 if (eap->arg && *eap->arg != NUL)
5959 {
5960 char_u *p = eap->arg;
5961 char_u *p_save;
5962 int relative = 0; /* argument +N/-N means: go to N places to the
5963 * right/left relative to the current position. */
5964
5965 if (*p == '-')
5966 {
5967 relative = -1;
5968 p++;
5969 }
5970 else if (*p == '+')
5971 {
5972 relative = 1;
5973 p++;
5974 }
5975
5976 p_save = p;
5977 tab_number = getdigits(&p);
5978
5979 if (relative == 0)
5980 {
5981 if (STRCMP(p, "$") == 0)
5982 tab_number = LAST_TAB_NR;
5983 else if (p == p_save || *p_save == '-' || *p != NUL
5984 || tab_number > LAST_TAB_NR)
5985 {
5986 /* No numbers as argument. */
5987 eap->errmsg = e_invarg;
5988 goto theend;
5989 }
5990 }
5991 else
5992 {
5993 if (*p_save == NUL)
5994 tab_number = 1;
5995 else if (p == p_save || *p_save == '-' || *p != NUL
5996 || tab_number == 0)
5997 {
5998 /* No numbers as argument. */
5999 eap->errmsg = e_invarg;
6000 goto theend;
6001 }
6002 tab_number = tab_number * relative + tabpage_index(curtab);
6003 if (!unaccept_arg0 && relative == -1)
6004 --tab_number;
6005 }
6006 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6007 eap->errmsg = e_invarg;
6008 }
6009 else if (eap->addr_count > 0)
6010 {
6011 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006012 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006013 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006014 tab_number = 0;
6015 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006016 else
6017 {
6018 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006019 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006020 {
6021 --tab_number;
6022 if (tab_number < unaccept_arg0)
6023 eap->errmsg = e_invarg;
6024 }
6025 }
6026 }
6027 else
6028 {
6029 switch (eap->cmdidx)
6030 {
6031 case CMD_tabnext:
6032 tab_number = tabpage_index(curtab) + 1;
6033 if (tab_number > LAST_TAB_NR)
6034 tab_number = 1;
6035 break;
6036 case CMD_tabmove:
6037 tab_number = LAST_TAB_NR;
6038 break;
6039 default:
6040 tab_number = tabpage_index(curtab);
6041 }
6042 }
6043
6044theend:
6045 return tab_number;
6046}
6047
6048/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006049 * ":tabclose": close current tab page, unless it is the last one.
6050 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 */
6052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006053ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006055 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006056 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006057
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006058# ifdef FEAT_CMDWIN
6059 if (cmdwin_type != 0)
6060 cmdwin_result = K_IGNORE;
6061 else
6062# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006063 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006064 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006067 tab_number = get_tabpage_arg(eap);
6068 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006069 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006070 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006071 if (tp == NULL)
6072 {
6073 beep_flush();
6074 return;
6075 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006076 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006077 {
6078 tabpage_close_other(tp, eap->forceit);
6079 return;
6080 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006081 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006082 tabpage_close(eap->forceit);
6083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006085}
6086
6087/*
6088 * ":tabonly": close all tab pages except the current one
6089 */
6090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006091ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006092{
6093 tabpage_T *tp;
6094 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006095 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006096
6097# ifdef FEAT_CMDWIN
6098 if (cmdwin_type != 0)
6099 cmdwin_result = K_IGNORE;
6100 else
6101# endif
6102 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006103 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006104 else
6105 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006106 tab_number = get_tabpage_arg(eap);
6107 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006108 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006109 goto_tabpage(tab_number);
6110 /* Repeat this up to a 1000 times, because autocommands may
6111 * mess up the lists. */
6112 for (done = 0; done < 1000; ++done)
6113 {
6114 FOR_ALL_TABPAGES(tp)
6115 if (tp->tp_topframe != topframe)
6116 {
6117 tabpage_close_other(tp, eap->forceit);
6118 /* if we failed to close it quit */
6119 if (valid_tabpage(tp))
6120 done = 1000;
6121 /* start over, "tp" is now invalid */
6122 break;
6123 }
6124 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006125 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006126 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006127 }
6128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130
6131/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006132 * Close the current tab page.
6133 */
6134 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006135tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006136{
6137 /* First close all the windows but the current one. If that worked then
6138 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006139 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006140 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006141 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006142 ex_win_close(forceit, curwin, NULL);
6143# ifdef FEAT_GUI
6144 need_mouse_correct = TRUE;
6145# endif
6146}
6147
6148/*
6149 * Close tab page "tp", which is not the current tab page.
6150 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006151 * Also takes care of the tab pages line disappearing when closing the
6152 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006153 */
6154 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006155tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006156{
6157 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006158 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006159 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006160
6161 /* Limit to 1000 windows, autocommands may add a window while we close
6162 * one. OK, so I'm paranoid... */
6163 while (++done < 1000)
6164 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006165 wp = tp->tp_firstwin;
6166 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006167
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006168 /* Autocommands may delete the tab page under our fingers and we may
6169 * fail to close a window with a modified buffer. */
6170 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006171 break;
6172 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006173
Bram Moolenaar29323592016-07-24 22:04:11 +02006174 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006175
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006176 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006177 if (h != tabline_height())
6178 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006179}
6180
6181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 * ":only".
6183 */
6184 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006185ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006187 win_T *wp;
6188 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189# ifdef FEAT_GUI
6190 need_mouse_correct = TRUE;
6191# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006192 if (eap->addr_count > 0)
6193 {
6194 wnr = eap->line2;
6195 for (wp = firstwin; --wnr > 0; )
6196 {
6197 if (wp->w_next == NULL)
6198 break;
6199 else
6200 wp = wp->w_next;
6201 }
6202 win_goto(wp);
6203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 close_others(TRUE, eap->forceit);
6205}
6206
6207/*
6208 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006209 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006211 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006212ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
6214 if (eap->addr_count == 0)
6215 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006216 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
6219 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006220ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006222 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006223 if (!eap->skip)
6224 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006225#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006226 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006227#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006228 if (eap->addr_count == 0)
6229 win_close(curwin, FALSE); /* don't free buffer */
6230 else
6231 {
6232 int winnr = 0;
6233 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006234
Bram Moolenaar2256c992016-11-15 21:17:07 +01006235 FOR_ALL_WINDOWS(win)
6236 {
6237 winnr++;
6238 if (winnr == eap->line2)
6239 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006240 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006241 if (win == NULL)
6242 win = lastwin;
6243 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 }
6246}
6247
6248/*
6249 * ":stop" and ":suspend": Suspend Vim.
6250 */
6251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006252ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253{
6254 /*
6255 * Disallow suspending for "rvim".
6256 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006257 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 {
6259 if (!eap->forceit)
6260 autowrite_all();
6261 windgoto((int)Rows - 1, 0);
6262 out_char('\n');
6263 out_flush();
6264 stoptermcap();
6265 out_flush(); /* needed for SUN to restore xterm buffer */
6266#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006267 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268#endif
6269 ui_suspend(); /* call machine specific function */
6270#ifdef FEAT_TITLE
6271 maketitle();
6272 resettitle(); /* force updating the title */
6273#endif
6274 starttermcap();
6275 scroll_start(); /* scroll screen before redrawing */
6276 redraw_later_clear();
6277 shell_resized(); /* may have resized window */
6278 }
6279}
6280
6281/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006282 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 */
6284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006285ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
6287#ifdef FEAT_CMDWIN
6288 if (cmdwin_type != 0)
6289 {
6290 cmdwin_result = Ctrl_C;
6291 return;
6292 }
6293#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006294 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006295 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006296 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006297 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006298 return;
6299 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006300
6301 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006302 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303
6304 /*
6305 * if more files or windows we won't exit
6306 */
6307 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6308 exiting = TRUE;
6309 if ( ((eap->cmdidx == CMD_wq
6310 || curbufIsChanged())
6311 && do_write(eap) == FAIL)
6312 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006313 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 {
6315 not_exiting();
6316 }
6317 else
6318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006321 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322# ifdef FEAT_GUI
6323 need_mouse_correct = TRUE;
6324# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006325 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006326 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 }
6328}
6329
6330/*
6331 * ":print", ":list", ":number".
6332 */
6333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006334ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006336 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006337 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006338 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006340 for ( ;!got_int; ui_breakcheck())
6341 {
6342 print_line(eap->line1,
6343 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6344 || (eap->flags & EXFLAG_NR)),
6345 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6346 if (++eap->line1 > eap->line2)
6347 break;
6348 out_flush(); /* show one line at a time */
6349 }
6350 setpcmark();
6351 /* put cursor at last line */
6352 curwin->w_cursor.lnum = eap->line2;
6353 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 }
6355
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357}
6358
6359#ifdef FEAT_BYTEOFF
6360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006361ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 goto_byte(eap->line2);
6364}
6365#endif
6366
6367/*
6368 * ":shell".
6369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006371ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 do_shell(NULL, 0);
6374}
6375
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006376#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006378static int drop_busy = FALSE;
6379static int drop_filec;
6380static char_u **drop_filev = NULL;
6381static int drop_split;
6382static void (*drop_callback)(void *);
6383static void *drop_cookie;
6384
6385 static void
6386handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 exarg_T ea;
6389 int save_msg_scroll = msg_scroll;
6390
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006391 // Setting the argument list may cause screen updates and being called
6392 // recursively. Avoid that by setting drop_busy.
6393 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395 /* Check whether the current buffer is changed. If so, we will need
6396 * to split the current window or data could be lost.
6397 * We don't need to check if the 'hidden' option is set, as in this
6398 * case the buffer won't be lost.
6399 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006400 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
6402 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006403 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 --emsg_off;
6405 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006406 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 if (win_split(0, 0) == FAIL)
6409 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006410 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411
6412 /* When splitting the window, create a new alist. Otherwise the
6413 * existing one is overwritten. */
6414 alist_unlink(curwin->w_alist);
6415 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 }
6417
6418 /*
6419 * Set up the new argument list.
6420 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006421 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422
6423 /*
6424 * Move to the first file.
6425 */
6426 /* Fake up a minimal "next" command for do_argfile() */
6427 vim_memset(&ea, 0, sizeof(ea));
6428 ea.cmd = (char_u *)"next";
6429 do_argfile(&ea, 0);
6430
6431 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6432 * mode that is not needed here. */
6433 need_start_insertmode = FALSE;
6434
6435 /* Restore msg_scroll, otherwise a following command may cause scrolling
6436 * unexpectedly. The screen will be redrawn by the caller, thus
6437 * msg_scroll being set by displaying a message is irrelevant. */
6438 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006439
6440 if (drop_callback != NULL)
6441 drop_callback(drop_cookie);
6442
6443 drop_filev = NULL;
6444 drop_busy = FALSE;
6445}
6446
6447/*
6448 * Handle a file drop. The code is here because a drop is *nearly* like an
6449 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006450 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006451 * code is very similar to :args and hence needs access to a lot of the static
6452 * functions in this file.
6453 *
6454 * The "filev" list must have been allocated using alloc(), as should each item
6455 * in the list. This function takes over responsibility for freeing the "filev"
6456 * list.
6457 */
6458 void
6459handle_drop(
6460 int filec, // the number of files dropped
6461 char_u **filev, // the list of files dropped
6462 int split, // force splitting the window
6463 void (*callback)(void *), // to be called after setting the argument
6464 // list
6465 void *cookie) // argument for "callback" (allocated)
6466{
6467 // Cannot handle recursive drops, finish the pending one.
6468 if (drop_busy)
6469 {
6470 FreeWild(filec, filev);
6471 vim_free(cookie);
6472 return;
6473 }
6474
6475 // When calling handle_drop() more than once in a row we only use the last
6476 // one.
6477 if (drop_filev != NULL)
6478 {
6479 FreeWild(drop_filec, drop_filev);
6480 vim_free(drop_cookie);
6481 }
6482
6483 drop_filec = filec;
6484 drop_filev = filev;
6485 drop_split = split;
6486 drop_callback = callback;
6487 drop_cookie = cookie;
6488
6489 // Postpone this when:
6490 // - editing the command line
6491 // - not possible to change the current buffer
6492 // - updating the screen
6493 // As it may change buffers and window structures that are in use and cause
6494 // freed memory to be used.
6495 if (text_locked() || curbuf_locked() || updating_screen)
6496 return;
6497
6498 handle_drop_internal();
6499}
6500
6501/*
6502 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6503 * reset: Handle a postponed drop.
6504 */
6505 void
6506handle_any_postponed_drop(void)
6507{
6508 if (!drop_busy && drop_filev != NULL
6509 && !text_locked() && !curbuf_locked() && !updating_screen)
6510 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511}
6512#endif
6513
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514/*
6515 * Clear an argument list: free all file names and reset it to zero entries.
6516 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006518alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 while (--al->al_ga.ga_len >= 0)
6521 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6522 ga_clear(&al->al_ga);
6523}
6524
6525/*
6526 * Init an argument list.
6527 */
6528 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006529alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530{
6531 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6532}
6533
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534/*
6535 * Remove a reference from an argument list.
6536 * Ignored when the argument list is the global one.
6537 * If the argument list is no longer used by any window, free it.
6538 */
6539 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006540alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541{
6542 if (al != &global_alist && --al->al_refcount <= 0)
6543 {
6544 alist_clear(al);
6545 vim_free(al);
6546 }
6547}
6548
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549/*
6550 * Create a new argument list and use it for the current window.
6551 */
6552 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006553alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006555 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (curwin->w_alist == NULL)
6557 {
6558 curwin->w_alist = &global_alist;
6559 ++global_alist.al_refcount;
6560 }
6561 else
6562 {
6563 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006564 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 alist_init(curwin->w_alist);
6566 }
6567}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006569#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570/*
6571 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006572 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6573 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 */
6575 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006576alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006579 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 char_u **new_arg_files;
6581 int new_arg_file_count;
6582 char_u *save_p_su = p_su;
6583 int i;
6584
6585 /* Don't use 'suffixes' here. This should work like the shell did the
6586 * expansion. Also, the vimrc file isn't read yet, thus the user
6587 * can't set the options. */
6588 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006589 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 if (old_arg_files != NULL)
6591 {
6592 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006593 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6594 old_arg_count = GARGCOUNT;
6595 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006597 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 && new_arg_file_count > 0)
6599 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006600 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6601 TRUE, fnum_list, fnum_len);
6602 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 }
6605 p_su = save_p_su;
6606}
6607#endif
6608
6609/*
6610 * Set the argument list for the current window.
6611 * Takes over the allocated files[] and the allocated fnames in it.
6612 */
6613 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006614alist_set(
6615 alist_T *al,
6616 int count,
6617 char_u **files,
6618 int use_curbuf,
6619 int *fnum_list,
6620 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006623 static int recursive = 0;
6624
6625 if (recursive)
6626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006627 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006628 return;
6629 }
6630 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
6632 alist_clear(al);
6633 if (ga_grow(&al->al_ga, count) == OK)
6634 {
6635 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006636 {
6637 if (got_int)
6638 {
6639 /* When adding many buffers this can take a long time. Allow
6640 * interrupting here. */
6641 while (i < count)
6642 vim_free(files[i++]);
6643 break;
6644 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006645
6646 /* May set buffer name of a buffer previously used for the
6647 * argument list, so that it's re-used by alist_add. */
6648 if (fnum_list != NULL && i < fnum_len)
6649 buf_set_name(fnum_list[i], files[i]);
6650
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006652 ui_breakcheck();
6653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 vim_free(files);
6655 }
6656 else
6657 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006660
6661 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662}
6663
6664/*
6665 * Add file "fname" to argument list "al".
6666 * "fname" must have been allocated and "al" must have been checked for room.
6667 */
6668 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006669alist_add(
6670 alist_T *al,
6671 char_u *fname,
6672 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673{
6674 if (fname == NULL) /* don't add NULL file names */
6675 return;
6676#ifdef BACKSLASH_IN_FILENAME
6677 slash_adjust(fname);
6678#endif
6679 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6680 if (set_fnum > 0)
6681 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6682 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6683 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684}
6685
6686#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6687/*
6688 * Adjust slashes in file names. Called after 'shellslash' was set.
6689 */
6690 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006691alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006695 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697 for (i = 0; i < GARGCOUNT; ++i)
6698 if (GARGLIST[i].ae_fname != NULL)
6699 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006700 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 if (wp->w_alist != &global_alist)
6702 for (i = 0; i < WARGCOUNT(wp); ++i)
6703 if (WARGLIST(wp)[i].ae_fname != NULL)
6704 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705}
6706#endif
6707
6708/*
6709 * ":preserve".
6710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006712ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006714 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 ml_preserve(curbuf, TRUE);
6716}
6717
6718/*
6719 * ":recover".
6720 */
6721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006722ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
6724 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6725 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006726 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6727 | CCGD_MULTWIN
6728 | (eap->forceit ? CCGD_FORCEIT : 0)
6729 | CCGD_EXCMD)
6730
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 && (*eap->arg == NUL
6732 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006733 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 recoverymode = FALSE;
6735}
6736
6737/*
6738 * Command modifier used in a wrong way.
6739 */
6740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006741ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
6743 eap->errmsg = e_invcmd;
6744}
6745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746/*
6747 * :sview [+command] file split window with new file, read-only
6748 * :split [[+command] file] split window with current or new file
6749 * :vsplit [[+command] file] split window vertically with current or new file
6750 * :new [[+command] file] split window with no or new file
6751 * :vnew [[+command] file] split vertically window with no or new file
6752 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006753 *
6754 * :tabedit open new Tab page with empty window
6755 * :tabedit [+command] file open new Tab page and edit "file"
6756 * :tabnew [[+command] file] just like :tabedit
6757 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 */
6759 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006760ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006762 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006763#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006765#endif
6766#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006768#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006769 int use_tab = eap->cmdidx == CMD_tabedit
6770 || eap->cmdidx == CMD_tabfind
6771 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006773 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006774 return;
6775
Bram Moolenaar4033c552017-09-16 20:54:51 +02006776#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
Bram Moolenaar4033c552017-09-16 20:54:51 +02006780#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006782 * quickfix windows. But it's OK when doing ":tab split". */
6783 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 {
6785 if (eap->cmdidx == CMD_split)
6786 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 if (eap->cmdidx == CMD_vsplit)
6788 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791
Bram Moolenaar4033c552017-09-16 20:54:51 +02006792#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006793 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {
6795 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6796 FNAME_MESS, TRUE, curbuf->b_ffname);
6797 if (fname == NULL)
6798 goto theend;
6799 eap->arg = fname;
6800 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006801# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006802 else
6803# endif
6804#endif
6805#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 && eap->cmdidx != CMD_new)
6809 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006810 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006811# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006812 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006813# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006814 au_has_group((char_u *)"FileExplorer"))
6815 {
6816 /* No browsing supported but we do have the file explorer:
6817 * Edit the directory. */
6818 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6819 eap->arg = (char_u *)".";
6820 }
6821 else
6822 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006823 fname = do_browse(0, (char_u *)(use_tab
6824 ? _("Edit File in new tab page")
6825 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006827 if (fname == NULL)
6828 goto theend;
6829 eap->arg = fname;
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006835 /*
6836 * Either open new tab page or split the window.
6837 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006838 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006839 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006840 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6841 : eap->addr_count == 0 ? 0
6842 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006843 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006844 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006845
6846 /* set the alternate buffer for the window we came from */
6847 if (curwin != old_curwin
6848 && win_valid(old_curwin)
6849 && old_curwin->w_buffer != curbuf
6850 && !cmdmod.keepalt)
6851 old_curwin->w_alt_fnum = curbuf->b_fnum;
6852 }
6853 }
6854 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6856 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 /* Reset 'scrollbind' when editing another file, but keep it when
6858 * doing ":split" without arguments. */
6859 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006860# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006862# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006864 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 else
6866 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 do_exedit(eap, old_curwin);
6868 }
6869
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006870# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006874# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875theend:
6876 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879
6880/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006881 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006882 */
6883 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006884tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006885{
6886 exarg_T ea;
6887
6888 vim_memset(&ea, 0, sizeof(ea));
6889 ea.cmdidx = CMD_tabnew;
6890 ea.cmd = (char_u *)"tabn";
6891 ea.arg = (char_u *)"";
6892 ex_splitview(&ea);
6893}
6894
6895/*
6896 * :tabnext command
6897 */
6898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006899ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006900{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006901 int tab_number;
6902
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006903 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006904 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006905 switch (eap->cmdidx)
6906 {
6907 case CMD_tabfirst:
6908 case CMD_tabrewind:
6909 goto_tabpage(1);
6910 break;
6911 case CMD_tablast:
6912 goto_tabpage(9999);
6913 break;
6914 case CMD_tabprevious:
6915 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006916 if (eap->arg && *eap->arg != NUL)
6917 {
6918 char_u *p = eap->arg;
6919 char_u *p_save = p;
6920
6921 tab_number = getdigits(&p);
6922 if (p == p_save || *p_save == '-' || *p != NUL
6923 || tab_number == 0)
6924 {
6925 /* No numbers as argument. */
6926 eap->errmsg = e_invarg;
6927 return;
6928 }
6929 }
6930 else
6931 {
6932 if (eap->addr_count == 0)
6933 tab_number = 1;
6934 else
6935 {
6936 tab_number = eap->line2;
6937 if (tab_number < 1)
6938 {
6939 eap->errmsg = e_invrange;
6940 return;
6941 }
6942 }
6943 }
6944 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006945 break;
6946 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006947 tab_number = get_tabpage_arg(eap);
6948 if (eap->errmsg == NULL)
6949 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006950 break;
6951 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006952}
6953
6954/*
6955 * :tabmove command
6956 */
6957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006958ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006959{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006960 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006961
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006962 tab_number = get_tabpage_arg(eap);
6963 if (eap->errmsg == NULL)
6964 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006965}
6966
6967/*
6968 * :tabs command: List tabs and their contents.
6969 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006971ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006972{
6973 tabpage_T *tp;
6974 win_T *wp;
6975 int tabcount = 1;
6976
6977 msg_start();
6978 msg_scroll = TRUE;
6979 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6980 {
6981 msg_putchar('\n');
6982 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006983 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006984 out_flush(); /* output one line at a time */
6985 ui_breakcheck();
6986
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006987 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006988 wp = firstwin;
6989 else
6990 wp = tp->tp_firstwin;
6991 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6992 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006993 msg_putchar('\n');
6994 msg_putchar(wp == curwin ? '>' : ' ');
6995 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006996 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6997 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006998 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006999 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007000 else
7001 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7002 IObuff, IOSIZE, TRUE);
7003 msg_outtrans(IObuff);
7004 out_flush(); /* output one line at a time */
7005 ui_breakcheck();
7006 }
7007 }
7008}
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010/*
7011 * ":mode": Set screen mode.
7012 * If no argument given, just get the screen size and redraw.
7013 */
7014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007015ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016{
7017 if (*eap->arg == NUL)
7018 shell_resized();
7019 else
7020 mch_screenmode(eap->arg);
7021}
7022
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023/*
7024 * ":resize".
7025 * set, increment or decrement current window height
7026 */
7027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007028ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 int n;
7031 win_T *wp = curwin;
7032
7033 if (eap->addr_count > 0)
7034 {
7035 n = eap->line2;
7036 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7037 ;
7038 }
7039
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007040# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 if (cmdmod.split & WSP_VERT)
7045 {
7046 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007047 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7049 n = 9999;
7050 win_setwidth_win((int)n, wp);
7051 }
7052 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {
7054 if (*eap->arg == '-' || *eap->arg == '+')
7055 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007056 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 n = 9999;
7058 win_setheight_win((int)n, wp);
7059 }
7060}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061
7062/*
7063 * ":find [+command] <file>" command.
7064 */
7065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007066ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067{
7068#ifdef FEAT_SEARCHPATH
7069 char_u *fname;
7070 int count;
7071
7072 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7073 TRUE, curbuf->b_ffname);
7074 if (eap->addr_count > 0)
7075 {
7076 /* Repeat finding the file "count" times. This matters when it
7077 * appears several times in the path. */
7078 count = eap->line2;
7079 while (fname != NULL && --count > 0)
7080 {
7081 vim_free(fname);
7082 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7083 FALSE, curbuf->b_ffname);
7084 }
7085 }
7086
7087 if (fname != NULL)
7088 {
7089 eap->arg = fname;
7090#endif
7091 do_exedit(eap, NULL);
7092#ifdef FEAT_SEARCHPATH
7093 vim_free(fname);
7094 }
7095#endif
7096}
7097
7098/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007099 * ":open" simulation: for now just work like ":visual".
7100 */
7101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007102ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007103{
7104 regmatch_T regmatch;
7105 char_u *p;
7106
7107 curwin->w_cursor.lnum = eap->line2;
7108 beginline(BL_SOL | BL_FIX);
7109 if (*eap->arg == '/')
7110 {
7111 /* ":open /pattern/": put cursor in column found with pattern */
7112 ++eap->arg;
7113 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7114 *p = NUL;
7115 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7116 if (regmatch.regprog != NULL)
7117 {
7118 regmatch.rm_ic = p_ic;
7119 p = ml_get_curline();
7120 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007121 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007122 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007123 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007124 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007125 }
7126 /* Move to the NUL, ignore any other arguments. */
7127 eap->arg += STRLEN(eap->arg);
7128 }
7129 check_cursor();
7130
7131 eap->cmdidx = CMD_visual;
7132 do_exedit(eap, NULL);
7133}
7134
7135/*
7136 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 */
7138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007139ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
7141 do_exedit(eap, NULL);
7142}
7143
7144/*
7145 * ":edit <file>" command and alikes.
7146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007148do_exedit(
7149 exarg_T *eap,
7150 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007154 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007156 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007157 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 /*
7159 * ":vi" command ends Ex mode.
7160 */
7161 if (exmode_active && (eap->cmdidx == CMD_visual
7162 || eap->cmdidx == CMD_view))
7163 {
7164 exmode_active = FALSE;
7165 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007166 {
7167 /* Special case: ":global/pat/visual\NLvi-commands" */
7168 if (global_busy)
7169 {
7170 int rd = RedrawingDisabled;
7171 int nwr = no_wait_return;
7172 int ms = msg_scroll;
7173#ifdef FEAT_GUI
7174 int he = hold_gui_events;
7175#endif
7176
7177 if (eap->nextcmd != NULL)
7178 {
7179 stuffReadbuff(eap->nextcmd);
7180 eap->nextcmd = NULL;
7181 }
7182
7183 if (exmode_was != EXMODE_VIM)
7184 settmode(TMODE_RAW);
7185 RedrawingDisabled = 0;
7186 no_wait_return = 0;
7187 need_wait_return = FALSE;
7188 msg_scroll = 0;
7189#ifdef FEAT_GUI
7190 hold_gui_events = 0;
7191#endif
7192 must_redraw = CLEAR;
7193
7194 main_loop(FALSE, TRUE);
7195
7196 RedrawingDisabled = rd;
7197 no_wait_return = nwr;
7198 msg_scroll = ms;
7199#ifdef FEAT_GUI
7200 hold_gui_events = he;
7201#endif
7202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206
7207 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007208 || eap->cmdidx == CMD_tabnew
7209 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007210 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007212 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 setpcmark();
7214 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007215 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7216 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007218 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 || *eap->arg != NUL
7220#ifdef FEAT_BROWSE
7221 || cmdmod.browse
7222#endif
7223 )
7224 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007225 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7226 * can bring us here, others are stopped earlier. */
7227 if (*eap->arg != NUL && curbuf_locked())
7228 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 n = readonlymode;
7231 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7232 readonlymode = TRUE;
7233 else if (eap->cmdidx == CMD_enew)
7234 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7235 empty buffer */
7236 setpcmark();
7237 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7238 NULL, eap,
7239 /* ":edit" goes to first line if Vi compatible */
7240 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7241 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7242 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007243 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007245 /* after a split we can use an existing buffer */
7246 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007248 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 {
7250 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 if (old_curwin != NULL)
7252 {
7253 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007254 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007256#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007257 cleanup_T cs;
7258
7259 /* Reset the error/interrupt/exception state here so that
7260 * aborting() returns FALSE when closing a window. */
7261 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007262#endif
7263#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007265#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007266 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007267
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007268#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007269 /* Restore the error/interrupt/exception state if not
7270 * discarded by a new aborting error, interrupt, or
7271 * uncaught exception. */
7272 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 }
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277 else if (readonlymode && curbuf->b_nwindows == 1)
7278 {
7279 /* When editing an already visited buffer, 'readonly' won't be set
7280 * but the previous value is kept. With ":view" and ":sview" we
7281 * want the file to be readonly, except when another window is
7282 * editing the same buffer. */
7283 curbuf->b_p_ro = TRUE;
7284 }
7285 readonlymode = n;
7286 }
7287 else
7288 {
7289 if (eap->do_ecmd_cmd != NULL)
7290 do_cmdline_cmd(eap->do_ecmd_cmd);
7291#ifdef FEAT_TITLE
7292 n = curwin->w_arg_idx_invalid;
7293#endif
7294 check_arg_idx(curwin);
7295#ifdef FEAT_TITLE
7296 if (n != curwin->w_arg_idx_invalid)
7297 maketitle();
7298#endif
7299 }
7300
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 /*
7302 * if ":split file" worked, set alternate file name in old window to new
7303 * file
7304 */
7305 if (old_curwin != NULL
7306 && *eap->arg != NUL
7307 && curwin != old_curwin
7308 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007309 && old_curwin->w_buffer != curbuf
7310 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
7313 ex_no_reprint = TRUE;
7314}
7315
7316#ifndef FEAT_GUI
7317/*
7318 * ":gui" and ":gvim" when there is no GUI.
7319 */
7320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007321ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
7323 eap->errmsg = e_nogvim;
7324}
7325#endif
7326
Bram Moolenaar4f974752019-02-17 17:44:42 +01007327#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007329ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
7331 gui_make_tearoff(eap->arg);
7332}
7333#endif
7334
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007335#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7336 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007338ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007340# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7341 if (gui.in_use)
7342 gui_make_popup(eap->arg, eap->forceit);
7343# ifdef FEAT_TERM_POPUP_MENU
7344 else
7345# endif
7346# endif
7347# ifdef FEAT_TERM_POPUP_MENU
7348 pum_make_popup(eap->arg, eap->forceit);
7349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350}
7351#endif
7352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007354ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355{
7356 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007357 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007359 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360}
7361
7362/*
7363 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7364 * offset.
7365 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007368ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007371 win_T *save_curwin = curwin;
7372 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 long topline;
7374 long y;
7375 linenr_T old_linenr = curwin->w_cursor.lnum;
7376
7377 setpcmark();
7378
7379 /*
7380 * determine max topline
7381 */
7382 if (curwin->w_p_scb)
7383 {
7384 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007385 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 {
7387 if (wp->w_p_scb && wp->w_buffer)
7388 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007389 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 if (topline > y)
7391 topline = y;
7392 }
7393 }
7394 if (topline < 1)
7395 topline = 1;
7396 }
7397 else
7398 {
7399 topline = 1;
7400 }
7401
7402
7403 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007404 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007406 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 {
7408 if (curwin->w_p_scb)
7409 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007410 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 y = topline - curwin->w_topline;
7412 if (y > 0)
7413 scrollup(y, TRUE);
7414 else
7415 scrolldown(-y, TRUE);
7416 curwin->w_scbind_pos = topline;
7417 redraw_later(VALID);
7418 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 }
7421 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007422 curwin = save_curwin;
7423 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 if (curwin->w_p_scb)
7425 {
7426 did_syncbind = TRUE;
7427 checkpcmark();
7428 if (old_linenr != curwin->w_cursor.lnum)
7429 {
7430 char_u ctrl_o[2];
7431
7432 ctrl_o[0] = Ctrl_O;
7433 ctrl_o[1] = 0;
7434 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7435 }
7436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437}
7438
7439
7440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007441ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007443 int i;
7444 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7445 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
7447 if (eap->usefilter) /* :r!cmd */
7448 do_bang(1, eap, FALSE, FALSE, TRUE);
7449 else
7450 {
7451 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7452 return;
7453
7454#ifdef FEAT_BROWSE
7455 if (cmdmod.browse)
7456 {
7457 char_u *browseFile;
7458
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007459 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 NULL, NULL, NULL, curbuf);
7461 if (browseFile != NULL)
7462 {
7463 i = readfile(browseFile, NULL,
7464 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7465 vim_free(browseFile);
7466 }
7467 else
7468 i = OK;
7469 }
7470 else
7471#endif
7472 if (*eap->arg == NUL)
7473 {
7474 if (check_fname() == FAIL) /* check for no file name */
7475 return;
7476 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7477 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7478 }
7479 else
7480 {
7481 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7482 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7483 i = readfile(eap->arg, NULL,
7484 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7485
7486 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007487 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007489#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 if (!aborting())
7491#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007492 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 }
7494 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007495 {
7496 if (empty && exmode_active)
7497 {
7498 /* Delete the empty line that remains. Historically ex does
7499 * this but vi doesn't. */
7500 if (eap->line2 == 0)
7501 lnum = curbuf->b_ml.ml_line_count;
7502 else
7503 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007504 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007505 {
7506 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007507 if (curwin->w_cursor.lnum > 1
7508 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007509 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007510 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007511 }
7512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 }
7516}
7517
Bram Moolenaarea408852005-06-25 22:49:46 +00007518static char_u *prev_dir = NULL;
7519
7520#if defined(EXITFREE) || defined(PROTO)
7521 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007522free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007523{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007524 VIM_CLEAR(prev_dir);
7525 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007526}
7527#endif
7528
Bram Moolenaarf4258302013-06-02 18:20:17 +02007529/*
7530 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007531 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7532 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007533 */
7534 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007535post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007536{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007537 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007538 // Clear tab local directory for both :cd and :tcd
7539 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007540 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007541 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007542 {
7543 /* If still in global directory, need to remember current
7544 * directory as global directory. */
7545 if (globaldir == NULL && prev_dir != NULL)
7546 globaldir = vim_strsave(prev_dir);
7547 /* Remember this local directory for the window. */
7548 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007549 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007550 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007551 curtab->tp_localdir = vim_strsave(NameBuff);
7552 else
7553 curwin->w_localdir = vim_strsave(NameBuff);
7554 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007555 }
7556 else
7557 {
7558 /* We are now in the global directory, no need to remember its
7559 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007560 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007561 }
7562
7563 shorten_fnames(TRUE);
7564}
7565
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007566/*
7567 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7568 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7569 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7570 * Otherwise changes the global directory.
7571 * Returns TRUE if the directory is successfully changed.
7572 */
7573 int
7574changedir_func(
7575 char_u *new_dir,
7576 int forceit,
7577 cdscope_T scope)
7578{
7579 char_u *tofree;
7580 int dir_differs;
7581 int retval = FALSE;
7582
7583 if (allbuf_locked())
7584 return FALSE;
7585
7586 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7587 {
7588 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7589 return FALSE;
7590 }
7591
7592 // ":cd -": Change to previous directory
7593 if (STRCMP(new_dir, "-") == 0)
7594 {
7595 if (prev_dir == NULL)
7596 {
7597 emsg(_("E186: No previous directory"));
7598 return FALSE;
7599 }
7600 new_dir = prev_dir;
7601 }
7602
7603 // Save current directory for next ":cd -"
7604 tofree = prev_dir;
7605 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7606 prev_dir = vim_strsave(NameBuff);
7607 else
7608 prev_dir = NULL;
7609
7610#if defined(UNIX) || defined(VMS)
7611 // for UNIX ":cd" means: go to home directory
7612 if (*new_dir == NUL)
7613 {
7614 // use NameBuff for home directory name
7615# ifdef VMS
7616 char_u *p;
7617
7618 p = mch_getenv((char_u *)"SYS$LOGIN");
7619 if (p == NULL || *p == NUL) // empty is the same as not set
7620 NameBuff[0] = NUL;
7621 else
7622 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7623# else
7624 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7625# endif
7626 new_dir = NameBuff;
7627 }
7628#endif
7629 dir_differs = new_dir == NULL || prev_dir == NULL
7630 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7631 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7632 emsg(_(e_failed));
7633 else
7634 {
7635 char_u *acmd_fname;
7636
7637 post_chdir(scope);
7638
7639 if (dir_differs)
7640 {
7641 if (scope == CDSCOPE_WINDOW)
7642 acmd_fname = (char_u *)"window";
7643 else if (scope == CDSCOPE_TABPAGE)
7644 acmd_fname = (char_u *)"tabpage";
7645 else
7646 acmd_fname = (char_u *)"global";
7647 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7648 curbuf);
7649 }
7650 retval = TRUE;
7651 }
7652 vim_free(tofree);
7653
7654 return retval;
7655}
Bram Moolenaarea408852005-06-25 22:49:46 +00007656
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007658 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007660 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007661ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007663 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664
7665 new_dir = eap->arg;
7666#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007667 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 if (*new_dir == NUL)
7669 ex_pwd(NULL);
7670 else
7671#endif
7672 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007673 cdscope_T scope = CDSCOPE_GLOBAL;
7674
7675 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7676 scope = CDSCOPE_WINDOW;
7677 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7678 scope = CDSCOPE_TABPAGE;
7679
7680 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007681 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007682 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007683 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 ex_pwd(eap);
7685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 }
7687}
7688
7689/*
7690 * ":pwd".
7691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007693ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694{
7695 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7696 {
7697#ifdef BACKSLASH_IN_FILENAME
7698 slash_adjust(NameBuff);
7699#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007700 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 }
7702 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007703 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704}
7705
7706/*
7707 * ":=".
7708 */
7709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007710ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007712 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007713 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714}
7715
7716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007717ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007719 int n;
7720 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721
7722 if (cursor_valid())
7723 {
7724 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7725 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007726 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007728
7729 len = eap->line2;
7730 switch (*eap->arg)
7731 {
7732 case 'm': break;
7733 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007734 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007735 }
7736 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737}
7738
7739/*
7740 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7741 */
7742 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007743do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007746 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747
7748 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007749 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007750 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007752 wait_now = msec - done > 1000L ? 1000L : msec - done;
7753#ifdef FEAT_TIMERS
7754 {
7755 long due_time = check_due_timer();
7756
7757 if (due_time > 0 && due_time < wait_now)
7758 wait_now = due_time;
7759 }
7760#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007761#ifdef FEAT_JOB_CHANNEL
7762 if (has_any_channel() && wait_now > 100L)
7763 wait_now = 100L;
7764#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007765 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007766#ifdef FEAT_JOB_CHANNEL
7767 if (has_any_channel())
7768 ui_breakcheck_force(TRUE);
7769 else
7770#endif
7771 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007772#ifdef MESSAGE_QUEUE
7773 /* Process the netbeans and clientserver messages that may have been
7774 * received in the call to ui_breakcheck() when the GUI is in use. This
7775 * may occur when running a test case. */
7776 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007779
7780 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7781 // input buffer, otherwise a following call to input() fails.
7782 if (got_int)
7783 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784}
7785
7786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007787do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 int mode;
7790 char_u *cmdp;
7791
7792 cmdp = eap->cmd;
7793 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7794
7795 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7796 eap->arg, mode, isabbrev))
7797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007798 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007800 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 break;
7802 }
7803}
7804
7805/*
7806 * ":winsize" command (obsolete).
7807 */
7808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007809ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 int w, h;
7812 char_u *arg = eap->arg;
7813 char_u *p;
7814
7815 w = getdigits(&arg);
7816 arg = skipwhite(arg);
7817 p = arg;
7818 h = getdigits(&arg);
7819 if (*p != NUL && *arg == NUL)
7820 set_shellsize(w, h, TRUE);
7821 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007822 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823}
7824
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007826ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 int xchar = NUL;
7829 char_u *p;
7830
7831 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7832 {
7833 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7834 if (eap->arg[1] == NUL)
7835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007836 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 return;
7838 }
7839 xchar = eap->arg[1];
7840 p = eap->arg + 2;
7841 }
7842 else
7843 p = eap->arg + 1;
7844
7845 eap->nextcmd = check_nextcmd(p);
7846 p = skipwhite(p);
7847 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007848 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007849 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {
7851 /* Pass flags on for ":vertical wincmd ]". */
7852 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007853 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7855 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007856 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 }
7858}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859
Bram Moolenaar843ee412004-06-30 16:16:41 +00007860#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861/*
7862 * ":winpos".
7863 */
7864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007865ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866{
7867 int x, y;
7868 char_u *arg = eap->arg;
7869 char_u *p;
7870
7871 if (*arg == NUL)
7872 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007873# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007874# ifdef VIMDLL
7875 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7876 mch_get_winpos(&x, &y) != FAIL)
7877# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007879# else
7880 if (mch_get_winpos(&x, &y) != FAIL)
7881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {
7883 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007884 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 }
7886 else
7887# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007888 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 }
7890 else
7891 {
7892 x = getdigits(&arg);
7893 arg = skipwhite(arg);
7894 p = arg;
7895 y = getdigits(&arg);
7896 if (*p == NUL || *arg != NUL)
7897 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007898 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 return;
7900 }
7901# ifdef FEAT_GUI
7902 if (gui.in_use)
7903 gui_mch_set_winpos(x, y);
7904 else if (gui.starting)
7905 {
7906 /* Remember the coordinates for when the window is opened. */
7907 gui_win_x = x;
7908 gui_win_y = y;
7909 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007910# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 else
7912# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007913# endif
7914# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007915 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916# endif
7917# ifdef HAVE_TGETENT
7918 if (*T_CWP)
7919 term_set_winpos(x, y);
7920# endif
7921 }
7922}
7923#endif
7924
7925/*
7926 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7927 */
7928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007929ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
7931 oparg_T oa;
7932
7933 clear_oparg(&oa);
7934 oa.regname = eap->regname;
7935 oa.start.lnum = eap->line1;
7936 oa.end.lnum = eap->line2;
7937 oa.line_count = eap->line2 - eap->line1 + 1;
7938 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7941 {
7942 setpcmark();
7943 curwin->w_cursor.lnum = eap->line1;
7944 beginline(BL_SOL | BL_FIX);
7945 }
7946
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007947 if (VIsual_active)
7948 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 switch (eap->cmdidx)
7951 {
7952 case CMD_delete:
7953 oa.op_type = OP_DELETE;
7954 op_delete(&oa);
7955 break;
7956
7957 case CMD_yank:
7958 oa.op_type = OP_YANK;
7959 (void)op_yank(&oa, FALSE, TRUE);
7960 break;
7961
7962 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007963 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007965 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7966#else
7967 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007969 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 oa.op_type = OP_RSHIFT;
7971 else
7972 oa.op_type = OP_LSHIFT;
7973 op_shift(&oa, FALSE, eap->amount);
7974 break;
7975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007977 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978}
7979
7980/*
7981 * ":put".
7982 */
7983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007984ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 /* ":0put" works like ":1put!". */
7987 if (eap->line2 == 0)
7988 {
7989 eap->line2 = 1;
7990 eap->forceit = TRUE;
7991 }
7992 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007993 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7994 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995}
7996
7997/*
7998 * Handle ":copy" and ":move".
7999 */
8000 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008001ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002{
8003 long n;
8004
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008005 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if (eap->arg == NULL) /* error detected */
8007 {
8008 eap->nextcmd = NULL;
8009 return;
8010 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008011 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
8013 /*
8014 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8015 */
8016 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8017 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008018 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 return;
8020 }
8021
8022 if (eap->cmdidx == CMD_move)
8023 {
8024 if (do_move(eap->line1, eap->line2, n) == FAIL)
8025 return;
8026 }
8027 else
8028 ex_copy(eap->line1, eap->line2, n);
8029 u_clearline();
8030 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008031 ex_may_print(eap);
8032}
8033
8034/*
8035 * Print the current line if flags were given to the Ex command.
8036 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008038ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008039{
8040 if (eap->flags != 0)
8041 {
8042 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8043 (eap->flags & EXFLAG_LIST));
8044 ex_no_reprint = TRUE;
8045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046}
8047
8048/*
8049 * ":smagic" and ":snomagic".
8050 */
8051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 int magic_save = p_magic;
8055
8056 p_magic = (eap->cmdidx == CMD_smagic);
8057 do_sub(eap);
8058 p_magic = magic_save;
8059}
8060
8061/*
8062 * ":join".
8063 */
8064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008065ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066{
8067 curwin->w_cursor.lnum = eap->line1;
8068 if (eap->line1 == eap->line2)
8069 {
8070 if (eap->addr_count >= 2) /* :2,2join does nothing */
8071 return;
8072 if (eap->line2 == curbuf->b_ml.ml_line_count)
8073 {
8074 beep_flush();
8075 return;
8076 }
8077 ++eap->line2;
8078 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008079 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008081 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082}
8083
8084/*
8085 * ":[addr]@r" or ":[addr]*r": execute register
8086 */
8087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008088ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089{
8090 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008091 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092
8093 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008094 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095
8096#ifdef USE_ON_FLY_SCROLL
8097 dont_scroll = TRUE; /* disallow scrolling here */
8098#endif
8099
8100 /* get the register name. No name means to use the previous one */
8101 c = *eap->arg;
8102 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8103 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008104 /* Put the register in the typeahead buffer with the "silent" flag. */
8105 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8106 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008107 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 else
8111 {
8112 int save_efr = exec_from_reg;
8113
8114 exec_from_reg = TRUE;
8115
8116 /*
8117 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008118 * Continue until the stuff buffer is empty and all added characters
8119 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008121 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8123
8124 exec_from_reg = save_efr;
8125 }
8126}
8127
8128/*
8129 * ":!".
8130 */
8131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008132ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133{
8134 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8135}
8136
8137/*
8138 * ":undo".
8139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008141ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008143 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008144 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008145 else
8146 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147}
8148
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008149#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008151ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008152{
8153 char_u hash[UNDO_HASH_SIZE];
8154
8155 u_compute_hash(hash);
8156 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8157}
8158
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008160ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008161{
8162 char_u hash[UNDO_HASH_SIZE];
8163
8164 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008165 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008166}
8167#endif
8168
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169/*
8170 * ":redo".
8171 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008173ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
8175 u_redo(1);
8176}
8177
8178/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008179 * ":earlier" and ":later".
8180 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008182ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008183{
8184 long count = 0;
8185 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008186 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008187 char_u *p = eap->arg;
8188
8189 if (*p == NUL)
8190 count = 1;
8191 else if (isdigit(*p))
8192 {
8193 count = getdigits(&p);
8194 switch (*p)
8195 {
8196 case 's': ++p; sec = TRUE; break;
8197 case 'm': ++p; sec = TRUE; count *= 60; break;
8198 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008199 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8200 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008201 }
8202 }
8203
8204 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008205 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008206 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008207 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8208 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008209}
8210
8211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 * ":redir": start/stop redirection.
8213 */
8214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008215ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216{
8217 char *mode;
8218 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008219 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220
Bram Moolenaarba768492016-07-08 15:32:54 +02008221#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008222 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008224 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008225 return;
8226 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008227#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008228
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 if (STRICMP(eap->arg, "END") == 0)
8230 close_redir();
8231 else
8232 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008233 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008235 ++arg;
8236 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008238 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 mode = "a";
8240 }
8241 else
8242 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008243 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244
8245 close_redir();
8246
8247 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008248 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (fname == NULL)
8250 return;
8251#ifdef FEAT_BROWSE
8252 if (cmdmod.browse)
8253 {
8254 char_u *browseFile;
8255
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008256 browseFile = do_browse(BROWSE_SAVE,
8257 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008258 fname, NULL, NULL,
8259 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 if (browseFile == NULL)
8261 return; /* operation cancelled */
8262 vim_free(fname);
8263 fname = browseFile;
8264 eap->forceit = TRUE; /* since dialog already asked */
8265 }
8266#endif
8267
8268 redir_fd = open_exfile(fname, eap->forceit, mode);
8269 vim_free(fname);
8270 }
8271#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008272 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {
8274 /* redirect to a register a-z (resp. A-Z for appending) */
8275 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008276 ++arg;
8277 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008279 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008280 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008282 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008284 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008285 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008286 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008287 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008289 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008290 if (*arg == '>')
8291 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008292 /* Make register empty when not using @A-@Z and the
8293 * command is valid. */
8294 if (*arg == NUL && !isupper(redir_reg))
8295 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008297 }
8298 if (*arg != NUL)
8299 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008300 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008301 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008302 }
8303 }
8304 else if (*arg == '=' && arg[1] == '>')
8305 {
8306 int append;
8307
8308 /* redirect to a variable */
8309 close_redir();
8310 arg += 2;
8311
8312 if (*arg == '>')
8313 {
8314 ++arg;
8315 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 }
8317 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008318 append = FALSE;
8319
8320 if (var_redir_start(skipwhite(arg), append) == OK)
8321 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 }
8323#endif
8324
8325 /* TODO: redirect to a buffer */
8326
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008328 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008330
8331 /* Make sure redirection is not off. Can happen for cmdline completion
8332 * that indirectly invokes a command to catch its output. */
8333 if (redir_fd != NULL
8334#ifdef FEAT_EVAL
8335 || redir_reg || redir_vname
8336#endif
8337 )
8338 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339}
8340
8341/*
8342 * ":redraw": force redraw
8343 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008344 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008345ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 int r = RedrawingDisabled;
8348 int p = p_lz;
8349
8350 RedrawingDisabled = 0;
8351 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008352 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008354 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355#ifdef FEAT_TITLE
8356 if (need_maketitle)
8357 maketitle();
8358#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008359#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8360# ifdef VIMDLL
8361 if (!gui.in_use)
8362# endif
8363 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 RedrawingDisabled = r;
8366 p_lz = p;
8367
8368 /* Reset msg_didout, so that a message that's there is overwritten. */
8369 msg_didout = FALSE;
8370 msg_col = 0;
8371
Bram Moolenaar56667a52013-07-17 11:54:28 +02008372 /* No need to wait after an intentional redraw. */
8373 need_wait_return = FALSE;
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 out_flush();
8376}
8377
8378/*
8379 * ":redrawstatus": force redraw of status line(s)
8380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008382ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 int r = RedrawingDisabled;
8385 int p = p_lz;
8386
8387 RedrawingDisabled = 0;
8388 p_lz = FALSE;
8389 if (eap->forceit)
8390 status_redraw_all();
8391 else
8392 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008393 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 RedrawingDisabled = r;
8395 p_lz = p;
8396 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397}
8398
Bram Moolenaare12bab32019-01-08 22:02:56 +01008399/*
8400 * ":redrawtabline": force redraw of the tabline
8401 */
8402 static void
8403ex_redrawtabline(exarg_T *eap UNUSED)
8404{
8405 int r = RedrawingDisabled;
8406 int p = p_lz;
8407
8408 RedrawingDisabled = 0;
8409 p_lz = FALSE;
8410
8411 draw_tabline();
8412
8413 RedrawingDisabled = r;
8414 p_lz = p;
8415 out_flush();
8416}
8417
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008419close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421 if (redir_fd != NULL)
8422 {
8423 fclose(redir_fd);
8424 redir_fd = NULL;
8425 }
8426#ifdef FEAT_EVAL
8427 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008428 if (redir_vname)
8429 {
8430 var_redir_stop();
8431 redir_vname = 0;
8432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433#endif
8434}
8435
Bram Moolenaardf177f62005-02-22 08:39:57 +00008436#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8437 || defined(PROTO)
8438 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008439vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008440{
8441 if (vim_mkdir(name, prot) != 0)
8442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008443 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008444 return FAIL;
8445 }
8446 return OK;
8447}
8448#endif
8449
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450/*
8451 * Open a file for writing for an Ex command, with some checks.
8452 * Return file descriptor, or NULL on failure.
8453 */
8454 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008455open_exfile(
8456 char_u *fname,
8457 int forceit,
8458 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
8460 FILE *fd;
8461
8462#ifdef UNIX
8463 /* with Unix it is possible to open a directory */
8464 if (mch_isdir(fname))
8465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008466 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 return NULL;
8468 }
8469#endif
8470 if (!forceit && *mode != 'a' && vim_fexists(fname))
8471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008472 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 return NULL;
8474 }
8475
8476 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008477 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478
8479 return fd;
8480}
8481
8482/*
8483 * ":mark" and ":k".
8484 */
8485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008486ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
8488 pos_T pos;
8489
8490 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008491 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008493 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 else
8495 {
8496 pos = curwin->w_cursor; /* save curwin->w_cursor */
8497 curwin->w_cursor.lnum = eap->line2;
8498 beginline(BL_WHITE | BL_FIX);
8499 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008500 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8502 }
8503}
8504
8505/*
8506 * Update w_topline, w_leftcol and the cursor position.
8507 */
8508 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008509update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510{
8511 check_cursor(); /* put cursor on valid line */
8512 update_topline();
8513 if (!curwin->w_p_wrap)
8514 validate_cursor();
8515 update_curswant();
8516}
8517
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008519 * Save the current State and go to Normal mode.
8520 * Return TRUE if the typeahead could be saved.
8521 */
8522 int
8523save_current_state(save_state_T *sst)
8524{
8525 sst->save_msg_scroll = msg_scroll;
8526 sst->save_restart_edit = restart_edit;
8527 sst->save_msg_didout = msg_didout;
8528 sst->save_State = State;
8529 sst->save_insertmode = p_im;
8530 sst->save_finish_op = finish_op;
8531 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008532 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008533
8534 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8535 restart_edit = 0; /* don't go to Insert mode */
8536 p_im = FALSE; /* don't use 'insertmode' */
8537
8538 /*
8539 * Save the current typeahead. This is required to allow using ":normal"
8540 * from an event handler and makes sure we don't hang when the argument
8541 * ends with half a command.
8542 */
8543 save_typeahead(&sst->tabuf);
8544 return sst->tabuf.typebuf_valid;
8545}
8546
8547 void
8548restore_current_state(save_state_T *sst)
8549{
8550 /* Restore the previous typeahead. */
8551 restore_typeahead(&sst->tabuf);
8552
8553 msg_scroll = sst->save_msg_scroll;
8554 restart_edit = sst->save_restart_edit;
8555 p_im = sst->save_insertmode;
8556 finish_op = sst->save_finish_op;
8557 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008558 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008559 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8560
8561 /* Restore the state (needed when called from a function executed for
8562 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8563 State = sst->save_State;
8564#ifdef CURSOR_SHAPE
8565 ui_cursor_shape(); /* may show different cursor shape */
8566#endif
8567}
8568
8569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 * ":normal[!] {commands}": Execute normal mode commands.
8571 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008572 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008573ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008575 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 char_u *arg = NULL;
8577 int l;
8578 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008580 if (ex_normal_lock > 0)
8581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008582 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008583 return;
8584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 if (ex_normal_busy >= p_mmd)
8586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008587 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 return;
8589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 /*
8592 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8593 * this for the K_SPECIAL leading byte, otherwise special keys will not
8594 * work.
8595 */
8596 if (has_mbyte)
8597 {
8598 int len = 0;
8599
8600 /* Count the number of characters to be escaped. */
8601 for (p = eap->arg; *p != NUL; ++p)
8602 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008603#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 if (*p == CSI) /* leadbyte CSI */
8605 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008606#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008607 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008609#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 )
8613 len += 2;
8614 }
8615 if (len > 0)
8616 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008617 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 if (arg != NULL)
8619 {
8620 len = 0;
8621 for (p = eap->arg; *p != NUL; ++p)
8622 {
8623 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008624#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 if (*p == CSI)
8626 {
8627 arg[len++] = KS_EXTRA;
8628 arg[len++] = (int)KE_CSI;
8629 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008630#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008631 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 {
8633 arg[len++] = *++p;
8634 if (*p == K_SPECIAL)
8635 {
8636 arg[len++] = KS_SPECIAL;
8637 arg[len++] = KE_FILLER;
8638 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008639#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 else if (*p == CSI)
8641 {
8642 arg[len++] = KS_EXTRA;
8643 arg[len++] = (int)KE_CSI;
8644 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 }
8647 arg[len] = NUL;
8648 }
8649 }
8650 }
8651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008653 ++ex_normal_busy;
8654 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 {
8656 /*
8657 * Repeat the :normal command for each line in the range. When no
8658 * range given, execute it just once, without positioning the cursor
8659 * first.
8660 */
8661 do
8662 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 if (eap->addr_count != 0)
8664 {
8665 curwin->w_cursor.lnum = eap->line1++;
8666 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008667 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 }
8669
Bram Moolenaar13505972019-01-24 15:04:48 +01008670 exec_normal_cmd(arg != NULL
8671 ? arg
8672 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 }
8674 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8675 }
8676
8677 /* Might not return to the main loop when in an event handler. */
8678 update_topline_cursor();
8679
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008680 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008682#ifdef FEAT_MOUSE
8683 setmouse();
8684#endif
8685#ifdef CURSOR_SHAPE
8686 ui_cursor_shape(); /* may show different cursor shape */
8687#endif
8688
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690}
8691
8692/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008693 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 */
8695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008696ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008698 if (eap->forceit)
8699 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008700 /* cursor line can be zero on startup */
8701 if (!curwin->w_cursor.lnum)
8702 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008703 coladvance((colnr_T)MAXCOL);
8704 curwin->w_curswant = MAXCOL;
8705 curwin->w_set_curswant = FALSE;
8706 }
8707
Bram Moolenaara40c5002005-01-09 21:16:21 +00008708 /* Ignore the command when already in Insert mode. Inserting an
8709 * expression register that invokes a function can do this. */
8710 if (State & INSERT)
8711 return;
8712
Bram Moolenaar64969662005-12-14 21:59:55 +00008713 if (eap->cmdidx == CMD_startinsert)
8714 restart_edit = 'a';
8715 else if (eap->cmdidx == CMD_startreplace)
8716 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008718 restart_edit = 'V';
8719
8720 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008722 if (eap->cmdidx == CMD_startinsert)
8723 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 curwin->w_curswant = 0; /* avoid MAXCOL */
8725 }
8726}
8727
8728/*
8729 * ":stopinsert"
8730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008732ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734 restart_edit = 0;
8735 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008736 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008739/*
8740 * Execute normal mode command "cmd".
8741 * "remap" can be REMAP_NONE or REMAP_YES.
8742 */
8743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008744exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008745{
Bram Moolenaar25281632016-01-21 23:32:32 +01008746 /* Stuff the argument into the typeahead buffer. */
8747 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008748 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008749}
Bram Moolenaar25281632016-01-21 23:32:32 +01008750
Bram Moolenaar25281632016-01-21 23:32:32 +01008751/*
8752 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008753 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008754 */
8755 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008756exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008757{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008758 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008759 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008760
Bram Moolenaar905dd902019-04-07 14:21:47 +02008761 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8762 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008763 clear_oparg(&oa);
8764 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008765 while ((!stuff_empty()
8766 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008767 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008768 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008769 {
8770 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008771#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008772 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008773 && oa.op_type == OP_NOP && oa.regname == NUL
8774 && !VIsual_active)
8775 {
8776 /* If terminal_loop() returns OK we got a key that is handled
8777 * in Normal model. With FAIL we first need to position the
8778 * cursor and the screen needs to be redrawn. */
8779 if (terminal_loop(TRUE) == OK)
8780 normal_cmd(&oa, TRUE);
8781 }
8782 else
8783#endif
8784 /* execute a Normal mode cmd */
8785 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008786 }
8787}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008788
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789#ifdef FEAT_FIND_ID
8790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008791ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
8793 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8794 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8795 (linenr_T)1, (linenr_T)MAXLNUM);
8796}
8797
Bram Moolenaar4033c552017-09-16 20:54:51 +02008798#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799/*
8800 * ":psearch"
8801 */
8802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008803ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 g_do_tagpreview = p_pvh;
8806 ex_findpat(eap);
8807 g_do_tagpreview = 0;
8808}
8809#endif
8810
8811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008812ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
8814 int whole = TRUE;
8815 long n;
8816 char_u *p;
8817 int action;
8818
8819 switch (cmdnames[eap->cmdidx].cmd_name[2])
8820 {
8821 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8822 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8823 action = ACTION_GOTO;
8824 else
8825 action = ACTION_SHOW;
8826 break;
8827 case 'i': /* ":ilist" and ":dlist" */
8828 action = ACTION_SHOW_ALL;
8829 break;
8830 case 'u': /* ":ijump" and ":djump" */
8831 action = ACTION_GOTO;
8832 break;
8833 default: /* ":isplit" and ":dsplit" */
8834 action = ACTION_SPLIT;
8835 break;
8836 }
8837
8838 n = 1;
8839 if (vim_isdigit(*eap->arg)) /* get count */
8840 {
8841 n = getdigits(&eap->arg);
8842 eap->arg = skipwhite(eap->arg);
8843 }
8844 if (*eap->arg == '/') /* Match regexp, not just whole words */
8845 {
8846 whole = FALSE;
8847 ++eap->arg;
8848 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8849 if (*p)
8850 {
8851 *p++ = NUL;
8852 p = skipwhite(p);
8853
8854 /* Check for trailing illegal characters */
8855 if (!ends_excmd(*p))
8856 eap->errmsg = e_trailing;
8857 else
8858 eap->nextcmd = check_nextcmd(p);
8859 }
8860 }
8861 if (!eap->skip)
8862 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8863 whole, !eap->forceit,
8864 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8865 n, action, eap->line1, eap->line2);
8866}
8867#endif
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869
Bram Moolenaar4033c552017-09-16 20:54:51 +02008870#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871/*
8872 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8873 */
8874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008875ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008877 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8879}
8880
8881/*
8882 * ":pedit"
8883 */
8884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008885ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886{
8887 win_T *curwin_save = curwin;
8888
8889 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00008890 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02008891 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 do_exedit(eap, NULL);
8893 keep_help_flag = FALSE;
8894 if (curwin != curwin_save && win_valid(curwin_save))
8895 {
8896 /* Return cursor to where we were */
8897 validate_cursor();
8898 redraw_later(VALID);
8899 win_enter(curwin_save, TRUE);
8900 }
8901 g_do_tagpreview = 0;
8902}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
8905/*
8906 * ":stag", ":stselect" and ":stjump".
8907 */
8908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008909ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910{
8911 postponed_split = -1;
8912 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008913 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8915 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008916 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918
8919/*
8920 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8921 */
8922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008923ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8926}
8927
8928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008929ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930{
8931 int cmd;
8932
8933 switch (name[1])
8934 {
8935 case 'j': cmd = DT_JUMP; /* ":tjump" */
8936 break;
8937 case 's': cmd = DT_SELECT; /* ":tselect" */
8938 break;
8939 case 'p': cmd = DT_PREV; /* ":tprevious" */
8940 break;
8941 case 'N': cmd = DT_PREV; /* ":tNext" */
8942 break;
8943 case 'n': cmd = DT_NEXT; /* ":tnext" */
8944 break;
8945 case 'o': cmd = DT_POP; /* ":pop" */
8946 break;
8947 case 'f': /* ":tfirst" */
8948 case 'r': cmd = DT_FIRST; /* ":trewind" */
8949 break;
8950 case 'l': cmd = DT_LAST; /* ":tlast" */
8951 break;
8952 default: /* ":tag" */
8953#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008954 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008956 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 return;
8958 }
8959#endif
8960 cmd = DT_TAG;
8961 break;
8962 }
8963
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008964 if (name[0] == 'l')
8965 {
8966#ifndef FEAT_QUICKFIX
8967 ex_ni(eap);
8968 return;
8969#else
8970 cmd = DT_LTAG;
8971#endif
8972 }
8973
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8975 eap->forceit, TRUE);
8976}
8977
8978/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008979 * Check "str" for starting with a special cmdline variable.
8980 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8981 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8982 */
8983 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008984find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008985{
8986 int len;
8987 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008988 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008989 "%",
8990#define SPEC_PERC 0
8991 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008992#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008993 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008994#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008995 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008996#define SPEC_CCWORD (SPEC_CWORD + 1)
8997 "<cexpr>", /* expr under cursor */
8998#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008999 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009000#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009001 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009002#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009003 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009004#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009005 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009006#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009007 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009008#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009009 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009010#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009011 "<sflnum>", /* script file line number */
9012#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009013#ifdef FEAT_CLIENTSERVER
9014 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009015# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009016#endif
9017 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009018
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009019 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009020 {
9021 len = (int)STRLEN(spec_str[i]);
9022 if (STRNCMP(src, spec_str[i], len) == 0)
9023 {
9024 *usedlen = len;
9025 return i;
9026 }
9027 }
9028 return -1;
9029}
9030
9031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 * Evaluate cmdline variables.
9033 *
9034 * change '%' to curbuf->b_ffname
9035 * '#' to curwin->w_altfile
9036 * '<cword>' to word under the cursor
9037 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02009038 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 * '<cfile>' to path name under the cursor
9040 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009041 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 * '<afile>' to file name for autocommand
9043 * '<abuf>' to buffer number for autocommand
9044 * '<amatch>' to matching name for autocommand
9045 *
9046 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9047 * "" for error without a message) and NULL is returned.
9048 * Returns an allocated string if a valid match was found.
9049 * Returns NULL if no match was found. "usedlen" then still contains the
9050 * number of characters to skip.
9051 */
9052 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009053eval_vars(
9054 char_u *src, /* pointer into commandline */
9055 char_u *srcstart, /* beginning of valid memory for src */
9056 int *usedlen, /* characters after src that are used */
9057 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009058 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009059 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009060 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061{
9062 int i;
9063 char_u *s;
9064 char_u *result;
9065 char_u *resultbuf = NULL;
9066 int resultlen;
9067 buf_T *buf;
9068 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9069 int spec_idx;
9070#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009071 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 int skip_mod = FALSE;
9073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075
9076 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009077 if (escaped != NULL)
9078 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
9080 /*
9081 * Check if there is something to do.
9082 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009083 spec_idx = find_cmdline_var(src, usedlen);
9084 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 {
9086 *usedlen = 1;
9087 return NULL;
9088 }
9089
9090 /*
9091 * Skip when preceded with a backslash "\%" and "\#".
9092 * Note: In "\\%" the % is also not recognized!
9093 */
9094 if (src > srcstart && src[-1] == '\\')
9095 {
9096 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009097 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 return NULL;
9099 }
9100
9101 /*
9102 * word or WORD under cursor
9103 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009104 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9105 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009107 resultlen = find_ident_under_cursor(&result,
9108 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9109 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9110 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 if (resultlen == 0)
9112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009113 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 return NULL;
9115 }
9116 }
9117
9118 /*
9119 * '#': Alternate file name
9120 * '%': Current file name
9121 * File name under the cursor
9122 * File name for autocommand
9123 * and following modifiers
9124 */
9125 else
9126 {
9127 switch (spec_idx)
9128 {
9129 case SPEC_PERC: /* '%': current file */
9130 if (curbuf->b_fname == NULL)
9131 {
9132 result = (char_u *)"";
9133 valid = 0; /* Must have ":p:h" to be valid */
9134 }
9135 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009138#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009139 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009140#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 break;
9143
9144 case SPEC_HASH: /* '#' or "#99": alternate file */
9145 if (src[1] == '#') /* "##": the argument list */
9146 {
9147 result = arg_all();
9148 resultbuf = result;
9149 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009150 if (escaped != NULL)
9151 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#ifdef FEAT_MODIFY_FNAME
9153 skip_mod = TRUE;
9154#endif
9155 break;
9156 }
9157 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009158 if (*s == '<') /* "#<99" uses v:oldfiles */
9159 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009161 if (s == src + 2 && src[1] == '-')
9162 /* just a minus sign, don't skip over it */
9163 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 *usedlen = (int)(s - src); /* length of what we expand */
9165
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009166 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009168 if (*usedlen < 2)
9169 {
9170 /* Should we give an error message for #<text? */
9171 *usedlen = 1;
9172 return NULL;
9173 }
9174#ifdef FEAT_EVAL
9175 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9176 (long)i);
9177 if (result == NULL)
9178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009179 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009180 return NULL;
9181 }
9182#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009183 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 }
9187 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009188 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009189 if (i == 0 && src[1] == '<' && *usedlen > 1)
9190 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009191 buf = buflist_findnr(i);
9192 if (buf == NULL)
9193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009194 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009195 return NULL;
9196 }
9197 if (lnump != NULL)
9198 *lnump = ECMD_LAST;
9199 if (buf->b_fname == NULL)
9200 {
9201 result = (char_u *)"";
9202 valid = 0; /* Must have ":p:h" to be valid */
9203 }
9204 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009205 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009206 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009207#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009208 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009209#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009210 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 break;
9213
9214#ifdef FEAT_SEARCHPATH
9215 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009216 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 if (result == NULL)
9218 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009219 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 return NULL;
9221 }
9222 resultbuf = result; /* remember allocated string */
9223 break;
9224#endif
9225
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 case SPEC_AFILE: /* file name for autocommand */
9227 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009228 if (result != NULL && !autocmd_fname_full)
9229 {
9230 /* Still need to turn the fname into a full path. It is
9231 * postponed to avoid a delay when <afile> is not used. */
9232 autocmd_fname_full = TRUE;
9233 result = FullName_save(autocmd_fname, FALSE);
9234 vim_free(autocmd_fname);
9235 autocmd_fname = result;
9236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 if (result == NULL)
9238 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009239 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 return NULL;
9241 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009242 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 break;
9244
9245 case SPEC_ABUF: /* buffer number for autocommand */
9246 if (autocmd_bufnr <= 0)
9247 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009248 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 return NULL;
9250 }
9251 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9252 result = strbuf;
9253 break;
9254
9255 case SPEC_AMATCH: /* match name for autocommand */
9256 result = autocmd_match;
9257 if (result == NULL)
9258 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009259 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 return NULL;
9261 }
9262 break;
9263
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264 case SPEC_SFILE: /* file name for ":so" command */
9265 result = sourcing_name;
9266 if (result == NULL)
9267 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009268 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 return NULL;
9270 }
9271 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009272
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009273 case SPEC_SLNUM: /* line in file for ":so" command */
9274 if (sourcing_name == NULL || sourcing_lnum == 0)
9275 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009276 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009277 return NULL;
9278 }
9279 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9280 result = strbuf;
9281 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009282
9283#ifdef FEAT_EVAL
9284 case SPEC_SFLNUM: /* line in script file */
9285 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009287 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009288 return NULL;
9289 }
9290 sprintf((char *)strbuf, "%ld",
9291 (long)(current_sctx.sc_lnum + sourcing_lnum));
9292 result = strbuf;
9293 break;
9294#endif
9295
9296#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009298 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9299 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 result = strbuf;
9301 break;
9302#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009303
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009304 default:
9305 result = (char_u *)""; /* avoid gcc warning */
9306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 }
9308
9309 resultlen = (int)STRLEN(result); /* length of new string */
9310 if (src[*usedlen] == '<') /* remove the file name extension */
9311 {
9312 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 resultlen = (int)(s - result);
9315 }
9316#ifdef FEAT_MODIFY_FNAME
9317 else if (!skip_mod)
9318 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009319 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 &resultlen);
9321 if (result == NULL)
9322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009323 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 return NULL;
9325 }
9326 }
9327#endif
9328 }
9329
9330 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9331 {
9332 if (valid != VALID_HEAD + VALID_PATH)
9333 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009334 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009336 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337 result = NULL;
9338 }
9339 else
9340 result = vim_strnsave(result, resultlen);
9341 vim_free(resultbuf);
9342 return result;
9343}
9344
9345/*
9346 * Concatenate all files in the argument list, separated by spaces, and return
9347 * it in one allocated string.
9348 * Spaces and backslashes in the file names are escaped with a backslash.
9349 * Returns NULL when out of memory.
9350 */
9351 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009352arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353{
9354 int len;
9355 int idx;
9356 char_u *retval = NULL;
9357 char_u *p;
9358
9359 /*
9360 * Do this loop two times:
9361 * first time: compute the total length
9362 * second time: concatenate the names
9363 */
9364 for (;;)
9365 {
9366 len = 0;
9367 for (idx = 0; idx < ARGCOUNT; ++idx)
9368 {
9369 p = alist_name(&ARGLIST[idx]);
9370 if (p != NULL)
9371 {
9372 if (len > 0)
9373 {
9374 /* insert a space in between names */
9375 if (retval != NULL)
9376 retval[len] = ' ';
9377 ++len;
9378 }
9379 for ( ; *p != NUL; ++p)
9380 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009381 if (*p == ' '
9382#ifndef BACKSLASH_IN_FILENAME
9383 || *p == '\\'
9384#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009385 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 {
9387 /* insert a backslash */
9388 if (retval != NULL)
9389 retval[len] = '\\';
9390 ++len;
9391 }
9392 if (retval != NULL)
9393 retval[len] = *p;
9394 ++len;
9395 }
9396 }
9397 }
9398
9399 /* second time: break here */
9400 if (retval != NULL)
9401 {
9402 retval[len] = NUL;
9403 break;
9404 }
9405
9406 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009407 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 if (retval == NULL)
9409 break;
9410 }
9411
9412 return retval;
9413}
9414
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415/*
9416 * Expand the <sfile> string in "arg".
9417 *
9418 * Returns an allocated string, or NULL for any error.
9419 */
9420 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009421expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009423 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 int len;
9425 char_u *result;
9426 char_u *newres;
9427 char_u *repl;
9428 int srclen;
9429 char_u *p;
9430
9431 result = vim_strsave(arg);
9432 if (result == NULL)
9433 return NULL;
9434
9435 for (p = result; *p; )
9436 {
9437 if (STRNCMP(p, "<sfile>", 7) != 0)
9438 ++p;
9439 else
9440 {
9441 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009442 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009443 if (errormsg != NULL)
9444 {
9445 if (*errormsg)
9446 emsg(errormsg);
9447 vim_free(result);
9448 return NULL;
9449 }
9450 if (repl == NULL) /* no match (cannot happen) */
9451 {
9452 p += srclen;
9453 continue;
9454 }
9455 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9456 newres = alloc(len);
9457 if (newres == NULL)
9458 {
9459 vim_free(repl);
9460 vim_free(result);
9461 return NULL;
9462 }
9463 mch_memmove(newres, result, (size_t)(p - result));
9464 STRCPY(newres + (p - result), repl);
9465 len = (int)STRLEN(newres);
9466 STRCAT(newres, p + srclen);
9467 vim_free(repl);
9468 vim_free(result);
9469 result = newres;
9470 p = newres + len; /* continue after the match */
9471 }
9472 }
9473
9474 return result;
9475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009478/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009479 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009480 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009483dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485 if (fname == NULL)
9486 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009487 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488}
9489#endif
9490
9491/*
9492 * ":behave {mswin,xterm}"
9493 */
9494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009495ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496{
9497 if (STRCMP(eap->arg, "mswin") == 0)
9498 {
9499 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9500 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9501 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9502 set_option_value((char_u *)"keymodel", 0L,
9503 (char_u *)"startsel,stopsel", 0);
9504 }
9505 else if (STRCMP(eap->arg, "xterm") == 0)
9506 {
9507 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9508 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9509 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9510 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9511 }
9512 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009513 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009514}
9515
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009516#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9517/*
9518 * Function given to ExpandGeneric() to obtain the possible arguments of the
9519 * ":behave {mswin,xterm}" command.
9520 */
9521 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009522get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009523{
9524 if (idx == 0)
9525 return (char_u *)"mswin";
9526 if (idx == 1)
9527 return (char_u *)"xterm";
9528 return NULL;
9529}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02009530
9531/*
9532 * Function given to ExpandGeneric() to obtain the possible arguments of the
9533 * ":messages {clear}" command.
9534 */
9535 char_u *
9536get_messages_arg(expand_T *xp UNUSED, int idx)
9537{
9538 if (idx == 0)
9539 return (char_u *)"clear";
9540 return NULL;
9541}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009542#endif
9543
Bram Moolenaar113e1072019-01-20 15:30:40 +01009544#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009545 char_u *
9546get_mapclear_arg(expand_T *xp UNUSED, int idx)
9547{
9548 if (idx == 0)
9549 return (char_u *)"<buffer>";
9550 return NULL;
9551}
Bram Moolenaar113e1072019-01-20 15:30:40 +01009552#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009553
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554static int filetype_detect = FALSE;
9555static int filetype_plugin = FALSE;
9556static int filetype_indent = FALSE;
9557
9558/*
9559 * ":filetype [plugin] [indent] {on,off,detect}"
9560 * on: Load the filetype.vim file to install autocommands for file types.
9561 * off: Load the ftoff.vim file to remove all autocommands for file types.
9562 * plugin on: load filetype.vim and ftplugin.vim
9563 * plugin off: load ftplugof.vim
9564 * indent on: load filetype.vim and indent.vim
9565 * indent off: load indoff.vim
9566 */
9567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009568ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
9570 char_u *arg = eap->arg;
9571 int plugin = FALSE;
9572 int indent = FALSE;
9573
9574 if (*eap->arg == NUL)
9575 {
9576 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009577 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 filetype_detect ? "ON" : "OFF",
9579 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9580 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9581 return;
9582 }
9583
9584 /* Accept "plugin" and "indent" in any order. */
9585 for (;;)
9586 {
9587 if (STRNCMP(arg, "plugin", 6) == 0)
9588 {
9589 plugin = TRUE;
9590 arg = skipwhite(arg + 6);
9591 continue;
9592 }
9593 if (STRNCMP(arg, "indent", 6) == 0)
9594 {
9595 indent = TRUE;
9596 arg = skipwhite(arg + 6);
9597 continue;
9598 }
9599 break;
9600 }
9601 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9602 {
9603 if (*arg == 'o' || !filetype_detect)
9604 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009605 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 filetype_detect = TRUE;
9607 if (plugin)
9608 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009609 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 filetype_plugin = TRUE;
9611 }
9612 if (indent)
9613 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009614 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 filetype_indent = TRUE;
9616 }
9617 }
9618 if (*arg == 'd')
9619 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009620 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009621 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 }
9623 }
9624 else if (STRCMP(arg, "off") == 0)
9625 {
9626 if (plugin || indent)
9627 {
9628 if (plugin)
9629 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009630 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631 filetype_plugin = FALSE;
9632 }
9633 if (indent)
9634 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009635 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636 filetype_indent = FALSE;
9637 }
9638 }
9639 else
9640 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009641 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 filetype_detect = FALSE;
9643 }
9644 }
9645 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009646 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647}
9648
9649/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009650 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 */
9652 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009653ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
9655 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009656 {
9657 char_u *arg = eap->arg;
9658
9659 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9660 arg += 9;
9661
9662 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9663 if (arg != eap->arg)
9664 did_filetype = FALSE;
9665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009669ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
9671#ifdef FEAT_DIGRAPHS
9672 if (*eap->arg != NUL)
9673 putdigraph(eap->arg);
9674 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009675 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009677 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678#endif
9679}
9680
9681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009682ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683{
9684 int flags = 0;
9685
9686 if (eap->cmdidx == CMD_setlocal)
9687 flags = OPT_LOCAL;
9688 else if (eap->cmdidx == CMD_setglobal)
9689 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009690#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 if (cmdmod.browse && flags == 0)
9692 ex_options(eap);
9693 else
9694#endif
9695 (void)do_set(eap->arg, flags);
9696}
9697
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009698#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9699 void
9700set_no_hlsearch(int flag)
9701{
9702 no_hlsearch = flag;
9703# ifdef FEAT_EVAL
9704 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9705# endif
9706}
9707
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708/*
9709 * ":nohlsearch"
9710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009712ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009714 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009715 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717#endif
9718
9719#ifdef FEAT_CRYPT
9720/*
9721 * ":X": Get crypt key
9722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009724ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009726 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009727 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728}
9729#endif
9730
9731#ifdef FEAT_FOLDING
9732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009733ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 if (foldManualAllowed(TRUE))
9736 foldCreate(eap->line1, eap->line2);
9737}
9738
9739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009740ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
9742 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9743 eap->forceit, FALSE);
9744}
9745
9746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009747ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748{
9749 linenr_T lnum;
9750
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009751#ifdef FEAT_CLIPBOARD
9752 start_global_changes();
9753#endif
9754
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 /* First set the marks for all lines closed/open. */
9756 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9757 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9758 ml_setmarked(lnum);
9759
9760 /* Execute the command on the marked lines. */
9761 global_exe(eap->arg);
9762 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009763#ifdef FEAT_CLIPBOARD
9764 end_global_changes();
9765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766}
9767#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009768
Bram Moolenaar39665952018-08-15 20:59:48 +02009769#ifdef FEAT_QUICKFIX
9770/*
9771 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9772 * instead of a quickfix command.
9773 */
9774 int
9775is_loclist_cmd(int cmdidx)
9776{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009777 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009778 return FALSE;
9779 return cmdnames[cmdidx].cmd_name[0] == 'l';
9780}
9781#endif
9782
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009783# if defined(FEAT_TIMERS) || defined(PROTO)
9784 int
9785get_pressedreturn(void)
9786{
9787 return ex_pressedreturn;
9788}
9789
9790 void
9791set_pressedreturn(int val)
9792{
9793 ex_pressedreturn = val;
9794}
9795#endif