blob: c6e00bec51bd7f0def8ffc37b6160db69e944566 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010023static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026static int if_level = 0; /* depth in :if */
27#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020028static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void append_command(char_u *cmd);
30static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void ex_abbreviate(exarg_T *eap);
33static void ex_map(exarg_T *eap);
34static void ex_unmap(exarg_T *eap);
35static void ex_mapclear(exarg_T *eap);
36static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifndef FEAT_MENU
38# define ex_emenu ex_ni
39# define ex_menu ex_ni
40# define ex_menutranslate ex_ni
41#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static void ex_autocmd(exarg_T *eap);
43static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static void ex_bunload(exarg_T *eap);
45static void ex_buffer(exarg_T *eap);
46static void ex_bmodified(exarg_T *eap);
47static void ex_bnext(exarg_T *eap);
48static void ex_bprevious(exarg_T *eap);
49static void ex_brewind(exarg_T *eap);
50static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static char_u *getargcmd(char_u **);
52static char_u *skip_cmd_arg(char_u *p, int rembs);
53static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#ifndef FEAT_QUICKFIX
55# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000056# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_cc ex_ni
58# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020059# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cfile ex_ni
61# define qf_list ex_ni
62# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020063# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000065# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020067#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068# define ex_cclose ex_ni
69# define ex_copen ex_ni
70# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020071# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000073#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
74# define ex_cexpr ex_ni
75#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaarb7316892019-05-01 18:08:42 +020077static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020079#if !defined(FEAT_PERL) \
80 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
81 || !defined(FEAT_TCL) \
82 || !defined(FEAT_RUBY) \
83 || !defined(FEAT_LUA) \
84 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000085# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010088static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000090#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000092#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
94static void ex_highlight(exarg_T *eap);
95static void ex_colorscheme(exarg_T *eap);
96static void ex_quit(exarg_T *eap);
97static void ex_cquit(exarg_T *eap);
98static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010099static void ex_close(exarg_T *eap);
100static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
101static void ex_only(exarg_T *eap);
102static void ex_resize(exarg_T *eap);
103static void ex_stag(exarg_T *eap);
104static void ex_tabclose(exarg_T *eap);
105static void ex_tabonly(exarg_T *eap);
106static void ex_tabnext(exarg_T *eap);
107static void ex_tabmove(exarg_T *eap);
108static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200109#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_pclose(exarg_T *eap);
111static void ex_ptag(exarg_T *eap);
112static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#else
114# define ex_pclose ex_ni
115# define ex_ptag ex_ni
116# define ex_pedit ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_hide(exarg_T *eap);
119static void ex_stop(exarg_T *eap);
120static void ex_exit(exarg_T *eap);
121static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#else
125# define ex_goto ex_ni
126#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_shell(exarg_T *eap);
128static void ex_preserve(exarg_T *eap);
129static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_mode(exarg_T *eap);
131static void ex_wrongmodifier(exarg_T *eap);
132static void ex_find(exarg_T *eap);
133static void ex_open(exarg_T *eap);
134static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifndef FEAT_GUI
136# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100139#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#else
142# define ex_tearoff ex_ni
143#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100144#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
145 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100146static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#else
148# define ex_popup ex_ni
149#endif
150#ifndef FEAT_GUI_MSWIN
151# define ex_simalt ex_ni
152#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000153#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154# define gui_mch_find_dialog ex_ni
155# define gui_mch_replace_dialog ex_ni
156#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000157#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# define ex_helpfind ex_ni
159#endif
160#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100161# define ex_cscope ex_ni
162# define ex_scscope ex_ni
163# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165#ifndef FEAT_SYN_HL
166# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200167# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000168#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100169#ifndef FEAT_EVAL
170# define ex_packadd ex_ni
171# define ex_packloadall ex_ni
172#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200173#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200174# define ex_syntime ex_ni
175#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000176#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000177# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000178# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000179# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000180# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000181# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000182#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200183#ifndef FEAT_PERSISTENT_UNDO
184# define ex_rundo ex_ni
185# define ex_wundo ex_ni
186#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200187#ifndef FEAT_LUA
188# define ex_lua ex_script_ni
189# define ex_luado ex_ni
190# define ex_luafile ex_ni
191#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000192#ifndef FEAT_MZSCHEME
193# define ex_mzscheme ex_script_ni
194# define ex_mzfile ex_ni
195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#ifndef FEAT_PERL
197# define ex_perl ex_script_ni
198# define ex_perldo ex_ni
199#endif
200#ifndef FEAT_PYTHON
201# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200202# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203# define ex_pyfile ex_ni
204#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200205#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200206# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200207# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200208# define ex_py3file ex_ni
209#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100210#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
211# define ex_pyx ex_script_ni
212# define ex_pyxdo ex_ni
213# define ex_pyxfile ex_ni
214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#ifndef FEAT_TCL
216# define ex_tcl ex_script_ni
217# define ex_tcldo ex_ni
218# define ex_tclfile ex_ni
219#endif
220#ifndef FEAT_RUBY
221# define ex_ruby ex_script_ni
222# define ex_rubydo ex_ni
223# define ex_rubyfile ex_ni
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifndef FEAT_KEYMAP
226# define ex_loadkeymap ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_swapname(exarg_T *eap);
229static void ex_syncbind(exarg_T *eap);
230static void ex_read(exarg_T *eap);
231static void ex_pwd(exarg_T *eap);
232static void ex_equal(exarg_T *eap);
233static void ex_sleep(exarg_T *eap);
234static void do_exmap(exarg_T *eap, int isabbrev);
235static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000237#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#else
240# define ex_winpos ex_ni
241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_operators(exarg_T *eap);
243static void ex_put(exarg_T *eap);
244static void ex_copymove(exarg_T *eap);
245static void ex_submagic(exarg_T *eap);
246static void ex_join(exarg_T *eap);
247static void ex_at(exarg_T *eap);
248static void ex_bang(exarg_T *eap);
249static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200250#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_wundo(exarg_T *eap);
252static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200253#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ex_redo(exarg_T *eap);
255static void ex_later(exarg_T *eap);
256static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100258static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void close_redir(void);
260static void ex_mkrc(exarg_T *eap);
261static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_startinsert(exarg_T *eap);
263static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static void ex_checkpath(exarg_T *eap);
266static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#else
268# define ex_findpat ex_ni
269# define ex_checkpath ex_ni
270#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200271#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100272static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#else
274# define ex_psearch ex_ni
275#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100276static void ex_tag(exarg_T *eap);
277static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifndef FEAT_EVAL
279# define ex_scriptnames ex_ni
280# define ex_finish ex_ni
281# define ex_echo ex_ni
282# define ex_echohl ex_ni
283# define ex_execute ex_ni
284# define ex_call ex_ni
285# define ex_if ex_ni
286# define ex_endif ex_ni
287# define ex_else ex_ni
288# define ex_while ex_ni
289# define ex_continue ex_ni
290# define ex_break ex_ni
291# define ex_endwhile ex_ni
292# define ex_throw ex_ni
293# define ex_try ex_ni
294# define ex_catch ex_ni
295# define ex_finally ex_ni
296# define ex_endtry ex_ni
297# define ex_endfunction ex_ni
298# define ex_let ex_ni
299# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000300# define ex_lockvar ex_ni
301# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# define ex_function ex_ni
303# define ex_delfunction ex_ni
304# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000305# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100307static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static int makeopens(FILE *fd, char_u *dirnow);
310static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
311static void ex_loadview(exarg_T *eap);
312static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000313static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#else
315# define ex_loadview ex_ni
316#endif
317#ifndef FEAT_EVAL
318# define ex_compiler ex_ni
319#endif
320#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#else
323# define ex_viminfo ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static void ex_filetype(exarg_T *eap);
327static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000329# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330# define ex_diffpatch ex_ni
331# define ex_diffgetput ex_ni
332# define ex_diffsplit ex_ni
333# define ex_diffthis ex_ni
334# define ex_diffupdate ex_ni
335#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_digraphs(exarg_T *eap);
337static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100338#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339# define ex_options ex_ni
340#endif
341#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_nohlsearch(exarg_T *eap);
343static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_nohlsearch ex_ni
346# define ex_match ex_ni
347#endif
348#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100349static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_X ex_ni
352#endif
353#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100354static void ex_fold(exarg_T *eap);
355static void ex_foldopen(exarg_T *eap);
356static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#else
358# define ex_fold ex_ni
359# define ex_foldopen ex_ni
360# define ex_folddo ex_ni
361#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100362#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363# define ex_language ex_ni
364#endif
365#ifndef FEAT_SIGNS
366# define ex_sign ex_ni
367#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000368#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200369# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000370# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200371# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifndef FEAT_EVAL
375# define ex_debug ex_ni
376# define ex_breakadd ex_ni
377# define ex_debuggreedy ex_ni
378# define ex_breakdel ex_ni
379# define ex_breaklist ex_ni
380#endif
381
382#ifndef FEAT_CMDHIST
383# define ex_history ex_ni
384#endif
385#ifndef FEAT_JUMPLIST
386# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200387# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# define ex_changes ex_ni
389#endif
390
Bram Moolenaar05159a02005-02-26 23:04:13 +0000391#ifndef FEAT_PROFILE
392# define ex_profile ex_ni
393#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200394#ifndef FEAT_TERMINAL
395# define ex_terminal ex_ni
396#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200397#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
398# define ex_xrestore ex_ni
399#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401/*
402 * Declare cmdnames[].
403 */
404#define DO_DECLARE_EXCMD
405#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200406#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408static char_u dollar_command[2] = {'$', 0};
409
410
411#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000412/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413typedef struct
414{
415 char_u *line; /* command line */
416 linenr_T lnum; /* sourcing_lnum of the line */
417} wcmd_T;
418
419/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000420 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000422 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000424struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425{
426 garray_T *lines_gap; /* growarray with line info */
427 int current_line; /* last read line from growarray */
428 int repeating; /* TRUE when looping a second time */
429 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100430 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 void *cookie;
432};
433
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100434static char_u *get_loop_line(int c, void *cookie, int indent);
435static int store_loop_line(garray_T *gap, char_u *line);
436static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000437
438/* Struct to save a few things while debugging. Used in do_cmdline() only. */
439struct dbg_stuff
440{
441 int trylevel;
442 int force_abort;
443 except_T *caught_stack;
444 char_u *vv_exception;
445 char_u *vv_throwpoint;
446 int did_emsg;
447 int got_int;
448 int did_throw;
449 int need_rethrow;
450 int check_cstack;
451 except_T *current_exception;
452};
453
Bram Moolenaared203462004-06-16 11:19:22 +0000454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100455save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000456{
457 dsp->trylevel = trylevel; trylevel = 0;
458 dsp->force_abort = force_abort; force_abort = FALSE;
459 dsp->caught_stack = caught_stack; caught_stack = NULL;
460 dsp->vv_exception = v_exception(NULL);
461 dsp->vv_throwpoint = v_throwpoint(NULL);
462
463 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
464 dsp->did_emsg = did_emsg; did_emsg = FALSE;
465 dsp->got_int = got_int; got_int = FALSE;
466 dsp->did_throw = did_throw; did_throw = FALSE;
467 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
468 dsp->check_cstack = check_cstack; check_cstack = FALSE;
469 dsp->current_exception = current_exception; current_exception = NULL;
470}
471
472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100473restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000474{
475 suppress_errthrow = FALSE;
476 trylevel = dsp->trylevel;
477 force_abort = dsp->force_abort;
478 caught_stack = dsp->caught_stack;
479 (void)v_exception(dsp->vv_exception);
480 (void)v_throwpoint(dsp->vv_throwpoint);
481 did_emsg = dsp->did_emsg;
482 got_int = dsp->got_int;
483 did_throw = dsp->did_throw;
484 need_rethrow = dsp->need_rethrow;
485 check_cstack = dsp->check_cstack;
486 current_exception = dsp->current_exception;
487}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488#endif
489
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490/*
491 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
492 * command is given.
493 */
494 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100495do_exmode(
496 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497{
498 int save_msg_scroll;
499 int prev_msg_row;
500 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100501 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000502
503 if (improved)
504 exmode_active = EXMODE_VIM;
505 else
506 exmode_active = EXMODE_NORMAL;
507 State = NORMAL;
508
509 /* When using ":global /pat/ visual" and then "Q" we return to continue
510 * the :global command. */
511 if (global_busy)
512 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513
514 save_msg_scroll = msg_scroll;
515 ++RedrawingDisabled; /* don't redisplay the window */
516 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517#ifdef FEAT_GUI
518 /* Ignore scrollbar and mouse events in Ex mode */
519 ++hold_gui_events;
520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521
Bram Moolenaar32526b32019-01-19 17:43:09 +0100522 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 while (exmode_active)
524 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000525 /* Check for a ":normal" command and no more characters left. */
526 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
527 {
528 exmode_active = FALSE;
529 break;
530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 msg_scroll = TRUE;
532 need_wait_return = FALSE;
533 ex_pressedreturn = FALSE;
534 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100535 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 prev_msg_row = msg_row;
537 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 if (improved)
539 {
540 cmdline_row = msg_row;
541 do_cmdline(NULL, getexline, NULL, 0);
542 }
543 else
544 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
545 lines_left = Rows - 1;
546
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100548 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 if (ex_pressedreturn)
555 {
556 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000557 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000558 msg_row = prev_msg_row;
559 if (prev_msg_row == Rows - 1)
560 msg_row--;
561 }
562 msg_col = 0;
563 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
564 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000567 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
568 {
569 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100570 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000571 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100572 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 }
575
576#ifdef FEAT_GUI
577 --hold_gui_events;
578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 --RedrawingDisabled;
580 --no_wait_return;
581 update_screen(CLEAR);
582 need_wait_return = FALSE;
583 msg_scroll = save_msg_scroll;
584}
585
586/*
587 * Execute a simple command line. Used for translated commands like "*".
588 */
589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100590do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 return do_cmdline(cmd, NULL, NULL,
593 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
594}
595
596/*
597 * do_cmdline(): execute one Ex command line
598 *
599 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100600 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 * 2. Split up in parts separated with '|'.
602 *
603 * This function can be called recursively!
604 *
605 * flags:
606 * DOCMD_VERBOSE - The command will be included in the error message.
607 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100608 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * DOCMD_KEYTYPED - Don't reset KeyTyped.
610 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
611 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
612 *
613 * return FAIL if cmdline could not be executed, OK otherwise
614 */
615 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100616do_cmdline(
617 char_u *cmdline,
618 char_u *(*fgetline)(int, void *, int),
619 void *cookie, /* argument for fgetline() */
620 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
622 char_u *next_cmdline; /* next cmd to execute */
623 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100624 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 static int recursive = 0; /* recursive depth */
626 int msg_didout_before_start = 0;
627 int count = 0; /* line number count */
628 int did_inc = FALSE; /* incremented RedrawingDisabled */
629 int retval = OK;
630#ifdef FEAT_EVAL
631 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000632 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 int current_line = 0; /* active line in lines_ga */
634 char_u *fname = NULL; /* function or script name */
635 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
636 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000637 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int initial_trylevel;
639 struct msglist **saved_msg_list = NULL;
640 struct msglist *private_msg_list;
641
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100642 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100643 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000645 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000647 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650# define cmd_cookie cookie
651#endif
652 static int call_depth = 0; /* recursiveness */
653
654#ifdef FEAT_EVAL
655 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
656 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000657 * This ensures that the do_errthrow() call in do_one_cmd() does not
658 * combine the messages stored by an earlier invocation of do_one_cmd()
659 * with the command name of the later one. This would happen when
660 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 saved_msg_list = msg_list;
662 msg_list = &private_msg_list;
663 private_msg_list = NULL;
664#endif
665
666 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100667 * here. The value of 200 allows nested function calls, ":source", etc.
668 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100669 if (call_depth >= 200
670#ifdef FEAT_EVAL
671 && call_depth >= p_mfd
672#endif
673 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#ifdef FEAT_EVAL
677 /* When converting to an exception, we do not include the command name
678 * since this is not an error of the specific command. */
679 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
680 msg_list = saved_msg_list;
681#endif
682 return FAIL;
683 }
684 ++call_depth;
685
686#ifdef FEAT_EVAL
687 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000688 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 cstack.cs_trylevel = 0;
690 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
693
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000698 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 ++ex_nesting_level;
700
701 /* Get the function or script name and the address where the next breakpoint
702 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 fname = func_name(real_cookie);
706 breakpoint = func_breakpoint(real_cookie);
707 dbg_tick = func_dbg_tick(real_cookie);
708 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100709 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
711 fname = sourcing_name;
712 breakpoint = source_breakpoint(real_cookie);
713 dbg_tick = source_dbg_tick(real_cookie);
714 }
715
716 /*
717 * Initialize "force_abort" and "suppress_errthrow" at the top level.
718 */
719 if (!recursive)
720 {
721 force_abort = FALSE;
722 suppress_errthrow = FALSE;
723 }
724
725 /*
726 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000727 * exception handling (used when debugging). Otherwise clear it to avoid
728 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000730 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000731 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000732 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200733 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
735 initial_trylevel = trylevel;
736
737 /*
738 * "did_throw" will be set to TRUE when an exception is being thrown.
739 */
740 did_throw = FALSE;
741#endif
742 /*
743 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000744 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * If force_abort is set, we cancel everything.
746 */
747 did_emsg = FALSE;
748
749 /*
750 * KeyTyped is only set when calling vgetc(). Reset it here when not
751 * calling vgetc() (sourced command lines).
752 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 if (!(flags & DOCMD_KEYTYPED)
754 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 KeyTyped = FALSE;
756
757 /*
758 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000759 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * - for multiple commands on one line, separated with '|'
761 * - when repeating until there are no more lines (for ":source")
762 */
763 next_cmdline = cmdline;
764 do
765 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100767 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#endif
769
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000770 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (next_cmdline == NULL
772#ifdef FEAT_EVAL
773 && !force_abort
774 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#endif
777 )
778 did_emsg = FALSE;
779
780 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * 3. If a line is given: Make a copy, so we can mess with it.
784 */
785
786#ifdef FEAT_EVAL
787 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
790 /* Each '|' separated command is stored separately in lines_ga, to
791 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100792 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 /* Check if a function has returned or, unless it has an unclosed
795 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000799 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 func_line_end(real_cookie);
801# endif
802 if (func_has_ended(real_cookie))
803 {
804 retval = FAIL;
805 break;
806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000809 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 script_line_end();
812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
814 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
817 retval = FAIL;
818 break;
819 }
820
821 /* If breakpoints have been added/deleted need to check for it. */
822 if (breakpoint != NULL && dbg_tick != NULL
823 && *dbg_tick != debug_tick)
824 {
825 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 fname, sourcing_lnum);
828 *dbg_tick = debug_tick;
829 }
830
831 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
832 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
833
834 /* Did we encounter a breakpoint? */
835 if (breakpoint != NULL && *breakpoint != 0
836 && *breakpoint <= sourcing_lnum)
837 {
838 dbg_breakpoint(fname, sourcing_lnum);
839 /* Find next breakpoint. */
840 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100841 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 fname, sourcing_lnum);
843 *dbg_tick = debug_tick;
844 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000846 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 {
848 if (getline_is_func)
849 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 script_line_start();
852 }
853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
855
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000856 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000858 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100859 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 * below, so that it stores lines in or reads them from
861 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 * while/for loop. */
863 cmd_getline = get_loop_line;
864 cmd_cookie = (void *)&cmd_loop_cookie;
865 cmd_loop_cookie.lines_gap = &lines_ga;
866 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000868 cmd_loop_cookie.cookie = cookie;
869 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 }
871 else
872 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 cmd_cookie = cookie;
875 }
876#endif
877
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100878 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (next_cmdline == NULL)
880 {
881 /*
882 * Need to set msg_didout for the first line after an ":if",
883 * otherwise the ":if" will be overwritten.
884 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100887 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#ifdef FEAT_EVAL
889 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
890#else
891 0
892#endif
893 )) == NULL)
894 {
895 /* Don't call wait_return for aborted command line. The NULL
896 * returned for the end of a sourced file or executed function
897 * doesn't do this. */
898 if (KeyTyped && !(flags & DOCMD_REPEAT))
899 need_wait_return = FALSE;
900 retval = FAIL;
901 break;
902 }
903 used_getline = TRUE;
904
905 /*
906 * Keep the first typed line. Clear it when more lines are typed.
907 */
908 if (flags & DOCMD_KEEPLINE)
909 {
910 vim_free(repeat_cmdline);
911 if (count == 0)
912 repeat_cmdline = vim_strsave(next_cmdline);
913 else
914 repeat_cmdline = NULL;
915 }
916 }
917
918 /* 3. Make a copy of the command so we can mess with it. */
919 else if (cmdline_copy == NULL)
920 {
921 next_cmdline = vim_strsave(next_cmdline);
922 if (next_cmdline == NULL)
923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100924 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 retval = FAIL;
926 break;
927 }
928 }
929 cmdline_copy = next_cmdline;
930
931#ifdef FEAT_EVAL
932 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000933 * Save the current line when inside a ":while" or ":for", and when
934 * the command looks like a ":while" or ":for", because we may need it
935 * later. When there is a '|' and another command, it is stored
936 * separately, because we need to be able to jump back to it from an
937 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000939 if (current_line == lines_ga.ga_len
940 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000942 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
944 retval = FAIL;
945 break;
946 }
947 }
948 did_endif = FALSE;
949#endif
950
951 if (count++ == 0)
952 {
953 /*
954 * All output from the commands is put below each other, without
955 * waiting for a return. Don't do this when executing commands
956 * from a script or when being called recursive (e.g. for ":e
957 * +command file").
958 */
959 if (!(flags & DOCMD_NOWAIT) && !recursive)
960 {
961 msg_didout_before_start = msg_didout;
962 msg_didany = FALSE; /* no output yet */
963 msg_start();
964 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200965 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 ++RedrawingDisabled;
967 did_inc = TRUE;
968 }
969 }
970
971 if (p_verbose >= 15 && sourcing_name != NULL)
972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000974 verbose_enter_scroll();
975
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100976 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000978 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100979 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000980
981 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 --no_wait_return;
983 }
984
985 /*
986 * 2. Execute one '|' separated command.
987 * do_one_cmd() will return NULL if there is no trailing '|'.
988 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
989 */
990 ++recursive;
991 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
992#ifdef FEAT_EVAL
993 &cstack,
994#endif
995 cmd_getline, cmd_cookie);
996 --recursive;
997
998#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 if (cmd_cookie == (void *)&cmd_loop_cookie)
1000 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001002 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#endif
1004
1005 if (next_cmdline == NULL)
1006 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001007 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#ifdef FEAT_CMDHIST
1009 /*
1010 * If the command was typed, remember it for the ':' register.
1011 * Do this AFTER executing the command to make :@: work.
1012 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001013 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 && new_last_cmdline != NULL)
1015 {
1016 vim_free(last_cmdline);
1017 last_cmdline = new_last_cmdline;
1018 new_last_cmdline = NULL;
1019 }
1020#endif
1021 }
1022 else
1023 {
1024 /* need to copy the command after the '|' to cmdline_copy, for the
1025 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001026 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 next_cmdline = cmdline_copy;
1028 }
1029
1030
1031#ifdef FEAT_EVAL
1032 /* reset did_emsg for a function that is not aborted by an error */
1033 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001034 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 && !func_has_abort(real_cookie))
1036 did_emsg = FALSE;
1037
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
1040 ++current_line;
1041
1042 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 * An ":endwhile", ":endfor" and ":continue" is handled here.
1044 * If we were executing commands, jump back to the ":while" or
1045 * ":for".
1046 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 /* Jump back to the matching ":while" or ":for". Be careful
1053 * not to use a cs_line[] from an entry that isn't a ":while"
1054 * or ":for": It would make "current_line" invalid and can
1055 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (!did_emsg && !got_int && !did_throw
1057 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001058 && (cstack.cs_flags[cstack.cs_idx]
1059 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 && cstack.cs_line[cstack.cs_idx] >= 0
1061 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1062 {
1063 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 /* remember we jumped there */
1065 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 line_breakcheck(); /* check if CTRL-C typed */
1067
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 /* Check for the next breakpoint at or after the ":while"
1069 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (breakpoint != NULL)
1071 {
1072 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001073 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 fname,
1075 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1076 *dbg_tick = debug_tick;
1077 }
1078 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001083 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1084 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 }
1086 }
1087
1088 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1095 }
1096 }
1097
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001098 /* Check for the next breakpoint after a watchexpression */
1099 if (breakpoint != NULL && has_watchexpr())
1100 {
1101 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1102 *dbg_tick = debug_tick;
1103 }
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 /*
1106 * When not inside any ":while" loop, clear remembered lines.
1107 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 {
1110 if (lines_ga.ga_len > 0)
1111 {
1112 sourcing_lnum =
1113 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1114 free_cmdlines(&lines_ga);
1115 }
1116 current_line = 0;
1117 }
1118
1119 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1121 * being restored at the ":endtry". Reset them here and set the
1122 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1123 * This includes the case where a missing ":endif", ":endwhile" or
1124 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001126 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 cstack.cs_lflags &= ~CSL_HAD_FINA;
1129 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1130 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 did_throw ? (void *)current_exception : NULL);
1132 did_emsg = got_int = did_throw = FALSE;
1133 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1134 }
1135
1136 /* Update global "trylevel" for recursive calls to do_cmdline() from
1137 * within this loop. */
1138 trylevel = initial_trylevel + cstack.cs_trylevel;
1139
1140 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001141 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 * files) is aborted because of an error, an interrupt, or an uncaught
1143 * exception, cancel everything. If it is left normally, reset
1144 * force_abort to get the non-EH compatible abortion behavior for
1145 * the rest of the script.
1146 */
1147 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1148 force_abort = FALSE;
1149
1150 /* Convert an interrupt to an exception if appropriate. */
1151 (void)do_intthrow(&cstack);
1152#endif /* FEAT_EVAL */
1153
1154 }
1155 /*
1156 * Continue executing command lines when:
1157 * - no CTRL-C typed, no aborting error, no exception thrown or try
1158 * conditionals need to be checked for executing finally clauses or
1159 * catching an interrupt exception
1160 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001161 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 * looping for ":source" command or function call.
1163 */
1164 while (!((got_int
1165#ifdef FEAT_EVAL
1166 || (did_emsg && force_abort) || did_throw
1167#endif
1168 )
1169#ifdef FEAT_EVAL
1170 && cstack.cs_trylevel == 0
1171#endif
1172 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001173 && !(did_emsg
1174#ifdef FEAT_EVAL
1175 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001176 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001177 * the :endtry to be missed. */
1178 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1179#endif
1180 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001181 && (getline_equal(fgetline, cookie, getexmodeline)
1182 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 && (next_cmdline != NULL
1184#ifdef FEAT_EVAL
1185 || cstack.cs_idx >= 0
1186#endif
1187 || (flags & DOCMD_REPEAT)));
1188
1189 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001190 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191#ifdef FEAT_EVAL
1192 free_cmdlines(&lines_ga);
1193 ga_clear(&lines_ga);
1194
1195 if (cstack.cs_idx >= 0)
1196 {
1197 /*
1198 * If a sourced file or executed function ran to its end, report the
1199 * unclosed conditional.
1200 */
1201 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001202 && ((getline_equal(fgetline, cookie, getsourceline)
1203 && !source_finished(fgetline, cookie))
1204 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 && !func_has_ended(real_cookie))))
1206 {
1207 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001210 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001212 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 }
1216
1217 /*
1218 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1219 * ":endtry" in a sourced file or executed function. If the try
1220 * conditional is in its finally clause, ignore anything pending.
1221 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001222 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 */
1224 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001225 {
1226 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1227
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001228 if (idx >= 0)
1229 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001230 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1231 &cstack.cs_looplevel);
1232 }
1233 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 trylevel = initial_trylevel;
1235 }
1236
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001237 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1238 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001240 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 ? (char_u *)"endfunction" : (char_u *)NULL);
1242
1243 if (trylevel == 0)
1244 {
1245 /*
1246 * When an exception is being thrown out of the outermost try
1247 * conditional, discard the uncaught exception, disable the conversion
1248 * of interrupts or errors to exceptions, and ensure that no more
1249 * commands are executed.
1250 */
1251 if (did_throw)
1252 {
1253 void *p = NULL;
1254 char_u *saved_sourcing_name;
1255 int saved_sourcing_lnum;
1256 struct msglist *messages = NULL, *next;
1257
1258 /*
1259 * If the uncaught exception is a user exception, report it as an
1260 * error. If it is an error exception, display the saved error
1261 * message now. For an interrupt exception, do nothing; the
1262 * interrupt message is given elsewhere.
1263 */
1264 switch (current_exception->type)
1265 {
1266 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001267 vim_snprintf((char *)IObuff, IOSIZE,
1268 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 current_exception->value);
1270 p = vim_strsave(IObuff);
1271 break;
1272 case ET_ERROR:
1273 messages = current_exception->messages;
1274 current_exception->messages = NULL;
1275 break;
1276 case ET_INTERRUPT:
1277 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279
1280 saved_sourcing_name = sourcing_name;
1281 saved_sourcing_lnum = sourcing_lnum;
1282 sourcing_name = current_exception->throw_name;
1283 sourcing_lnum = current_exception->throw_lnum;
1284 current_exception->throw_name = NULL;
1285
1286 discard_current_exception(); /* uses IObuff if 'verbose' */
1287 suppress_errthrow = TRUE;
1288 force_abort = TRUE;
1289
1290 if (messages != NULL)
1291 {
1292 do
1293 {
1294 next = messages->next;
1295 emsg(messages->msg);
1296 vim_free(messages->msg);
1297 vim_free(messages);
1298 messages = next;
1299 }
1300 while (messages != NULL);
1301 }
1302 else if (p != NULL)
1303 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001304 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 vim_free(p);
1306 }
1307 vim_free(sourcing_name);
1308 sourcing_name = saved_sourcing_name;
1309 sourcing_lnum = saved_sourcing_lnum;
1310 }
1311
1312 /*
1313 * On an interrupt or an aborting error not converted to an exception,
1314 * disable the conversion of errors to exceptions. (Interrupts are not
1315 * converted any more, here.) This enables also the interrupt message
1316 * when force_abort is set and did_emsg unset in case of an interrupt
1317 * from a finally clause after an error.
1318 */
1319 else if (got_int || (did_emsg && force_abort))
1320 suppress_errthrow = TRUE;
1321 }
1322
1323 /*
1324 * The current cstack will be freed when do_cmdline() returns. An uncaught
1325 * exception will have to be rethrown in the previous cstack. If a function
1326 * has just returned or a script file was just finished and the previous
1327 * cstack belongs to the same function or, respectively, script file, it
1328 * will have to be checked for finally clauses to be executed due to the
1329 * ":return" or ":finish". This is done in do_one_cmd().
1330 */
1331 if (did_throw)
1332 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001335 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 && ex_nesting_level > func_level(real_cookie) + 1))
1337 {
1338 if (!did_throw)
1339 check_cstack = TRUE;
1340 }
1341 else
1342 {
1343 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001344 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 --ex_nesting_level;
1346 /*
1347 * Go to debug mode when returning from a function in which we are
1348 * single-stepping.
1349 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001350 if ((getline_equal(fgetline, cookie, getsourceline)
1351 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001353 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 ? (char_u *)_("End of sourced file")
1355 : (char_u *)_("End of function"));
1356 }
1357
1358 /*
1359 * Restore the exception environment (done after returning from the
1360 * debugger).
1361 */
1362 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001363 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365 msg_list = saved_msg_list;
1366#endif /* FEAT_EVAL */
1367
1368 /*
1369 * If there was too much output to fit on the command line, ask the user to
1370 * hit return before redrawing the screen. With the ":global" command we do
1371 * this only once after the command is finished.
1372 */
1373 if (did_inc)
1374 {
1375 --RedrawingDisabled;
1376 --no_wait_return;
1377 msg_scroll = FALSE;
1378
1379 /*
1380 * When just finished an ":if"-":else" which was typed, no need to
1381 * wait for hit-return. Also for an error situation.
1382 */
1383 if (retval == FAIL
1384#ifdef FEAT_EVAL
1385 || (did_endif && KeyTyped && !did_emsg)
1386#endif
1387 )
1388 {
1389 need_wait_return = FALSE;
1390 msg_didany = FALSE; /* don't wait when restarting edit */
1391 }
1392 else if (need_wait_return)
1393 {
1394 /*
1395 * The msg_start() above clears msg_didout. The wait_return we do
1396 * here should not overwrite the command that may be shown before
1397 * doing that.
1398 */
1399 msg_didout |= msg_didout_before_start;
1400 wait_return(FALSE);
1401 }
1402 }
1403
Bram Moolenaar2a942252012-11-28 23:03:07 +01001404#ifdef FEAT_EVAL
1405 did_endif = FALSE; /* in case do_cmdline used recursively */
1406#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 /*
1408 * Reset if_level, in case a sourced script file contains more ":if" than
1409 * ":endif" (could be ":if x | foo | endif").
1410 */
1411 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001412#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 --call_depth;
1415 return retval;
1416}
1417
1418#ifdef FEAT_EVAL
1419/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001420 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 */
1422 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001423get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001425 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 wcmd_T *wp;
1427 char_u *line;
1428
1429 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1430 {
1431 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001432 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001434 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (cp->getline == NULL)
1436 line = getcmdline(c, 0L, indent);
1437 else
1438 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001439 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 ++cp->current_line;
1441
1442 return line;
1443 }
1444
1445 KeyTyped = FALSE;
1446 ++cp->current_line;
1447 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1448 sourcing_lnum = wp->lnum;
1449 return vim_strsave(wp->line);
1450}
1451
1452/*
1453 * Store a line in "gap" so that a ":while" loop can execute it again.
1454 */
1455 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001456store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 if (ga_grow(gap, 1) == FAIL)
1459 return FAIL;
1460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1461 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1462 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 return OK;
1464}
1465
1466/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001467 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 */
1469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001470free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 while (gap->ga_len > 0)
1473 {
1474 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1475 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477}
1478#endif
1479
1480/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001481 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1482 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001485getline_equal(
1486 char_u *(*fgetline)(int, void *, int),
1487 void *cookie UNUSED, /* argument for fgetline() */
1488 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001491 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001492 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001495 * function that's originally used to obtain the lines. This may be
1496 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001497 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001498 cp = (struct loop_cookie *)cookie;
1499 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
1501 gp = cp->getline;
1502 cp = cp->cookie;
1503 }
1504 return gp == func;
1505#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507#endif
1508}
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001511 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 * getline function. Otherwise return "cookie".
1513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001515getline_cookie(
1516 char_u *(*fgetline)(int, void *, int) UNUSED,
1517 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaar13505972019-01-24 15:04:48 +01001519#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001520 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001521 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar89d40322006-08-29 15:30:07 +00001523 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001524 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001526 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 cp = (struct loop_cookie *)cookie;
1528 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 gp = cp->getline;
1531 cp = cp->cookie;
1532 }
1533 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001534#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001537}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001539
1540/*
1541 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1542 * ":bwipeout", etc.
1543 * Returns the buffer number.
1544 */
1545 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001546compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001547{
1548 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001549 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001550 int count = offset;
1551
1552 buf = firstbuf;
1553 while (buf->b_next != NULL && buf->b_fnum < lnum)
1554 buf = buf->b_next;
1555 while (count != 0)
1556 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001557 count += (offset < 0) ? 1 : -1;
1558 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1559 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001560 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001561 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001562 if (addr_type == ADDR_LOADED_BUFFERS)
1563 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001564 while (buf->b_ml.ml_mfp == NULL)
1565 {
1566 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1567 if (nextbuf == NULL)
1568 break;
1569 buf = nextbuf;
1570 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001571 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001572 /* we might have gone too far, last buffer is not loadedd */
1573 if (addr_type == ADDR_LOADED_BUFFERS)
1574 while (buf->b_ml.ml_mfp == NULL)
1575 {
1576 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1577 if (nextbuf == NULL)
1578 break;
1579 buf = nextbuf;
1580 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001581 return buf->b_fnum;
1582}
1583
Bram Moolenaarb7316892019-05-01 18:08:42 +02001584/*
1585 * Return the window number of "win".
1586 * When "win" is NULL return the number of windows.
1587 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001588 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001589current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001590{
1591 win_T *wp;
1592 int nr = 0;
1593
Bram Moolenaar29323592016-07-24 22:04:11 +02001594 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001595 {
1596 ++nr;
1597 if (wp == win)
1598 break;
1599 }
1600 return nr;
1601}
1602
1603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001604current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001605{
1606 tabpage_T *tp;
1607 int nr = 0;
1608
Bram Moolenaar29323592016-07-24 22:04:11 +02001609 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001610 {
1611 ++nr;
1612 if (tp == tab)
1613 break;
1614 }
1615 return nr;
1616}
1617
1618# define CURRENT_WIN_NR current_win_nr(curwin)
1619# define LAST_WIN_NR current_win_nr(NULL)
1620# define CURRENT_TAB_NR current_tab_nr(curtab)
1621# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623/*
1624 * Execute one Ex command.
1625 *
1626 * If 'sourcing' is TRUE, the command will be included in the error message.
1627 *
1628 * 1. skip comment lines and leading space
1629 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001630 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001631 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001632 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633 * 6. parse arguments
1634 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001636 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 *
1638 * This function may be called recursively!
1639 */
1640#if (_MSC_VER == 1200)
1641/*
Bram Moolenaared203462004-06-16 11:19:22 +00001642 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001644 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645#endif
1646 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001647do_one_cmd(
1648 char_u **cmdlinep,
1649 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001651 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001653 char_u *(*fgetline)(int, void *, int),
1654 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655{
1656 char_u *p;
1657 linenr_T lnum;
1658 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001659 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001660 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001662 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001664 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668 vim_memset(&ea, 0, sizeof(ea));
1669 ea.line1 = 1;
1670 ea.line2 = 1;
1671#ifdef FEAT_EVAL
1672 ++ex_nesting_level;
1673#endif
1674
Bram Moolenaar21691f82012-12-05 19:13:18 +01001675 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (quitmore
1677#ifdef FEAT_EVAL
1678 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001679 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001680#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001681 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 --quitmore;
1684
1685 /*
1686 * Reset browse, confirm, etc.. They are restored when returning, for
1687 * recursive calls.
1688 */
1689 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001691 /* "#!anything" is handled like a comment. */
1692 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1693 goto doend;
1694
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001695/*
1696 * 1. Skip comment lines and leading white space and colons.
1697 * 2. Handle command modifiers.
1698 */
1699 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001701 ea.cmdlinep = cmdlinep;
1702 ea.getline = fgetline;
1703 ea.cookie = cookie;
1704#ifdef FEAT_EVAL
1705 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001707 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001708 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001710 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712#ifdef FEAT_EVAL
1713 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1714 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1715#else
1716 ea.skip = (if_level > 0);
1717#endif
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001720 * 3. Skip over the range to find the command. Let "p" point to after it.
1721 *
1722 * We need the command to know what kind of range it uses.
1723 */
1724 cmd = ea.cmd;
1725 ea.cmd = skip_range(ea.cmd, NULL);
1726 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1727 ea.cmd = skipwhite(ea.cmd + 1);
1728 p = find_command(&ea, NULL);
1729
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001730#ifdef FEAT_EVAL
1731# ifdef FEAT_PROFILE
1732 // Count this line for profiling if skip is TRUE.
1733 if (do_profiling == PROF_YES
1734 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1735 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1736 {
1737 int skip = did_emsg || got_int || did_throw;
1738
1739 if (ea.cmdidx == CMD_catch)
1740 skip = !skip && !(cstack->cs_idx >= 0
1741 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1742 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1743 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1744 skip = skip || !(cstack->cs_idx >= 0
1745 && !(cstack->cs_flags[cstack->cs_idx]
1746 & (CSF_ACTIVE | CSF_TRUE)));
1747 else if (ea.cmdidx == CMD_finally)
1748 skip = FALSE;
1749 else if (ea.cmdidx != CMD_endif
1750 && ea.cmdidx != CMD_endfor
1751 && ea.cmdidx != CMD_endtry
1752 && ea.cmdidx != CMD_endwhile)
1753 skip = ea.skip;
1754
1755 if (!skip)
1756 {
1757 if (getline_equal(fgetline, cookie, get_func_line))
1758 func_line_exec(getline_cookie(fgetline, cookie));
1759 else if (getline_equal(fgetline, cookie, getsourceline))
1760 script_line_exec();
1761 }
1762 }
1763# endif
1764
1765 /* May go to debug mode. If this happens and the ">quit" debug command is
1766 * used, throw an interrupt exception and skip the next command. */
1767 dbg_check_breakpoint(&ea);
1768 if (!ea.skip && got_int)
1769 {
1770 ea.skip = TRUE;
1771 (void)do_intthrow(cstack);
1772 }
1773#endif
1774
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001775/*
1776 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 *
1778 * where 'addr' is:
1779 *
1780 * % (entire file)
1781 * $ [+-NUM]
1782 * 'x [+-NUM] (where x denotes a currently defined mark)
1783 * . [+-NUM]
1784 * [+-NUM]..
1785 * NUM
1786 *
1787 * The ea.cmd pointer is updated to point to the first character following the
1788 * range spec. If an initial address is found, but no second, the upper bound
1789 * is equal to the lower.
1790 */
1791
Bram Moolenaar25190db2019-05-04 15:05:28 +02001792 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001793 if (!IS_USER_CMDIDX(ea.cmdidx))
1794 {
1795 if (ea.cmdidx != CMD_SIZE)
1796 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1797 else
1798 ea.addr_type = ADDR_LINES;
1799
Bram Moolenaar25190db2019-05-04 15:05:28 +02001800 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001801 if (ea.cmdidx == CMD_wincmd && p != NULL)
1802 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001803#ifdef FEAT_QUICKFIX
1804 // :.cc in quickfix window uses line number
1805 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1806 ea.addr_type = ADDR_OTHER;
1807#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001808 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001809
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001810 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001811 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001812 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001815 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 */
1817
1818 /*
1819 * Skip ':' and any white space
1820 */
1821 ea.cmd = skipwhite(ea.cmd);
1822 while (*ea.cmd == ':')
1823 ea.cmd = skipwhite(ea.cmd + 1);
1824
1825 /*
1826 * If we got a line, but no command, then go to the line.
1827 * If we find a '|' or '\n' we set ea.nextcmd.
1828 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001829 if (*ea.cmd == NUL || *ea.cmd == '"'
1830 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 {
1832 /*
1833 * strange vi behaviour:
1834 * ":3" jumps to line 3
1835 * ":3|..." prints line 3
1836 * ":|" prints current line
1837 */
1838 if (ea.skip) /* skip this if inside :if */
1839 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001840 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
1842 ea.cmdidx = CMD_print;
1843 ea.argt = RANGE+COUNT+TRLBAR;
1844 if ((errormsg = invalid_range(&ea)) == NULL)
1845 {
1846 correct_range(&ea);
1847 ex_print(&ea);
1848 }
1849 }
1850 else if (ea.addr_count != 0)
1851 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001852 if (ea.line2 > curbuf->b_ml.ml_line_count)
1853 {
1854 /* With '-' in 'cpoptions' a line number past the file is an
1855 * error, otherwise put it at the end of the file. */
1856 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1857 ea.line2 = -1;
1858 else
1859 ea.line2 = curbuf->b_ml.ml_line_count;
1860 }
1861
1862 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001863 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 else
1865 {
1866 if (ea.line2 == 0)
1867 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 else
1869 curwin->w_cursor.lnum = ea.line2;
1870 beginline(BL_SOL | BL_FIX);
1871 }
1872 }
1873 goto doend;
1874 }
1875
Bram Moolenaard5005162014-08-22 23:05:54 +02001876 /* If this looks like an undefined user command and there are CmdUndefined
1877 * autocommands defined, trigger the matching autocommands. */
1878 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1879 && ASCII_ISUPPER(*ea.cmd)
1880 && has_cmdundefined())
1881 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001882 int ret;
1883
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001884 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001885 while (ASCII_ISALNUM(*p))
1886 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001887 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001888 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1889 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001890 /* If the autocommands did something and didn't cause an error, try
1891 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001892 p = (ret
1893#ifdef FEAT_EVAL
1894 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001895#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001896 ) ? find_command(&ea, NULL) : ea.cmd;
1897 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (p == NULL)
1900 {
1901 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001902 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 goto doend;
1904 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001905 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001906 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1907 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 {
1909 errormsg = uc_fun_cmd();
1910 goto doend;
1911 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (ea.cmdidx == CMD_SIZE)
1914 {
1915 if (!ea.skip)
1916 {
1917 STRCPY(IObuff, _("E492: Not an editor command"));
1918 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001919 {
1920 /* If the modifier was parsed OK the error must be in the
1921 * following command */
1922 if (after_modifier != NULL)
1923 append_command(after_modifier);
1924 else
1925 append_command(*cmdlinep);
1926 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001927 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001928 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930 goto doend;
1931 }
1932
Bram Moolenaar958636c2014-10-21 20:01:58 +02001933 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1934 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001935#ifdef HAVE_EX_SCRIPT_NI
1936 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1937#endif
1938 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
1940#ifndef FEAT_EVAL
1941 /*
1942 * When the expression evaluation is disabled, recognize the ":if" and
1943 * ":endif" commands and ignore everything in between it.
1944 */
1945 if (ea.cmdidx == CMD_if)
1946 ++if_level;
1947 if (if_level)
1948 {
1949 if (ea.cmdidx == CMD_endif)
1950 --if_level;
1951 goto doend;
1952 }
1953
1954#endif
1955
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001956 /* forced commands */
1957 if (*p == '!' && ea.cmdidx != CMD_substitute
1958 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
1960 ++p;
1961 ea.forceit = TRUE;
1962 }
1963 else
1964 ea.forceit = FALSE;
1965
1966/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001967 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001969 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001970 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
1972 if (!ea.skip)
1973 {
1974#ifdef HAVE_SANDBOX
1975 if (sandbox != 0 && !(ea.argt & SBOXOK))
1976 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001977 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 goto doend;
1980 }
1981#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001982 if (restricted != 0 && (ea.argt & RESTRICT))
1983 {
1984 errormsg = _("E981: Command not allowed in rvim");
1985 goto doend;
1986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
1988 {
1989 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001990 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 goto doend;
1992 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001993
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001994 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001995 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001997 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001998 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 goto doend;
2000 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002001
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002002 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002003 * Do allow ":checktime" (it is postponed).
2004 * Do allow ":edit" (check for an argument later).
2005 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002006 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002007 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002008 && ea.cmdidx != CMD_edit
2009 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002010 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002011 && curbuf_locked())
2012 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2015 {
2016 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002017 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 goto doend;
2019 }
2020 }
2021
2022 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002024 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 goto doend;
2026 }
2027
2028 /*
2029 * Don't complain about the range if it is not used
2030 * (could happen if line_count is accidentally set to 0).
2031 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002032 if (!ea.skip && !ni && (ea.argt & RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
2034 /*
2035 * If the range is backwards, ask for confirmation and, if given, swap
2036 * ea.line1 & ea.line2 so it's forwards again.
2037 * When global command is busy, don't ask, will fail below.
2038 */
2039 if (!global_busy && ea.line1 > ea.line2)
2040 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002041 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002043 if (sourcing || exmode_active)
2044 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002045 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002046 goto doend;
2047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 if (ask_yesno((char_u *)
2049 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002050 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
2052 lnum = ea.line1;
2053 ea.line1 = ea.line2;
2054 ea.line2 = lnum;
2055 }
2056 if ((errormsg = invalid_range(&ea)) != NULL)
2057 goto doend;
2058 }
2059
Bram Moolenaarb7316892019-05-01 18:08:42 +02002060 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2061 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 ea.line2 = 1;
2063
2064 correct_range(&ea);
2065
2066#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002067 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2068 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
2070 /* Put the first line at the start of a closed fold, put the last line
2071 * at the end of a closed fold. */
2072 (void)hasFolding(ea.line1, &ea.line1, NULL);
2073 (void)hasFolding(ea.line2, NULL, &ea.line2);
2074 }
2075#endif
2076
2077#ifdef FEAT_QUICKFIX
2078 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002079 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 * option here, so things like % get expanded.
2081 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002082 p = replace_makeprg(&ea, p, cmdlinep);
2083 if (p == NULL)
2084 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085#endif
2086
2087 /*
2088 * Skip to start of argument.
2089 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2090 */
2091 if (ea.cmdidx == CMD_bang)
2092 ea.arg = p;
2093 else
2094 ea.arg = skipwhite(p);
2095
Bram Moolenaar379fb762018-08-30 15:58:28 +02002096 // ":file" cannot be run with an argument when "curbuf_lock" is set
2097 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2098 goto doend;
2099
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 /*
2101 * Check for "++opt=val" argument.
2102 * Must be first, allow ":w ++enc=utf8 !cmd"
2103 */
2104 if (ea.argt & ARGOPT)
2105 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2106 if (getargopt(&ea) == FAIL && !ni)
2107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002108 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 goto doend;
2110 }
2111
2112 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2113 {
2114 if (*ea.arg == '>') /* append */
2115 {
2116 if (*++ea.arg != '>') /* typed wrong */
2117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002118 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 goto doend;
2120 }
2121 ea.arg = skipwhite(ea.arg + 1);
2122 ea.append = TRUE;
2123 }
2124 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2125 {
2126 ++ea.arg;
2127 ea.usefilter = TRUE;
2128 }
2129 }
2130
2131 if (ea.cmdidx == CMD_read)
2132 {
2133 if (ea.forceit)
2134 {
2135 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2136 ea.forceit = FALSE;
2137 }
2138 else if (*ea.arg == '!') /* :r !filter */
2139 {
2140 ++ea.arg;
2141 ea.usefilter = TRUE;
2142 }
2143 }
2144
2145 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2146 {
2147 ea.amount = 1;
2148 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2149 {
2150 ++ea.arg;
2151 ++ea.amount;
2152 }
2153 ea.arg = skipwhite(ea.arg);
2154 }
2155
2156 /*
2157 * Check for "+command" argument, before checking for next command.
2158 * Don't do this for ":read !cmd" and ":write !cmd".
2159 */
2160 if ((ea.argt & EDITCMD) && !ea.usefilter)
2161 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2162
2163 /*
2164 * Check for '|' to separate commands and '"' to start comments.
2165 * Don't do this for ":read !cmd" and ":write !cmd".
2166 */
2167 if ((ea.argt & TRLBAR) && !ea.usefilter)
2168 separate_nextcmd(&ea);
2169
2170 /*
2171 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002172 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2173 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002175 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002176 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002177 || ea.cmdidx == CMD_global
2178 || ea.cmdidx == CMD_vglobal
2179 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 {
2181 for (p = ea.arg; *p; ++p)
2182 {
2183 /* Remove one backslash before a newline, so that it's possible to
2184 * pass a newline to the shell and also a newline that is preceded
2185 * with a backslash. This makes it impossible to end a shell
2186 * command in a backslash, but that doesn't appear useful.
2187 * Halving the number of backslashes is incompatible with previous
2188 * versions. */
2189 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002190 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 else if (*p == '\n')
2192 {
2193 ea.nextcmd = p + 1;
2194 *p = NUL;
2195 break;
2196 }
2197 }
2198 }
2199
2200 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2201 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002202 buf_T *buf;
2203
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002205 switch (ea.addr_type)
2206 {
2207 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002208 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002209 ea.line2 = curbuf->b_ml.ml_line_count;
2210 break;
2211 case ADDR_LOADED_BUFFERS:
2212 buf = firstbuf;
2213 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2214 buf = buf->b_next;
2215 ea.line1 = buf->b_fnum;
2216 buf = lastbuf;
2217 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2218 buf = buf->b_prev;
2219 ea.line2 = buf->b_fnum;
2220 break;
2221 case ADDR_BUFFERS:
2222 ea.line1 = firstbuf->b_fnum;
2223 ea.line2 = lastbuf->b_fnum;
2224 break;
2225 case ADDR_WINDOWS:
2226 ea.line2 = LAST_WIN_NR;
2227 break;
2228 case ADDR_TABS:
2229 ea.line2 = LAST_TAB_NR;
2230 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002231 case ADDR_TABS_RELATIVE:
2232 ea.line2 = 1;
2233 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002234 case ADDR_ARGUMENTS:
2235 if (ARGCOUNT == 0)
2236 ea.line1 = ea.line2 = 0;
2237 else
2238 ea.line2 = ARGCOUNT;
2239 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002240 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002241#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002242 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002243 if (ea.line2 == 0)
2244 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002245#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002246 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002247 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002248 case ADDR_UNSIGNED:
2249 case ADDR_QUICKFIX:
2250 iemsg(_("INTERNAL: Cannot use DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002251 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 }
2254
2255 /* accept numbered register only when no count allowed (:put) */
2256 if ( (ea.argt & REGSTR)
2257 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002258 /* Do not allow register = for user commands */
2259 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2261 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002262#ifndef FEAT_CLIPBOARD
2263 /* check these explicitly for a more specific error message */
2264 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002266 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002267 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
2269#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002270 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2271 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002272 {
2273 ea.regname = *ea.arg++;
2274#ifdef FEAT_EVAL
2275 /* for '=' register: accept the rest of the line as an expression */
2276 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2277 {
2278 set_expr_line(vim_strsave(ea.arg));
2279 ea.arg += STRLEN(ea.arg);
2280 }
2281#endif
2282 ea.arg = skipwhite(ea.arg);
2283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285
2286 /*
2287 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2288 * count, it's a buffer name.
2289 */
2290 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2291 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002292 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 n = getdigits(&ea.arg);
2295 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002296 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002298 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 goto doend;
2300 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002301 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 ea.line2 = n;
2304 if (ea.addr_count == 0)
2305 ea.addr_count = 1;
2306 }
2307 else
2308 {
2309 ea.line1 = ea.line2;
2310 ea.line2 += n - 1;
2311 ++ea.addr_count;
2312 /*
2313 * Be vi compatible: no error message for out of range.
2314 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002315 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 ea.line2 = curbuf->b_ml.ml_line_count;
2317 }
2318 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002319
2320 /*
2321 * Check for flags: 'l', 'p' and '#'.
2322 */
2323 if (ea.argt & EXFLAGS)
2324 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002326 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002327 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002329 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 goto doend;
2331 }
2332
2333 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002335 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 goto doend;
2337 }
2338
2339#ifdef FEAT_EVAL
2340 /*
2341 * Skip the command when it's not going to be executed.
2342 * The commands like :if, :endif, etc. always need to be executed.
2343 * Also make an exception for commands that handle a trailing command
2344 * themselves.
2345 */
2346 if (ea.skip)
2347 {
2348 switch (ea.cmdidx)
2349 {
2350 /* commands that need evaluation */
2351 case CMD_while:
2352 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002353 case CMD_for:
2354 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 case CMD_if:
2356 case CMD_elseif:
2357 case CMD_else:
2358 case CMD_endif:
2359 case CMD_try:
2360 case CMD_catch:
2361 case CMD_finally:
2362 case CMD_endtry:
2363 case CMD_function:
2364 break;
2365
2366 /* Commands that handle '|' themselves. Check: A command should
2367 * either have the TRLBAR flag, appear in this list or appear in
2368 * the list at ":help :bar". */
2369 case CMD_aboveleft:
2370 case CMD_and:
2371 case CMD_belowright:
2372 case CMD_botright:
2373 case CMD_browse:
2374 case CMD_call:
2375 case CMD_confirm:
2376 case CMD_delfunction:
2377 case CMD_djump:
2378 case CMD_dlist:
2379 case CMD_dsearch:
2380 case CMD_dsplit:
2381 case CMD_echo:
2382 case CMD_echoerr:
2383 case CMD_echomsg:
2384 case CMD_echon:
2385 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002386 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 case CMD_help:
2388 case CMD_hide:
2389 case CMD_ijump:
2390 case CMD_ilist:
2391 case CMD_isearch:
2392 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002393 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 case CMD_keepjumps:
2395 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002396 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 case CMD_leftabove:
2398 case CMD_let:
2399 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002400 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002402 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002403 case CMD_noautocmd:
2404 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_perl:
2406 case CMD_psearch:
2407 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002408 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002409 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_return:
2411 case CMD_rightbelow:
2412 case CMD_ruby:
2413 case CMD_silent:
2414 case CMD_smagic:
2415 case CMD_snomagic:
2416 case CMD_substitute:
2417 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002418 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_tcl:
2420 case CMD_throw:
2421 case CMD_tilde:
2422 case CMD_topleft:
2423 case CMD_unlet:
2424 case CMD_verbose:
2425 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002426 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 break;
2428
2429 default: goto doend;
2430 }
2431 }
2432#endif
2433
2434 if (ea.argt & XFILE)
2435 {
2436 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2437 goto doend;
2438 }
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 /*
2441 * Accept buffer name. Cannot be used at the same time with a buffer
2442 * number. Don't do this for a user command.
2443 */
2444 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002445 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
2447 /*
2448 * :bdelete, :bwipeout and :bunload take several arguments, separated
2449 * by spaces: find next space (skipping over escaped characters).
2450 * The others take one argument: ignore trailing spaces.
2451 */
2452 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2453 || ea.cmdidx == CMD_bunload)
2454 p = skiptowhite_esc(ea.arg);
2455 else
2456 {
2457 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002458 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 --p;
2460 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002461 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2462 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (ea.line2 < 0) /* failed */
2464 goto doend;
2465 ea.addr_count = 1;
2466 ea.arg = skipwhite(p);
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaar2be57332018-02-13 18:05:18 +01002469 /* The :try command saves the emsg_silent flag, reset it here when
2470 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002471 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002472 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002473 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002474 if (emsg_silent < 0)
2475 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002476 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002477 }
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002480 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaar958636c2014-10-21 20:01:58 +02002483 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 {
2485 /*
2486 * Execute a user-defined command.
2487 */
2488 do_ucmd(&ea);
2489 }
2490 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
2492 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002493 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 */
2495 ea.errmsg = NULL;
2496 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2497 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002498 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
2500
2501#ifdef FEAT_EVAL
2502 /*
2503 * If the command just executed called do_cmdline(), any throw or ":return"
2504 * or ":finish" encountered there must also check the cstack of the still
2505 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2506 * exception, or reanimate a returned function or finished script file and
2507 * return or finish it again.
2508 */
2509 if (need_rethrow)
2510 do_throw(cstack);
2511 else if (check_cstack)
2512 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002513 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002515 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 && current_func_returned())
2517 do_return(&ea, TRUE, FALSE, NULL);
2518 }
2519 need_rethrow = check_cstack = FALSE;
2520#endif
2521
2522doend:
2523 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002524 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002526 curwin->w_cursor.col = 0;
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2530 {
2531 if (sourcing)
2532 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002533 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002536 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002538 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540 emsg(errormsg);
2541 }
2542#ifdef FEAT_EVAL
2543 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002544 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2545 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546#endif
2547
Bram Moolenaareffed932018-08-14 13:38:17 +02002548 if (ea.verbose_save >= 0)
2549 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002550
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002551 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002553 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaareffed932018-08-14 13:38:17 +02002555 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
2557 /* messages could be enabled for a serious error, need to check if the
2558 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002559 if (!did_emsg || msg_silent > ea.save_msg_silent)
2560 msg_silent = ea.save_msg_silent;
2561 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (emsg_silent < 0)
2563 emsg_silent = 0;
2564 /* Restore msg_scroll, it's set by file I/O commands, even when no
2565 * message is actually displayed. */
2566 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002567
2568 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2569 * somewhere in the line. Put it back in the first column. */
2570 if (redirecting())
2571 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002574#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002575 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002576 --sandbox;
2577#endif
2578
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2580 ea.nextcmd = NULL;
2581
2582#ifdef FEAT_EVAL
2583 --ex_nesting_level;
2584#endif
2585
2586 return ea.nextcmd;
2587}
2588#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002589 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#endif
2591
2592/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002593 * Parse and skip over command modifiers:
2594 * - update eap->cmd
2595 * - store flags in "cmdmod".
2596 * - Set ex_pressedreturn for an empty command line.
2597 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002598 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 * - set p_verbose for ":verbose"
2600 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002601 * When "skip_only" is TRUE the global variables are not changed, except for
2602 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 * Return FAIL when the command is not to be executed.
2604 * May set "errormsg" to an error message.
2605 */
2606 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002608{
2609 char_u *p;
2610
2611 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2612 eap->verbose_save = -1;
2613 eap->save_msg_silent = -1;
2614
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002615 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002616 for (;;)
2617 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002618 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2619 ++eap->cmd;
2620
2621 /* in ex mode, an empty line works like :+ */
2622 if (*eap->cmd == NUL && exmode_active
2623 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2624 || getline_equal(eap->getline, eap->cookie, getexline))
2625 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2626 {
2627 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002628 if (!skip_only)
2629 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002630 }
2631
2632 /* ignore comment and empty lines */
2633 if (*eap->cmd == '"')
2634 return FAIL;
2635 if (*eap->cmd == NUL)
2636 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002637 if (!skip_only)
2638 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 return FAIL;
2640 }
2641
Bram Moolenaareffed932018-08-14 13:38:17 +02002642 p = skip_range(eap->cmd, NULL);
2643 switch (*p)
2644 {
2645 /* When adding an entry, also modify cmd_exists(). */
2646 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2647 break;
2648 cmdmod.split |= WSP_ABOVE;
2649 continue;
2650
2651 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2652 {
2653 cmdmod.split |= WSP_BELOW;
2654 continue;
2655 }
2656 if (checkforcmd(&eap->cmd, "browse", 3))
2657 {
2658#ifdef FEAT_BROWSE_CMD
2659 cmdmod.browse = TRUE;
2660#endif
2661 continue;
2662 }
2663 if (!checkforcmd(&eap->cmd, "botright", 2))
2664 break;
2665 cmdmod.split |= WSP_BOT;
2666 continue;
2667
2668 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2669 break;
2670#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2671 cmdmod.confirm = TRUE;
2672#endif
2673 continue;
2674
2675 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2676 {
2677 cmdmod.keepmarks = TRUE;
2678 continue;
2679 }
2680 if (checkforcmd(&eap->cmd, "keepalt", 5))
2681 {
2682 cmdmod.keepalt = TRUE;
2683 continue;
2684 }
2685 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2686 {
2687 cmdmod.keeppatterns = TRUE;
2688 continue;
2689 }
2690 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2691 break;
2692 cmdmod.keepjumps = TRUE;
2693 continue;
2694
2695 case 'f': /* only accept ":filter {pat} cmd" */
2696 {
2697 char_u *reg_pat;
2698
2699 if (!checkforcmd(&p, "filter", 4)
2700 || *p == NUL || ends_excmd(*p))
2701 break;
2702 if (*p == '!')
2703 {
2704 cmdmod.filter_force = TRUE;
2705 p = skipwhite(p + 1);
2706 if (*p == NUL || ends_excmd(*p))
2707 break;
2708 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002709 if (skip_only)
2710 p = skip_vimgrep_pat(p, NULL, NULL);
2711 else
2712 // NOTE: This puts a NUL after the pattern.
2713 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002714 if (p == NULL || *p == NUL)
2715 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002716 if (!skip_only)
2717 {
2718 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002719 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002720 if (cmdmod.filter_regmatch.regprog == NULL)
2721 break;
2722 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002723 eap->cmd = p;
2724 continue;
2725 }
2726
2727 /* ":hide" and ":hide | cmd" are not modifiers */
2728 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2729 || *p == NUL || ends_excmd(*p))
2730 break;
2731 eap->cmd = p;
2732 cmdmod.hide = TRUE;
2733 continue;
2734
2735 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2736 {
2737 cmdmod.lockmarks = TRUE;
2738 continue;
2739 }
2740
2741 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2742 break;
2743 cmdmod.split |= WSP_ABOVE;
2744 continue;
2745
2746 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2747 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002748 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002749 {
2750 /* Set 'eventignore' to "all". Restore the
2751 * existing option value later. */
2752 cmdmod.save_ei = vim_strsave(p_ei);
2753 set_string_option_direct((char_u *)"ei", -1,
2754 (char_u *)"all", OPT_FREE, SID_NONE);
2755 }
2756 continue;
2757 }
2758 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2759 break;
2760 cmdmod.noswapfile = TRUE;
2761 continue;
2762
2763 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2764 break;
2765 cmdmod.split |= WSP_BELOW;
2766 continue;
2767
2768 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2769 {
2770#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002771 if (!skip_only)
2772 {
2773 if (!eap->did_sandbox)
2774 ++sandbox;
2775 eap->did_sandbox = TRUE;
2776 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002777#endif
2778 continue;
2779 }
2780 if (!checkforcmd(&eap->cmd, "silent", 3))
2781 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002782 if (!skip_only)
2783 {
2784 if (eap->save_msg_silent == -1)
2785 eap->save_msg_silent = msg_silent;
2786 ++msg_silent;
2787 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002788 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2789 {
2790 /* ":silent!", but not "silent !cmd" */
2791 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002792 if (!skip_only)
2793 {
2794 ++emsg_silent;
2795 ++eap->did_esilent;
2796 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 }
2798 continue;
2799
2800 case 't': if (checkforcmd(&p, "tab", 3))
2801 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002802 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002804 long tabnr = get_address(eap, &eap->cmd,
2805 ADDR_TABS, eap->skip,
2806 skip_only, FALSE, 1);
2807 if (tabnr == MAXLNUM)
2808 cmdmod.tab = tabpage_index(curtab) + 1;
2809 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002811 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2812 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002813 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002814 return FAIL;
2815 }
2816 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 }
2819 eap->cmd = p;
2820 continue;
2821 }
2822 if (!checkforcmd(&eap->cmd, "topleft", 2))
2823 break;
2824 cmdmod.split |= WSP_TOP;
2825 continue;
2826
2827 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2828 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002829 if (!skip_only)
2830 {
2831 if (eap->save_msg_silent == -1)
2832 eap->save_msg_silent = msg_silent;
2833 msg_silent = 0;
2834 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 continue;
2836
2837 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2838 {
2839 cmdmod.split |= WSP_VERT;
2840 continue;
2841 }
2842 if (!checkforcmd(&p, "verbose", 4))
2843 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002844 if (!skip_only)
2845 {
2846 if (eap->verbose_save < 0)
2847 eap->verbose_save = p_verbose;
2848 if (vim_isdigit(*eap->cmd))
2849 p_verbose = atoi((char *)eap->cmd);
2850 else
2851 p_verbose = 1;
2852 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002853 eap->cmd = p;
2854 continue;
2855 }
2856 break;
2857 }
2858
2859 return OK;
2860}
2861
2862/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002863 * Free contents of "cmdmod".
2864 */
2865 static void
2866free_cmdmod(void)
2867{
2868 if (cmdmod.save_ei != NULL)
2869 {
2870 /* Restore 'eventignore' to the value before ":noautocmd". */
2871 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2872 OPT_FREE, SID_NONE);
2873 free_string_option(cmdmod.save_ei);
2874 }
2875
2876 if (cmdmod.filter_regmatch.regprog != NULL)
2877 vim_regfree(cmdmod.filter_regmatch.regprog);
2878}
2879
2880/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002881 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002882 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002883 * Return FAIL and set "errormsg" or return OK.
2884 */
2885 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002886parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002887{
2888 int address_count = 1;
2889 linenr_T lnum;
2890
2891 // Repeat for all ',' or ';' separated addresses.
2892 for (;;)
2893 {
2894 eap->line1 = eap->line2;
2895 switch (eap->addr_type)
2896 {
2897 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002898 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002899 // default is current line number
2900 eap->line2 = curwin->w_cursor.lnum;
2901 break;
2902 case ADDR_WINDOWS:
2903 eap->line2 = CURRENT_WIN_NR;
2904 break;
2905 case ADDR_ARGUMENTS:
2906 eap->line2 = curwin->w_arg_idx + 1;
2907 if (eap->line2 > ARGCOUNT)
2908 eap->line2 = ARGCOUNT;
2909 break;
2910 case ADDR_LOADED_BUFFERS:
2911 case ADDR_BUFFERS:
2912 eap->line2 = curbuf->b_fnum;
2913 break;
2914 case ADDR_TABS:
2915 eap->line2 = CURRENT_TAB_NR;
2916 break;
2917 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002918 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002919 eap->line2 = 1;
2920 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002921 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002922#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002923 eap->line2 = qf_get_cur_idx(eap);
2924#endif
2925 break;
2926 case ADDR_QUICKFIX_VALID:
2927#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002928 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002929#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002930 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002931 case ADDR_NONE:
2932 // Will give an error later if a range is found.
2933 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002934 }
2935 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002936 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002937 eap->addr_count == 0, address_count++);
2938 if (eap->cmd == NULL) // error detected
2939 return FAIL;
2940 if (lnum == MAXLNUM)
2941 {
2942 if (*eap->cmd == '%') // '%' - all lines
2943 {
2944 ++eap->cmd;
2945 switch (eap->addr_type)
2946 {
2947 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002948 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002949 eap->line1 = 1;
2950 eap->line2 = curbuf->b_ml.ml_line_count;
2951 break;
2952 case ADDR_LOADED_BUFFERS:
2953 {
2954 buf_T *buf = firstbuf;
2955
2956 while (buf->b_next != NULL
2957 && buf->b_ml.ml_mfp == NULL)
2958 buf = buf->b_next;
2959 eap->line1 = buf->b_fnum;
2960 buf = lastbuf;
2961 while (buf->b_prev != NULL
2962 && buf->b_ml.ml_mfp == NULL)
2963 buf = buf->b_prev;
2964 eap->line2 = buf->b_fnum;
2965 break;
2966 }
2967 case ADDR_BUFFERS:
2968 eap->line1 = firstbuf->b_fnum;
2969 eap->line2 = lastbuf->b_fnum;
2970 break;
2971 case ADDR_WINDOWS:
2972 case ADDR_TABS:
2973 if (IS_USER_CMDIDX(eap->cmdidx))
2974 {
2975 eap->line1 = 1;
2976 eap->line2 = eap->addr_type == ADDR_WINDOWS
2977 ? LAST_WIN_NR : LAST_TAB_NR;
2978 }
2979 else
2980 {
2981 // there is no Vim command which uses '%' and
2982 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002983 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002984 return FAIL;
2985 }
2986 break;
2987 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002988 case ADDR_UNSIGNED:
2989 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002990 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002991 return FAIL;
2992 case ADDR_ARGUMENTS:
2993 if (ARGCOUNT == 0)
2994 eap->line1 = eap->line2 = 0;
2995 else
2996 {
2997 eap->line1 = 1;
2998 eap->line2 = ARGCOUNT;
2999 }
3000 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003001 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003002#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003003 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003004 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 if (eap->line2 == 0)
3006 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003008 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003009 case ADDR_NONE:
3010 // Will give an error later if a range is found.
3011 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003012 }
3013 ++eap->addr_count;
3014 }
3015 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3016 {
3017 pos_T *fp;
3018
3019 // '*' - visual area
3020 if (eap->addr_type != ADDR_LINES)
3021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 return FAIL;
3024 }
3025
3026 ++eap->cmd;
3027 if (!eap->skip)
3028 {
3029 fp = getmark('<', FALSE);
3030 if (check_mark(fp) == FAIL)
3031 return FAIL;
3032 eap->line1 = fp->lnum;
3033 fp = getmark('>', FALSE);
3034 if (check_mark(fp) == FAIL)
3035 return FAIL;
3036 eap->line2 = fp->lnum;
3037 ++eap->addr_count;
3038 }
3039 }
3040 }
3041 else
3042 eap->line2 = lnum;
3043 eap->addr_count++;
3044
3045 if (*eap->cmd == ';')
3046 {
3047 if (!eap->skip)
3048 {
3049 curwin->w_cursor.lnum = eap->line2;
3050 // don't leave the cursor on an illegal line or column
3051 check_cursor();
3052 }
3053 }
3054 else if (*eap->cmd != ',')
3055 break;
3056 ++eap->cmd;
3057 }
3058
3059 // One address given: set start and end lines.
3060 if (eap->addr_count == 1)
3061 {
3062 eap->line1 = eap->line2;
3063 // ... but only implicit: really no address given
3064 if (lnum == MAXLNUM)
3065 eap->addr_count = 0;
3066 }
3067 return OK;
3068}
3069
3070/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003071 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 * If there is a match advance "pp" to the argument and return TRUE.
3073 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003074 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003075checkforcmd(
3076 char_u **pp, /* start of command */
3077 char *cmd, /* name of command */
3078 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
3080 int i;
3081
3082 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003083 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 break;
3085 if (i >= len && !isalpha((*pp)[i]))
3086 {
3087 *pp = skipwhite(*pp + i);
3088 return TRUE;
3089 }
3090 return FALSE;
3091}
3092
3093/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003094 * Append "cmd" to the error message in IObuff.
3095 * Takes care of limiting the length and handling 0xa0, which would be
3096 * invisible otherwise.
3097 */
3098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003099append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003100{
3101 char_u *s = cmd;
3102 char_u *d;
3103
3104 STRCAT(IObuff, ": ");
3105 d = IObuff + STRLEN(IObuff);
3106 while (*s != NUL && d - IObuff < IOSIZE - 7)
3107 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003108 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003109 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003110 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003111 STRCPY(d, "<a0>");
3112 d += 4;
3113 }
3114 else
3115 MB_COPY_CHAR(s, d);
3116 }
3117 *d = NUL;
3118}
3119
3120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003122 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003123 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003124 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * Returns NULL for an ambiguous user command.
3126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 int len;
3131 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003132 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133
3134 /*
3135 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003136 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 * - the 'k' command can directly be followed by any character.
3138 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003139 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003141 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 */
3143 p = eap->cmd;
3144 if (*p == 'k')
3145 {
3146 eap->cmdidx = CMD_k;
3147 ++p;
3148 }
3149 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003150 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3151 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 || p[1] == 'g'
3153 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3154 || p[1] == 'I'
3155 || (p[1] == 'r' && p[2] != 'e')))
3156 {
3157 eap->cmdidx = CMD_substitute;
3158 ++p;
3159 }
3160 else
3161 {
3162 while (ASCII_ISALPHA(*p))
3163 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003164 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003165 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003166 while (ASCII_ISALNUM(*p))
3167 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003168
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 /* check for non-alpha command */
3170 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3171 ++p;
3172 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003173 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3174 {
3175 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3176 * :delete with the 'l' flag. Same for 'p'. */
3177 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003178 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003179 break;
3180 if (i == len - 1)
3181 {
3182 --len;
3183 if (p[-1] == 'l')
3184 eap->flags |= EXFLAG_LIST;
3185 else
3186 eap->flags |= EXFLAG_PRINT;
3187 }
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003190 if (ASCII_ISLOWER(eap->cmd[0]))
3191 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003192 int c1 = eap->cmd[0];
3193 int c2 = eap->cmd[1];
3194
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003195 if (command_count != (int)CMD_SIZE)
3196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003197 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003198 getout(1);
3199 }
3200
3201 /* Use a precomputed index for fast look-up in cmdnames[]
3202 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003203 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3204 if (ASCII_ISLOWER(c2))
3205 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003208 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209
3210 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3211 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3212 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3213 (size_t)len) == 0)
3214 {
3215#ifdef FEAT_EVAL
3216 if (full != NULL
3217 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3218 *full = TRUE;
3219#endif
3220 break;
3221 }
3222
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003223 // Look for a user defined command as a last resort. Let ":Print" be
3224 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003225 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3226 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003228 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 while (ASCII_ISALNUM(*p))
3230 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003231 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 eap->cmdidx = CMD_SIZE;
3235 }
3236
3237 return p;
3238}
3239
3240#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003241static struct cmdmod
3242{
3243 char *name;
3244 int minlen;
3245 int has_count; /* :123verbose :3tab */
3246} cmdmods[] = {
3247 {"aboveleft", 3, FALSE},
3248 {"belowright", 3, FALSE},
3249 {"botright", 2, FALSE},
3250 {"browse", 3, FALSE},
3251 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003252 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003253 {"hide", 3, FALSE},
3254 {"keepalt", 5, FALSE},
3255 {"keepjumps", 5, FALSE},
3256 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003257 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003258 {"leftabove", 5, FALSE},
3259 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003260 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003261 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003262 {"rightbelow", 6, FALSE},
3263 {"sandbox", 3, FALSE},
3264 {"silent", 3, FALSE},
3265 {"tab", 3, TRUE},
3266 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003267 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003268 {"verbose", 4, TRUE},
3269 {"vertical", 4, FALSE},
3270};
3271
3272/*
3273 * Return length of a command modifier (including optional count).
3274 * Return zero when it's not a modifier.
3275 */
3276 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003277modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003278{
3279 int i, j;
3280 char_u *p = cmd;
3281
3282 if (VIM_ISDIGIT(*cmd))
3283 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003284 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003285 {
3286 for (j = 0; p[j] != NUL; ++j)
3287 if (p[j] != cmdmods[i].name[j])
3288 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003289 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003290 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003291 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003292 }
3293 return 0;
3294}
3295
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296/*
3297 * Return > 0 if an Ex command "name" exists.
3298 * Return 2 if there is an exact match.
3299 * Return 3 if there is an ambiguous match.
3300 */
3301 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003302cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304 exarg_T ea;
3305 int full = FALSE;
3306 int i;
3307 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003308 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309
3310 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003311 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 for (j = 0; name[j] != NUL; ++j)
3314 if (name[j] != cmdmods[i].name[j])
3315 break;
3316 if (name[j] == NUL && j >= cmdmods[i].minlen)
3317 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3318 }
3319
Bram Moolenaara9587612006-05-04 21:47:50 +00003320 /* Check built-in commands and user defined commands.
3321 * For ":2match" and ":3match" we need to skip the number. */
3322 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003324 p = find_command(&ea, &full);
3325 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003327 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3328 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003329 if (*skipwhite(p) != NUL)
3330 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3332}
3333#endif
3334
3335/*
3336 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3337 * we don't need/want deleted. Maybe this could be done better if we didn't
3338 * repeat all this stuff. The only problem is that they may not stay
3339 * perfectly compatible with each other, but then the command line syntax
3340 * probably won't change that much -- webb.
3341 */
3342 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003343set_one_cmd_context(
3344 expand_T *xp,
3345 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 char_u *p;
3348 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003349 int len = 0;
3350 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003352 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 int delim;
3354#endif
3355 int forceit = FALSE;
3356 int usefilter = FALSE; /* filter instead of file name */
3357
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003358 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 xp->xp_pattern = buff;
3360 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003361 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
3363/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003364 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 */
3366 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3367 ;
3368 xp->xp_pattern = cmd;
3369
3370 if (*cmd == NUL)
3371 return NULL;
3372 if (*cmd == '"') /* ignore comment lines */
3373 {
3374 xp->xp_context = EXPAND_NOTHING;
3375 return NULL;
3376 }
3377
3378/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003379 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 */
3381 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 xp->xp_pattern = cmd;
3383 if (*cmd == NUL)
3384 return NULL;
3385 if (*cmd == '"')
3386 {
3387 xp->xp_context = EXPAND_NOTHING;
3388 return NULL;
3389 }
3390
3391 if (*cmd == '|' || *cmd == '\n')
3392 return cmd + 1; /* There's another command */
3393
3394 /*
3395 * Isolate the command and search for it in the command table.
3396 * Exceptions:
3397 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003398 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3400 */
3401 if (*cmd == 'k' && cmd[1] != 'e')
3402 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003403 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 p = cmd + 1;
3405 }
3406 else
3407 {
3408 p = cmd;
3409 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3410 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003411 /* a user command may contain digits */
3412 if (ASCII_ISUPPER(cmd[0]))
3413 while (ASCII_ISALNUM(*p) || *p == '*')
3414 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003415 /* for python 3.x: ":py3*" commands completion */
3416 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003417 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003418 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003419 while (ASCII_ISALPHA(*p) || *p == '*')
3420 ++p;
3421 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003422 /* check for non-alpha command */
3423 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3424 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003425 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
3429 xp->xp_context = EXPAND_UNSUCCESSFUL;
3430 return NULL;
3431 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003432 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003433 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3434 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3435 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 break;
3437
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003439 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 }
3442
3443 /*
3444 * If the cursor is touching the command, and it ends in an alpha-numeric
3445 * character, complete the command name.
3446 */
3447 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3448 return NULL;
3449
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003450 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 {
3452 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3453 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003454 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 p = cmd + 1;
3456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3458 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003459 ea.cmd = cmd;
3460 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003461#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003462 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003463#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003464 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003465#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003467 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003468 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003471 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
3473 /* Not still touching the command and it was an illegal one */
3474 xp->xp_context = EXPAND_UNSUCCESSFUL;
3475 return NULL;
3476 }
3477
3478 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3479
3480 if (*p == '!') /* forced commands */
3481 {
3482 forceit = TRUE;
3483 ++p;
3484 }
3485
3486/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003487 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003489 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003490 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 arg = skipwhite(p);
3493
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003494 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 if (*arg == '>') /* append */
3497 {
3498 if (*++arg == '>')
3499 ++arg;
3500 arg = skipwhite(arg);
3501 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003502 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 ++arg;
3505 usefilter = TRUE;
3506 }
3507 }
3508
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003509 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 {
3511 usefilter = forceit; /* :r! filter if forced */
3512 if (*arg == '!') /* :r !filter */
3513 {
3514 ++arg;
3515 usefilter = TRUE;
3516 }
3517 }
3518
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003519 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 {
3521 while (*arg == *cmd) /* allow any number of '>' or '<' */
3522 ++arg;
3523 arg = skipwhite(arg);
3524 }
3525
3526 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003527 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 {
3529 /* Check if we're in the +command */
3530 p = arg + 1;
3531 arg = skip_cmd_arg(arg, FALSE);
3532
3533 /* Still touching the command after '+'? */
3534 if (*arg == NUL)
3535 return p;
3536
3537 /* Skip space(s) after +command to get to the real argument */
3538 arg = skipwhite(arg);
3539 }
3540
3541 /*
3542 * Check for '|' to separate commands and '"' to start comments.
3543 * Don't do this for ":read !cmd" and ":write !cmd".
3544 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003545 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
3547 p = arg;
3548 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003549 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 p += 2;
3551 while (*p)
3552 {
3553 if (*p == Ctrl_V)
3554 {
3555 if (p[1] != NUL)
3556 ++p;
3557 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003558 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 || *p == '|' || *p == '\n')
3560 {
3561 if (*(p - 1) != '\\')
3562 {
3563 if (*p == '|' || *p == '\n')
3564 return p + 1;
3565 return NULL; /* It's a comment */
3566 }
3567 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003568 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 }
3570 }
3571
3572 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003573 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 vim_strchr((char_u *)"|\"", *arg) == NULL)
3575 return NULL;
3576
3577 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003578 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003580 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003581 while (*p && p < buff + len)
3582 {
3583 if (*p == ' ' || *p == TAB)
3584 {
3585 /* argument starts after a space */
3586 xp->xp_pattern = ++p;
3587 }
3588 else
3589 {
3590 if (*p == '\\' && *(p + 1) != NUL)
3591 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003592 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003593 }
3594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003596 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003598 int c;
3599 int in_quote = FALSE;
3600 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
3602 /*
3603 * Allow spaces within back-quotes to count as part of the argument
3604 * being expanded.
3605 */
3606 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003607 p = xp->xp_pattern;
3608 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003610 if (has_mbyte)
3611 c = mb_ptr2char(p);
3612 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003613 c = *p;
3614 if (c == '\\' && p[1] != NUL)
3615 ++p;
3616 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 if (!in_quote)
3619 {
3620 xp->xp_pattern = p;
3621 bow = p + 1;
3622 }
3623 in_quote = !in_quote;
3624 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003625 /* An argument can contain just about everything, except
3626 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003627 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003628#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003629 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003630#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003631 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003632 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003633 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003634 while (*p != NUL)
3635 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003636 if (has_mbyte)
3637 c = mb_ptr2char(p);
3638 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003639 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003640 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003641 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003642 if (has_mbyte)
3643 len = (*mb_ptr2len)(p);
3644 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003645 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003646 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003647 }
3648 if (in_quote)
3649 bow = p;
3650 else
3651 xp->xp_pattern = p;
3652 p -= len;
3653 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003654 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656
3657 /*
3658 * If we are still inside the quotes, and we passed a space, just
3659 * expand from there.
3660 */
3661 if (bow != NULL && in_quote)
3662 xp->xp_pattern = bow;
3663 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003664
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003665 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003666 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003667 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003668#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003669 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003670#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003671 /* When still after the command name expand executables. */
3672 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003673 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
3676 /* Check for environment variable */
3677 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003678#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 || *xp->xp_pattern == '%'
3680#endif
3681 )
3682 {
3683 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3684 if (!vim_isIDc(*p))
3685 break;
3686 if (*p == NUL)
3687 {
3688 xp->xp_context = EXPAND_ENV_VARS;
3689 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003690#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003691 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003692 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003693 compl = EXPAND_ENV_VARS;
3694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003697#if defined(FEAT_CMDL_COMPL)
3698 /* Check for user names */
3699 if (*xp->xp_pattern == '~')
3700 {
3701 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3702 ;
3703 /* Complete ~user only if it partially matches a user name.
3704 * A full match ~user<Tab> will be replaced by user's home
3705 * directory i.e. something like ~user<Tab> -> /home/user/ */
3706 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003707 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003708 {
3709 xp->xp_context = EXPAND_USER;
3710 ++xp->xp_pattern;
3711 }
3712 }
3713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
3715
3716/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003717 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003719 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003721 case CMD_find:
3722 case CMD_sfind:
3723 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003724 if (xp->xp_context == EXPAND_FILES)
3725 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003726 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 case CMD_cd:
3728 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003729 case CMD_tcd:
3730 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 case CMD_lcd:
3732 case CMD_lchdir:
3733 if (xp->xp_context == EXPAND_FILES)
3734 xp->xp_context = EXPAND_DIRECTORIES;
3735 break;
3736 case CMD_help:
3737 xp->xp_context = EXPAND_HELP;
3738 xp->xp_pattern = arg;
3739 break;
3740
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003741 /* Command modifiers: return the argument.
3742 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003744 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 case CMD_belowright:
3746 case CMD_botright:
3747 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003748 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003749 case CMD_cdo:
3750 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003752 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 case CMD_folddoclosed:
3754 case CMD_folddoopen:
3755 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003756 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 case CMD_keepjumps:
3758 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003759 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003760 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003762 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003764 case CMD_noautocmd:
3765 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003767 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003769 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003770 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 case CMD_topleft:
3772 case CMD_verbose:
3773 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003774 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 return arg;
3776
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003777 case CMD_filter:
3778 if (*arg != NUL)
3779 arg = skip_vimgrep_pat(arg, NULL, NULL);
3780 if (arg == NULL || *arg == NUL)
3781 {
3782 xp->xp_context = EXPAND_NOTHING;
3783 return NULL;
3784 }
3785 return skipwhite(arg);
3786
Bram Moolenaar4f688582007-07-24 12:34:30 +00003787#ifdef FEAT_CMDL_COMPL
3788# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 case CMD_match:
3790 if (*arg == NUL || !ends_excmd(*arg))
3791 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003792 /* also complete "None" */
3793 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 arg = skipwhite(skiptowhite(arg));
3795 if (*arg != NUL)
3796 {
3797 xp->xp_context = EXPAND_NOTHING;
3798 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3799 }
3800 }
3801 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804/*
3805 * All completion for the +cmdline_compl feature goes here.
3806 */
3807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003809 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811 case CMD_delcommand:
3812 xp->xp_context = EXPAND_USER_COMMANDS;
3813 xp->xp_pattern = arg;
3814 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815
3816 case CMD_global:
3817 case CMD_vglobal:
3818 delim = *arg; /* get the delimiter */
3819 if (delim)
3820 ++arg; /* skip delimiter if there is one */
3821
3822 while (arg[0] != NUL && arg[0] != delim)
3823 {
3824 if (arg[0] == '\\' && arg[1] != NUL)
3825 ++arg;
3826 ++arg;
3827 }
3828 if (arg[0] != NUL)
3829 return arg + 1;
3830 break;
3831 case CMD_and:
3832 case CMD_substitute:
3833 delim = *arg;
3834 if (delim)
3835 {
3836 /* skip "from" part */
3837 ++arg;
3838 arg = skip_regexp(arg, delim, p_magic, NULL);
3839 }
3840 /* skip "to" part */
3841 while (arg[0] != NUL && arg[0] != delim)
3842 {
3843 if (arg[0] == '\\' && arg[1] != NUL)
3844 ++arg;
3845 ++arg;
3846 }
3847 if (arg[0] != NUL) /* skip delimiter */
3848 ++arg;
3849 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3850 ++arg;
3851 if (arg[0] != NUL)
3852 return arg;
3853 break;
3854 case CMD_isearch:
3855 case CMD_dsearch:
3856 case CMD_ilist:
3857 case CMD_dlist:
3858 case CMD_ijump:
3859 case CMD_psearch:
3860 case CMD_djump:
3861 case CMD_isplit:
3862 case CMD_dsplit:
3863 arg = skipwhite(skipdigits(arg)); /* skip count */
3864 if (*arg == '/') /* Match regexp, not just whole words */
3865 {
3866 for (++arg; *arg && *arg != '/'; arg++)
3867 if (*arg == '\\' && arg[1] != NUL)
3868 arg++;
3869 if (*arg)
3870 {
3871 arg = skipwhite(arg + 1);
3872
3873 /* Check for trailing illegal characters */
3874 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3875 xp->xp_context = EXPAND_NOTHING;
3876 else
3877 return arg;
3878 }
3879 }
3880 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 case CMD_autocmd:
3883 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003885 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_set:
3888 set_context_in_set_cmd(xp, arg, 0);
3889 break;
3890 case CMD_setglobal:
3891 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3892 break;
3893 case CMD_setlocal:
3894 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3895 break;
3896 case CMD_tag:
3897 case CMD_stag:
3898 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003899 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_tselect:
3901 case CMD_stselect:
3902 case CMD_ptselect:
3903 case CMD_tjump:
3904 case CMD_stjump:
3905 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003906 if (*p_wop != NUL)
3907 xp->xp_context = EXPAND_TAGS_LISTFILES;
3908 else
3909 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 xp->xp_pattern = arg;
3911 break;
3912 case CMD_augroup:
3913 xp->xp_context = EXPAND_AUGROUP;
3914 xp->xp_pattern = arg;
3915 break;
3916#ifdef FEAT_SYN_HL
3917 case CMD_syntax:
3918 set_context_in_syntax_cmd(xp, arg);
3919 break;
3920#endif
3921#ifdef FEAT_EVAL
3922 case CMD_let:
3923 case CMD_if:
3924 case CMD_elseif:
3925 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003926 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 case CMD_echo:
3928 case CMD_echon:
3929 case CMD_execute:
3930 case CMD_echomsg:
3931 case CMD_echoerr:
3932 case CMD_call:
3933 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003934 case CMD_cexpr:
3935 case CMD_caddexpr:
3936 case CMD_cgetexpr:
3937 case CMD_lexpr:
3938 case CMD_laddexpr:
3939 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003940 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 break;
3942
3943 case CMD_unlet:
3944 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3945 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 xp->xp_context = EXPAND_USER_VARS;
3948 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003949
3950 if (*xp->xp_pattern == '$')
3951 {
3952 xp->xp_context = EXPAND_ENV_VARS;
3953 ++xp->xp_pattern;
3954 }
3955
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 break;
3957
3958 case CMD_function:
3959 case CMD_delfunction:
3960 xp->xp_context = EXPAND_USER_FUNC;
3961 xp->xp_pattern = arg;
3962 break;
3963
3964 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003965 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 break;
3967#endif
3968 case CMD_highlight:
3969 set_context_in_highlight_cmd(xp, arg);
3970 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003971#ifdef FEAT_CSCOPE
3972 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003973 case CMD_lcscope:
3974 case CMD_scscope:
3975 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003976 break;
3977#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003978#ifdef FEAT_SIGNS
3979 case CMD_sign:
3980 set_context_in_sign_cmd(xp, arg);
3981 break;
3982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 case CMD_bdelete:
3984 case CMD_bwipeout:
3985 case CMD_bunload:
3986 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3987 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003988 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 case CMD_buffer:
3990 case CMD_sbuffer:
3991 case CMD_checktime:
3992 xp->xp_context = EXPAND_BUFFERS;
3993 xp->xp_pattern = arg;
3994 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003995
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 case CMD_USER:
3997 case CMD_USER_BUF:
3998 if (compl != EXPAND_NOTHING)
3999 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004000 // XFILE: file names are handled above
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004001 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004003#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 if (compl == EXPAND_MENUS)
4005 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (compl == EXPAND_COMMANDS)
4008 return arg;
4009 if (compl == EXPAND_MAPPINGS)
4010 return set_context_in_map_cmd(xp, (char_u *)"map",
4011 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004012 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004013 p = arg;
4014 while (*p)
4015 {
4016 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004017 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004018 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004019 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004020 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004021 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 xp->xp_pattern = arg;
4024 }
4025 xp->xp_context = compl;
4026 }
4027 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 case CMD_map: case CMD_noremap:
4030 case CMD_nmap: case CMD_nnoremap:
4031 case CMD_vmap: case CMD_vnoremap:
4032 case CMD_omap: case CMD_onoremap:
4033 case CMD_imap: case CMD_inoremap:
4034 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004035 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004036 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004037 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004038 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004040 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 case CMD_unmap:
4042 case CMD_nunmap:
4043 case CMD_vunmap:
4044 case CMD_ounmap:
4045 case CMD_iunmap:
4046 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004047 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004048 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004049 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004050 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004052 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004053 case CMD_mapclear:
4054 case CMD_nmapclear:
4055 case CMD_vmapclear:
4056 case CMD_omapclear:
4057 case CMD_imapclear:
4058 case CMD_cmapclear:
4059 case CMD_lmapclear:
4060 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004061 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004062 case CMD_xmapclear:
4063 xp->xp_context = EXPAND_MAPCLEAR;
4064 xp->xp_pattern = arg;
4065 break;
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 case CMD_abbreviate: case CMD_noreabbrev:
4068 case CMD_cabbrev: case CMD_cnoreabbrev:
4069 case CMD_iabbrev: case CMD_inoreabbrev:
4070 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004071 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 case CMD_unabbreviate:
4073 case CMD_cunabbrev:
4074 case CMD_iunabbrev:
4075 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004076 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077#ifdef FEAT_MENU
4078 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4079 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4080 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4081 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4082 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4083 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4084 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004085 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 case CMD_tmenu: case CMD_tunmenu:
4087 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4088 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4089#endif
4090
4091 case CMD_colorscheme:
4092 xp->xp_context = EXPAND_COLORS;
4093 xp->xp_pattern = arg;
4094 break;
4095
4096 case CMD_compiler:
4097 xp->xp_context = EXPAND_COMPILER;
4098 xp->xp_pattern = arg;
4099 break;
4100
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004101 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004102 xp->xp_context = EXPAND_OWNSYNTAX;
4103 xp->xp_pattern = arg;
4104 break;
4105
4106 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004107 xp->xp_context = EXPAND_FILETYPE;
4108 xp->xp_pattern = arg;
4109 break;
4110
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004111 case CMD_packadd:
4112 xp->xp_context = EXPAND_PACKADD;
4113 xp->xp_pattern = arg;
4114 break;
4115
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004116#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004118 p = skiptowhite(arg);
4119 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121 xp->xp_context = EXPAND_LANGUAGE;
4122 xp->xp_pattern = arg;
4123 }
4124 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004125 {
4126 if ( STRNCMP(arg, "messages", p - arg) == 0
4127 || STRNCMP(arg, "ctype", p - arg) == 0
4128 || STRNCMP(arg, "time", p - arg) == 0)
4129 {
4130 xp->xp_context = EXPAND_LOCALES;
4131 xp->xp_pattern = skipwhite(p);
4132 }
4133 else
4134 xp->xp_context = EXPAND_NOTHING;
4135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 break;
4137#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004138#if defined(FEAT_PROFILE)
4139 case CMD_profile:
4140 set_context_in_profile_cmd(xp, arg);
4141 break;
4142#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004143 case CMD_behave:
4144 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004145 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004146 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004148 case CMD_messages:
4149 xp->xp_context = EXPAND_MESSAGES;
4150 xp->xp_pattern = arg;
4151 break;
4152
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004153#if defined(FEAT_CMDHIST)
4154 case CMD_history:
4155 xp->xp_context = EXPAND_HISTORY;
4156 xp->xp_pattern = arg;
4157 break;
4158#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004159#if defined(FEAT_PROFILE)
4160 case CMD_syntime:
4161 xp->xp_context = EXPAND_SYNTIME;
4162 xp->xp_pattern = arg;
4163 break;
4164#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004165
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004166 case CMD_argdelete:
4167 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4168 arg = xp->xp_pattern + 1;
4169 xp->xp_context = EXPAND_ARGLIST;
4170 xp->xp_pattern = arg;
4171 break;
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173#endif /* FEAT_CMDL_COMPL */
4174
4175 default:
4176 break;
4177 }
4178 return NULL;
4179}
4180
4181/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004182 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 *
4184 * Backslashed delimiters after / or ? will be skipped, and commands will
4185 * not be expanded between /'s and ?'s or after "'".
4186 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004187 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 * Returns the "cmd" pointer advanced to beyond the range.
4189 */
4190 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004191skip_range(
4192 char_u *cmd,
4193 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004195 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004197 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004199 if (*cmd == '\\')
4200 {
4201 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4202 ++cmd;
4203 else
4204 break;
4205 }
4206 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
4208 if (*++cmd == NUL && ctx != NULL)
4209 *ctx = EXPAND_NOTHING;
4210 }
4211 else if (*cmd == '/' || *cmd == '?')
4212 {
4213 delim = *cmd++;
4214 while (*cmd != NUL && *cmd != delim)
4215 if (*cmd++ == '\\' && *cmd != NUL)
4216 ++cmd;
4217 if (*cmd == NUL && ctx != NULL)
4218 *ctx = EXPAND_NOTHING;
4219 }
4220 if (*cmd != NUL)
4221 ++cmd;
4222 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004223
4224 /* Skip ":" and white space. */
4225 while (*cmd == ':')
4226 cmd = skipwhite(cmd + 1);
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return cmd;
4229}
4230
4231/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004232 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 *
4234 * Set ptr to the next character after the part that was interpreted.
4235 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004236 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 *
4238 * Return MAXLNUM when no Ex address was found.
4239 */
4240 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004241get_address(
4242 exarg_T *eap UNUSED,
4243 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004244 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004245 int skip, // only skip the address, don't use it
4246 int silent, // no errors or side effects
4247 int to_other_file, // flag: may jump to other file
4248 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004250 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 int c;
4252 int i;
4253 long n;
4254 char_u *cmd;
4255 pos_T pos;
4256 pos_T *fp;
4257 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004258 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 cmd = skipwhite(*ptr);
4261 lnum = MAXLNUM;
4262 do
4263 {
4264 switch (*cmd)
4265 {
4266 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004267 ++cmd;
4268 switch (addr_type)
4269 {
4270 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004271 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 lnum = curwin->w_cursor.lnum;
4273 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004274 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004275 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004276 break;
4277 case ADDR_ARGUMENTS:
4278 lnum = curwin->w_arg_idx + 1;
4279 break;
4280 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004281 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004282 lnum = curbuf->b_fnum;
4283 break;
4284 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004285 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004287 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004288 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004289 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004290 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004291 cmd = NULL;
4292 goto error;
4293 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004294 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004295#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004296 lnum = qf_get_cur_idx(eap);
4297#endif
4298 break;
4299 case ADDR_QUICKFIX_VALID:
4300#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004301 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004302#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004303 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004304 }
4305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004308 ++cmd;
4309 switch (addr_type)
4310 {
4311 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004312 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 lnum = curbuf->b_ml.ml_line_count;
4314 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004315 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004316 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004317 break;
4318 case ADDR_ARGUMENTS:
4319 lnum = ARGCOUNT;
4320 break;
4321 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004322 buf = lastbuf;
4323 while (buf->b_ml.ml_mfp == NULL)
4324 {
4325 if (buf->b_prev == NULL)
4326 break;
4327 buf = buf->b_prev;
4328 }
4329 lnum = buf->b_fnum;
4330 break;
4331 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004332 lnum = lastbuf->b_fnum;
4333 break;
4334 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004335 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004337 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004338 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004339 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004340 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004341 cmd = NULL;
4342 goto error;
4343 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004344 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004345#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004346 lnum = qf_get_size(eap);
4347 if (lnum == 0)
4348 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004349#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004350 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004351 case ADDR_QUICKFIX_VALID:
4352#ifdef FEAT_QUICKFIX
4353 lnum = qf_get_valid_size(eap);
4354 if (lnum == 0)
4355 lnum = 1;
4356#endif
4357 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 }
4359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004362 if (*++cmd == NUL)
4363 {
4364 cmd = NULL;
4365 goto error;
4366 }
4367 if (addr_type != ADDR_LINES)
4368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004369 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004370 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 goto error;
4372 }
4373 if (skip)
4374 ++cmd;
4375 else
4376 {
4377 /* Only accept a mark in another file when it is
4378 * used by itself: ":'M". */
4379 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4380 ++cmd;
4381 if (fp == (pos_T *)-1)
4382 /* Jumped to another file. */
4383 lnum = curwin->w_cursor.lnum;
4384 else
4385 {
4386 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
4388 cmd = NULL;
4389 goto error;
4390 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 lnum = fp->lnum;
4392 }
4393 }
4394 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 case '/':
4397 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 c = *cmd++;
4399 if (addr_type != ADDR_LINES)
4400 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004401 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004402 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004403 goto error;
4404 }
4405 if (skip) /* skip "/pat/" */
4406 {
4407 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4408 if (*cmd == c)
4409 ++cmd;
4410 }
4411 else
4412 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004413 int flags;
4414
4415 pos = curwin->w_cursor; // save curwin->w_cursor
4416
4417 // When '/' or '?' follows another address, start from
4418 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004419 if (lnum != MAXLNUM)
4420 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004421
4422 // Start a forward search at the end of the line (unless
4423 // before the first line).
4424 // Start a backward search at the start of the line.
4425 // This makes sure we never match in the current
4426 // line, and can match anywhere in the
4427 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004428 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 curwin->w_cursor.col = MAXCOL;
4430 else
4431 curwin->w_cursor.col = 0;
4432 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004433 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4434 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 {
4436 curwin->w_cursor = pos;
4437 cmd = NULL;
4438 goto error;
4439 }
4440 lnum = curwin->w_cursor.lnum;
4441 curwin->w_cursor = pos;
4442 /* adjust command string pointer */
4443 cmd += searchcmdlen;
4444 }
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 ++cmd;
4449 if (addr_type != ADDR_LINES)
4450 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004451 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004452 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004453 goto error;
4454 }
4455 if (*cmd == '&')
4456 i = RE_SUBST;
4457 else if (*cmd == '?' || *cmd == '/')
4458 i = RE_SEARCH;
4459 else
4460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004461 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004462 cmd = NULL;
4463 goto error;
4464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 if (!skip)
4467 {
4468 /*
4469 * When search follows another address, start from
4470 * there.
4471 */
4472 if (lnum != MAXLNUM)
4473 pos.lnum = lnum;
4474 else
4475 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004477 /*
4478 * Start the search just like for the above
4479 * do_search().
4480 */
4481 if (*cmd != '?')
4482 pos.col = MAXCOL;
4483 else
4484 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004485 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004486 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 *cmd == '?' ? BACKWARD : FORWARD,
4488 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004489 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 lnum = pos.lnum;
4491 else
4492 {
4493 cmd = NULL;
4494 goto error;
4495 }
4496 }
4497 ++cmd;
4498 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
4500 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4502 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504
4505 for (;;)
4506 {
4507 cmd = skipwhite(cmd);
4508 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4509 break;
4510
4511 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 {
4513 switch (addr_type)
4514 {
4515 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004516 case ADDR_OTHER:
4517 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004518 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004519 break;
4520 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004521 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 break;
4523 case ADDR_ARGUMENTS:
4524 lnum = curwin->w_arg_idx + 1;
4525 break;
4526 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004527 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004528 lnum = curbuf->b_fnum;
4529 break;
4530 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004531 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004533 case ADDR_TABS_RELATIVE:
4534 lnum = 1;
4535 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004536 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004537#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004538 lnum = qf_get_cur_idx(eap);
4539#endif
4540 break;
4541 case ADDR_QUICKFIX_VALID:
4542#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004543 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004544#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004545 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004546 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004547 case ADDR_UNSIGNED:
4548 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004549 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004550 }
4551 }
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (VIM_ISDIGIT(*cmd))
4554 i = '+'; /* "number" is same as "+number" */
4555 else
4556 i = *cmd++;
4557 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4558 n = 1;
4559 else
4560 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004561
4562 if (addr_type == ADDR_TABS_RELATIVE)
4563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004564 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004565 cmd = NULL;
4566 goto error;
4567 }
4568 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004569 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004570 lnum = compute_buffer_local_count(
4571 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004573 {
4574#ifdef FEAT_FOLDING
4575 /* Relative line addressing, need to adjust for folded lines
4576 * now, but only do it after the first address. */
4577 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4578 && address_count >= 2)
4579 (void)hasFolding(lnum, NULL, &lnum);
4580#endif
4581 if (i == '-')
4582 lnum -= n;
4583 else
4584 lnum += n;
4585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
4587 } while (*cmd == '/' || *cmd == '?');
4588
4589error:
4590 *ptr = cmd;
4591 return lnum;
4592}
4593
4594/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004595 * Get flags from an Ex command argument.
4596 */
4597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004598get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004599{
4600 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4601 {
4602 if (*eap->arg == 'l')
4603 eap->flags |= EXFLAG_LIST;
4604 else if (*eap->arg == 'p')
4605 eap->flags |= EXFLAG_PRINT;
4606 else
4607 eap->flags |= EXFLAG_NR;
4608 eap->arg = skipwhite(eap->arg + 1);
4609 }
4610}
4611
4612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 * Function called for command which is Not Implemented. NI!
4614 */
4615 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004616ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004619 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620}
4621
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004622#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623/*
4624 * Function called for script command which is Not Implemented. NI!
4625 * Skips over ":perl <<EOF" constructs.
4626 */
4627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004628ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 if (!eap->skip)
4631 ex_ni(eap);
4632 else
4633 vim_free(script_get(eap, eap->arg));
4634}
4635#endif
4636
4637/*
4638 * Check range in Ex command for validity.
4639 * Return NULL when valid, error message when invalid.
4640 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004641 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004642invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004644 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004645
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 if ( eap->line1 < 0
4647 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004648 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004649 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004650
4651 if (eap->argt & RANGE)
4652 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004653 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654 {
4655 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004656 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004657#ifdef FEAT_DIFF
4658 + (eap->cmdidx == CMD_diffget)
4659#endif
4660 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004661 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004662 break;
4663 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004664 /* add 1 if ARGCOUNT is 0 */
4665 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004666 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004667 break;
4668 case ADDR_BUFFERS:
4669 if (eap->line1 < firstbuf->b_fnum
4670 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004671 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004672 break;
4673 case ADDR_LOADED_BUFFERS:
4674 buf = firstbuf;
4675 while (buf->b_ml.ml_mfp == NULL)
4676 {
4677 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004678 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004679 buf = buf->b_next;
4680 }
4681 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004682 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004683 buf = lastbuf;
4684 while (buf->b_ml.ml_mfp == NULL)
4685 {
4686 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004687 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004688 buf = buf->b_prev;
4689 }
4690 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004691 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004692 break;
4693 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004694 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004695 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004696 break;
4697 case ADDR_TABS:
4698 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004699 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004700 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004701 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004702 case ADDR_OTHER:
4703 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004704 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004705 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004706#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004707 // No error for value that is too big, will use the last entry.
4708 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004709 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004710#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004711 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004712 case ADDR_QUICKFIX_VALID:
4713#ifdef FEAT_QUICKFIX
4714 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4715 || eap->line2 < 0)
4716 return _(e_invrange);
4717#endif
4718 break;
4719 case ADDR_UNSIGNED:
4720 if (eap->line2 < 0)
4721 return _(e_invrange);
4722 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004723 case ADDR_NONE:
4724 // Will give an error elsewhere.
4725 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004726 }
4727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 return NULL;
4729}
4730
4731/*
4732 * Correct the range for zero line number, if required.
4733 */
4734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004735correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4738 {
4739 if (eap->line1 == 0)
4740 eap->line1 = 1;
4741 if (eap->line2 == 0)
4742 eap->line2 = 1;
4743 }
4744}
4745
Bram Moolenaar748bf032005-02-02 23:04:36 +00004746#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004747/*
4748 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4749 * pattern. Otherwise return eap->arg.
4750 */
4751 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004752skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004753{
4754 char_u *p = eap->arg;
4755
Bram Moolenaara37420f2006-02-04 22:37:47 +00004756 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4757 || eap->cmdidx == CMD_vimgrepadd
4758 || eap->cmdidx == CMD_lvimgrepadd
4759 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004760 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004761 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004762 if (p == NULL)
4763 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004764 }
4765 return p;
4766}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004767
4768/*
4769 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4770 * in the command line, so that things like % get expanded.
4771 */
4772 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004773replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004774{
4775 char_u *new_cmdline;
4776 char_u *program;
4777 char_u *pos;
4778 char_u *ptr;
4779 int len;
4780 int i;
4781
4782 /*
4783 * Don't do it when ":vimgrep" is used for ":grep".
4784 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004785 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4786 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4787 || eap->cmdidx == CMD_grepadd
4788 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004789 && !grep_internal(eap->cmdidx))
4790 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004791 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4792 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004793 {
4794 if (*curbuf->b_p_gp == NUL)
4795 program = p_gp;
4796 else
4797 program = curbuf->b_p_gp;
4798 }
4799 else
4800 {
4801 if (*curbuf->b_p_mp == NUL)
4802 program = p_mp;
4803 else
4804 program = curbuf->b_p_mp;
4805 }
4806
4807 p = skipwhite(p);
4808
4809 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4810 {
4811 /* replace $* by given arguments */
4812 i = 1;
4813 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4814 ++i;
4815 len = (int)STRLEN(p);
4816 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4817 if (new_cmdline == NULL)
4818 return NULL; /* out of memory */
4819 ptr = new_cmdline;
4820 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4821 {
4822 i = (int)(pos - program);
4823 STRNCPY(ptr, program, i);
4824 STRCPY(ptr += i, p);
4825 ptr += len;
4826 program = pos + 2;
4827 }
4828 STRCPY(ptr, program);
4829 }
4830 else
4831 {
4832 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4833 if (new_cmdline == NULL)
4834 return NULL; /* out of memory */
4835 STRCPY(new_cmdline, program);
4836 STRCAT(new_cmdline, " ");
4837 STRCAT(new_cmdline, p);
4838 }
4839 msg_make(p);
4840
4841 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4842 vim_free(*cmdlinep);
4843 *cmdlinep = new_cmdline;
4844 p = new_cmdline;
4845 }
4846 return p;
4847}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004848#endif
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850/*
4851 * Expand file name in Ex command argument.
4852 * Return FAIL for failure, OK otherwise.
4853 */
4854 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004855expand_filename(
4856 exarg_T *eap,
4857 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004858 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
4860 int has_wildcards; /* need to expand wildcards */
4861 char_u *repl;
4862 int srclen;
4863 char_u *p;
4864 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004865 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
Bram Moolenaar748bf032005-02-02 23:04:36 +00004867#ifdef FEAT_QUICKFIX
4868 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4869 p = skip_grep_pat(eap);
4870#else
4871 p = eap->arg;
4872#endif
4873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 /*
4875 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4876 * the file name contains a wildcard it should not cause expanding.
4877 * (it will be expanded anyway if there is a wildcard before replacing).
4878 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004879 has_wildcards = mch_has_wildcard(p);
4880 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004882#ifdef FEAT_EVAL
4883 /* Skip over `=expr`, wildcards in it are not expanded. */
4884 if (p[0] == '`' && p[1] == '=')
4885 {
4886 p += 2;
4887 (void)skip_expr(&p);
4888 if (*p == '`')
4889 ++p;
4890 continue;
4891 }
4892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 /*
4894 * Quick check if this cannot be the start of a special string.
4895 * Also removes backslash before '%', '#' and '<'.
4896 */
4897 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4898 {
4899 ++p;
4900 continue;
4901 }
4902
4903 /*
4904 * Try to find a match at this position.
4905 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004906 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4907 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 if (*errormsgp != NULL) /* error detected */
4909 return FAIL;
4910 if (repl == NULL) /* no match found */
4911 {
4912 p += srclen;
4913 continue;
4914 }
4915
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004916 /* Wildcards won't be expanded below, the replacement is taken
4917 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4918 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4919 {
4920 char_u *l = repl;
4921
4922 repl = expand_env_save(repl);
4923 vim_free(l);
4924 }
4925
Bram Moolenaar63b92542007-03-27 14:57:09 +00004926 /* Need to escape white space et al. with a backslash.
4927 * Don't do this for:
4928 * - replacement that already has been escaped: "##"
4929 * - shell commands (may have to use quotes instead).
4930 * - non-unix systems when there is a single argument (spaces don't
4931 * separate arguments then).
4932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004934 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 && eap->cmdidx != CMD_grep
4937 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004938 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004939 && eap->cmdidx != CMD_lgrep
4940 && eap->cmdidx != CMD_lgrepadd
4941 && eap->cmdidx != CMD_lmake
4942 && eap->cmdidx != CMD_make
4943 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#ifndef UNIX
4945 && !(eap->argt & NOSPC)
4946#endif
4947 )
4948 {
4949 char_u *l;
4950#ifdef BACKSLASH_IN_FILENAME
4951 /* Don't escape a backslash here, because rem_backslash() doesn't
4952 * remove it later. */
4953 static char_u *nobslash = (char_u *)" \t\"|";
4954# define ESCAPE_CHARS nobslash
4955#else
4956# define ESCAPE_CHARS escape_chars
4957#endif
4958
4959 for (l = repl; *l; ++l)
4960 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4961 {
4962 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4963 if (l != NULL)
4964 {
4965 vim_free(repl);
4966 repl = l;
4967 }
4968 break;
4969 }
4970 }
4971
4972 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004973 if ((eap->usefilter || eap->cmdidx == CMD_bang
4974 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004975 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 char_u *l;
4978
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004979 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (l != NULL)
4981 {
4982 vim_free(repl);
4983 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 }
4985 }
4986
4987 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4988 vim_free(repl);
4989 if (p == NULL)
4990 return FAIL;
4991 }
4992
4993 /*
4994 * One file argument: Expand wildcards.
4995 * Don't do this with ":r !command" or ":w !command".
4996 */
4997 if ((eap->argt & NOSPC) && !eap->usefilter)
4998 {
4999 /*
5000 * May do this twice:
5001 * 1. Replace environment variables.
5002 * 2. Replace any other wildcards, remove backslashes.
5003 */
5004 for (n = 1; n <= 2; ++n)
5005 {
5006 if (n == 2)
5007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 /*
5009 * Halve the number of backslashes (this is Vi compatible).
5010 * For Unix and OS/2, when wildcards are expanded, this is
5011 * done by ExpandOne() below.
5012 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005013#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 if (!has_wildcards)
5015#endif
5016 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
5018
5019 if (has_wildcards)
5020 {
5021 if (n == 1)
5022 {
5023 /*
5024 * First loop: May expand environment variables. This
5025 * can be done much faster with expand_env() than with
5026 * something else (e.g., calling a shell).
5027 * After expanding environment variables, check again
5028 * if there are still wildcards present.
5029 */
5030 if (vim_strchr(eap->arg, '$') != NULL
5031 || vim_strchr(eap->arg, '~') != NULL)
5032 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005033 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005034 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 has_wildcards = mch_has_wildcard(NameBuff);
5036 p = NameBuff;
5037 }
5038 else
5039 p = NULL;
5040 }
5041 else /* n == 2 */
5042 {
5043 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005044 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045
5046 ExpandInit(&xpc);
5047 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005048 if (p_wic)
5049 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005051 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 if (p == NULL)
5053 return FAIL;
5054 }
5055 if (p != NULL)
5056 {
5057 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5058 p, cmdlinep);
5059 if (n == 2) /* p came from ExpandOne() */
5060 vim_free(p);
5061 }
5062 }
5063 }
5064 }
5065 return OK;
5066}
5067
5068/*
5069 * Replace part of the command line, keeping eap->cmd, eap->arg and
5070 * eap->nextcmd correct.
5071 * "src" points to the part that is to be replaced, of length "srclen".
5072 * "repl" is the replacement string.
5073 * Returns a pointer to the character after the replaced string.
5074 * Returns NULL for failure.
5075 */
5076 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005077repl_cmdline(
5078 exarg_T *eap,
5079 char_u *src,
5080 int srclen,
5081 char_u *repl,
5082 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
5084 int len;
5085 int i;
5086 char_u *new_cmdline;
5087
5088 /*
5089 * The new command line is build in new_cmdline[].
5090 * First allocate it.
5091 * Careful: a "+cmd" argument may have been NUL terminated.
5092 */
5093 len = (int)STRLEN(repl);
5094 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005095 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005097 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 return NULL; /* out of memory! */
5099
5100 /*
5101 * Copy the stuff before the expanded part.
5102 * Copy the expanded stuff.
5103 * Copy what came after the expanded part.
5104 * Copy the next commands, if there are any.
5105 */
5106 i = (int)(src - *cmdlinep); /* length of part before match */
5107 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005108
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 mch_memmove(new_cmdline + i, repl, (size_t)len);
5110 i += len; /* remember the end of the string */
5111 STRCPY(new_cmdline + i, src + srclen);
5112 src = new_cmdline + i; /* remember where to continue */
5113
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005114 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 i = (int)STRLEN(new_cmdline) + 1;
5117 STRCPY(new_cmdline + i, eap->nextcmd);
5118 eap->nextcmd = new_cmdline + i;
5119 }
5120 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5121 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5122 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5123 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5124 vim_free(*cmdlinep);
5125 *cmdlinep = new_cmdline;
5126
5127 return src;
5128}
5129
5130/*
5131 * Check for '|' to separate commands and '"' to start comments.
5132 */
5133 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005134separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135{
5136 char_u *p;
5137
Bram Moolenaar86b68352004-12-27 21:59:20 +00005138#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005139 p = skip_grep_pat(eap);
5140#else
5141 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005142#endif
5143
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005144 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 if (*p == Ctrl_V)
5147 {
5148 if (eap->argt & (USECTRLV | XFILE))
5149 ++p; /* skip CTRL-V and next char */
5150 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005151 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005152 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (*p == NUL) /* stop at NUL after CTRL-V */
5154 break;
5155 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005156
5157#ifdef FEAT_EVAL
5158 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005159 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005160 {
5161 p += 2;
5162 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005163 }
5164#endif
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 /* Check for '"': start of comment or '|': next command */
5167 /* :@" and :*" do not start a comment!
5168 * :redir @" doesn't either. */
5169 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5170 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5171 || p != eap->arg)
5172 && (eap->cmdidx != CMD_redir
5173 || p != eap->arg + 1 || p[-1] != '@'))
5174 || *p == '|' || *p == '\n')
5175 {
5176 /*
5177 * We remove the '\' before the '|', unless USECTRLV is used
5178 * AND 'b' is present in 'cpoptions'.
5179 */
5180 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5181 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5182 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005183 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 --p;
5185 }
5186 else
5187 {
5188 eap->nextcmd = check_nextcmd(p);
5189 *p = NUL;
5190 break;
5191 }
5192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5196 del_trailing_spaces(eap->arg);
5197}
5198
5199/*
5200 * get + command from ex argument
5201 */
5202 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005203getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204{
5205 char_u *arg = *argp;
5206 char_u *command = NULL;
5207
5208 if (*arg == '+') /* +[command] */
5209 {
5210 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005211 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 command = dollar_command;
5213 else
5214 {
5215 command = arg;
5216 arg = skip_cmd_arg(command, TRUE);
5217 if (*arg != NUL)
5218 *arg++ = NUL; /* terminate command with NUL */
5219 }
5220
5221 arg = skipwhite(arg); /* skip over spaces */
5222 *argp = arg;
5223 }
5224 return command;
5225}
5226
5227/*
5228 * Find end of "+command" argument. Skip over "\ " and "\\".
5229 */
5230 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005231skip_cmd_arg(
5232 char_u *p,
5233 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234{
5235 while (*p && !vim_isspace(*p))
5236 {
5237 if (*p == '\\' && p[1] != NUL)
5238 {
5239 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005240 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 else
5242 ++p;
5243 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005244 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 }
5246 return p;
5247}
5248
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005249 int
5250get_bad_opt(char_u *p, exarg_T *eap)
5251{
5252 if (STRICMP(p, "keep") == 0)
5253 eap->bad_char = BAD_KEEP;
5254 else if (STRICMP(p, "drop") == 0)
5255 eap->bad_char = BAD_DROP;
5256 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5257 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005258 else
5259 return FAIL;
5260 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005261}
5262
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263/*
5264 * Get "++opt=arg" argument.
5265 * Return FAIL or OK.
5266 */
5267 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005268getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269{
5270 char_u *arg = eap->arg + 2;
5271 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005272 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 /* ":edit ++[no]bin[ary] file" */
5276 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5277 {
5278 if (*arg == 'n')
5279 {
5280 arg += 2;
5281 eap->force_bin = FORCE_NOBIN;
5282 }
5283 else
5284 eap->force_bin = FORCE_BIN;
5285 if (!checkforcmd(&arg, "binary", 3))
5286 return FAIL;
5287 eap->arg = skipwhite(arg);
5288 return OK;
5289 }
5290
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005291 /* ":read ++edit file" */
5292 if (STRNCMP(arg, "edit", 4) == 0)
5293 {
5294 eap->read_edit = TRUE;
5295 eap->arg = skipwhite(arg + 4);
5296 return OK;
5297 }
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 if (STRNCMP(arg, "ff", 2) == 0)
5300 {
5301 arg += 2;
5302 pp = &eap->force_ff;
5303 }
5304 else if (STRNCMP(arg, "fileformat", 10) == 0)
5305 {
5306 arg += 10;
5307 pp = &eap->force_ff;
5308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 else if (STRNCMP(arg, "enc", 3) == 0)
5310 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005311 if (STRNCMP(arg, "encoding", 8) == 0)
5312 arg += 8;
5313 else
5314 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 pp = &eap->force_enc;
5316 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005317 else if (STRNCMP(arg, "bad", 3) == 0)
5318 {
5319 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005320 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323 if (pp == NULL || *arg != '=')
5324 return FAIL;
5325
5326 ++arg;
5327 *pp = (int)(arg - eap->cmd);
5328 arg = skip_cmd_arg(arg, FALSE);
5329 eap->arg = skipwhite(arg);
5330 *arg = NUL;
5331
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (pp == &eap->force_ff)
5333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5335 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005336 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005338 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 {
5340 /* Make 'fileencoding' lower case. */
5341 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5342 *p = TOLOWER_ASC(*p);
5343 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005344 else
5345 {
5346 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5347 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005348 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005349 return FAIL;
5350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
5352 return OK;
5353}
5354
5355/*
5356 * ":abbreviate" and friends.
5357 */
5358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 do_exmap(eap, TRUE); /* almost the same as mapping */
5362}
5363
5364/*
5365 * ":map" and friends.
5366 */
5367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005368ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 /*
5371 * If we are sourcing .exrc or .vimrc in current directory we
5372 * print the mappings for security reasons.
5373 */
5374 if (secure)
5375 {
5376 secure = 2;
5377 msg_outtrans(eap->cmd);
5378 msg_putchar('\n');
5379 }
5380 do_exmap(eap, FALSE);
5381}
5382
5383/*
5384 * ":unmap" and friends.
5385 */
5386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005387ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 do_exmap(eap, FALSE);
5390}
5391
5392/*
5393 * ":mapclear" and friends.
5394 */
5395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397{
5398 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5399}
5400
5401/*
5402 * ":abclear" and friends.
5403 */
5404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5408}
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005411ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005414 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 * directory for security reasons.
5416 */
5417 if (secure)
5418 {
5419 secure = 2;
5420 eap->errmsg = e_curdir;
5421 }
5422 else if (eap->cmdidx == CMD_autocmd)
5423 do_autocmd(eap->arg, eap->forceit);
5424 else
5425 do_augroup(eap->arg, eap->forceit);
5426}
5427
5428/*
5429 * ":doautocmd": Apply the automatic commands to the current buffer.
5430 */
5431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005432ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005434 char_u *arg = eap->arg;
5435 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005436 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005437
Bram Moolenaar1610d052016-06-09 22:53:01 +02005438 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5439 /* Only when there is no <nomodeline>. */
5440 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005441 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444/*
5445 * :[N]bunload[!] [N] [bufname] unload buffer
5446 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5447 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5448 */
5449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005450ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
5452 eap->errmsg = do_bufdel(
5453 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5454 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5455 : DOBUF_UNLOAD, eap->arg,
5456 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5457}
5458
5459/*
5460 * :[N]buffer [N] to buffer N
5461 * :[N]sbuffer [N] to buffer N
5462 */
5463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005464ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465{
5466 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 Moolenaar29323592016-07-24 22:04:11 +02005887 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005888 if (win->w_p_pvw)
5889 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005890 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005891 break;
5892 }
5893}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005894#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895
Bram Moolenaarf740b292006-02-16 22:11:02 +00005896/*
5897 * Close window "win" and take care of handling closing the last window for a
5898 * modified buffer.
5899 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005901ex_win_close(
5902 int forceit,
5903 win_T *win,
5904 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905{
5906 int need_hide;
5907 buf_T *buf = win->w_buffer;
5908
5909 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005910 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005912#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 if ((p_confirm || cmdmod.confirm) && p_write)
5914 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005915 bufref_T bufref;
5916
5917 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005919 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 return;
5921 need_hide = FALSE;
5922 }
5923 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005926 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 return;
5928 }
5929 }
5930
Bram Moolenaar4033c552017-09-16 20:54:51 +02005931#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005933#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005934
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005936 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005937 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005938 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005939 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940}
5941
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005943 * Handle the argument for a tabpage related ex command.
5944 * Returns a tabpage number.
5945 * When an error is encountered then eap->errmsg is set.
5946 */
5947 static int
5948get_tabpage_arg(exarg_T *eap)
5949{
5950 int tab_number;
5951 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5952
5953 if (eap->arg && *eap->arg != NUL)
5954 {
5955 char_u *p = eap->arg;
5956 char_u *p_save;
5957 int relative = 0; /* argument +N/-N means: go to N places to the
5958 * right/left relative to the current position. */
5959
5960 if (*p == '-')
5961 {
5962 relative = -1;
5963 p++;
5964 }
5965 else if (*p == '+')
5966 {
5967 relative = 1;
5968 p++;
5969 }
5970
5971 p_save = p;
5972 tab_number = getdigits(&p);
5973
5974 if (relative == 0)
5975 {
5976 if (STRCMP(p, "$") == 0)
5977 tab_number = LAST_TAB_NR;
5978 else if (p == p_save || *p_save == '-' || *p != NUL
5979 || tab_number > LAST_TAB_NR)
5980 {
5981 /* No numbers as argument. */
5982 eap->errmsg = e_invarg;
5983 goto theend;
5984 }
5985 }
5986 else
5987 {
5988 if (*p_save == NUL)
5989 tab_number = 1;
5990 else if (p == p_save || *p_save == '-' || *p != NUL
5991 || tab_number == 0)
5992 {
5993 /* No numbers as argument. */
5994 eap->errmsg = e_invarg;
5995 goto theend;
5996 }
5997 tab_number = tab_number * relative + tabpage_index(curtab);
5998 if (!unaccept_arg0 && relative == -1)
5999 --tab_number;
6000 }
6001 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6002 eap->errmsg = e_invarg;
6003 }
6004 else if (eap->addr_count > 0)
6005 {
6006 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006007 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006008 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006009 tab_number = 0;
6010 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006011 else
6012 {
6013 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006014 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006015 {
6016 --tab_number;
6017 if (tab_number < unaccept_arg0)
6018 eap->errmsg = e_invarg;
6019 }
6020 }
6021 }
6022 else
6023 {
6024 switch (eap->cmdidx)
6025 {
6026 case CMD_tabnext:
6027 tab_number = tabpage_index(curtab) + 1;
6028 if (tab_number > LAST_TAB_NR)
6029 tab_number = 1;
6030 break;
6031 case CMD_tabmove:
6032 tab_number = LAST_TAB_NR;
6033 break;
6034 default:
6035 tab_number = tabpage_index(curtab);
6036 }
6037 }
6038
6039theend:
6040 return tab_number;
6041}
6042
6043/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006044 * ":tabclose": close current tab page, unless it is the last one.
6045 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 */
6047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006048ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006050 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006051 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006052
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006053# ifdef FEAT_CMDWIN
6054 if (cmdwin_type != 0)
6055 cmdwin_result = K_IGNORE;
6056 else
6057# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006058 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006059 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006062 tab_number = get_tabpage_arg(eap);
6063 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006064 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006065 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006066 if (tp == NULL)
6067 {
6068 beep_flush();
6069 return;
6070 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006071 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006072 {
6073 tabpage_close_other(tp, eap->forceit);
6074 return;
6075 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006076 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006077 tabpage_close(eap->forceit);
6078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006080}
6081
6082/*
6083 * ":tabonly": close all tab pages except the current one
6084 */
6085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006086ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006087{
6088 tabpage_T *tp;
6089 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006090 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006091
6092# ifdef FEAT_CMDWIN
6093 if (cmdwin_type != 0)
6094 cmdwin_result = K_IGNORE;
6095 else
6096# endif
6097 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006098 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006099 else
6100 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006101 tab_number = get_tabpage_arg(eap);
6102 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006103 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006104 goto_tabpage(tab_number);
6105 /* Repeat this up to a 1000 times, because autocommands may
6106 * mess up the lists. */
6107 for (done = 0; done < 1000; ++done)
6108 {
6109 FOR_ALL_TABPAGES(tp)
6110 if (tp->tp_topframe != topframe)
6111 {
6112 tabpage_close_other(tp, eap->forceit);
6113 /* if we failed to close it quit */
6114 if (valid_tabpage(tp))
6115 done = 1000;
6116 /* start over, "tp" is now invalid */
6117 break;
6118 }
6119 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006120 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006121 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006122 }
6123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125
6126/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006127 * Close the current tab page.
6128 */
6129 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006130tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006131{
6132 /* First close all the windows but the current one. If that worked then
6133 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006134 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006135 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006136 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006137 ex_win_close(forceit, curwin, NULL);
6138# ifdef FEAT_GUI
6139 need_mouse_correct = TRUE;
6140# endif
6141}
6142
6143/*
6144 * Close tab page "tp", which is not the current tab page.
6145 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006146 * Also takes care of the tab pages line disappearing when closing the
6147 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006148 */
6149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006150tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006151{
6152 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006153 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006154 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006155
6156 /* Limit to 1000 windows, autocommands may add a window while we close
6157 * one. OK, so I'm paranoid... */
6158 while (++done < 1000)
6159 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006160 wp = tp->tp_firstwin;
6161 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006162
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006163 /* Autocommands may delete the tab page under our fingers and we may
6164 * fail to close a window with a modified buffer. */
6165 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006166 break;
6167 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006168
Bram Moolenaar29323592016-07-24 22:04:11 +02006169 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006170
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006171 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006172 if (h != tabline_height())
6173 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006174}
6175
6176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 * ":only".
6178 */
6179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006180ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006182 win_T *wp;
6183 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184# ifdef FEAT_GUI
6185 need_mouse_correct = TRUE;
6186# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006187 if (eap->addr_count > 0)
6188 {
6189 wnr = eap->line2;
6190 for (wp = firstwin; --wnr > 0; )
6191 {
6192 if (wp->w_next == NULL)
6193 break;
6194 else
6195 wp = wp->w_next;
6196 }
6197 win_goto(wp);
6198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 close_others(TRUE, eap->forceit);
6200}
6201
6202/*
6203 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006204 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006206 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006207ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 if (eap->addr_count == 0)
6210 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006211 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006215ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006217 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006218 if (!eap->skip)
6219 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006220#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006221 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006222#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006223 if (eap->addr_count == 0)
6224 win_close(curwin, FALSE); /* don't free buffer */
6225 else
6226 {
6227 int winnr = 0;
6228 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006229
Bram Moolenaar2256c992016-11-15 21:17:07 +01006230 FOR_ALL_WINDOWS(win)
6231 {
6232 winnr++;
6233 if (winnr == eap->line2)
6234 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006235 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006236 if (win == NULL)
6237 win = lastwin;
6238 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 }
6241}
6242
6243/*
6244 * ":stop" and ":suspend": Suspend Vim.
6245 */
6246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006247ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 /*
6250 * Disallow suspending for "rvim".
6251 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006252 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
6254 if (!eap->forceit)
6255 autowrite_all();
6256 windgoto((int)Rows - 1, 0);
6257 out_char('\n');
6258 out_flush();
6259 stoptermcap();
6260 out_flush(); /* needed for SUN to restore xterm buffer */
6261#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006262 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263#endif
6264 ui_suspend(); /* call machine specific function */
6265#ifdef FEAT_TITLE
6266 maketitle();
6267 resettitle(); /* force updating the title */
6268#endif
6269 starttermcap();
6270 scroll_start(); /* scroll screen before redrawing */
6271 redraw_later_clear();
6272 shell_resized(); /* may have resized window */
6273 }
6274}
6275
6276/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006277 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 */
6279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006280ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281{
6282#ifdef FEAT_CMDWIN
6283 if (cmdwin_type != 0)
6284 {
6285 cmdwin_result = Ctrl_C;
6286 return;
6287 }
6288#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006289 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006290 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006291 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006292 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006293 return;
6294 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006295
6296 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006297 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299 /*
6300 * if more files or windows we won't exit
6301 */
6302 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6303 exiting = TRUE;
6304 if ( ((eap->cmdidx == CMD_wq
6305 || curbufIsChanged())
6306 && do_write(eap) == FAIL)
6307 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006308 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
6310 not_exiting();
6311 }
6312 else
6313 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006316 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317# ifdef FEAT_GUI
6318 need_mouse_correct = TRUE;
6319# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006320 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006321 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 }
6323}
6324
6325/*
6326 * ":print", ":list", ":number".
6327 */
6328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006329ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006331 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006332 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006333 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006335 for ( ;!got_int; ui_breakcheck())
6336 {
6337 print_line(eap->line1,
6338 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6339 || (eap->flags & EXFLAG_NR)),
6340 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6341 if (++eap->line1 > eap->line2)
6342 break;
6343 out_flush(); /* show one line at a time */
6344 }
6345 setpcmark();
6346 /* put cursor at last line */
6347 curwin->w_cursor.lnum = eap->line2;
6348 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 }
6350
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352}
6353
6354#ifdef FEAT_BYTEOFF
6355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006356ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 goto_byte(eap->line2);
6359}
6360#endif
6361
6362/*
6363 * ":shell".
6364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006366ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 do_shell(NULL, 0);
6369}
6370
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006371#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006373static int drop_busy = FALSE;
6374static int drop_filec;
6375static char_u **drop_filev = NULL;
6376static int drop_split;
6377static void (*drop_callback)(void *);
6378static void *drop_cookie;
6379
6380 static void
6381handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 exarg_T ea;
6384 int save_msg_scroll = msg_scroll;
6385
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006386 // Setting the argument list may cause screen updates and being called
6387 // recursively. Avoid that by setting drop_busy.
6388 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389
6390 /* Check whether the current buffer is changed. If so, we will need
6391 * to split the current window or data could be lost.
6392 * We don't need to check if the 'hidden' option is set, as in this
6393 * case the buffer won't be lost.
6394 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006395 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 {
6397 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006398 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 --emsg_off;
6400 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006401 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (win_split(0, 0) == FAIL)
6404 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006405 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407 /* When splitting the window, create a new alist. Otherwise the
6408 * existing one is overwritten. */
6409 alist_unlink(curwin->w_alist);
6410 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 }
6412
6413 /*
6414 * Set up the new argument list.
6415 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006416 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
6418 /*
6419 * Move to the first file.
6420 */
6421 /* Fake up a minimal "next" command for do_argfile() */
6422 vim_memset(&ea, 0, sizeof(ea));
6423 ea.cmd = (char_u *)"next";
6424 do_argfile(&ea, 0);
6425
6426 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6427 * mode that is not needed here. */
6428 need_start_insertmode = FALSE;
6429
6430 /* Restore msg_scroll, otherwise a following command may cause scrolling
6431 * unexpectedly. The screen will be redrawn by the caller, thus
6432 * msg_scroll being set by displaying a message is irrelevant. */
6433 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006434
6435 if (drop_callback != NULL)
6436 drop_callback(drop_cookie);
6437
6438 drop_filev = NULL;
6439 drop_busy = FALSE;
6440}
6441
6442/*
6443 * Handle a file drop. The code is here because a drop is *nearly* like an
6444 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006445 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006446 * code is very similar to :args and hence needs access to a lot of the static
6447 * functions in this file.
6448 *
6449 * The "filev" list must have been allocated using alloc(), as should each item
6450 * in the list. This function takes over responsibility for freeing the "filev"
6451 * list.
6452 */
6453 void
6454handle_drop(
6455 int filec, // the number of files dropped
6456 char_u **filev, // the list of files dropped
6457 int split, // force splitting the window
6458 void (*callback)(void *), // to be called after setting the argument
6459 // list
6460 void *cookie) // argument for "callback" (allocated)
6461{
6462 // Cannot handle recursive drops, finish the pending one.
6463 if (drop_busy)
6464 {
6465 FreeWild(filec, filev);
6466 vim_free(cookie);
6467 return;
6468 }
6469
6470 // When calling handle_drop() more than once in a row we only use the last
6471 // one.
6472 if (drop_filev != NULL)
6473 {
6474 FreeWild(drop_filec, drop_filev);
6475 vim_free(drop_cookie);
6476 }
6477
6478 drop_filec = filec;
6479 drop_filev = filev;
6480 drop_split = split;
6481 drop_callback = callback;
6482 drop_cookie = cookie;
6483
6484 // Postpone this when:
6485 // - editing the command line
6486 // - not possible to change the current buffer
6487 // - updating the screen
6488 // As it may change buffers and window structures that are in use and cause
6489 // freed memory to be used.
6490 if (text_locked() || curbuf_locked() || updating_screen)
6491 return;
6492
6493 handle_drop_internal();
6494}
6495
6496/*
6497 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6498 * reset: Handle a postponed drop.
6499 */
6500 void
6501handle_any_postponed_drop(void)
6502{
6503 if (!drop_busy && drop_filev != NULL
6504 && !text_locked() && !curbuf_locked() && !updating_screen)
6505 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506}
6507#endif
6508
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509/*
6510 * Clear an argument list: free all file names and reset it to zero entries.
6511 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006512 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006513alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
6515 while (--al->al_ga.ga_len >= 0)
6516 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6517 ga_clear(&al->al_ga);
6518}
6519
6520/*
6521 * Init an argument list.
6522 */
6523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006524alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6527}
6528
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529/*
6530 * Remove a reference from an argument list.
6531 * Ignored when the argument list is the global one.
6532 * If the argument list is no longer used by any window, free it.
6533 */
6534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006535alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 if (al != &global_alist && --al->al_refcount <= 0)
6538 {
6539 alist_clear(al);
6540 vim_free(al);
6541 }
6542}
6543
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544/*
6545 * Create a new argument list and use it for the current window.
6546 */
6547 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006548alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549{
Bram Moolenaar964b3742019-05-24 18:54:09 +02006550 curwin->w_alist = (alist_T *)alloc(sizeof(alist_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (curwin->w_alist == NULL)
6552 {
6553 curwin->w_alist = &global_alist;
6554 ++global_alist.al_refcount;
6555 }
6556 else
6557 {
6558 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006559 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 alist_init(curwin->w_alist);
6561 }
6562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006564#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565/*
6566 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006567 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6568 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 */
6570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006571alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006574 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 char_u **new_arg_files;
6576 int new_arg_file_count;
6577 char_u *save_p_su = p_su;
6578 int i;
6579
6580 /* Don't use 'suffixes' here. This should work like the shell did the
6581 * expansion. Also, the vimrc file isn't read yet, thus the user
6582 * can't set the options. */
6583 p_su = empty_option;
Bram Moolenaar964b3742019-05-24 18:54:09 +02006584 old_arg_files = (char_u **)alloc(sizeof(char_u *) * GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 if (old_arg_files != NULL)
6586 {
6587 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006588 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6589 old_arg_count = GARGCOUNT;
6590 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006592 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 && new_arg_file_count > 0)
6594 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006595 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6596 TRUE, fnum_list, fnum_len);
6597 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600 p_su = save_p_su;
6601}
6602#endif
6603
6604/*
6605 * Set the argument list for the current window.
6606 * Takes over the allocated files[] and the allocated fnames in it.
6607 */
6608 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006609alist_set(
6610 alist_T *al,
6611 int count,
6612 char_u **files,
6613 int use_curbuf,
6614 int *fnum_list,
6615 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616{
6617 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006618 static int recursive = 0;
6619
6620 if (recursive)
6621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006622 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006623 return;
6624 }
6625 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627 alist_clear(al);
6628 if (ga_grow(&al->al_ga, count) == OK)
6629 {
6630 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006631 {
6632 if (got_int)
6633 {
6634 /* When adding many buffers this can take a long time. Allow
6635 * interrupting here. */
6636 while (i < count)
6637 vim_free(files[i++]);
6638 break;
6639 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006640
6641 /* May set buffer name of a buffer previously used for the
6642 * argument list, so that it's re-used by alist_add. */
6643 if (fnum_list != NULL && i < fnum_len)
6644 buf_set_name(fnum_list[i], files[i]);
6645
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006647 ui_breakcheck();
6648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 vim_free(files);
6650 }
6651 else
6652 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006655
6656 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657}
6658
6659/*
6660 * Add file "fname" to argument list "al".
6661 * "fname" must have been allocated and "al" must have been checked for room.
6662 */
6663 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006664alist_add(
6665 alist_T *al,
6666 char_u *fname,
6667 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 if (fname == NULL) /* don't add NULL file names */
6670 return;
6671#ifdef BACKSLASH_IN_FILENAME
6672 slash_adjust(fname);
6673#endif
6674 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6675 if (set_fnum > 0)
6676 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6677 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6678 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679}
6680
6681#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6682/*
6683 * Adjust slashes in file names. Called after 'shellslash' was set.
6684 */
6685 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006686alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006690 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 for (i = 0; i < GARGCOUNT; ++i)
6693 if (GARGLIST[i].ae_fname != NULL)
6694 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006695 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 if (wp->w_alist != &global_alist)
6697 for (i = 0; i < WARGCOUNT(wp); ++i)
6698 if (WARGLIST(wp)[i].ae_fname != NULL)
6699 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700}
6701#endif
6702
6703/*
6704 * ":preserve".
6705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006707ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006709 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 ml_preserve(curbuf, TRUE);
6711}
6712
6713/*
6714 * ":recover".
6715 */
6716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006717ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6720 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006721 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6722 | CCGD_MULTWIN
6723 | (eap->forceit ? CCGD_FORCEIT : 0)
6724 | CCGD_EXCMD)
6725
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 && (*eap->arg == NUL
6727 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006728 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 recoverymode = FALSE;
6730}
6731
6732/*
6733 * Command modifier used in a wrong way.
6734 */
6735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006736ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
6738 eap->errmsg = e_invcmd;
6739}
6740
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741/*
6742 * :sview [+command] file split window with new file, read-only
6743 * :split [[+command] file] split window with current or new file
6744 * :vsplit [[+command] file] split window vertically with current or new file
6745 * :new [[+command] file] split window with no or new file
6746 * :vnew [[+command] file] split vertically window with no or new file
6747 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006748 *
6749 * :tabedit open new Tab page with empty window
6750 * :tabedit [+command] file open new Tab page and edit "file"
6751 * :tabnew [[+command] file] just like :tabedit
6752 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 */
6754 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006755ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006757 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006758#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006760#endif
6761#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006763#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006764 int use_tab = eap->cmdidx == CMD_tabedit
6765 || eap->cmdidx == CMD_tabfind
6766 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
Bram Moolenaar4033c552017-09-16 20:54:51 +02006768#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
Bram Moolenaar4033c552017-09-16 20:54:51 +02006772#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006774 * quickfix windows. But it's OK when doing ":tab split". */
6775 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
6777 if (eap->cmdidx == CMD_split)
6778 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (eap->cmdidx == CMD_vsplit)
6780 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783
Bram Moolenaar4033c552017-09-16 20:54:51 +02006784#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006785 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
6787 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6788 FNAME_MESS, TRUE, curbuf->b_ffname);
6789 if (fname == NULL)
6790 goto theend;
6791 eap->arg = fname;
6792 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006793# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006794 else
6795# endif
6796#endif
6797#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 && eap->cmdidx != CMD_new)
6801 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006802 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006803# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006804 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006805# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006806 au_has_group((char_u *)"FileExplorer"))
6807 {
6808 /* No browsing supported but we do have the file explorer:
6809 * Edit the directory. */
6810 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6811 eap->arg = (char_u *)".";
6812 }
6813 else
6814 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006815 fname = do_browse(0, (char_u *)(use_tab
6816 ? _("Edit File in new tab page")
6817 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006819 if (fname == NULL)
6820 goto theend;
6821 eap->arg = fname;
6822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
6824 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006827 /*
6828 * Either open new tab page or split the window.
6829 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006830 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006831 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006832 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6833 : eap->addr_count == 0 ? 0
6834 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006835 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006836 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006837
6838 /* set the alternate buffer for the window we came from */
6839 if (curwin != old_curwin
6840 && win_valid(old_curwin)
6841 && old_curwin->w_buffer != curbuf
6842 && !cmdmod.keepalt)
6843 old_curwin->w_alt_fnum = curbuf->b_fnum;
6844 }
6845 }
6846 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 /* Reset 'scrollbind' when editing another file, but keep it when
6850 * doing ":split" without arguments. */
6851 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006852# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006856 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 else
6858 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 do_exedit(eap, old_curwin);
6860 }
6861
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006862# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006866# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867theend:
6868 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006871
6872/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006873 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006874 */
6875 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006876tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006877{
6878 exarg_T ea;
6879
6880 vim_memset(&ea, 0, sizeof(ea));
6881 ea.cmdidx = CMD_tabnew;
6882 ea.cmd = (char_u *)"tabn";
6883 ea.arg = (char_u *)"";
6884 ex_splitview(&ea);
6885}
6886
6887/*
6888 * :tabnext command
6889 */
6890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006891ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006892{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006893 int tab_number;
6894
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006895 switch (eap->cmdidx)
6896 {
6897 case CMD_tabfirst:
6898 case CMD_tabrewind:
6899 goto_tabpage(1);
6900 break;
6901 case CMD_tablast:
6902 goto_tabpage(9999);
6903 break;
6904 case CMD_tabprevious:
6905 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006906 if (eap->arg && *eap->arg != NUL)
6907 {
6908 char_u *p = eap->arg;
6909 char_u *p_save = p;
6910
6911 tab_number = getdigits(&p);
6912 if (p == p_save || *p_save == '-' || *p != NUL
6913 || tab_number == 0)
6914 {
6915 /* No numbers as argument. */
6916 eap->errmsg = e_invarg;
6917 return;
6918 }
6919 }
6920 else
6921 {
6922 if (eap->addr_count == 0)
6923 tab_number = 1;
6924 else
6925 {
6926 tab_number = eap->line2;
6927 if (tab_number < 1)
6928 {
6929 eap->errmsg = e_invrange;
6930 return;
6931 }
6932 }
6933 }
6934 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006935 break;
6936 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006937 tab_number = get_tabpage_arg(eap);
6938 if (eap->errmsg == NULL)
6939 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006940 break;
6941 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006942}
6943
6944/*
6945 * :tabmove command
6946 */
6947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006948ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006949{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006950 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006951
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006952 tab_number = get_tabpage_arg(eap);
6953 if (eap->errmsg == NULL)
6954 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006955}
6956
6957/*
6958 * :tabs command: List tabs and their contents.
6959 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006961ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006962{
6963 tabpage_T *tp;
6964 win_T *wp;
6965 int tabcount = 1;
6966
6967 msg_start();
6968 msg_scroll = TRUE;
6969 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6970 {
6971 msg_putchar('\n');
6972 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006973 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006974 out_flush(); /* output one line at a time */
6975 ui_breakcheck();
6976
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006977 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006978 wp = firstwin;
6979 else
6980 wp = tp->tp_firstwin;
6981 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6982 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006983 msg_putchar('\n');
6984 msg_putchar(wp == curwin ? '>' : ' ');
6985 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006986 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6987 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006988 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006989 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006990 else
6991 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6992 IObuff, IOSIZE, TRUE);
6993 msg_outtrans(IObuff);
6994 out_flush(); /* output one line at a time */
6995 ui_breakcheck();
6996 }
6997 }
6998}
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000/*
7001 * ":mode": Set screen mode.
7002 * If no argument given, just get the screen size and redraw.
7003 */
7004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007005ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007 if (*eap->arg == NUL)
7008 shell_resized();
7009 else
7010 mch_screenmode(eap->arg);
7011}
7012
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013/*
7014 * ":resize".
7015 * set, increment or decrement current window height
7016 */
7017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007018ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
7020 int n;
7021 win_T *wp = curwin;
7022
7023 if (eap->addr_count > 0)
7024 {
7025 n = eap->line2;
7026 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7027 ;
7028 }
7029
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007030# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007032# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 if (cmdmod.split & WSP_VERT)
7035 {
7036 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007037 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7039 n = 9999;
7040 win_setwidth_win((int)n, wp);
7041 }
7042 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
7044 if (*eap->arg == '-' || *eap->arg == '+')
7045 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007046 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 n = 9999;
7048 win_setheight_win((int)n, wp);
7049 }
7050}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
7052/*
7053 * ":find [+command] <file>" command.
7054 */
7055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007056ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
7058#ifdef FEAT_SEARCHPATH
7059 char_u *fname;
7060 int count;
7061
7062 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7063 TRUE, curbuf->b_ffname);
7064 if (eap->addr_count > 0)
7065 {
7066 /* Repeat finding the file "count" times. This matters when it
7067 * appears several times in the path. */
7068 count = eap->line2;
7069 while (fname != NULL && --count > 0)
7070 {
7071 vim_free(fname);
7072 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7073 FALSE, curbuf->b_ffname);
7074 }
7075 }
7076
7077 if (fname != NULL)
7078 {
7079 eap->arg = fname;
7080#endif
7081 do_exedit(eap, NULL);
7082#ifdef FEAT_SEARCHPATH
7083 vim_free(fname);
7084 }
7085#endif
7086}
7087
7088/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007089 * ":open" simulation: for now just work like ":visual".
7090 */
7091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007092ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007093{
7094 regmatch_T regmatch;
7095 char_u *p;
7096
7097 curwin->w_cursor.lnum = eap->line2;
7098 beginline(BL_SOL | BL_FIX);
7099 if (*eap->arg == '/')
7100 {
7101 /* ":open /pattern/": put cursor in column found with pattern */
7102 ++eap->arg;
7103 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7104 *p = NUL;
7105 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7106 if (regmatch.regprog != NULL)
7107 {
7108 regmatch.rm_ic = p_ic;
7109 p = ml_get_curline();
7110 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007111 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007112 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007113 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007114 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007115 }
7116 /* Move to the NUL, ignore any other arguments. */
7117 eap->arg += STRLEN(eap->arg);
7118 }
7119 check_cursor();
7120
7121 eap->cmdidx = CMD_visual;
7122 do_exedit(eap, NULL);
7123}
7124
7125/*
7126 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 */
7128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007129ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 do_exedit(eap, NULL);
7132}
7133
7134/*
7135 * ":edit <file>" command and alikes.
7136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007138do_exedit(
7139 exarg_T *eap,
7140 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141{
7142 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007144 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146 /*
7147 * ":vi" command ends Ex mode.
7148 */
7149 if (exmode_active && (eap->cmdidx == CMD_visual
7150 || eap->cmdidx == CMD_view))
7151 {
7152 exmode_active = FALSE;
7153 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007154 {
7155 /* Special case: ":global/pat/visual\NLvi-commands" */
7156 if (global_busy)
7157 {
7158 int rd = RedrawingDisabled;
7159 int nwr = no_wait_return;
7160 int ms = msg_scroll;
7161#ifdef FEAT_GUI
7162 int he = hold_gui_events;
7163#endif
7164
7165 if (eap->nextcmd != NULL)
7166 {
7167 stuffReadbuff(eap->nextcmd);
7168 eap->nextcmd = NULL;
7169 }
7170
7171 if (exmode_was != EXMODE_VIM)
7172 settmode(TMODE_RAW);
7173 RedrawingDisabled = 0;
7174 no_wait_return = 0;
7175 need_wait_return = FALSE;
7176 msg_scroll = 0;
7177#ifdef FEAT_GUI
7178 hold_gui_events = 0;
7179#endif
7180 must_redraw = CLEAR;
7181
7182 main_loop(FALSE, TRUE);
7183
7184 RedrawingDisabled = rd;
7185 no_wait_return = nwr;
7186 msg_scroll = ms;
7187#ifdef FEAT_GUI
7188 hold_gui_events = he;
7189#endif
7190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
7194
7195 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007196 || eap->cmdidx == CMD_tabnew
7197 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007198 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007200 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 setpcmark();
7202 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007203 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7204 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007206 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 || *eap->arg != NUL
7208#ifdef FEAT_BROWSE
7209 || cmdmod.browse
7210#endif
7211 )
7212 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007213 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7214 * can bring us here, others are stopped earlier. */
7215 if (*eap->arg != NUL && curbuf_locked())
7216 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007217
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 n = readonlymode;
7219 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7220 readonlymode = TRUE;
7221 else if (eap->cmdidx == CMD_enew)
7222 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7223 empty buffer */
7224 setpcmark();
7225 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7226 NULL, eap,
7227 /* ":edit" goes to first line if Vi compatible */
7228 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7229 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7230 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007231 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007233 /* after a split we can use an existing buffer */
7234 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007236 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
7238 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 if (old_curwin != NULL)
7240 {
7241 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007242 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007244#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007245 cleanup_T cs;
7246
7247 /* Reset the error/interrupt/exception state here so that
7248 * aborting() returns FALSE when closing a window. */
7249 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007250#endif
7251#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007253#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007254 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007255
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007256#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007257 /* Restore the error/interrupt/exception state if not
7258 * discarded by a new aborting error, interrupt, or
7259 * uncaught exception. */
7260 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 }
7263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
7265 else if (readonlymode && curbuf->b_nwindows == 1)
7266 {
7267 /* When editing an already visited buffer, 'readonly' won't be set
7268 * but the previous value is kept. With ":view" and ":sview" we
7269 * want the file to be readonly, except when another window is
7270 * editing the same buffer. */
7271 curbuf->b_p_ro = TRUE;
7272 }
7273 readonlymode = n;
7274 }
7275 else
7276 {
7277 if (eap->do_ecmd_cmd != NULL)
7278 do_cmdline_cmd(eap->do_ecmd_cmd);
7279#ifdef FEAT_TITLE
7280 n = curwin->w_arg_idx_invalid;
7281#endif
7282 check_arg_idx(curwin);
7283#ifdef FEAT_TITLE
7284 if (n != curwin->w_arg_idx_invalid)
7285 maketitle();
7286#endif
7287 }
7288
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 /*
7290 * if ":split file" worked, set alternate file name in old window to new
7291 * file
7292 */
7293 if (old_curwin != NULL
7294 && *eap->arg != NUL
7295 && curwin != old_curwin
7296 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007297 && old_curwin->w_buffer != curbuf
7298 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
7301 ex_no_reprint = TRUE;
7302}
7303
7304#ifndef FEAT_GUI
7305/*
7306 * ":gui" and ":gvim" when there is no GUI.
7307 */
7308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007309ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311 eap->errmsg = e_nogvim;
7312}
7313#endif
7314
Bram Moolenaar4f974752019-02-17 17:44:42 +01007315#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007317ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
7319 gui_make_tearoff(eap->arg);
7320}
7321#endif
7322
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007323#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7324 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007326ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007328# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7329 if (gui.in_use)
7330 gui_make_popup(eap->arg, eap->forceit);
7331# ifdef FEAT_TERM_POPUP_MENU
7332 else
7333# endif
7334# endif
7335# ifdef FEAT_TERM_POPUP_MENU
7336 pum_make_popup(eap->arg, eap->forceit);
7337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338}
7339#endif
7340
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007342ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343{
7344 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007345 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007347 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348}
7349
7350/*
7351 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7352 * offset.
7353 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007356ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007359 win_T *save_curwin = curwin;
7360 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 long topline;
7362 long y;
7363 linenr_T old_linenr = curwin->w_cursor.lnum;
7364
7365 setpcmark();
7366
7367 /*
7368 * determine max topline
7369 */
7370 if (curwin->w_p_scb)
7371 {
7372 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007373 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 {
7375 if (wp->w_p_scb && wp->w_buffer)
7376 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007377 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (topline > y)
7379 topline = y;
7380 }
7381 }
7382 if (topline < 1)
7383 topline = 1;
7384 }
7385 else
7386 {
7387 topline = 1;
7388 }
7389
7390
7391 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007392 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007394 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
7396 if (curwin->w_p_scb)
7397 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007398 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 y = topline - curwin->w_topline;
7400 if (y > 0)
7401 scrollup(y, TRUE);
7402 else
7403 scrolldown(-y, TRUE);
7404 curwin->w_scbind_pos = topline;
7405 redraw_later(VALID);
7406 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 }
7409 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007410 curwin = save_curwin;
7411 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 if (curwin->w_p_scb)
7413 {
7414 did_syncbind = TRUE;
7415 checkpcmark();
7416 if (old_linenr != curwin->w_cursor.lnum)
7417 {
7418 char_u ctrl_o[2];
7419
7420 ctrl_o[0] = Ctrl_O;
7421 ctrl_o[1] = 0;
7422 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7423 }
7424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425}
7426
7427
7428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007429ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007431 int i;
7432 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7433 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435 if (eap->usefilter) /* :r!cmd */
7436 do_bang(1, eap, FALSE, FALSE, TRUE);
7437 else
7438 {
7439 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7440 return;
7441
7442#ifdef FEAT_BROWSE
7443 if (cmdmod.browse)
7444 {
7445 char_u *browseFile;
7446
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007447 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 NULL, NULL, NULL, curbuf);
7449 if (browseFile != NULL)
7450 {
7451 i = readfile(browseFile, NULL,
7452 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7453 vim_free(browseFile);
7454 }
7455 else
7456 i = OK;
7457 }
7458 else
7459#endif
7460 if (*eap->arg == NUL)
7461 {
7462 if (check_fname() == FAIL) /* check for no file name */
7463 return;
7464 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7465 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7466 }
7467 else
7468 {
7469 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7470 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7471 i = readfile(eap->arg, NULL,
7472 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7473
7474 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007475 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007477#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 if (!aborting())
7479#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007480 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007483 {
7484 if (empty && exmode_active)
7485 {
7486 /* Delete the empty line that remains. Historically ex does
7487 * this but vi doesn't. */
7488 if (eap->line2 == 0)
7489 lnum = curbuf->b_ml.ml_line_count;
7490 else
7491 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007492 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007493 {
7494 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007495 if (curwin->w_cursor.lnum > 1
7496 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007497 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007498 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007499 }
7500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 }
7504}
7505
Bram Moolenaarea408852005-06-25 22:49:46 +00007506static char_u *prev_dir = NULL;
7507
7508#if defined(EXITFREE) || defined(PROTO)
7509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007510free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007511{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007512 VIM_CLEAR(prev_dir);
7513 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007514}
7515#endif
7516
Bram Moolenaarf4258302013-06-02 18:20:17 +02007517/*
7518 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007519 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7520 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007521 */
7522 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007523post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007524{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007525 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007526 // Clear tab local directory for both :cd and :tcd
7527 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007528 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007529 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007530 {
7531 /* If still in global directory, need to remember current
7532 * directory as global directory. */
7533 if (globaldir == NULL && prev_dir != NULL)
7534 globaldir = vim_strsave(prev_dir);
7535 /* Remember this local directory for the window. */
7536 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007537 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007538 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007539 curtab->tp_localdir = vim_strsave(NameBuff);
7540 else
7541 curwin->w_localdir = vim_strsave(NameBuff);
7542 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007543 }
7544 else
7545 {
7546 /* We are now in the global directory, no need to remember its
7547 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007548 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007549 }
7550
7551 shorten_fnames(TRUE);
7552}
7553
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007554/*
7555 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7556 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7557 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7558 * Otherwise changes the global directory.
7559 * Returns TRUE if the directory is successfully changed.
7560 */
7561 int
7562changedir_func(
7563 char_u *new_dir,
7564 int forceit,
7565 cdscope_T scope)
7566{
7567 char_u *tofree;
7568 int dir_differs;
7569 int retval = FALSE;
7570
7571 if (allbuf_locked())
7572 return FALSE;
7573
7574 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7575 {
7576 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7577 return FALSE;
7578 }
7579
7580 // ":cd -": Change to previous directory
7581 if (STRCMP(new_dir, "-") == 0)
7582 {
7583 if (prev_dir == NULL)
7584 {
7585 emsg(_("E186: No previous directory"));
7586 return FALSE;
7587 }
7588 new_dir = prev_dir;
7589 }
7590
7591 // Save current directory for next ":cd -"
7592 tofree = prev_dir;
7593 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7594 prev_dir = vim_strsave(NameBuff);
7595 else
7596 prev_dir = NULL;
7597
7598#if defined(UNIX) || defined(VMS)
7599 // for UNIX ":cd" means: go to home directory
7600 if (*new_dir == NUL)
7601 {
7602 // use NameBuff for home directory name
7603# ifdef VMS
7604 char_u *p;
7605
7606 p = mch_getenv((char_u *)"SYS$LOGIN");
7607 if (p == NULL || *p == NUL) // empty is the same as not set
7608 NameBuff[0] = NUL;
7609 else
7610 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7611# else
7612 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7613# endif
7614 new_dir = NameBuff;
7615 }
7616#endif
7617 dir_differs = new_dir == NULL || prev_dir == NULL
7618 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7619 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7620 emsg(_(e_failed));
7621 else
7622 {
7623 char_u *acmd_fname;
7624
7625 post_chdir(scope);
7626
7627 if (dir_differs)
7628 {
7629 if (scope == CDSCOPE_WINDOW)
7630 acmd_fname = (char_u *)"window";
7631 else if (scope == CDSCOPE_TABPAGE)
7632 acmd_fname = (char_u *)"tabpage";
7633 else
7634 acmd_fname = (char_u *)"global";
7635 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7636 curbuf);
7637 }
7638 retval = TRUE;
7639 }
7640 vim_free(tofree);
7641
7642 return retval;
7643}
Bram Moolenaarea408852005-06-25 22:49:46 +00007644
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007646 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007648 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007649ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007651 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652
7653 new_dir = eap->arg;
7654#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007655 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656 if (*new_dir == NUL)
7657 ex_pwd(NULL);
7658 else
7659#endif
7660 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007661 cdscope_T scope = CDSCOPE_GLOBAL;
7662
7663 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7664 scope = CDSCOPE_WINDOW;
7665 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7666 scope = CDSCOPE_TABPAGE;
7667
7668 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007669 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007670 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007671 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 ex_pwd(eap);
7673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675}
7676
7677/*
7678 * ":pwd".
7679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007681ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7684 {
7685#ifdef BACKSLASH_IN_FILENAME
7686 slash_adjust(NameBuff);
7687#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007688 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
7690 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007691 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692}
7693
7694/*
7695 * ":=".
7696 */
7697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007701 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702}
7703
7704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007705ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007707 int n;
7708 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709
7710 if (cursor_valid())
7711 {
7712 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7713 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007714 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007716
7717 len = eap->line2;
7718 switch (*eap->arg)
7719 {
7720 case 'm': break;
7721 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007722 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007723 }
7724 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725}
7726
7727/*
7728 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7729 */
7730 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007731do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007734 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
7736 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007737 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007738 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007740 wait_now = msec - done > 1000L ? 1000L : msec - done;
7741#ifdef FEAT_TIMERS
7742 {
7743 long due_time = check_due_timer();
7744
7745 if (due_time > 0 && due_time < wait_now)
7746 wait_now = due_time;
7747 }
7748#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007749#ifdef FEAT_JOB_CHANNEL
7750 if (has_any_channel() && wait_now > 100L)
7751 wait_now = 100L;
7752#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007753 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007754#ifdef FEAT_JOB_CHANNEL
7755 if (has_any_channel())
7756 ui_breakcheck_force(TRUE);
7757 else
7758#endif
7759 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007760#ifdef MESSAGE_QUEUE
7761 /* Process the netbeans and clientserver messages that may have been
7762 * received in the call to ui_breakcheck() when the GUI is in use. This
7763 * may occur when running a test case. */
7764 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007767
7768 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7769 // input buffer, otherwise a following call to input() fails.
7770 if (got_int)
7771 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772}
7773
7774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007775do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 int mode;
7778 char_u *cmdp;
7779
7780 cmdp = eap->cmd;
7781 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7782
7783 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7784 eap->arg, mode, isabbrev))
7785 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007786 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007788 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 break;
7790 }
7791}
7792
7793/*
7794 * ":winsize" command (obsolete).
7795 */
7796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007797ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798{
7799 int w, h;
7800 char_u *arg = eap->arg;
7801 char_u *p;
7802
7803 w = getdigits(&arg);
7804 arg = skipwhite(arg);
7805 p = arg;
7806 h = getdigits(&arg);
7807 if (*p != NUL && *arg == NUL)
7808 set_shellsize(w, h, TRUE);
7809 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007810 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811}
7812
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007814ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815{
7816 int xchar = NUL;
7817 char_u *p;
7818
7819 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7820 {
7821 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7822 if (eap->arg[1] == NUL)
7823 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007824 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 return;
7826 }
7827 xchar = eap->arg[1];
7828 p = eap->arg + 2;
7829 }
7830 else
7831 p = eap->arg + 1;
7832
7833 eap->nextcmd = check_nextcmd(p);
7834 p = skipwhite(p);
7835 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007836 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007837 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
7839 /* Pass flags on for ":vertical wincmd ]". */
7840 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007841 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7843 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007844 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
7846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
Bram Moolenaar843ee412004-06-30 16:16:41 +00007848#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849/*
7850 * ":winpos".
7851 */
7852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007853ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854{
7855 int x, y;
7856 char_u *arg = eap->arg;
7857 char_u *p;
7858
7859 if (*arg == NUL)
7860 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007861# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007862# ifdef VIMDLL
7863 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7864 mch_get_winpos(&x, &y) != FAIL)
7865# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007867# else
7868 if (mch_get_winpos(&x, &y) != FAIL)
7869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
7871 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007872 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 }
7874 else
7875# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007876 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 }
7878 else
7879 {
7880 x = getdigits(&arg);
7881 arg = skipwhite(arg);
7882 p = arg;
7883 y = getdigits(&arg);
7884 if (*p == NUL || *arg != NUL)
7885 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007886 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 return;
7888 }
7889# ifdef FEAT_GUI
7890 if (gui.in_use)
7891 gui_mch_set_winpos(x, y);
7892 else if (gui.starting)
7893 {
7894 /* Remember the coordinates for when the window is opened. */
7895 gui_win_x = x;
7896 gui_win_y = y;
7897 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007898# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 else
7900# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007901# endif
7902# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007903 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904# endif
7905# ifdef HAVE_TGETENT
7906 if (*T_CWP)
7907 term_set_winpos(x, y);
7908# endif
7909 }
7910}
7911#endif
7912
7913/*
7914 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7915 */
7916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007917ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918{
7919 oparg_T oa;
7920
7921 clear_oparg(&oa);
7922 oa.regname = eap->regname;
7923 oa.start.lnum = eap->line1;
7924 oa.end.lnum = eap->line2;
7925 oa.line_count = eap->line2 - eap->line1 + 1;
7926 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7929 {
7930 setpcmark();
7931 curwin->w_cursor.lnum = eap->line1;
7932 beginline(BL_SOL | BL_FIX);
7933 }
7934
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007935 if (VIsual_active)
7936 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007937
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 switch (eap->cmdidx)
7939 {
7940 case CMD_delete:
7941 oa.op_type = OP_DELETE;
7942 op_delete(&oa);
7943 break;
7944
7945 case CMD_yank:
7946 oa.op_type = OP_YANK;
7947 (void)op_yank(&oa, FALSE, TRUE);
7948 break;
7949
7950 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007951 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007953 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7954#else
7955 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007957 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 oa.op_type = OP_RSHIFT;
7959 else
7960 oa.op_type = OP_LSHIFT;
7961 op_shift(&oa, FALSE, eap->amount);
7962 break;
7963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007965 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966}
7967
7968/*
7969 * ":put".
7970 */
7971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007972ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
7974 /* ":0put" works like ":1put!". */
7975 if (eap->line2 == 0)
7976 {
7977 eap->line2 = 1;
7978 eap->forceit = TRUE;
7979 }
7980 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007981 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7982 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983}
7984
7985/*
7986 * Handle ":copy" and ":move".
7987 */
7988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007989ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990{
7991 long n;
7992
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007993 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 if (eap->arg == NULL) /* error detected */
7995 {
7996 eap->nextcmd = NULL;
7997 return;
7998 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007999 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000
8001 /*
8002 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8003 */
8004 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008006 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 return;
8008 }
8009
8010 if (eap->cmdidx == CMD_move)
8011 {
8012 if (do_move(eap->line1, eap->line2, n) == FAIL)
8013 return;
8014 }
8015 else
8016 ex_copy(eap->line1, eap->line2, n);
8017 u_clearline();
8018 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008019 ex_may_print(eap);
8020}
8021
8022/*
8023 * Print the current line if flags were given to the Ex command.
8024 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008025 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008026ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008027{
8028 if (eap->flags != 0)
8029 {
8030 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8031 (eap->flags & EXFLAG_LIST));
8032 ex_no_reprint = TRUE;
8033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034}
8035
8036/*
8037 * ":smagic" and ":snomagic".
8038 */
8039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008040ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041{
8042 int magic_save = p_magic;
8043
8044 p_magic = (eap->cmdidx == CMD_smagic);
8045 do_sub(eap);
8046 p_magic = magic_save;
8047}
8048
8049/*
8050 * ":join".
8051 */
8052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008053ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
8055 curwin->w_cursor.lnum = eap->line1;
8056 if (eap->line1 == eap->line2)
8057 {
8058 if (eap->addr_count >= 2) /* :2,2join does nothing */
8059 return;
8060 if (eap->line2 == curbuf->b_ml.ml_line_count)
8061 {
8062 beep_flush();
8063 return;
8064 }
8065 ++eap->line2;
8066 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008067 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008069 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070}
8071
8072/*
8073 * ":[addr]@r" or ":[addr]*r": execute register
8074 */
8075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008076ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077{
8078 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008079 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080
8081 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008082 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083
8084#ifdef USE_ON_FLY_SCROLL
8085 dont_scroll = TRUE; /* disallow scrolling here */
8086#endif
8087
8088 /* get the register name. No name means to use the previous one */
8089 c = *eap->arg;
8090 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8091 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008092 /* Put the register in the typeahead buffer with the "silent" flag. */
8093 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8094 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 else
8099 {
8100 int save_efr = exec_from_reg;
8101
8102 exec_from_reg = TRUE;
8103
8104 /*
8105 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008106 * Continue until the stuff buffer is empty and all added characters
8107 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008109 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8111
8112 exec_from_reg = save_efr;
8113 }
8114}
8115
8116/*
8117 * ":!".
8118 */
8119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008120ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121{
8122 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8123}
8124
8125/*
8126 * ":undo".
8127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008129ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008131 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008132 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008133 else
8134 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135}
8136
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008137#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008139ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008140{
8141 char_u hash[UNDO_HASH_SIZE];
8142
8143 u_compute_hash(hash);
8144 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8145}
8146
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008148ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008149{
8150 char_u hash[UNDO_HASH_SIZE];
8151
8152 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008153 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008154}
8155#endif
8156
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157/*
8158 * ":redo".
8159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008161ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162{
8163 u_redo(1);
8164}
8165
8166/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008167 * ":earlier" and ":later".
8168 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008170ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008171{
8172 long count = 0;
8173 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008174 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008175 char_u *p = eap->arg;
8176
8177 if (*p == NUL)
8178 count = 1;
8179 else if (isdigit(*p))
8180 {
8181 count = getdigits(&p);
8182 switch (*p)
8183 {
8184 case 's': ++p; sec = TRUE; break;
8185 case 'm': ++p; sec = TRUE; count *= 60; break;
8186 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008187 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8188 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008189 }
8190 }
8191
8192 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008193 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008194 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008195 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8196 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008197}
8198
8199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 * ":redir": start/stop redirection.
8201 */
8202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008203ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
8205 char *mode;
8206 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008207 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208
Bram Moolenaarba768492016-07-08 15:32:54 +02008209#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008210 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008211 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008212 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008213 return;
8214 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008215#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008216
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 if (STRICMP(eap->arg, "END") == 0)
8218 close_redir();
8219 else
8220 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008221 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008223 ++arg;
8224 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008226 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 mode = "a";
8228 }
8229 else
8230 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008231 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232
8233 close_redir();
8234
8235 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008236 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (fname == NULL)
8238 return;
8239#ifdef FEAT_BROWSE
8240 if (cmdmod.browse)
8241 {
8242 char_u *browseFile;
8243
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008244 browseFile = do_browse(BROWSE_SAVE,
8245 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008246 fname, NULL, NULL,
8247 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 if (browseFile == NULL)
8249 return; /* operation cancelled */
8250 vim_free(fname);
8251 fname = browseFile;
8252 eap->forceit = TRUE; /* since dialog already asked */
8253 }
8254#endif
8255
8256 redir_fd = open_exfile(fname, eap->forceit, mode);
8257 vim_free(fname);
8258 }
8259#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008260 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
8262 /* redirect to a register a-z (resp. A-Z for appending) */
8263 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008264 ++arg;
8265 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008267 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008268 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008270 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008272 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008273 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008274 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008275 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008277 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008278 if (*arg == '>')
8279 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008280 /* Make register empty when not using @A-@Z and the
8281 * command is valid. */
8282 if (*arg == NUL && !isupper(redir_reg))
8283 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008285 }
8286 if (*arg != NUL)
8287 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008288 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008289 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008290 }
8291 }
8292 else if (*arg == '=' && arg[1] == '>')
8293 {
8294 int append;
8295
8296 /* redirect to a variable */
8297 close_redir();
8298 arg += 2;
8299
8300 if (*arg == '>')
8301 {
8302 ++arg;
8303 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
8305 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008306 append = FALSE;
8307
8308 if (var_redir_start(skipwhite(arg), append) == OK)
8309 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 }
8311#endif
8312
8313 /* TODO: redirect to a buffer */
8314
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008316 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008318
8319 /* Make sure redirection is not off. Can happen for cmdline completion
8320 * that indirectly invokes a command to catch its output. */
8321 if (redir_fd != NULL
8322#ifdef FEAT_EVAL
8323 || redir_reg || redir_vname
8324#endif
8325 )
8326 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327}
8328
8329/*
8330 * ":redraw": force redraw
8331 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008332 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008333ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 int r = RedrawingDisabled;
8336 int p = p_lz;
8337
8338 RedrawingDisabled = 0;
8339 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008340 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008342 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343#ifdef FEAT_TITLE
8344 if (need_maketitle)
8345 maketitle();
8346#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008347#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8348# ifdef VIMDLL
8349 if (!gui.in_use)
8350# endif
8351 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 RedrawingDisabled = r;
8354 p_lz = p;
8355
8356 /* Reset msg_didout, so that a message that's there is overwritten. */
8357 msg_didout = FALSE;
8358 msg_col = 0;
8359
Bram Moolenaar56667a52013-07-17 11:54:28 +02008360 /* No need to wait after an intentional redraw. */
8361 need_wait_return = FALSE;
8362
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 out_flush();
8364}
8365
8366/*
8367 * ":redrawstatus": force redraw of status line(s)
8368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008370ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int r = RedrawingDisabled;
8373 int p = p_lz;
8374
8375 RedrawingDisabled = 0;
8376 p_lz = FALSE;
8377 if (eap->forceit)
8378 status_redraw_all();
8379 else
8380 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008381 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 RedrawingDisabled = r;
8383 p_lz = p;
8384 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385}
8386
Bram Moolenaare12bab32019-01-08 22:02:56 +01008387/*
8388 * ":redrawtabline": force redraw of the tabline
8389 */
8390 static void
8391ex_redrawtabline(exarg_T *eap UNUSED)
8392{
8393 int r = RedrawingDisabled;
8394 int p = p_lz;
8395
8396 RedrawingDisabled = 0;
8397 p_lz = FALSE;
8398
8399 draw_tabline();
8400
8401 RedrawingDisabled = r;
8402 p_lz = p;
8403 out_flush();
8404}
8405
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008407close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408{
8409 if (redir_fd != NULL)
8410 {
8411 fclose(redir_fd);
8412 redir_fd = NULL;
8413 }
8414#ifdef FEAT_EVAL
8415 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008416 if (redir_vname)
8417 {
8418 var_redir_stop();
8419 redir_vname = 0;
8420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421#endif
8422}
8423
8424#if defined(FEAT_SESSION) && defined(USE_CRNL)
8425# define MKSESSION_NL
8426static int mksession_nl = FALSE; /* use NL only in put_eol() */
8427#endif
8428
8429/*
8430 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8431 */
8432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008433ex_mkrc(
8434 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435{
8436 FILE *fd;
8437 int failed = FALSE;
8438 char_u *fname;
8439#ifdef FEAT_BROWSE
8440 char_u *browseFile = NULL;
8441#endif
8442#ifdef FEAT_SESSION
8443 int view_session = FALSE;
8444 int using_vdir = FALSE; /* using 'viewdir'? */
8445 char_u *viewFile = NULL;
8446 unsigned *flagp;
8447#endif
8448
8449 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8450 {
8451#ifdef FEAT_SESSION
8452 view_session = TRUE;
8453#else
8454 ex_ni(eap);
8455 return;
8456#endif
8457 }
8458
8459#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008460 /* Use the short file name until ":lcd" is used. We also don't use the
8461 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008462 did_lcd = FALSE;
8463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8465 if (eap->cmdidx == CMD_mkview
8466 && (*eap->arg == NUL
8467 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8468 {
8469 eap->forceit = TRUE;
8470 fname = get_view_file(*eap->arg);
8471 if (fname == NULL)
8472 return;
8473 viewFile = fname;
8474 using_vdir = TRUE;
8475 }
8476 else
8477#endif
8478 if (*eap->arg != NUL)
8479 fname = eap->arg;
8480 else if (eap->cmdidx == CMD_mkvimrc)
8481 fname = (char_u *)VIMRC_FILE;
8482#ifdef FEAT_SESSION
8483 else if (eap->cmdidx == CMD_mksession)
8484 fname = (char_u *)SESSION_FILE;
8485#endif
8486 else
8487 fname = (char_u *)EXRC_FILE;
8488
8489#ifdef FEAT_BROWSE
8490 if (cmdmod.browse)
8491 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008492 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493# ifdef FEAT_SESSION
8494 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8495 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8496# endif
8497 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008498 fname, (char_u *)"vim", NULL,
8499 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 if (browseFile == NULL)
8501 goto theend;
8502 fname = browseFile;
8503 eap->forceit = TRUE; /* since dialog already asked */
8504 }
8505#endif
8506
8507#if defined(FEAT_SESSION) && defined(vim_mkdir)
8508 /* When using 'viewdir' may have to create the directory. */
8509 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008510 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511#endif
8512
8513 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8514 if (fd != NULL)
8515 {
8516#ifdef FEAT_SESSION
8517 if (eap->cmdidx == CMD_mkview)
8518 flagp = &vop_flags;
8519 else
8520 flagp = &ssop_flags;
8521#endif
8522
8523#ifdef MKSESSION_NL
8524 /* "unix" in 'sessionoptions': use NL line separator */
8525 if (view_session && (*flagp & SSOP_UNIX))
8526 mksession_nl = TRUE;
8527#endif
8528
8529 /* Write the version command for :mkvimrc */
8530 if (eap->cmdidx == CMD_mkvimrc)
8531 (void)put_line(fd, "version 6.0");
8532
8533#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008534 if (eap->cmdidx == CMD_mksession)
8535 {
8536 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8537 failed = TRUE;
8538 }
8539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (eap->cmdidx != CMD_mkview)
8541#endif
8542 {
8543 /* Write setting 'compatible' first, because it has side effects.
8544 * For that same reason only do it when needed. */
8545 if (p_cp)
8546 (void)put_line(fd, "if !&cp | set cp | endif");
8547 else
8548 (void)put_line(fd, "if &cp | set nocp | endif");
8549 }
8550
8551#ifdef FEAT_SESSION
8552 if (!view_session
8553 || (eap->cmdidx == CMD_mksession
8554 && (*flagp & SSOP_OPTIONS)))
8555#endif
8556 failed |= (makemap(fd, NULL) == FAIL
8557 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8558
8559#ifdef FEAT_SESSION
8560 if (!failed && view_session)
8561 {
8562 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8563 failed = TRUE;
8564 if (eap->cmdidx == CMD_mksession)
8565 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008566 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567
Bram Moolenaard9462e32011-04-11 21:35:11 +02008568 dirnow = alloc(MAXPATHL);
8569 if (dirnow == NULL)
8570 failed = TRUE;
8571 else
8572 {
8573 /*
8574 * Change to session file's dir.
8575 */
8576 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008578 *dirnow = NUL;
8579 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8580 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008581 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008582 shorten_fnames(TRUE);
8583 }
8584 else if (*dirnow != NUL
8585 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8586 {
8587 if (mch_chdir((char *)globaldir) == 0)
8588 shorten_fnames(TRUE);
8589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590
Bram Moolenaard9462e32011-04-11 21:35:11 +02008591 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592
Bram Moolenaard9462e32011-04-11 21:35:11 +02008593 /* restore original dir */
8594 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008596 {
8597 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008598 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008599 shorten_fnames(TRUE);
8600 }
8601 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 }
8603 }
8604 else
8605 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008606 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8607 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 }
8609 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8610 == FAIL)
8611 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008612# ifdef FEAT_SEARCH_EXTRA
8613 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8614 failed = TRUE;
8615# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008616 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8617 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008618 if (eap->cmdidx == CMD_mksession)
8619 {
8620 if (put_line(fd, "unlet SessionLoad") == FAIL)
8621 failed = TRUE;
8622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 }
8624#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008625 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8626 failed = TRUE;
8627
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 failed |= fclose(fd);
8629
8630 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008631 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8633 else if (eap->cmdidx == CMD_mksession)
8634 {
8635 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008636 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
Bram Moolenaard9462e32011-04-11 21:35:11 +02008638 tbuf = alloc(MAXPATHL);
8639 if (tbuf != NULL)
8640 {
8641 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8642 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8643 vim_free(tbuf);
8644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 }
8646#endif
8647#ifdef MKSESSION_NL
8648 mksession_nl = FALSE;
8649#endif
8650 }
8651
8652#ifdef FEAT_BROWSE
8653theend:
8654 vim_free(browseFile);
8655#endif
8656#ifdef FEAT_SESSION
8657 vim_free(viewFile);
8658#endif
8659}
8660
Bram Moolenaardf177f62005-02-22 08:39:57 +00008661#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8662 || defined(PROTO)
8663 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008664vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008665{
8666 if (vim_mkdir(name, prot) != 0)
8667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008668 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008669 return FAIL;
8670 }
8671 return OK;
8672}
8673#endif
8674
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675/*
8676 * Open a file for writing for an Ex command, with some checks.
8677 * Return file descriptor, or NULL on failure.
8678 */
8679 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008680open_exfile(
8681 char_u *fname,
8682 int forceit,
8683 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
8685 FILE *fd;
8686
8687#ifdef UNIX
8688 /* with Unix it is possible to open a directory */
8689 if (mch_isdir(fname))
8690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008691 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 return NULL;
8693 }
8694#endif
8695 if (!forceit && *mode != 'a' && vim_fexists(fname))
8696 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008697 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 return NULL;
8699 }
8700
8701 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008702 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703
8704 return fd;
8705}
8706
8707/*
8708 * ":mark" and ":k".
8709 */
8710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
8713 pos_T pos;
8714
8715 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008716 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008718 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 else
8720 {
8721 pos = curwin->w_cursor; /* save curwin->w_cursor */
8722 curwin->w_cursor.lnum = eap->line2;
8723 beginline(BL_WHITE | BL_FIX);
8724 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008725 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8727 }
8728}
8729
8730/*
8731 * Update w_topline, w_leftcol and the cursor position.
8732 */
8733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008734update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735{
8736 check_cursor(); /* put cursor on valid line */
8737 update_topline();
8738 if (!curwin->w_p_wrap)
8739 validate_cursor();
8740 update_curswant();
8741}
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008744 * Save the current State and go to Normal mode.
8745 * Return TRUE if the typeahead could be saved.
8746 */
8747 int
8748save_current_state(save_state_T *sst)
8749{
8750 sst->save_msg_scroll = msg_scroll;
8751 sst->save_restart_edit = restart_edit;
8752 sst->save_msg_didout = msg_didout;
8753 sst->save_State = State;
8754 sst->save_insertmode = p_im;
8755 sst->save_finish_op = finish_op;
8756 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008757 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008758
8759 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8760 restart_edit = 0; /* don't go to Insert mode */
8761 p_im = FALSE; /* don't use 'insertmode' */
8762
8763 /*
8764 * Save the current typeahead. This is required to allow using ":normal"
8765 * from an event handler and makes sure we don't hang when the argument
8766 * ends with half a command.
8767 */
8768 save_typeahead(&sst->tabuf);
8769 return sst->tabuf.typebuf_valid;
8770}
8771
8772 void
8773restore_current_state(save_state_T *sst)
8774{
8775 /* Restore the previous typeahead. */
8776 restore_typeahead(&sst->tabuf);
8777
8778 msg_scroll = sst->save_msg_scroll;
8779 restart_edit = sst->save_restart_edit;
8780 p_im = sst->save_insertmode;
8781 finish_op = sst->save_finish_op;
8782 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008783 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008784 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8785
8786 /* Restore the state (needed when called from a function executed for
8787 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8788 State = sst->save_State;
8789#ifdef CURSOR_SHAPE
8790 ui_cursor_shape(); /* may show different cursor shape */
8791#endif
8792}
8793
8794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 * ":normal[!] {commands}": Execute normal mode commands.
8796 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008797 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008798ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008800 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 char_u *arg = NULL;
8802 int l;
8803 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008805 if (ex_normal_lock > 0)
8806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008807 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008808 return;
8809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 if (ex_normal_busy >= p_mmd)
8811 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008812 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 return;
8814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 /*
8817 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8818 * this for the K_SPECIAL leading byte, otherwise special keys will not
8819 * work.
8820 */
8821 if (has_mbyte)
8822 {
8823 int len = 0;
8824
8825 /* Count the number of characters to be escaped. */
8826 for (p = eap->arg; *p != NUL; ++p)
8827 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008828#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 if (*p == CSI) /* leadbyte CSI */
8830 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008831#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008832 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008834#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 )
8838 len += 2;
8839 }
8840 if (len > 0)
8841 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008842 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (arg != NULL)
8844 {
8845 len = 0;
8846 for (p = eap->arg; *p != NUL; ++p)
8847 {
8848 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008849#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (*p == CSI)
8851 {
8852 arg[len++] = KS_EXTRA;
8853 arg[len++] = (int)KE_CSI;
8854 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008855#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008856 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 {
8858 arg[len++] = *++p;
8859 if (*p == K_SPECIAL)
8860 {
8861 arg[len++] = KS_SPECIAL;
8862 arg[len++] = KE_FILLER;
8863 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008864#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 else if (*p == CSI)
8866 {
8867 arg[len++] = KS_EXTRA;
8868 arg[len++] = (int)KE_CSI;
8869 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 }
8872 arg[len] = NUL;
8873 }
8874 }
8875 }
8876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008878 ++ex_normal_busy;
8879 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 /*
8882 * Repeat the :normal command for each line in the range. When no
8883 * range given, execute it just once, without positioning the cursor
8884 * first.
8885 */
8886 do
8887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 if (eap->addr_count != 0)
8889 {
8890 curwin->w_cursor.lnum = eap->line1++;
8891 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008892 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 }
8894
Bram Moolenaar13505972019-01-24 15:04:48 +01008895 exec_normal_cmd(arg != NULL
8896 ? arg
8897 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 }
8899 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8900 }
8901
8902 /* Might not return to the main loop when in an event handler. */
8903 update_topline_cursor();
8904
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008905 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008907#ifdef FEAT_MOUSE
8908 setmouse();
8909#endif
8910#ifdef CURSOR_SHAPE
8911 ui_cursor_shape(); /* may show different cursor shape */
8912#endif
8913
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915}
8916
8917/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008918 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 */
8920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008921ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008923 if (eap->forceit)
8924 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008925 /* cursor line can be zero on startup */
8926 if (!curwin->w_cursor.lnum)
8927 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008928 coladvance((colnr_T)MAXCOL);
8929 curwin->w_curswant = MAXCOL;
8930 curwin->w_set_curswant = FALSE;
8931 }
8932
Bram Moolenaara40c5002005-01-09 21:16:21 +00008933 /* Ignore the command when already in Insert mode. Inserting an
8934 * expression register that invokes a function can do this. */
8935 if (State & INSERT)
8936 return;
8937
Bram Moolenaar64969662005-12-14 21:59:55 +00008938 if (eap->cmdidx == CMD_startinsert)
8939 restart_edit = 'a';
8940 else if (eap->cmdidx == CMD_startreplace)
8941 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008943 restart_edit = 'V';
8944
8945 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008947 if (eap->cmdidx == CMD_startinsert)
8948 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 curwin->w_curswant = 0; /* avoid MAXCOL */
8950 }
8951}
8952
8953/*
8954 * ":stopinsert"
8955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008957ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958{
8959 restart_edit = 0;
8960 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008961 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008964/*
8965 * Execute normal mode command "cmd".
8966 * "remap" can be REMAP_NONE or REMAP_YES.
8967 */
8968 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008969exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008970{
Bram Moolenaar25281632016-01-21 23:32:32 +01008971 /* Stuff the argument into the typeahead buffer. */
8972 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008973 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008974}
Bram Moolenaar25281632016-01-21 23:32:32 +01008975
Bram Moolenaar25281632016-01-21 23:32:32 +01008976/*
8977 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008978 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008979 */
8980 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008981exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008982{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008983 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008984 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008985
Bram Moolenaar905dd902019-04-07 14:21:47 +02008986 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8987 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008988 clear_oparg(&oa);
8989 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008990 while ((!stuff_empty()
8991 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008992 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008993 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008994 {
8995 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008996#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008997 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008998 && oa.op_type == OP_NOP && oa.regname == NUL
8999 && !VIsual_active)
9000 {
9001 /* If terminal_loop() returns OK we got a key that is handled
9002 * in Normal model. With FAIL we first need to position the
9003 * cursor and the screen needs to be redrawn. */
9004 if (terminal_loop(TRUE) == OK)
9005 normal_cmd(&oa, TRUE);
9006 }
9007 else
9008#endif
9009 /* execute a Normal mode cmd */
9010 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009011 }
9012}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009013
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014#ifdef FEAT_FIND_ID
9015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009016ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017{
9018 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9019 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9020 (linenr_T)1, (linenr_T)MAXLNUM);
9021}
9022
Bram Moolenaar4033c552017-09-16 20:54:51 +02009023#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024/*
9025 * ":psearch"
9026 */
9027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009028ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
9030 g_do_tagpreview = p_pvh;
9031 ex_findpat(eap);
9032 g_do_tagpreview = 0;
9033}
9034#endif
9035
9036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009037ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 int whole = TRUE;
9040 long n;
9041 char_u *p;
9042 int action;
9043
9044 switch (cmdnames[eap->cmdidx].cmd_name[2])
9045 {
9046 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9047 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9048 action = ACTION_GOTO;
9049 else
9050 action = ACTION_SHOW;
9051 break;
9052 case 'i': /* ":ilist" and ":dlist" */
9053 action = ACTION_SHOW_ALL;
9054 break;
9055 case 'u': /* ":ijump" and ":djump" */
9056 action = ACTION_GOTO;
9057 break;
9058 default: /* ":isplit" and ":dsplit" */
9059 action = ACTION_SPLIT;
9060 break;
9061 }
9062
9063 n = 1;
9064 if (vim_isdigit(*eap->arg)) /* get count */
9065 {
9066 n = getdigits(&eap->arg);
9067 eap->arg = skipwhite(eap->arg);
9068 }
9069 if (*eap->arg == '/') /* Match regexp, not just whole words */
9070 {
9071 whole = FALSE;
9072 ++eap->arg;
9073 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9074 if (*p)
9075 {
9076 *p++ = NUL;
9077 p = skipwhite(p);
9078
9079 /* Check for trailing illegal characters */
9080 if (!ends_excmd(*p))
9081 eap->errmsg = e_trailing;
9082 else
9083 eap->nextcmd = check_nextcmd(p);
9084 }
9085 }
9086 if (!eap->skip)
9087 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9088 whole, !eap->forceit,
9089 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9090 n, action, eap->line1, eap->line2);
9091}
9092#endif
9093
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094
Bram Moolenaar4033c552017-09-16 20:54:51 +02009095#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096/*
9097 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9098 */
9099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009100ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009102 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9104}
9105
9106/*
9107 * ":pedit"
9108 */
9109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009110ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
9112 win_T *curwin_save = curwin;
9113
9114 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009115 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009116 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 do_exedit(eap, NULL);
9118 keep_help_flag = FALSE;
9119 if (curwin != curwin_save && win_valid(curwin_save))
9120 {
9121 /* Return cursor to where we were */
9122 validate_cursor();
9123 redraw_later(VALID);
9124 win_enter(curwin_save, TRUE);
9125 }
9126 g_do_tagpreview = 0;
9127}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129
9130/*
9131 * ":stag", ":stselect" and ":stjump".
9132 */
9133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009134ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135{
9136 postponed_split = -1;
9137 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009138 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9140 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009141 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143
9144/*
9145 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9146 */
9147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009148ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9151}
9152
9153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009154ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
9156 int cmd;
9157
9158 switch (name[1])
9159 {
9160 case 'j': cmd = DT_JUMP; /* ":tjump" */
9161 break;
9162 case 's': cmd = DT_SELECT; /* ":tselect" */
9163 break;
9164 case 'p': cmd = DT_PREV; /* ":tprevious" */
9165 break;
9166 case 'N': cmd = DT_PREV; /* ":tNext" */
9167 break;
9168 case 'n': cmd = DT_NEXT; /* ":tnext" */
9169 break;
9170 case 'o': cmd = DT_POP; /* ":pop" */
9171 break;
9172 case 'f': /* ":tfirst" */
9173 case 'r': cmd = DT_FIRST; /* ":trewind" */
9174 break;
9175 case 'l': cmd = DT_LAST; /* ":tlast" */
9176 break;
9177 default: /* ":tag" */
9178#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009179 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009181 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 return;
9183 }
9184#endif
9185 cmd = DT_TAG;
9186 break;
9187 }
9188
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009189 if (name[0] == 'l')
9190 {
9191#ifndef FEAT_QUICKFIX
9192 ex_ni(eap);
9193 return;
9194#else
9195 cmd = DT_LTAG;
9196#endif
9197 }
9198
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9200 eap->forceit, TRUE);
9201}
9202
9203/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009204 * Check "str" for starting with a special cmdline variable.
9205 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9206 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9207 */
9208 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009209find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009210{
9211 int len;
9212 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009213 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009214 "%",
9215#define SPEC_PERC 0
9216 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009217#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009218 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009219#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009220 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009221#define SPEC_CCWORD (SPEC_CWORD + 1)
9222 "<cexpr>", /* expr under cursor */
9223#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009224 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009225#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009226 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009227#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009228 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009229#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009230 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009231#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009232 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009233#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009234 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009235#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009236 "<sflnum>", /* script file line number */
9237#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009238#ifdef FEAT_CLIENTSERVER
9239 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009240# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009241#endif
9242 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009243
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009244 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009245 {
9246 len = (int)STRLEN(spec_str[i]);
9247 if (STRNCMP(src, spec_str[i], len) == 0)
9248 {
9249 *usedlen = len;
9250 return i;
9251 }
9252 }
9253 return -1;
9254}
9255
9256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 * Evaluate cmdline variables.
9258 *
9259 * change '%' to curbuf->b_ffname
9260 * '#' to curwin->w_altfile
9261 * '<cword>' to word under the cursor
9262 * '<cWORD>' to WORD under the cursor
9263 * '<cfile>' to path name under the cursor
9264 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009265 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 * '<afile>' to file name for autocommand
9267 * '<abuf>' to buffer number for autocommand
9268 * '<amatch>' to matching name for autocommand
9269 *
9270 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9271 * "" for error without a message) and NULL is returned.
9272 * Returns an allocated string if a valid match was found.
9273 * Returns NULL if no match was found. "usedlen" then still contains the
9274 * number of characters to skip.
9275 */
9276 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009277eval_vars(
9278 char_u *src, /* pointer into commandline */
9279 char_u *srcstart, /* beginning of valid memory for src */
9280 int *usedlen, /* characters after src that are used */
9281 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009282 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009283 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009284 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
9286 int i;
9287 char_u *s;
9288 char_u *result;
9289 char_u *resultbuf = NULL;
9290 int resultlen;
9291 buf_T *buf;
9292 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9293 int spec_idx;
9294#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009295 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 int skip_mod = FALSE;
9297#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299
9300 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009301 if (escaped != NULL)
9302 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303
9304 /*
9305 * Check if there is something to do.
9306 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009307 spec_idx = find_cmdline_var(src, usedlen);
9308 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 {
9310 *usedlen = 1;
9311 return NULL;
9312 }
9313
9314 /*
9315 * Skip when preceded with a backslash "\%" and "\#".
9316 * Note: In "\\%" the % is also not recognized!
9317 */
9318 if (src > srcstart && src[-1] == '\\')
9319 {
9320 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009321 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 return NULL;
9323 }
9324
9325 /*
9326 * word or WORD under cursor
9327 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009328 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9329 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009331 resultlen = find_ident_under_cursor(&result,
9332 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9333 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9334 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 if (resultlen == 0)
9336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009337 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 return NULL;
9339 }
9340 }
9341
9342 /*
9343 * '#': Alternate file name
9344 * '%': Current file name
9345 * File name under the cursor
9346 * File name for autocommand
9347 * and following modifiers
9348 */
9349 else
9350 {
9351 switch (spec_idx)
9352 {
9353 case SPEC_PERC: /* '%': current file */
9354 if (curbuf->b_fname == NULL)
9355 {
9356 result = (char_u *)"";
9357 valid = 0; /* Must have ":p:h" to be valid */
9358 }
9359 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009360 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009362#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009363 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009364#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 break;
9367
9368 case SPEC_HASH: /* '#' or "#99": alternate file */
9369 if (src[1] == '#') /* "##": the argument list */
9370 {
9371 result = arg_all();
9372 resultbuf = result;
9373 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009374 if (escaped != NULL)
9375 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376#ifdef FEAT_MODIFY_FNAME
9377 skip_mod = TRUE;
9378#endif
9379 break;
9380 }
9381 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009382 if (*s == '<') /* "#<99" uses v:oldfiles */
9383 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009385 if (s == src + 2 && src[1] == '-')
9386 /* just a minus sign, don't skip over it */
9387 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 *usedlen = (int)(s - src); /* length of what we expand */
9389
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009390 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009392 if (*usedlen < 2)
9393 {
9394 /* Should we give an error message for #<text? */
9395 *usedlen = 1;
9396 return NULL;
9397 }
9398#ifdef FEAT_EVAL
9399 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9400 (long)i);
9401 if (result == NULL)
9402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009403 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009404 return NULL;
9405 }
9406#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009407 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 }
9411 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009412 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009413 if (i == 0 && src[1] == '<' && *usedlen > 1)
9414 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009415 buf = buflist_findnr(i);
9416 if (buf == NULL)
9417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009418 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009419 return NULL;
9420 }
9421 if (lnump != NULL)
9422 *lnump = ECMD_LAST;
9423 if (buf->b_fname == NULL)
9424 {
9425 result = (char_u *)"";
9426 valid = 0; /* Must have ":p:h" to be valid */
9427 }
9428 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009429 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009430 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009431#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009432 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009433#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009434 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 break;
9437
9438#ifdef FEAT_SEARCHPATH
9439 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009440 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 if (result == NULL)
9442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009443 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 return NULL;
9445 }
9446 resultbuf = result; /* remember allocated string */
9447 break;
9448#endif
9449
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 case SPEC_AFILE: /* file name for autocommand */
9451 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009452 if (result != NULL && !autocmd_fname_full)
9453 {
9454 /* Still need to turn the fname into a full path. It is
9455 * postponed to avoid a delay when <afile> is not used. */
9456 autocmd_fname_full = TRUE;
9457 result = FullName_save(autocmd_fname, FALSE);
9458 vim_free(autocmd_fname);
9459 autocmd_fname = result;
9460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 if (result == NULL)
9462 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009463 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 return NULL;
9465 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009466 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 break;
9468
9469 case SPEC_ABUF: /* buffer number for autocommand */
9470 if (autocmd_bufnr <= 0)
9471 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009472 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 return NULL;
9474 }
9475 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9476 result = strbuf;
9477 break;
9478
9479 case SPEC_AMATCH: /* match name for autocommand */
9480 result = autocmd_match;
9481 if (result == NULL)
9482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009483 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 return NULL;
9485 }
9486 break;
9487
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 case SPEC_SFILE: /* file name for ":so" command */
9489 result = sourcing_name;
9490 if (result == NULL)
9491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009492 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 return NULL;
9494 }
9495 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009496
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009497 case SPEC_SLNUM: /* line in file for ":so" command */
9498 if (sourcing_name == NULL || sourcing_lnum == 0)
9499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009500 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009501 return NULL;
9502 }
9503 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9504 result = strbuf;
9505 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009506
9507#ifdef FEAT_EVAL
9508 case SPEC_SFLNUM: /* line in script file */
9509 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9510 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009511 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009512 return NULL;
9513 }
9514 sprintf((char *)strbuf, "%ld",
9515 (long)(current_sctx.sc_lnum + sourcing_lnum));
9516 result = strbuf;
9517 break;
9518#endif
9519
9520#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009522 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9523 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 result = strbuf;
9525 break;
9526#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009527
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009528 default:
9529 result = (char_u *)""; /* avoid gcc warning */
9530 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531 }
9532
9533 resultlen = (int)STRLEN(result); /* length of new string */
9534 if (src[*usedlen] == '<') /* remove the file name extension */
9535 {
9536 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 resultlen = (int)(s - result);
9539 }
9540#ifdef FEAT_MODIFY_FNAME
9541 else if (!skip_mod)
9542 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009543 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 &resultlen);
9545 if (result == NULL)
9546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009547 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548 return NULL;
9549 }
9550 }
9551#endif
9552 }
9553
9554 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9555 {
9556 if (valid != VALID_HEAD + VALID_PATH)
9557 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009558 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009560 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 result = NULL;
9562 }
9563 else
9564 result = vim_strnsave(result, resultlen);
9565 vim_free(resultbuf);
9566 return result;
9567}
9568
9569/*
9570 * Concatenate all files in the argument list, separated by spaces, and return
9571 * it in one allocated string.
9572 * Spaces and backslashes in the file names are escaped with a backslash.
9573 * Returns NULL when out of memory.
9574 */
9575 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009576arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578 int len;
9579 int idx;
9580 char_u *retval = NULL;
9581 char_u *p;
9582
9583 /*
9584 * Do this loop two times:
9585 * first time: compute the total length
9586 * second time: concatenate the names
9587 */
9588 for (;;)
9589 {
9590 len = 0;
9591 for (idx = 0; idx < ARGCOUNT; ++idx)
9592 {
9593 p = alist_name(&ARGLIST[idx]);
9594 if (p != NULL)
9595 {
9596 if (len > 0)
9597 {
9598 /* insert a space in between names */
9599 if (retval != NULL)
9600 retval[len] = ' ';
9601 ++len;
9602 }
9603 for ( ; *p != NUL; ++p)
9604 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009605 if (*p == ' '
9606#ifndef BACKSLASH_IN_FILENAME
9607 || *p == '\\'
9608#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009609 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 {
9611 /* insert a backslash */
9612 if (retval != NULL)
9613 retval[len] = '\\';
9614 ++len;
9615 }
9616 if (retval != NULL)
9617 retval[len] = *p;
9618 ++len;
9619 }
9620 }
9621 }
9622
9623 /* second time: break here */
9624 if (retval != NULL)
9625 {
9626 retval[len] = NUL;
9627 break;
9628 }
9629
9630 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009631 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 if (retval == NULL)
9633 break;
9634 }
9635
9636 return retval;
9637}
9638
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639/*
9640 * Expand the <sfile> string in "arg".
9641 *
9642 * Returns an allocated string, or NULL for any error.
9643 */
9644 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009645expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009647 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 int len;
9649 char_u *result;
9650 char_u *newres;
9651 char_u *repl;
9652 int srclen;
9653 char_u *p;
9654
9655 result = vim_strsave(arg);
9656 if (result == NULL)
9657 return NULL;
9658
9659 for (p = result; *p; )
9660 {
9661 if (STRNCMP(p, "<sfile>", 7) != 0)
9662 ++p;
9663 else
9664 {
9665 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009666 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 if (errormsg != NULL)
9668 {
9669 if (*errormsg)
9670 emsg(errormsg);
9671 vim_free(result);
9672 return NULL;
9673 }
9674 if (repl == NULL) /* no match (cannot happen) */
9675 {
9676 p += srclen;
9677 continue;
9678 }
9679 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9680 newres = alloc(len);
9681 if (newres == NULL)
9682 {
9683 vim_free(repl);
9684 vim_free(result);
9685 return NULL;
9686 }
9687 mch_memmove(newres, result, (size_t)(p - result));
9688 STRCPY(newres + (p - result), repl);
9689 len = (int)STRLEN(newres);
9690 STRCAT(newres, p + srclen);
9691 vim_free(repl);
9692 vim_free(result);
9693 result = newres;
9694 p = newres + len; /* continue after the match */
9695 }
9696 }
9697
9698 return result;
9699}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700
9701#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009702static int ses_winsizes(FILE *fd, int restore_size,
9703 win_T *tab_firstwin);
9704static int ses_win_rec(FILE *fd, frame_T *fr);
9705static frame_T *ses_skipframe(frame_T *fr);
9706static int ses_do_frame(frame_T *fr);
9707static int ses_do_win(win_T *wp);
9708static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9709static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009710static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
9712/*
9713 * Write openfile commands for the current buffers to an .exrc file.
9714 * Return FAIL on error, OK otherwise.
9715 */
9716 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009717makeopens(
9718 FILE *fd,
9719 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720{
9721 buf_T *buf;
9722 int only_save_windows = TRUE;
9723 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 int restore_size = TRUE;
9725 win_T *wp;
9726 char_u *sname;
9727 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009728 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009729 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009730 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009731 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009732 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009733 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734
9735 if (ssop_flags & SSOP_BUFFERS)
9736 only_save_windows = FALSE; /* Save ALL buffers */
9737
9738 /*
9739 * Begin by setting the this_session variable, and then other
9740 * sessionable variables.
9741 */
9742#ifdef FEAT_EVAL
9743 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9744 return FAIL;
9745 if (ssop_flags & SSOP_GLOBALS)
9746 if (store_session_globals(fd) == FAIL)
9747 return FAIL;
9748#endif
9749
9750 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009751 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 */
9753 if (put_line(fd, "silent only") == FAIL)
9754 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009755 if ((ssop_flags & SSOP_TABPAGES)
9756 && put_line(fd, "silent tabonly") == FAIL)
9757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758
9759 /*
9760 * Now a :cd command to the session directory or the current directory
9761 */
9762 if (ssop_flags & SSOP_SESDIR)
9763 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009764 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9765 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 return FAIL;
9767 }
9768 else if (ssop_flags & SSOP_CURDIR)
9769 {
9770 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9771 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009772 || fputs("cd ", fd) < 0
9773 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9774 || put_eol(fd) == FAIL)
9775 {
9776 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 vim_free(sname);
9780 }
9781
9782 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009783 * If there is an empty, unnamed buffer we will wipe it out later.
9784 * Remember the buffer number.
9785 */
9786 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9787 return FAIL;
9788 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9789 return FAIL;
9790 if (put_line(fd, "endif") == FAIL)
9791 return FAIL;
9792
9793 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 * Now save the current files, current buffer first.
9795 */
9796 if (put_line(fd, "set shortmess=aoO") == FAIL)
9797 return FAIL;
9798
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009800 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9802 return FAIL;
9803
9804 if (ssop_flags & SSOP_RESIZE)
9805 {
9806 /* Note: after the restore we still check it worked!*/
9807 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9808 || put_eol(fd) == FAIL)
9809 return FAIL;
9810 }
9811
9812#ifdef FEAT_GUI
9813 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9814 {
9815 int x, y;
9816
9817 if (gui_mch_get_winpos(&x, &y) == OK)
9818 {
9819 /* Note: after the restore we still check it worked!*/
9820 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9821 return FAIL;
9822 }
9823 }
9824#endif
9825
9826 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009827 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9828 * will be displayed when creating the next tab. That resizes the windows
9829 * in the first tab, which may cause problems. Set 'showtabline' to 2
9830 * temporarily to avoid that.
9831 */
9832 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9833 {
9834 if (put_line(fd, "set stal=2") == FAIL)
9835 return FAIL;
9836 restore_stal = TRUE;
9837 }
9838
9839 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009840 * May repeat putting Windows for each tab, when "tabpages" is in
9841 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009842 * Don't use goto_tabpage(), it may change directory and trigger
9843 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009845 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009846 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009847 if ((ssop_flags & SSOP_TABPAGES))
9848 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009849 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009850
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009851 // Similar to ses_win_rec() below, populate the tab pages first so
9852 // later local options won't be copied to the new tabs.
9853 FOR_ALL_TABPAGES(tp)
9854 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009855 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009856 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009857 return FAIL;
9858 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009859 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009861 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009862 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009863 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009864
Bram Moolenaar18144c82006-04-12 21:52:12 +00009865 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009867 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009868
9869 if (tp == NULL)
9870 break; /* done all tab pages */
9871 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009872 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009873 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009874 tab_topframe = topframe;
9875 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009876 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009877 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009878 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009879 tab_topframe = tp->tp_topframe;
9880 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009881 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009882 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884
Bram Moolenaar18144c82006-04-12 21:52:12 +00009885 /*
9886 * Before creating the window layout, try loading one file. If this
9887 * is aborted we don't end up with a number of useless windows.
9888 * This may have side effects! (e.g., compressed or network file).
9889 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009890 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009891 {
9892 if (ses_do_win(wp)
9893 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009894 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009895#ifdef FEAT_QUICKFIX
9896 && !bt_nofile(wp->w_buffer)
9897#endif
9898 )
9899 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009900 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9901 return FAIL;
9902 need_tabnext = FALSE;
9903
9904 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009905 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9906 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009907 return FAIL;
9908 if (!wp->w_arg_idx_invalid)
9909 edited_win = wp;
9910 break;
9911 }
9912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009914 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009915 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009916 return FAIL;
9917
Bram Moolenaar18144c82006-04-12 21:52:12 +00009918 /*
9919 * Save current window layout.
9920 */
9921 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009923 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009925 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9926 return FAIL;
9927 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
Bram Moolenaar18144c82006-04-12 21:52:12 +00009930 /*
9931 * Check if window sizes can be restored (no windows omitted).
9932 * Remember the window number of the current window after restoring.
9933 */
9934 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009935 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009936 {
9937 if (ses_do_win(wp))
9938 ++nr;
9939 else
9940 restore_size = FALSE;
9941 if (curwin == wp)
9942 cnr = nr;
9943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944
Bram Moolenaar18144c82006-04-12 21:52:12 +00009945 /* Go to the first window. */
9946 if (put_line(fd, "wincmd t") == FAIL)
9947 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009948
Bram Moolenaar18144c82006-04-12 21:52:12 +00009949 /*
9950 * If more than one window, see if sizes can be restored.
9951 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9952 * resized when moving between windows.
9953 * Do this before restoring the view, so that the topline and the
9954 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009955 * winminheight and winminwidth need to be set to avoid an error if the
9956 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009957 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009958 if (put_line(fd, "set winminheight=0") == FAIL
9959 || put_line(fd, "set winheight=1") == FAIL
9960 || put_line(fd, "set winminwidth=0") == FAIL
9961 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009962 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009963 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009964 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009965
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009966 // Restore the tab-local working directory if specified
9967 // Do this before the windows, so that the window-local directory can
9968 // override the tab-local directory.
9969 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9970 {
9971 if (fputs("tcd ", fd) < 0
9972 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9973 || put_eol(fd) == FAIL)
9974 return FAIL;
9975 did_lcd = TRUE;
9976 }
9977
Bram Moolenaar18144c82006-04-12 21:52:12 +00009978 /*
9979 * Restore the view of the window (options, file, cursor, etc.).
9980 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009981 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009982 {
9983 if (!ses_do_win(wp))
9984 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009985 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
9986 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009987 return FAIL;
9988 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9989 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009990 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009991 }
9992
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009993 /* The argument index in the first tab page is zero, need to set it in
9994 * each window. For further tab pages it's the window where we do
9995 * "tabedit". */
9996 cur_arg_idx = next_arg_idx;
9997
Bram Moolenaar18144c82006-04-12 21:52:12 +00009998 /*
9999 * Restore cursor to the current window if it's not the first one.
10000 */
10001 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10002 || put_eol(fd) == FAIL))
10003 return FAIL;
10004
10005 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010006 * Restore window sizes again after jumping around in windows, because
10007 * the current window has a minimum size while others may not.
10008 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010009 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010010 return FAIL;
10011
Bram Moolenaar18144c82006-04-12 21:52:12 +000010012 /* Don't continue in another tab page when doing only the current one
10013 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010014 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010015 break;
10016 }
10017
10018 if (ssop_flags & SSOP_TABPAGES)
10019 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010020 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10021 || put_eol(fd) == FAIL)
10022 return FAIL;
10023 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010024 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10025 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010026
Bram Moolenaard39e2752019-01-26 20:07:38 +010010027 // Now put the remaining buffers into the buffer list.
10028 // This is near the end, so that when 'hidden' is set we don't create extra
10029 // buffers. If the buffer was already created with another command the
10030 // ":badd" will have no effect.
10031 FOR_ALL_BUFFERS(buf)
10032 {
10033 if (!(only_save_windows && buf->b_nwindows == 0)
10034 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10035#ifdef FEAT_TERMINAL
10036 // Skip terminal buffers: finished ones are not useful, others
10037 // will be resurrected and result in a new buffer.
10038 && !bt_terminal(buf)
10039#endif
10040 && buf->b_fname != NULL
10041 && buf->b_p_bl)
10042 {
10043 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10044 : buf->b_wininfo->wi_fpos.lnum) < 0
10045 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
10046 return FAIL;
10047 }
10048 }
10049
Bram Moolenaar9c102382006-05-03 21:26:49 +000010050 /*
10051 * Wipe out an empty unnamed buffer we started in.
10052 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020010053 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010054 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010055 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010056 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010057 return FAIL;
10058 if (put_line(fd, "endif") == FAIL)
10059 return FAIL;
10060 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10061 return FAIL;
10062
10063 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10064 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10065 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10066 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010010067 /* Re-apply 'winminheight' and 'winminwidth'. */
10068 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
10069 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
10070 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071
10072 /*
10073 * Lastly, execute the x.vim file if it exists.
10074 */
10075 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10076 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010077 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 || put_line(fd, "endif") == FAIL)
10079 return FAIL;
10080
10081 return OK;
10082}
10083
10084 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010085ses_winsizes(
10086 FILE *fd,
10087 int restore_size,
10088 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089{
10090 int n = 0;
10091 win_T *wp;
10092
10093 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10094 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010095 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 {
10097 if (!ses_do_win(wp))
10098 continue;
10099 ++n;
10100
10101 /* restore height when not full height */
10102 if (wp->w_height + wp->w_status_height < topframe->fr_height
10103 && (fprintf(fd,
10104 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10105 n, (long)wp->w_height, Rows / 2, Rows) < 0
10106 || put_eol(fd) == FAIL))
10107 return FAIL;
10108
10109 /* restore width when not full width */
10110 if (wp->w_width < Columns && (fprintf(fd,
10111 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10112 n, (long)wp->w_width, Columns / 2, Columns) < 0
10113 || put_eol(fd) == FAIL))
10114 return FAIL;
10115 }
10116 }
10117 else
10118 {
10119 /* Just equalise window sizes */
10120 if (put_line(fd, "wincmd =") == FAIL)
10121 return FAIL;
10122 }
10123 return OK;
10124}
10125
10126/*
10127 * Write commands to "fd" to recursively create windows for frame "fr",
10128 * horizontally and vertically split.
10129 * After the commands the last window in the frame is the current window.
10130 * Returns FAIL when writing the commands to "fd" fails.
10131 */
10132 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010133ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134{
10135 frame_T *frc;
10136 int count = 0;
10137
10138 if (fr->fr_layout != FR_LEAF)
10139 {
10140 /* Find first frame that's not skipped and then create a window for
10141 * each following one (first frame is already there). */
10142 frc = ses_skipframe(fr->fr_child);
10143 if (frc != NULL)
10144 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10145 {
10146 /* Make window as big as possible so that we have lots of room
10147 * to split. */
10148 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10149 || put_line(fd, fr->fr_layout == FR_COL
10150 ? "split" : "vsplit") == FAIL)
10151 return FAIL;
10152 ++count;
10153 }
10154
10155 /* Go back to the first window. */
10156 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10157 ? "%dwincmd k" : "%dwincmd h", count) < 0
10158 || put_eol(fd) == FAIL))
10159 return FAIL;
10160
10161 /* Recursively create frames/windows in each window of this column or
10162 * row. */
10163 frc = ses_skipframe(fr->fr_child);
10164 while (frc != NULL)
10165 {
10166 ses_win_rec(fd, frc);
10167 frc = ses_skipframe(frc->fr_next);
10168 /* Go to next window. */
10169 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10170 return FAIL;
10171 }
10172 }
10173 return OK;
10174}
10175
10176/*
10177 * Skip frames that don't contain windows we want to save in the Session.
10178 * Returns NULL when there none.
10179 */
10180 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010181ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182{
10183 frame_T *frc;
10184
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010185 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 if (ses_do_frame(frc))
10187 break;
10188 return frc;
10189}
10190
10191/*
10192 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10193 * the Session.
10194 */
10195 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010196ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197{
10198 frame_T *frc;
10199
10200 if (fr->fr_layout == FR_LEAF)
10201 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010202 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010203 if (ses_do_frame(frc))
10204 return TRUE;
10205 return FALSE;
10206}
10207
10208/*
10209 * Return non-zero if window "wp" is to be stored in the Session.
10210 */
10211 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010212ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010214#ifdef FEAT_TERMINAL
10215 if (bt_terminal(wp->w_buffer))
10216 return !term_is_finished(wp->w_buffer)
10217 && (ssop_flags & SSOP_TERMINAL)
10218 && term_should_restore(wp->w_buffer);
10219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 if (wp->w_buffer->b_fname == NULL
10221#ifdef FEAT_QUICKFIX
10222 /* When 'buftype' is "nofile" can't restore the window contents. */
10223 || bt_nofile(wp->w_buffer)
10224#endif
10225 )
10226 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010227 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 return (ssop_flags & SSOP_HELP);
10229 return TRUE;
10230}
10231
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010232 static int
10233put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10234{
10235 int r;
10236
10237 if (wp->w_curswant == MAXCOL)
10238 r = fprintf(fd, "%snormal! $", spaces);
10239 else
10240 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10241 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10242}
10243
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244/*
10245 * Write commands to "fd" to restore the view of a window.
10246 * Caller must make sure 'scrolloff' is zero.
10247 */
10248 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010249put_view(
10250 FILE *fd,
10251 win_T *wp,
10252 int add_edit, /* add ":edit" command to view */
10253 unsigned *flagp, /* vop_flags or ssop_flags */
10254 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010255 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256{
10257 win_T *save_curwin;
10258 int f;
10259 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010260 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261
10262 /* Always restore cursor position for ":mksession". For ":mkview" only
10263 * when 'viewoptions' contains "cursor". */
10264 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10265
10266 /*
10267 * Local argument list.
10268 */
10269 if (wp->w_alist == &global_alist)
10270 {
10271 if (put_line(fd, "argglobal") == FAIL)
10272 return FAIL;
10273 }
10274 else
10275 {
10276 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10277 flagp == &vop_flags
10278 || !(*flagp & SSOP_CURDIR)
10279 || wp->w_localdir != NULL, flagp) == FAIL)
10280 return FAIL;
10281 }
10282
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010283 /* Only when part of a session: restore the argument index. Some
10284 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010285 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010286 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010288 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 || put_eol(fd) == FAIL)
10290 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010291 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 }
10293
10294 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010295 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010297# ifdef FEAT_TERMINAL
10298 if (bt_terminal(wp->w_buffer))
10299 {
10300 if (term_write_session(fd, wp) == FAIL)
10301 return FAIL;
10302 }
10303 else
10304# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 /*
10306 * Load the file.
10307 */
10308 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010309# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 )
10313 {
10314 /*
10315 * Editing a file in this buffer: use ":edit file".
10316 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010317 *
10318 * Note, if a buffer for that file already exists, use :badd to
10319 * edit that buffer, to not lose folding information (:edit resets
10320 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010322 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010323 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010324 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010325 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10326 || fputs(" | else | edit ", fd) < 0
10327 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10328 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010329 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 return FAIL;
10331 }
10332 else
10333 {
10334 /* No file in this buffer, just make it empty. */
10335 if (put_line(fd, "enew") == FAIL)
10336 return FAIL;
10337#ifdef FEAT_QUICKFIX
10338 if (wp->w_buffer->b_ffname != NULL)
10339 {
10340 /* The buffer does have a name, but it's not a file name. */
10341 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010342 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 return FAIL;
10344 }
10345#endif
10346 do_cursor = FALSE;
10347 }
10348 }
10349
10350 /*
10351 * Local mappings and abbreviations.
10352 */
10353 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10354 && makemap(fd, wp->w_buffer) == FAIL)
10355 return FAIL;
10356
10357 /*
10358 * Local options. Need to go to the window temporarily.
10359 * Store only local values when using ":mkview" and when ":mksession" is
10360 * used and 'sessionoptions' doesn't include "options".
10361 * Some folding options are always stored when "folds" is included,
10362 * otherwise the folds would not be restored correctly.
10363 */
10364 save_curwin = curwin;
10365 curwin = wp;
10366 curbuf = curwin->w_buffer;
10367 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10368 f = makeset(fd, OPT_LOCAL,
10369 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10370#ifdef FEAT_FOLDING
10371 else if (*flagp & SSOP_FOLDS)
10372 f = makefoldset(fd);
10373#endif
10374 else
10375 f = OK;
10376 curwin = save_curwin;
10377 curbuf = curwin->w_buffer;
10378 if (f == FAIL)
10379 return FAIL;
10380
10381#ifdef FEAT_FOLDING
10382 /*
10383 * Save Folds when 'buftype' is empty and for help files.
10384 */
10385 if ((*flagp & SSOP_FOLDS)
10386 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010387 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 {
10389 if (put_folds(fd, wp) == FAIL)
10390 return FAIL;
10391 }
10392#endif
10393
10394 /*
10395 * Set the cursor after creating folds, since that moves the cursor.
10396 */
10397 if (do_cursor)
10398 {
10399
10400 /* Restore the cursor line in the file and relatively in the
10401 * window. Don't use "G", it changes the jumplist. */
10402 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10403 (long)wp->w_cursor.lnum,
10404 (long)(wp->w_cursor.lnum - wp->w_topline),
10405 (long)wp->w_height / 2, (long)wp->w_height) < 0
10406 || put_eol(fd) == FAIL
10407 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10408 || put_line(fd, "exe s:l") == FAIL
10409 || put_line(fd, "normal! zt") == FAIL
10410 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10411 || put_eol(fd) == FAIL)
10412 return FAIL;
10413 /* Restore the cursor column and left offset when not wrapping. */
10414 if (wp->w_cursor.col == 0)
10415 {
10416 if (put_line(fd, "normal! 0") == FAIL)
10417 return FAIL;
10418 }
10419 else
10420 {
10421 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10422 {
10423 if (fprintf(fd,
10424 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010425 (long)wp->w_virtcol + 1,
10426 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 (long)wp->w_width / 2, (long)wp->w_width) < 0
10428 || put_eol(fd) == FAIL
10429 || put_line(fd, "if s:c > 0") == FAIL
10430 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010431 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10432 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433 || put_eol(fd) == FAIL
10434 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010435 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 || put_line(fd, "endif") == FAIL)
10437 return FAIL;
10438 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010439 else if (put_view_curpos(fd, wp, "") == FAIL)
10440 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 }
10442 }
10443
10444 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010445 * Local directory, if the current flag is not view options or the "curdir"
10446 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010448 if (wp->w_localdir != NULL
10449 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 {
10451 if (fputs("lcd ", fd) < 0
10452 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10453 || put_eol(fd) == FAIL)
10454 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010455 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 }
10457
10458 return OK;
10459}
10460
10461/*
10462 * Write an argument list to the session file.
10463 * Returns FAIL if writing fails.
10464 */
10465 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010466ses_arglist(
10467 FILE *fd,
10468 char *cmd,
10469 garray_T *gap,
10470 int fullname, /* TRUE: use full path name */
10471 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472{
10473 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010474 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 char_u *s;
10476
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010477 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10478 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010479 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 return FAIL;
10481 for (i = 0; i < gap->ga_len; ++i)
10482 {
10483 /* NULL file names are skipped (only happens when out of memory). */
10484 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10485 if (s != NULL)
10486 {
10487 if (fullname)
10488 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010489 buf = alloc(MAXPATHL);
10490 if (buf != NULL)
10491 {
10492 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10493 s = buf;
10494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010496 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010497 || ses_put_fname(fd, s, flagp) == FAIL
10498 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010499 {
10500 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010502 }
10503 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 }
10505 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010506 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507}
10508
10509/*
10510 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010511 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512 * Returns FAIL if writing fails.
10513 */
10514 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010515ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516{
10517 char_u *name;
10518
10519 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010520 * the session file will be sourced.
10521 * Don't do this for ":mkview", we don't know the current directory.
10522 * Don't do this after ":lcd", we don't keep track of what the current
10523 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 if (buf->b_sfname != NULL
10525 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010526 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010527#ifdef FEAT_AUTOCHDIR
10528 && !p_acd
10529#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010530 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 name = buf->b_sfname;
10532 else
10533 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010534 if (ses_put_fname(fd, name, flagp) == FAIL
10535 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 return FAIL;
10537 return OK;
10538}
10539
10540/*
10541 * Write a file name to the session file.
10542 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10543 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010544 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 */
10546 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010547ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548{
10549 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010550 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552
10553 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010554 if (sname == NULL)
10555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010557 if (*flagp & SSOP_SLASH)
10558 {
10559 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010560 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010561 if (*p == '\\')
10562 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010563 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010564
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010565 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010566 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010568 if (p == NULL)
10569 return FAIL;
10570
10571 /* write the result */
10572 if (fputs((char *)p, fd) < 0)
10573 retval = FAIL;
10574
10575 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 return retval;
10577}
10578
10579/*
10580 * ":loadview [nr]"
10581 */
10582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010583ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584{
10585 char_u *fname;
10586
10587 fname = get_view_file(*eap->arg);
10588 if (fname != NULL)
10589 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010590 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 vim_free(fname);
10592 }
10593}
10594
10595/*
10596 * Get the name of the view file for the current buffer.
10597 */
10598 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010599get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600{
10601 int len = 0;
10602 char_u *p, *s;
10603 char_u *retval;
10604 char_u *sname;
10605
10606 if (curbuf->b_ffname == NULL)
10607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010608 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 return NULL;
10610 }
10611 sname = home_replace_save(NULL, curbuf->b_ffname);
10612 if (sname == NULL)
10613 return NULL;
10614
10615 /*
10616 * We want a file name without separators, because we're not going to make
10617 * a directory.
10618 * "normal" path separator -> "=+"
10619 * "=" -> "=="
10620 * ":" path separator -> "=-"
10621 */
10622 for (p = sname; *p; ++p)
10623 if (*p == '=' || vim_ispathsep(*p))
10624 ++len;
Bram Moolenaar964b3742019-05-24 18:54:09 +020010625 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626 if (retval != NULL)
10627 {
10628 STRCPY(retval, p_vdir);
10629 add_pathsep(retval);
10630 s = retval + STRLEN(retval);
10631 for (p = sname; *p; ++p)
10632 {
10633 if (*p == '=')
10634 {
10635 *s++ = '=';
10636 *s++ = '=';
10637 }
10638 else if (vim_ispathsep(*p))
10639 {
10640 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010641#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 if (*p == ':')
10643 *s++ = '-';
10644 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010646 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 }
10648 else
10649 *s++ = *p;
10650 }
10651 *s++ = '=';
10652 *s++ = c;
10653 STRCPY(s, ".vim");
10654 }
10655
10656 vim_free(sname);
10657 return retval;
10658}
10659
10660#endif /* FEAT_SESSION */
10661
10662/*
10663 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10664 * Return FAIL for a write error.
10665 */
10666 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010667put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668{
10669 if (
10670#ifdef USE_CRNL
10671 (
10672# ifdef MKSESSION_NL
10673 !mksession_nl &&
10674# endif
10675 (putc('\r', fd) < 0)) ||
10676#endif
10677 (putc('\n', fd) < 0))
10678 return FAIL;
10679 return OK;
10680}
10681
10682/*
10683 * Write a line to "fd".
10684 * Return FAIL for a write error.
10685 */
10686 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010687put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10690 return FAIL;
10691 return OK;
10692}
10693
10694#ifdef FEAT_VIMINFO
10695/*
10696 * ":rviminfo" and ":wviminfo".
10697 */
10698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010699ex_viminfo(
10700 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701{
10702 char_u *save_viminfo;
10703
10704 save_viminfo = p_viminfo;
10705 if (*p_viminfo == NUL)
10706 p_viminfo = (char_u *)"'100";
10707 if (eap->cmdidx == CMD_rviminfo)
10708 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010709 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10710 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010711 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 }
10713 else
10714 write_viminfo(eap->arg, eap->forceit);
10715 p_viminfo = save_viminfo;
10716}
10717#endif
10718
10719#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010720/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010721 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010722 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010725dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 if (fname == NULL)
10728 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010729 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730}
10731#endif
10732
10733/*
10734 * ":behave {mswin,xterm}"
10735 */
10736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010737ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738{
10739 if (STRCMP(eap->arg, "mswin") == 0)
10740 {
10741 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10742 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10743 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10744 set_option_value((char_u *)"keymodel", 0L,
10745 (char_u *)"startsel,stopsel", 0);
10746 }
10747 else if (STRCMP(eap->arg, "xterm") == 0)
10748 {
10749 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10750 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10751 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10752 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10753 }
10754 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010755 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756}
10757
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010758#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10759/*
10760 * Function given to ExpandGeneric() to obtain the possible arguments of the
10761 * ":behave {mswin,xterm}" command.
10762 */
10763 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010764get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010765{
10766 if (idx == 0)
10767 return (char_u *)"mswin";
10768 if (idx == 1)
10769 return (char_u *)"xterm";
10770 return NULL;
10771}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010772
10773/*
10774 * Function given to ExpandGeneric() to obtain the possible arguments of the
10775 * ":messages {clear}" command.
10776 */
10777 char_u *
10778get_messages_arg(expand_T *xp UNUSED, int idx)
10779{
10780 if (idx == 0)
10781 return (char_u *)"clear";
10782 return NULL;
10783}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010784#endif
10785
Bram Moolenaar113e1072019-01-20 15:30:40 +010010786#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010787 char_u *
10788get_mapclear_arg(expand_T *xp UNUSED, int idx)
10789{
10790 if (idx == 0)
10791 return (char_u *)"<buffer>";
10792 return NULL;
10793}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010794#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010795
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796static int filetype_detect = FALSE;
10797static int filetype_plugin = FALSE;
10798static int filetype_indent = FALSE;
10799
10800/*
10801 * ":filetype [plugin] [indent] {on,off,detect}"
10802 * on: Load the filetype.vim file to install autocommands for file types.
10803 * off: Load the ftoff.vim file to remove all autocommands for file types.
10804 * plugin on: load filetype.vim and ftplugin.vim
10805 * plugin off: load ftplugof.vim
10806 * indent on: load filetype.vim and indent.vim
10807 * indent off: load indoff.vim
10808 */
10809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010810ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811{
10812 char_u *arg = eap->arg;
10813 int plugin = FALSE;
10814 int indent = FALSE;
10815
10816 if (*eap->arg == NUL)
10817 {
10818 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010819 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 filetype_detect ? "ON" : "OFF",
10821 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10822 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10823 return;
10824 }
10825
10826 /* Accept "plugin" and "indent" in any order. */
10827 for (;;)
10828 {
10829 if (STRNCMP(arg, "plugin", 6) == 0)
10830 {
10831 plugin = TRUE;
10832 arg = skipwhite(arg + 6);
10833 continue;
10834 }
10835 if (STRNCMP(arg, "indent", 6) == 0)
10836 {
10837 indent = TRUE;
10838 arg = skipwhite(arg + 6);
10839 continue;
10840 }
10841 break;
10842 }
10843 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10844 {
10845 if (*arg == 'o' || !filetype_detect)
10846 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010847 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 filetype_detect = TRUE;
10849 if (plugin)
10850 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010851 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 filetype_plugin = TRUE;
10853 }
10854 if (indent)
10855 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010856 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 filetype_indent = TRUE;
10858 }
10859 }
10860 if (*arg == 'd')
10861 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010862 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010863 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 }
10865 }
10866 else if (STRCMP(arg, "off") == 0)
10867 {
10868 if (plugin || indent)
10869 {
10870 if (plugin)
10871 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010872 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 filetype_plugin = FALSE;
10874 }
10875 if (indent)
10876 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010877 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 filetype_indent = FALSE;
10879 }
10880 }
10881 else
10882 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010883 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 filetype_detect = FALSE;
10885 }
10886 }
10887 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010888 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889}
10890
10891/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010892 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 */
10894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010895ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896{
10897 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010898 {
10899 char_u *arg = eap->arg;
10900
10901 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10902 arg += 9;
10903
10904 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10905 if (arg != eap->arg)
10906 did_filetype = FALSE;
10907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010911ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912{
10913#ifdef FEAT_DIGRAPHS
10914 if (*eap->arg != NUL)
10915 putdigraph(eap->arg);
10916 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010917 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010919 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920#endif
10921}
10922
10923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010924ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925{
10926 int flags = 0;
10927
10928 if (eap->cmdidx == CMD_setlocal)
10929 flags = OPT_LOCAL;
10930 else if (eap->cmdidx == CMD_setglobal)
10931 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010932#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 if (cmdmod.browse && flags == 0)
10934 ex_options(eap);
10935 else
10936#endif
10937 (void)do_set(eap->arg, flags);
10938}
10939
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010940#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10941 void
10942set_no_hlsearch(int flag)
10943{
10944 no_hlsearch = flag;
10945# ifdef FEAT_EVAL
10946 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10947# endif
10948}
10949
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950/*
10951 * ":nohlsearch"
10952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010954ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010956 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010957 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958}
10959
10960/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010961 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 * Sets nextcmd to the start of the next command, if any. Also called when
10963 * skipping commands to find the next command.
10964 */
10965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010966ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967{
10968 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010969 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970 char_u *end;
10971 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010972 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010973
10974 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010975 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010976 else
10977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010978 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010979 return;
10980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981
10982 /* First clear any old pattern. */
10983 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010984 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985
10986 if (ends_excmd(*eap->arg))
10987 end = eap->arg;
10988 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010010989 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 end = eap->arg + 4;
10991 else
10992 {
10993 p = skiptowhite(eap->arg);
10994 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010995 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996 p = skipwhite(p);
10997 if (*p == NUL)
10998 {
10999 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011000 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011001 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 return;
11003 }
11004 end = skip_regexp(p + 1, *p, TRUE, NULL);
11005 if (!eap->skip)
11006 {
11007 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11008 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011009 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 eap->errmsg = e_trailing;
11011 return;
11012 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011013 if (*end != *p)
11014 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011015 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011016 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011017 return;
11018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019
11020 c = *end;
11021 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011022 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011023 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011024 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 }
11026 }
11027 eap->nextcmd = find_nextcmd(end);
11028}
11029#endif
11030
11031#ifdef FEAT_CRYPT
11032/*
11033 * ":X": Get crypt key
11034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011036ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011038 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011039 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040}
11041#endif
11042
11043#ifdef FEAT_FOLDING
11044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011045ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046{
11047 if (foldManualAllowed(TRUE))
11048 foldCreate(eap->line1, eap->line2);
11049}
11050
11051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011052ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053{
11054 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11055 eap->forceit, FALSE);
11056}
11057
11058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011059ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
11061 linenr_T lnum;
11062
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011063#ifdef FEAT_CLIPBOARD
11064 start_global_changes();
11065#endif
11066
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 /* First set the marks for all lines closed/open. */
11068 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11069 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11070 ml_setmarked(lnum);
11071
11072 /* Execute the command on the marked lines. */
11073 global_exe(eap->arg);
11074 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011075#ifdef FEAT_CLIPBOARD
11076 end_global_changes();
11077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011078}
11079#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011080
Bram Moolenaar39665952018-08-15 20:59:48 +020011081#ifdef FEAT_QUICKFIX
11082/*
11083 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11084 * instead of a quickfix command.
11085 */
11086 int
11087is_loclist_cmd(int cmdidx)
11088{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011089 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011090 return FALSE;
11091 return cmdnames[cmdidx].cmd_name[0] == 'l';
11092}
11093#endif
11094
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011095# if defined(FEAT_TIMERS) || defined(PROTO)
11096 int
11097get_pressedreturn(void)
11098{
11099 return ex_pressedreturn;
11100}
11101
11102 void
11103set_pressedreturn(int val)
11104{
11105 ex_pressedreturn = val;
11106}
11107#endif