blob: 462851d6a1256306457812e2ba9d60d606891485 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +020023static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026static int if_level = 0; /* depth in :if */
27#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020028static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void append_command(char_u *cmd);
30static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void ex_abbreviate(exarg_T *eap);
33static void ex_map(exarg_T *eap);
34static void ex_unmap(exarg_T *eap);
35static void ex_mapclear(exarg_T *eap);
36static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifndef FEAT_MENU
38# define ex_emenu ex_ni
39# define ex_menu ex_ni
40# define ex_menutranslate ex_ni
41#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static void ex_autocmd(exarg_T *eap);
43static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static void ex_bunload(exarg_T *eap);
45static void ex_buffer(exarg_T *eap);
46static void ex_bmodified(exarg_T *eap);
47static void ex_bnext(exarg_T *eap);
48static void ex_bprevious(exarg_T *eap);
49static void ex_brewind(exarg_T *eap);
50static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static char_u *getargcmd(char_u **);
52static char_u *skip_cmd_arg(char_u *p, int rembs);
53static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#ifndef FEAT_QUICKFIX
55# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000056# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_cc ex_ni
58# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020059# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cfile ex_ni
61# define qf_list ex_ni
62# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020063# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000065# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020067#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068# define ex_cclose ex_ni
69# define ex_copen ex_ni
70# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020071# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000073#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
74# define ex_cexpr ex_ni
75#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaarb7316892019-05-01 18:08:42 +020077static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020079#if !defined(FEAT_PERL) \
80 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
81 || !defined(FEAT_TCL) \
82 || !defined(FEAT_RUBY) \
83 || !defined(FEAT_LUA) \
84 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000085# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010088static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000090#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000092#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
94static void ex_highlight(exarg_T *eap);
95static void ex_colorscheme(exarg_T *eap);
96static void ex_quit(exarg_T *eap);
97static void ex_cquit(exarg_T *eap);
98static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010099static void ex_close(exarg_T *eap);
100static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
101static void ex_only(exarg_T *eap);
102static void ex_resize(exarg_T *eap);
103static void ex_stag(exarg_T *eap);
104static void ex_tabclose(exarg_T *eap);
105static void ex_tabonly(exarg_T *eap);
106static void ex_tabnext(exarg_T *eap);
107static void ex_tabmove(exarg_T *eap);
108static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200109#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_pclose(exarg_T *eap);
111static void ex_ptag(exarg_T *eap);
112static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#else
114# define ex_pclose ex_ni
115# define ex_ptag ex_ni
116# define ex_pedit ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_hide(exarg_T *eap);
119static void ex_stop(exarg_T *eap);
120static void ex_exit(exarg_T *eap);
121static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#else
125# define ex_goto ex_ni
126#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_shell(exarg_T *eap);
128static void ex_preserve(exarg_T *eap);
129static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_mode(exarg_T *eap);
131static void ex_wrongmodifier(exarg_T *eap);
132static void ex_find(exarg_T *eap);
133static void ex_open(exarg_T *eap);
134static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifndef FEAT_GUI
136# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100139#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#else
142# define ex_tearoff ex_ni
143#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100144#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
145 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100146static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#else
148# define ex_popup ex_ni
149#endif
150#ifndef FEAT_GUI_MSWIN
151# define ex_simalt ex_ni
152#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000153#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154# define gui_mch_find_dialog ex_ni
155# define gui_mch_replace_dialog ex_ni
156#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000157#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# define ex_helpfind ex_ni
159#endif
160#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100161# define ex_cscope ex_ni
162# define ex_scscope ex_ni
163# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165#ifndef FEAT_SYN_HL
166# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200167# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000168#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100169#ifndef FEAT_EVAL
170# define ex_packadd ex_ni
171# define ex_packloadall ex_ni
172#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200173#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200174# define ex_syntime ex_ni
175#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000176#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000177# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000178# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000179# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000180# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000181# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000182#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200183#ifndef FEAT_PERSISTENT_UNDO
184# define ex_rundo ex_ni
185# define ex_wundo ex_ni
186#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200187#ifndef FEAT_LUA
188# define ex_lua ex_script_ni
189# define ex_luado ex_ni
190# define ex_luafile ex_ni
191#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000192#ifndef FEAT_MZSCHEME
193# define ex_mzscheme ex_script_ni
194# define ex_mzfile ex_ni
195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#ifndef FEAT_PERL
197# define ex_perl ex_script_ni
198# define ex_perldo ex_ni
199#endif
200#ifndef FEAT_PYTHON
201# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200202# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203# define ex_pyfile ex_ni
204#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200205#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200206# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200207# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200208# define ex_py3file ex_ni
209#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100210#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
211# define ex_pyx ex_script_ni
212# define ex_pyxdo ex_ni
213# define ex_pyxfile ex_ni
214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#ifndef FEAT_TCL
216# define ex_tcl ex_script_ni
217# define ex_tcldo ex_ni
218# define ex_tclfile ex_ni
219#endif
220#ifndef FEAT_RUBY
221# define ex_ruby ex_script_ni
222# define ex_rubydo ex_ni
223# define ex_rubyfile ex_ni
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifndef FEAT_KEYMAP
226# define ex_loadkeymap ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_swapname(exarg_T *eap);
229static void ex_syncbind(exarg_T *eap);
230static void ex_read(exarg_T *eap);
231static void ex_pwd(exarg_T *eap);
232static void ex_equal(exarg_T *eap);
233static void ex_sleep(exarg_T *eap);
234static void do_exmap(exarg_T *eap, int isabbrev);
235static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000237#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#else
240# define ex_winpos ex_ni
241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_operators(exarg_T *eap);
243static void ex_put(exarg_T *eap);
244static void ex_copymove(exarg_T *eap);
245static void ex_submagic(exarg_T *eap);
246static void ex_join(exarg_T *eap);
247static void ex_at(exarg_T *eap);
248static void ex_bang(exarg_T *eap);
249static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200250#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_wundo(exarg_T *eap);
252static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200253#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ex_redo(exarg_T *eap);
255static void ex_later(exarg_T *eap);
256static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100258static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void close_redir(void);
260static void ex_mkrc(exarg_T *eap);
261static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_startinsert(exarg_T *eap);
263static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static void ex_checkpath(exarg_T *eap);
266static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#else
268# define ex_findpat ex_ni
269# define ex_checkpath ex_ni
270#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200271#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100272static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#else
274# define ex_psearch ex_ni
275#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100276static void ex_tag(exarg_T *eap);
277static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifndef FEAT_EVAL
279# define ex_scriptnames ex_ni
280# define ex_finish ex_ni
281# define ex_echo ex_ni
282# define ex_echohl ex_ni
283# define ex_execute ex_ni
284# define ex_call ex_ni
285# define ex_if ex_ni
286# define ex_endif ex_ni
287# define ex_else ex_ni
288# define ex_while ex_ni
289# define ex_continue ex_ni
290# define ex_break ex_ni
291# define ex_endwhile ex_ni
292# define ex_throw ex_ni
293# define ex_try ex_ni
294# define ex_catch ex_ni
295# define ex_finally ex_ni
296# define ex_endtry ex_ni
297# define ex_endfunction ex_ni
298# define ex_let ex_ni
Bram Moolenaar0abb4272019-06-15 16:06:00 +0200299# define ex_const ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000301# define ex_lockvar ex_ni
302# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_function ex_ni
304# define ex_delfunction ex_ni
305# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000306# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100308static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100310static int makeopens(FILE *fd, char_u *dirnow);
311static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
312static void ex_loadview(exarg_T *eap);
313static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000314static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#else
316# define ex_loadview ex_ni
317#endif
318#ifndef FEAT_EVAL
319# define ex_compiler ex_ni
320#endif
321#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#else
324# define ex_viminfo ex_ni
325#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_filetype(exarg_T *eap);
328static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000330# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331# define ex_diffpatch ex_ni
332# define ex_diffgetput ex_ni
333# define ex_diffsplit ex_ni
334# define ex_diffthis ex_ni
335# define ex_diffupdate ex_ni
336#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_digraphs(exarg_T *eap);
338static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100339#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340# define ex_options ex_ni
341#endif
342#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100343static void ex_nohlsearch(exarg_T *eap);
344static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#else
346# define ex_nohlsearch ex_ni
347# define ex_match ex_ni
348#endif
349#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100350static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351#else
352# define ex_X ex_ni
353#endif
354#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100355static void ex_fold(exarg_T *eap);
356static void ex_foldopen(exarg_T *eap);
357static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#else
359# define ex_fold ex_ni
360# define ex_foldopen ex_ni
361# define ex_folddo ex_ni
362#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100363#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_language ex_ni
365#endif
366#ifndef FEAT_SIGNS
367# define ex_sign ex_ni
368#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000369#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200370# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000371# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200372# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifndef FEAT_EVAL
376# define ex_debug ex_ni
377# define ex_breakadd ex_ni
378# define ex_debuggreedy ex_ni
379# define ex_breakdel ex_ni
380# define ex_breaklist ex_ni
381#endif
382
383#ifndef FEAT_CMDHIST
384# define ex_history ex_ni
385#endif
386#ifndef FEAT_JUMPLIST
387# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200388# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389# define ex_changes ex_ni
390#endif
391
Bram Moolenaar05159a02005-02-26 23:04:13 +0000392#ifndef FEAT_PROFILE
393# define ex_profile ex_ni
394#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200395#ifndef FEAT_TERMINAL
396# define ex_terminal ex_ni
397#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200398#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
399# define ex_xrestore ex_ni
400#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200401#if !defined(FEAT_TEXT_PROP)
402# define ex_popupclear ex_ni
403#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000404
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405/*
406 * Declare cmdnames[].
407 */
408#define DO_DECLARE_EXCMD
409#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200410#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100411
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412static char_u dollar_command[2] = {'$', 0};
413
414
415#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000416/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417typedef struct
418{
419 char_u *line; /* command line */
420 linenr_T lnum; /* sourcing_lnum of the line */
421} wcmd_T;
422
423/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000424 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000426 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000428struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429{
430 garray_T *lines_gap; /* growarray with line info */
431 int current_line; /* last read line from growarray */
432 int repeating; /* TRUE when looping a second time */
433 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200434 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 void *cookie;
436};
437
Bram Moolenaare96a2492019-06-25 04:12:16 +0200438static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100439static int store_loop_line(garray_T *gap, char_u *line);
440static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000441
442/* Struct to save a few things while debugging. Used in do_cmdline() only. */
443struct dbg_stuff
444{
445 int trylevel;
446 int force_abort;
447 except_T *caught_stack;
448 char_u *vv_exception;
449 char_u *vv_throwpoint;
450 int did_emsg;
451 int got_int;
452 int did_throw;
453 int need_rethrow;
454 int check_cstack;
455 except_T *current_exception;
456};
457
Bram Moolenaared203462004-06-16 11:19:22 +0000458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100459save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000460{
461 dsp->trylevel = trylevel; trylevel = 0;
462 dsp->force_abort = force_abort; force_abort = FALSE;
463 dsp->caught_stack = caught_stack; caught_stack = NULL;
464 dsp->vv_exception = v_exception(NULL);
465 dsp->vv_throwpoint = v_throwpoint(NULL);
466
467 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
468 dsp->did_emsg = did_emsg; did_emsg = FALSE;
469 dsp->got_int = got_int; got_int = FALSE;
470 dsp->did_throw = did_throw; did_throw = FALSE;
471 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
472 dsp->check_cstack = check_cstack; check_cstack = FALSE;
473 dsp->current_exception = current_exception; current_exception = NULL;
474}
475
476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100477restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000478{
479 suppress_errthrow = FALSE;
480 trylevel = dsp->trylevel;
481 force_abort = dsp->force_abort;
482 caught_stack = dsp->caught_stack;
483 (void)v_exception(dsp->vv_exception);
484 (void)v_throwpoint(dsp->vv_throwpoint);
485 did_emsg = dsp->did_emsg;
486 got_int = dsp->got_int;
487 did_throw = dsp->did_throw;
488 need_rethrow = dsp->need_rethrow;
489 check_cstack = dsp->check_cstack;
490 current_exception = dsp->current_exception;
491}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492#endif
493
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494/*
495 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
496 * command is given.
497 */
498 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100499do_exmode(
500 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501{
502 int save_msg_scroll;
503 int prev_msg_row;
504 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100505 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000506
507 if (improved)
508 exmode_active = EXMODE_VIM;
509 else
510 exmode_active = EXMODE_NORMAL;
511 State = NORMAL;
512
513 /* When using ":global /pat/ visual" and then "Q" we return to continue
514 * the :global command. */
515 if (global_busy)
516 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
518 save_msg_scroll = msg_scroll;
519 ++RedrawingDisabled; /* don't redisplay the window */
520 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521#ifdef FEAT_GUI
522 /* Ignore scrollbar and mouse events in Ex mode */
523 ++hold_gui_events;
524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525
Bram Moolenaar32526b32019-01-19 17:43:09 +0100526 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 while (exmode_active)
528 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000529 /* Check for a ":normal" command and no more characters left. */
530 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
531 {
532 exmode_active = FALSE;
533 break;
534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 msg_scroll = TRUE;
536 need_wait_return = FALSE;
537 ex_pressedreturn = FALSE;
538 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100539 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 prev_msg_row = msg_row;
541 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 if (improved)
543 {
544 cmdline_row = msg_row;
545 do_cmdline(NULL, getexline, NULL, 0);
546 }
547 else
548 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
549 lines_left = Rows - 1;
550
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100552 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100555 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000558 if (ex_pressedreturn)
559 {
560 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000561 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000562 msg_row = prev_msg_row;
563 if (prev_msg_row == Rows - 1)
564 msg_row--;
565 }
566 msg_col = 0;
567 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
568 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000571 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
572 {
573 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100574 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000575 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100576 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
579
580#ifdef FEAT_GUI
581 --hold_gui_events;
582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 --RedrawingDisabled;
584 --no_wait_return;
585 update_screen(CLEAR);
586 need_wait_return = FALSE;
587 msg_scroll = save_msg_scroll;
588}
589
590/*
591 * Execute a simple command line. Used for translated commands like "*".
592 */
593 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100594do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595{
596 return do_cmdline(cmd, NULL, NULL,
597 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
598}
599
600/*
601 * do_cmdline(): execute one Ex command line
602 *
603 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100604 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 * 2. Split up in parts separated with '|'.
606 *
607 * This function can be called recursively!
608 *
609 * flags:
610 * DOCMD_VERBOSE - The command will be included in the error message.
611 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100612 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * DOCMD_KEYTYPED - Don't reset KeyTyped.
614 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
615 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
616 *
617 * return FAIL if cmdline could not be executed, OK otherwise
618 */
619 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100620do_cmdline(
621 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200622 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100623 void *cookie, /* argument for fgetline() */
624 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625{
626 char_u *next_cmdline; /* next cmd to execute */
627 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100628 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 static int recursive = 0; /* recursive depth */
630 int msg_didout_before_start = 0;
631 int count = 0; /* line number count */
632 int did_inc = FALSE; /* incremented RedrawingDisabled */
633 int retval = OK;
634#ifdef FEAT_EVAL
635 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000636 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int current_line = 0; /* active line in lines_ga */
638 char_u *fname = NULL; /* function or script name */
639 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
640 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000641 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 int initial_trylevel;
643 struct msglist **saved_msg_list = NULL;
644 struct msglist *private_msg_list;
645
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100646 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200647 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000649 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000651 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100653# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654# define cmd_cookie cookie
655#endif
656 static int call_depth = 0; /* recursiveness */
657
658#ifdef FEAT_EVAL
659 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
660 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000661 * This ensures that the do_errthrow() call in do_one_cmd() does not
662 * combine the messages stored by an earlier invocation of do_one_cmd()
663 * with the command name of the later one. This would happen when
664 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 saved_msg_list = msg_list;
666 msg_list = &private_msg_list;
667 private_msg_list = NULL;
668#endif
669
670 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100671 * here. The value of 200 allows nested function calls, ":source", etc.
672 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100673 if (call_depth >= 200
674#ifdef FEAT_EVAL
675 && call_depth >= p_mfd
676#endif
677 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100679 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680#ifdef FEAT_EVAL
681 /* When converting to an exception, we do not include the command name
682 * since this is not an error of the specific command. */
683 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
684 msg_list = saved_msg_list;
685#endif
686 return FAIL;
687 }
688 ++call_depth;
689
690#ifdef FEAT_EVAL
691 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000692 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 cstack.cs_trylevel = 0;
694 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000695 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
697
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699
700 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100701 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 ++ex_nesting_level;
704
705 /* Get the function or script name and the address where the next breakpoint
706 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000707 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
709 fname = func_name(real_cookie);
710 breakpoint = func_breakpoint(real_cookie);
711 dbg_tick = func_dbg_tick(real_cookie);
712 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100713 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
715 fname = sourcing_name;
716 breakpoint = source_breakpoint(real_cookie);
717 dbg_tick = source_dbg_tick(real_cookie);
718 }
719
720 /*
721 * Initialize "force_abort" and "suppress_errthrow" at the top level.
722 */
723 if (!recursive)
724 {
725 force_abort = FALSE;
726 suppress_errthrow = FALSE;
727 }
728
729 /*
730 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 * exception handling (used when debugging). Otherwise clear it to avoid
732 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000734 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000735 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000736 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200737 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
739 initial_trylevel = trylevel;
740
741 /*
742 * "did_throw" will be set to TRUE when an exception is being thrown.
743 */
744 did_throw = FALSE;
745#endif
746 /*
747 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000748 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 * If force_abort is set, we cancel everything.
750 */
751 did_emsg = FALSE;
752
753 /*
754 * KeyTyped is only set when calling vgetc(). Reset it here when not
755 * calling vgetc() (sourced command lines).
756 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 if (!(flags & DOCMD_KEYTYPED)
758 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 KeyTyped = FALSE;
760
761 /*
762 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000763 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 * - for multiple commands on one line, separated with '|'
765 * - when repeating until there are no more lines (for ":source")
766 */
767 next_cmdline = cmdline;
768 do
769 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100771 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772#endif
773
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000774 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 if (next_cmdline == NULL
776#ifdef FEAT_EVAL
777 && !force_abort
778 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000779 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
781 )
782 did_emsg = FALSE;
783
784 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000785 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100786 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * 3. If a line is given: Make a copy, so we can mess with it.
788 */
789
790#ifdef FEAT_EVAL
791 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000792 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
794 /* Each '|' separated command is stored separately in lines_ga, to
795 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100796 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
798 /* Check if a function has returned or, unless it has an unclosed
799 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000803 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804 func_line_end(real_cookie);
805# endif
806 if (func_has_ended(real_cookie))
807 {
808 retval = FAIL;
809 break;
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000813 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000815 script_line_end();
816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817
818 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100819 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 {
821 retval = FAIL;
822 break;
823 }
824
825 /* If breakpoints have been added/deleted need to check for it. */
826 if (breakpoint != NULL && dbg_tick != NULL
827 && *dbg_tick != debug_tick)
828 {
829 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100830 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 fname, sourcing_lnum);
832 *dbg_tick = debug_tick;
833 }
834
835 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
836 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
837
838 /* Did we encounter a breakpoint? */
839 if (breakpoint != NULL && *breakpoint != 0
840 && *breakpoint <= sourcing_lnum)
841 {
842 dbg_breakpoint(fname, sourcing_lnum);
843 /* Find next breakpoint. */
844 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100845 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 fname, sourcing_lnum);
847 *dbg_tick = debug_tick;
848 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000850 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 {
852 if (getline_is_func)
853 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100854 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000855 script_line_start();
856 }
857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000860 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * below, so that it stores lines in or reads them from
865 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000866 * while/for loop. */
867 cmd_getline = get_loop_line;
868 cmd_cookie = (void *)&cmd_loop_cookie;
869 cmd_loop_cookie.lines_gap = &lines_ga;
870 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000872 cmd_loop_cookie.cookie = cookie;
873 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
875 else
876 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100877 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 cmd_cookie = cookie;
879 }
880#endif
881
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (next_cmdline == NULL)
884 {
885 /*
886 * Need to set msg_didout for the first line after an ":if",
887 * otherwise the ":if" will be overwritten.
888 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100889 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100891 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892#ifdef FEAT_EVAL
893 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
894#else
895 0
896#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200897 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
899 /* Don't call wait_return for aborted command line. The NULL
900 * returned for the end of a sourced file or executed function
901 * doesn't do this. */
902 if (KeyTyped && !(flags & DOCMD_REPEAT))
903 need_wait_return = FALSE;
904 retval = FAIL;
905 break;
906 }
907 used_getline = TRUE;
908
909 /*
910 * Keep the first typed line. Clear it when more lines are typed.
911 */
912 if (flags & DOCMD_KEEPLINE)
913 {
914 vim_free(repeat_cmdline);
915 if (count == 0)
916 repeat_cmdline = vim_strsave(next_cmdline);
917 else
918 repeat_cmdline = NULL;
919 }
920 }
921
922 /* 3. Make a copy of the command so we can mess with it. */
923 else if (cmdline_copy == NULL)
924 {
925 next_cmdline = vim_strsave(next_cmdline);
926 if (next_cmdline == NULL)
927 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100928 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 retval = FAIL;
930 break;
931 }
932 }
933 cmdline_copy = next_cmdline;
934
935#ifdef FEAT_EVAL
936 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 * Save the current line when inside a ":while" or ":for", and when
938 * the command looks like a ":while" or ":for", because we may need it
939 * later. When there is a '|' and another command, it is stored
940 * separately, because we need to be able to jump back to it from an
941 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000943 if (current_line == lines_ga.ga_len
944 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000946 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
948 retval = FAIL;
949 break;
950 }
951 }
952 did_endif = FALSE;
953#endif
954
955 if (count++ == 0)
956 {
957 /*
958 * All output from the commands is put below each other, without
959 * waiting for a return. Don't do this when executing commands
960 * from a script or when being called recursive (e.g. for ":e
961 * +command file").
962 */
963 if (!(flags & DOCMD_NOWAIT) && !recursive)
964 {
965 msg_didout_before_start = msg_didout;
966 msg_didany = FALSE; /* no output yet */
967 msg_start();
968 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200969 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 ++RedrawingDisabled;
971 did_inc = TRUE;
972 }
973 }
974
975 if (p_verbose >= 15 && sourcing_name != NULL)
976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000978 verbose_enter_scroll();
979
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100980 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000982 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100983 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000984
985 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 --no_wait_return;
987 }
988
989 /*
990 * 2. Execute one '|' separated command.
991 * do_one_cmd() will return NULL if there is no trailing '|'.
992 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
993 */
994 ++recursive;
995 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
996#ifdef FEAT_EVAL
997 &cstack,
998#endif
999 cmd_getline, cmd_cookie);
1000 --recursive;
1001
1002#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 if (cmd_cookie == (void *)&cmd_loop_cookie)
1004 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001006 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#endif
1008
1009 if (next_cmdline == NULL)
1010 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001011 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#ifdef FEAT_CMDHIST
1013 /*
1014 * If the command was typed, remember it for the ':' register.
1015 * Do this AFTER executing the command to make :@: work.
1016 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 && new_last_cmdline != NULL)
1019 {
1020 vim_free(last_cmdline);
1021 last_cmdline = new_last_cmdline;
1022 new_last_cmdline = NULL;
1023 }
1024#endif
1025 }
1026 else
1027 {
1028 /* need to copy the command after the '|' to cmdline_copy, for the
1029 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001030 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 next_cmdline = cmdline_copy;
1032 }
1033
1034
1035#ifdef FEAT_EVAL
1036 /* reset did_emsg for a function that is not aborted by an error */
1037 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001038 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 && !func_has_abort(real_cookie))
1040 did_emsg = FALSE;
1041
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 ++current_line;
1045
1046 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 * An ":endwhile", ":endfor" and ":continue" is handled here.
1048 * If we were executing commands, jump back to the ":while" or
1049 * ":for".
1050 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 /* Jump back to the matching ":while" or ":for". Be careful
1057 * not to use a cs_line[] from an entry that isn't a ":while"
1058 * or ":for": It would make "current_line" invalid and can
1059 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (!did_emsg && !got_int && !did_throw
1061 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 && (cstack.cs_flags[cstack.cs_idx]
1063 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 && cstack.cs_line[cstack.cs_idx] >= 0
1065 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1066 {
1067 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 /* remember we jumped there */
1069 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 line_breakcheck(); /* check if CTRL-C typed */
1071
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 /* Check for the next breakpoint at or after the ":while"
1073 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (breakpoint != NULL)
1075 {
1076 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001077 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 fname,
1079 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1080 *dbg_tick = debug_tick;
1081 }
1082 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001087 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1088 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091
1092 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001097 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1099 }
1100 }
1101
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001102 /* Check for the next breakpoint after a watchexpression */
1103 if (breakpoint != NULL && has_watchexpr())
1104 {
1105 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1106 *dbg_tick = debug_tick;
1107 }
1108
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 /*
1110 * When not inside any ":while" loop, clear remembered lines.
1111 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
1114 if (lines_ga.ga_len > 0)
1115 {
1116 sourcing_lnum =
1117 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1118 free_cmdlines(&lines_ga);
1119 }
1120 current_line = 0;
1121 }
1122
1123 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1125 * being restored at the ":endtry". Reset them here and set the
1126 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1127 * This includes the case where a missing ":endif", ":endwhile" or
1128 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001132 cstack.cs_lflags &= ~CSL_HAD_FINA;
1133 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1134 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 did_throw ? (void *)current_exception : NULL);
1136 did_emsg = got_int = did_throw = FALSE;
1137 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1138 }
1139
1140 /* Update global "trylevel" for recursive calls to do_cmdline() from
1141 * within this loop. */
1142 trylevel = initial_trylevel + cstack.cs_trylevel;
1143
1144 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001145 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * files) is aborted because of an error, an interrupt, or an uncaught
1147 * exception, cancel everything. If it is left normally, reset
1148 * force_abort to get the non-EH compatible abortion behavior for
1149 * the rest of the script.
1150 */
1151 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1152 force_abort = FALSE;
1153
1154 /* Convert an interrupt to an exception if appropriate. */
1155 (void)do_intthrow(&cstack);
1156#endif /* FEAT_EVAL */
1157
1158 }
1159 /*
1160 * Continue executing command lines when:
1161 * - no CTRL-C typed, no aborting error, no exception thrown or try
1162 * conditionals need to be checked for executing finally clauses or
1163 * catching an interrupt exception
1164 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001165 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 * looping for ":source" command or function call.
1167 */
1168 while (!((got_int
1169#ifdef FEAT_EVAL
1170 || (did_emsg && force_abort) || did_throw
1171#endif
1172 )
1173#ifdef FEAT_EVAL
1174 && cstack.cs_trylevel == 0
1175#endif
1176 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001177 && !(did_emsg
1178#ifdef FEAT_EVAL
1179 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001180 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001181 * the :endtry to be missed. */
1182 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1183#endif
1184 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001185 && (getline_equal(fgetline, cookie, getexmodeline)
1186 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 && (next_cmdline != NULL
1188#ifdef FEAT_EVAL
1189 || cstack.cs_idx >= 0
1190#endif
1191 || (flags & DOCMD_REPEAT)));
1192
1193 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001194 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195#ifdef FEAT_EVAL
1196 free_cmdlines(&lines_ga);
1197 ga_clear(&lines_ga);
1198
1199 if (cstack.cs_idx >= 0)
1200 {
1201 /*
1202 * If a sourced file or executed function ran to its end, report the
1203 * unclosed conditional.
1204 */
1205 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001206 && ((getline_equal(fgetline, cookie, getsourceline)
1207 && !source_finished(fgetline, cookie))
1208 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 && !func_has_ended(real_cookie))))
1210 {
1211 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001212 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001215 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001218 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 }
1220
1221 /*
1222 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1223 * ":endtry" in a sourced file or executed function. If the try
1224 * conditional is in its finally clause, ignore anything pending.
1225 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001226 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
1228 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001229 {
1230 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1231
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001232 if (idx >= 0)
1233 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001234 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1235 &cstack.cs_looplevel);
1236 }
1237 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 trylevel = initial_trylevel;
1239 }
1240
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001241 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1242 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001244 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 ? (char_u *)"endfunction" : (char_u *)NULL);
1246
1247 if (trylevel == 0)
1248 {
1249 /*
1250 * When an exception is being thrown out of the outermost try
1251 * conditional, discard the uncaught exception, disable the conversion
1252 * of interrupts or errors to exceptions, and ensure that no more
1253 * commands are executed.
1254 */
1255 if (did_throw)
1256 {
1257 void *p = NULL;
1258 char_u *saved_sourcing_name;
1259 int saved_sourcing_lnum;
1260 struct msglist *messages = NULL, *next;
1261
1262 /*
1263 * If the uncaught exception is a user exception, report it as an
1264 * error. If it is an error exception, display the saved error
1265 * message now. For an interrupt exception, do nothing; the
1266 * interrupt message is given elsewhere.
1267 */
1268 switch (current_exception->type)
1269 {
1270 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001271 vim_snprintf((char *)IObuff, IOSIZE,
1272 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 current_exception->value);
1274 p = vim_strsave(IObuff);
1275 break;
1276 case ET_ERROR:
1277 messages = current_exception->messages;
1278 current_exception->messages = NULL;
1279 break;
1280 case ET_INTERRUPT:
1281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 }
1283
1284 saved_sourcing_name = sourcing_name;
1285 saved_sourcing_lnum = sourcing_lnum;
1286 sourcing_name = current_exception->throw_name;
1287 sourcing_lnum = current_exception->throw_lnum;
1288 current_exception->throw_name = NULL;
1289
1290 discard_current_exception(); /* uses IObuff if 'verbose' */
1291 suppress_errthrow = TRUE;
1292 force_abort = TRUE;
1293
1294 if (messages != NULL)
1295 {
1296 do
1297 {
1298 next = messages->next;
1299 emsg(messages->msg);
1300 vim_free(messages->msg);
1301 vim_free(messages);
1302 messages = next;
1303 }
1304 while (messages != NULL);
1305 }
1306 else if (p != NULL)
1307 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001308 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 vim_free(p);
1310 }
1311 vim_free(sourcing_name);
1312 sourcing_name = saved_sourcing_name;
1313 sourcing_lnum = saved_sourcing_lnum;
1314 }
1315
1316 /*
1317 * On an interrupt or an aborting error not converted to an exception,
1318 * disable the conversion of errors to exceptions. (Interrupts are not
1319 * converted any more, here.) This enables also the interrupt message
1320 * when force_abort is set and did_emsg unset in case of an interrupt
1321 * from a finally clause after an error.
1322 */
1323 else if (got_int || (did_emsg && force_abort))
1324 suppress_errthrow = TRUE;
1325 }
1326
1327 /*
1328 * The current cstack will be freed when do_cmdline() returns. An uncaught
1329 * exception will have to be rethrown in the previous cstack. If a function
1330 * has just returned or a script file was just finished and the previous
1331 * cstack belongs to the same function or, respectively, script file, it
1332 * will have to be checked for finally clauses to be executed due to the
1333 * ":return" or ":finish". This is done in do_one_cmd().
1334 */
1335 if (did_throw)
1336 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && ex_nesting_level > func_level(real_cookie) + 1))
1341 {
1342 if (!did_throw)
1343 check_cstack = TRUE;
1344 }
1345 else
1346 {
1347 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 --ex_nesting_level;
1350 /*
1351 * Go to debug mode when returning from a function in which we are
1352 * single-stepping.
1353 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001354 if ((getline_equal(fgetline, cookie, getsourceline)
1355 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001357 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 ? (char_u *)_("End of sourced file")
1359 : (char_u *)_("End of function"));
1360 }
1361
1362 /*
1363 * Restore the exception environment (done after returning from the
1364 * debugger).
1365 */
1366 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001367 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 msg_list = saved_msg_list;
1370#endif /* FEAT_EVAL */
1371
1372 /*
1373 * If there was too much output to fit on the command line, ask the user to
1374 * hit return before redrawing the screen. With the ":global" command we do
1375 * this only once after the command is finished.
1376 */
1377 if (did_inc)
1378 {
1379 --RedrawingDisabled;
1380 --no_wait_return;
1381 msg_scroll = FALSE;
1382
1383 /*
1384 * When just finished an ":if"-":else" which was typed, no need to
1385 * wait for hit-return. Also for an error situation.
1386 */
1387 if (retval == FAIL
1388#ifdef FEAT_EVAL
1389 || (did_endif && KeyTyped && !did_emsg)
1390#endif
1391 )
1392 {
1393 need_wait_return = FALSE;
1394 msg_didany = FALSE; /* don't wait when restarting edit */
1395 }
1396 else if (need_wait_return)
1397 {
1398 /*
1399 * The msg_start() above clears msg_didout. The wait_return we do
1400 * here should not overwrite the command that may be shown before
1401 * doing that.
1402 */
1403 msg_didout |= msg_didout_before_start;
1404 wait_return(FALSE);
1405 }
1406 }
1407
Bram Moolenaar2a942252012-11-28 23:03:07 +01001408#ifdef FEAT_EVAL
1409 did_endif = FALSE; /* in case do_cmdline used recursively */
1410#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 /*
1412 * Reset if_level, in case a sourced script file contains more ":if" than
1413 * ":endif" (could be ":if x | foo | endif").
1414 */
1415 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001416#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 --call_depth;
1419 return retval;
1420}
1421
1422#ifdef FEAT_EVAL
1423/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001424 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 */
1426 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001427get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001429 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 wcmd_T *wp;
1431 char_u *line;
1432
1433 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1434 {
1435 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001436 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001438 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001440 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001442 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001443 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 ++cp->current_line;
1445
1446 return line;
1447 }
1448
1449 KeyTyped = FALSE;
1450 ++cp->current_line;
1451 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1452 sourcing_lnum = wp->lnum;
1453 return vim_strsave(wp->line);
1454}
1455
1456/*
1457 * Store a line in "gap" so that a ":while" loop can execute it again.
1458 */
1459 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001460store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 if (ga_grow(gap, 1) == FAIL)
1463 return FAIL;
1464 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1465 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1466 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 return OK;
1468}
1469
1470/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001471 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 */
1473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001474free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 while (gap->ga_len > 0)
1477 {
1478 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1479 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 }
1481}
1482#endif
1483
1484/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001485 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1486 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001489getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001491 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001492 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
1494#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001495 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001496 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaar89d40322006-08-29 15:30:07 +00001498 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001499 * function that's originally used to obtain the lines. This may be
1500 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001501 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 cp = (struct loop_cookie *)cookie;
1503 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 {
1505 gp = cp->getline;
1506 cp = cp->cookie;
1507 }
1508 return gp == func;
1509#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
1512}
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001515 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 * getline function. Otherwise return "cookie".
1517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001519getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001520 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001521 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522{
Bram Moolenaar13505972019-01-24 15:04:48 +01001523#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001524 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaar89d40322006-08-29 15:30:07 +00001527 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001528 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001530 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001531 cp = (struct loop_cookie *)cookie;
1532 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 {
1534 gp = cp->getline;
1535 cp = cp->cookie;
1536 }
1537 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001538#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543
1544/*
1545 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1546 * ":bwipeout", etc.
1547 * Returns the buffer number.
1548 */
1549 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001550compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001551{
1552 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001553 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001554 int count = offset;
1555
1556 buf = firstbuf;
1557 while (buf->b_next != NULL && buf->b_fnum < lnum)
1558 buf = buf->b_next;
1559 while (count != 0)
1560 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001561 count += (offset < 0) ? 1 : -1;
1562 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1563 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001564 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001565 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001566 if (addr_type == ADDR_LOADED_BUFFERS)
1567 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001568 while (buf->b_ml.ml_mfp == NULL)
1569 {
1570 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1571 if (nextbuf == NULL)
1572 break;
1573 buf = nextbuf;
1574 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001575 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001576 /* we might have gone too far, last buffer is not loadedd */
1577 if (addr_type == ADDR_LOADED_BUFFERS)
1578 while (buf->b_ml.ml_mfp == NULL)
1579 {
1580 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1581 if (nextbuf == NULL)
1582 break;
1583 buf = nextbuf;
1584 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001585 return buf->b_fnum;
1586}
1587
Bram Moolenaarb7316892019-05-01 18:08:42 +02001588/*
1589 * Return the window number of "win".
1590 * When "win" is NULL return the number of windows.
1591 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001592 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001593current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001594{
1595 win_T *wp;
1596 int nr = 0;
1597
Bram Moolenaar29323592016-07-24 22:04:11 +02001598 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001599 {
1600 ++nr;
1601 if (wp == win)
1602 break;
1603 }
1604 return nr;
1605}
1606
1607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001608current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001609{
1610 tabpage_T *tp;
1611 int nr = 0;
1612
Bram Moolenaar29323592016-07-24 22:04:11 +02001613 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001614 {
1615 ++nr;
1616 if (tp == tab)
1617 break;
1618 }
1619 return nr;
1620}
1621
1622# define CURRENT_WIN_NR current_win_nr(curwin)
1623# define LAST_WIN_NR current_win_nr(NULL)
1624# define CURRENT_TAB_NR current_tab_nr(curtab)
1625# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627/*
1628 * Execute one Ex command.
1629 *
1630 * If 'sourcing' is TRUE, the command will be included in the error message.
1631 *
1632 * 1. skip comment lines and leading space
1633 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001634 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001635 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001636 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001637 * 6. parse arguments
1638 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 *
1642 * This function may be called recursively!
1643 */
1644#if (_MSC_VER == 1200)
1645/*
Bram Moolenaared203462004-06-16 11:19:22 +00001646 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001648 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
1650 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001651do_one_cmd(
1652 char_u **cmdlinep,
1653 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001655 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001657 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001658 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659{
1660 char_u *p;
1661 linenr_T lnum;
1662 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001663 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001664 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001666 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001668 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001670 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672 vim_memset(&ea, 0, sizeof(ea));
1673 ea.line1 = 1;
1674 ea.line2 = 1;
1675#ifdef FEAT_EVAL
1676 ++ex_nesting_level;
1677#endif
1678
Bram Moolenaar21691f82012-12-05 19:13:18 +01001679 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (quitmore
1681#ifdef FEAT_EVAL
1682 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001683 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001684#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001685 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001686 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 --quitmore;
1688
1689 /*
1690 * Reset browse, confirm, etc.. They are restored when returning, for
1691 * recursive calls.
1692 */
1693 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001695 /* "#!anything" is handled like a comment. */
1696 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1697 goto doend;
1698
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001699/*
1700 * 1. Skip comment lines and leading white space and colons.
1701 * 2. Handle command modifiers.
1702 */
1703 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001705 ea.cmdlinep = cmdlinep;
1706 ea.getline = fgetline;
1707 ea.cookie = cookie;
1708#ifdef FEAT_EVAL
1709 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001711 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001712 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001714 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715
1716#ifdef FEAT_EVAL
1717 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1718 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1719#else
1720 ea.skip = (if_level > 0);
1721#endif
1722
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001724 * 3. Skip over the range to find the command. Let "p" point to after it.
1725 *
1726 * We need the command to know what kind of range it uses.
1727 */
1728 cmd = ea.cmd;
1729 ea.cmd = skip_range(ea.cmd, NULL);
1730 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1731 ea.cmd = skipwhite(ea.cmd + 1);
1732 p = find_command(&ea, NULL);
1733
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001734#ifdef FEAT_EVAL
1735# ifdef FEAT_PROFILE
1736 // Count this line for profiling if skip is TRUE.
1737 if (do_profiling == PROF_YES
1738 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1739 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1740 {
1741 int skip = did_emsg || got_int || did_throw;
1742
1743 if (ea.cmdidx == CMD_catch)
1744 skip = !skip && !(cstack->cs_idx >= 0
1745 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1746 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1747 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1748 skip = skip || !(cstack->cs_idx >= 0
1749 && !(cstack->cs_flags[cstack->cs_idx]
1750 & (CSF_ACTIVE | CSF_TRUE)));
1751 else if (ea.cmdidx == CMD_finally)
1752 skip = FALSE;
1753 else if (ea.cmdidx != CMD_endif
1754 && ea.cmdidx != CMD_endfor
1755 && ea.cmdidx != CMD_endtry
1756 && ea.cmdidx != CMD_endwhile)
1757 skip = ea.skip;
1758
1759 if (!skip)
1760 {
1761 if (getline_equal(fgetline, cookie, get_func_line))
1762 func_line_exec(getline_cookie(fgetline, cookie));
1763 else if (getline_equal(fgetline, cookie, getsourceline))
1764 script_line_exec();
1765 }
1766 }
1767# endif
1768
1769 /* May go to debug mode. If this happens and the ">quit" debug command is
1770 * used, throw an interrupt exception and skip the next command. */
1771 dbg_check_breakpoint(&ea);
1772 if (!ea.skip && got_int)
1773 {
1774 ea.skip = TRUE;
1775 (void)do_intthrow(cstack);
1776 }
1777#endif
1778
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001779/*
1780 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 *
1782 * where 'addr' is:
1783 *
1784 * % (entire file)
1785 * $ [+-NUM]
1786 * 'x [+-NUM] (where x denotes a currently defined mark)
1787 * . [+-NUM]
1788 * [+-NUM]..
1789 * NUM
1790 *
1791 * The ea.cmd pointer is updated to point to the first character following the
1792 * range spec. If an initial address is found, but no second, the upper bound
1793 * is equal to the lower.
1794 */
1795
Bram Moolenaar25190db2019-05-04 15:05:28 +02001796 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001797 if (!IS_USER_CMDIDX(ea.cmdidx))
1798 {
1799 if (ea.cmdidx != CMD_SIZE)
1800 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1801 else
1802 ea.addr_type = ADDR_LINES;
1803
Bram Moolenaar25190db2019-05-04 15:05:28 +02001804 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001805 if (ea.cmdidx == CMD_wincmd && p != NULL)
1806 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001807#ifdef FEAT_QUICKFIX
1808 // :.cc in quickfix window uses line number
1809 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1810 ea.addr_type = ADDR_OTHER;
1811#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001812 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001813
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001814 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001815 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001816 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001819 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 */
1821
1822 /*
1823 * Skip ':' and any white space
1824 */
1825 ea.cmd = skipwhite(ea.cmd);
1826 while (*ea.cmd == ':')
1827 ea.cmd = skipwhite(ea.cmd + 1);
1828
1829 /*
1830 * If we got a line, but no command, then go to the line.
1831 * If we find a '|' or '\n' we set ea.nextcmd.
1832 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001833 if (*ea.cmd == NUL || *ea.cmd == '"'
1834 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 /*
1837 * strange vi behaviour:
1838 * ":3" jumps to line 3
1839 * ":3|..." prints line 3
1840 * ":|" prints current line
1841 */
1842 if (ea.skip) /* skip this if inside :if */
1843 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001844 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
1846 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001847 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if ((errormsg = invalid_range(&ea)) == NULL)
1849 {
1850 correct_range(&ea);
1851 ex_print(&ea);
1852 }
1853 }
1854 else if (ea.addr_count != 0)
1855 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001856 if (ea.line2 > curbuf->b_ml.ml_line_count)
1857 {
1858 /* With '-' in 'cpoptions' a line number past the file is an
1859 * error, otherwise put it at the end of the file. */
1860 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1861 ea.line2 = -1;
1862 else
1863 ea.line2 = curbuf->b_ml.ml_line_count;
1864 }
1865
1866 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001867 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 else
1869 {
1870 if (ea.line2 == 0)
1871 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 else
1873 curwin->w_cursor.lnum = ea.line2;
1874 beginline(BL_SOL | BL_FIX);
1875 }
1876 }
1877 goto doend;
1878 }
1879
Bram Moolenaard5005162014-08-22 23:05:54 +02001880 /* If this looks like an undefined user command and there are CmdUndefined
1881 * autocommands defined, trigger the matching autocommands. */
1882 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1883 && ASCII_ISUPPER(*ea.cmd)
1884 && has_cmdundefined())
1885 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001886 int ret;
1887
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001888 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001889 while (ASCII_ISALNUM(*p))
1890 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001891 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001892 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1893 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001894 /* If the autocommands did something and didn't cause an error, try
1895 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001896 p = (ret
1897#ifdef FEAT_EVAL
1898 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001899#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001900 ) ? find_command(&ea, NULL) : ea.cmd;
1901 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001902
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (p == NULL)
1904 {
1905 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001906 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 goto doend;
1908 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001909 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001910 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1911 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
1913 errormsg = uc_fun_cmd();
1914 goto doend;
1915 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (ea.cmdidx == CMD_SIZE)
1918 {
1919 if (!ea.skip)
1920 {
1921 STRCPY(IObuff, _("E492: Not an editor command"));
1922 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001923 {
1924 /* If the modifier was parsed OK the error must be in the
1925 * following command */
1926 if (after_modifier != NULL)
1927 append_command(after_modifier);
1928 else
1929 append_command(*cmdlinep);
1930 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001931 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001932 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 }
1934 goto doend;
1935 }
1936
Bram Moolenaar958636c2014-10-21 20:01:58 +02001937 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1938 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001939#ifdef HAVE_EX_SCRIPT_NI
1940 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1941#endif
1942 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
1944#ifndef FEAT_EVAL
1945 /*
1946 * When the expression evaluation is disabled, recognize the ":if" and
1947 * ":endif" commands and ignore everything in between it.
1948 */
1949 if (ea.cmdidx == CMD_if)
1950 ++if_level;
1951 if (if_level)
1952 {
1953 if (ea.cmdidx == CMD_endif)
1954 --if_level;
1955 goto doend;
1956 }
1957
1958#endif
1959
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001960 /* forced commands */
1961 if (*p == '!' && ea.cmdidx != CMD_substitute
1962 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 ++p;
1965 ea.forceit = TRUE;
1966 }
1967 else
1968 ea.forceit = FALSE;
1969
1970/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001971 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001973 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001974 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
1976 if (!ea.skip)
1977 {
1978#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001979 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001981 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 goto doend;
1984 }
1985#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001986 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001987 {
1988 errormsg = _("E981: Command not allowed in rvim");
1989 goto doend;
1990 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001991 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001994 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 goto doend;
1996 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001997
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001998 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001999 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002001 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002002 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 goto doend;
2004 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002005
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002006 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002007 * Do allow ":checktime" (it is postponed).
2008 * Do allow ":edit" (check for an argument later).
2009 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002010 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002011 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002012 && ea.cmdidx != CMD_edit
2013 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002014 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002015 && curbuf_locked())
2016 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002018 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
2020 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002021 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 goto doend;
2023 }
2024 }
2025
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002026 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 goto doend;
2030 }
2031
2032 /*
2033 * Don't complain about the range if it is not used
2034 * (could happen if line_count is accidentally set to 0).
2035 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002036 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
2038 /*
2039 * If the range is backwards, ask for confirmation and, if given, swap
2040 * ea.line1 & ea.line2 so it's forwards again.
2041 * When global command is busy, don't ask, will fail below.
2042 */
2043 if (!global_busy && ea.line1 > ea.line2)
2044 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002045 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002047 if (sourcing || exmode_active)
2048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002049 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002050 goto doend;
2051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 if (ask_yesno((char_u *)
2053 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002054 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 lnum = ea.line1;
2057 ea.line1 = ea.line2;
2058 ea.line2 = lnum;
2059 }
2060 if ((errormsg = invalid_range(&ea)) != NULL)
2061 goto doend;
2062 }
2063
Bram Moolenaarb7316892019-05-01 18:08:42 +02002064 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2065 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 ea.line2 = 1;
2067
2068 correct_range(&ea);
2069
2070#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002071 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002072 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
2074 /* Put the first line at the start of a closed fold, put the last line
2075 * at the end of a closed fold. */
2076 (void)hasFolding(ea.line1, &ea.line1, NULL);
2077 (void)hasFolding(ea.line2, NULL, &ea.line2);
2078 }
2079#endif
2080
2081#ifdef FEAT_QUICKFIX
2082 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002083 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 * option here, so things like % get expanded.
2085 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002086 p = replace_makeprg(&ea, p, cmdlinep);
2087 if (p == NULL)
2088 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#endif
2090
2091 /*
2092 * Skip to start of argument.
2093 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2094 */
2095 if (ea.cmdidx == CMD_bang)
2096 ea.arg = p;
2097 else
2098 ea.arg = skipwhite(p);
2099
Bram Moolenaar379fb762018-08-30 15:58:28 +02002100 // ":file" cannot be run with an argument when "curbuf_lock" is set
2101 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2102 goto doend;
2103
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 /*
2105 * Check for "++opt=val" argument.
2106 * Must be first, allow ":w ++enc=utf8 !cmd"
2107 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002108 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2110 if (getargopt(&ea) == FAIL && !ni)
2111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002112 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 goto doend;
2114 }
2115
2116 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2117 {
2118 if (*ea.arg == '>') /* append */
2119 {
2120 if (*++ea.arg != '>') /* typed wrong */
2121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002122 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 goto doend;
2124 }
2125 ea.arg = skipwhite(ea.arg + 1);
2126 ea.append = TRUE;
2127 }
2128 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2129 {
2130 ++ea.arg;
2131 ea.usefilter = TRUE;
2132 }
2133 }
2134
2135 if (ea.cmdidx == CMD_read)
2136 {
2137 if (ea.forceit)
2138 {
2139 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2140 ea.forceit = FALSE;
2141 }
2142 else if (*ea.arg == '!') /* :r !filter */
2143 {
2144 ++ea.arg;
2145 ea.usefilter = TRUE;
2146 }
2147 }
2148
2149 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2150 {
2151 ea.amount = 1;
2152 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2153 {
2154 ++ea.arg;
2155 ++ea.amount;
2156 }
2157 ea.arg = skipwhite(ea.arg);
2158 }
2159
2160 /*
2161 * Check for "+command" argument, before checking for next command.
2162 * Don't do this for ":read !cmd" and ":write !cmd".
2163 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002164 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2166
2167 /*
2168 * Check for '|' to separate commands and '"' to start comments.
2169 * Don't do this for ":read !cmd" and ":write !cmd".
2170 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002171 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 separate_nextcmd(&ea);
2173
2174 /*
2175 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002176 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2177 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002179 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002180 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002181 || ea.cmdidx == CMD_global
2182 || ea.cmdidx == CMD_vglobal
2183 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
2185 for (p = ea.arg; *p; ++p)
2186 {
2187 /* Remove one backslash before a newline, so that it's possible to
2188 * pass a newline to the shell and also a newline that is preceded
2189 * with a backslash. This makes it impossible to end a shell
2190 * command in a backslash, but that doesn't appear useful.
2191 * Halving the number of backslashes is incompatible with previous
2192 * versions. */
2193 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002194 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 else if (*p == '\n')
2196 {
2197 ea.nextcmd = p + 1;
2198 *p = NUL;
2199 break;
2200 }
2201 }
2202 }
2203
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002204 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002206 buf_T *buf;
2207
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002209 switch (ea.addr_type)
2210 {
2211 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002212 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 ea.line2 = curbuf->b_ml.ml_line_count;
2214 break;
2215 case ADDR_LOADED_BUFFERS:
2216 buf = firstbuf;
2217 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2218 buf = buf->b_next;
2219 ea.line1 = buf->b_fnum;
2220 buf = lastbuf;
2221 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2222 buf = buf->b_prev;
2223 ea.line2 = buf->b_fnum;
2224 break;
2225 case ADDR_BUFFERS:
2226 ea.line1 = firstbuf->b_fnum;
2227 ea.line2 = lastbuf->b_fnum;
2228 break;
2229 case ADDR_WINDOWS:
2230 ea.line2 = LAST_WIN_NR;
2231 break;
2232 case ADDR_TABS:
2233 ea.line2 = LAST_TAB_NR;
2234 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002235 case ADDR_TABS_RELATIVE:
2236 ea.line2 = 1;
2237 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002238 case ADDR_ARGUMENTS:
2239 if (ARGCOUNT == 0)
2240 ea.line1 = ea.line2 = 0;
2241 else
2242 ea.line2 = ARGCOUNT;
2243 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002244 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002245#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002246 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002247 if (ea.line2 == 0)
2248 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002249#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002250 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002251 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002252 case ADDR_UNSIGNED:
2253 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002254 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002255 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258
2259 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002260 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002262 /* Do not allow register = for user commands */
2263 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002266#ifndef FEAT_CLIPBOARD
2267 /* check these explicitly for a more specific error message */
2268 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002270 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002271 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002274 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2275 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002276 {
2277 ea.regname = *ea.arg++;
2278#ifdef FEAT_EVAL
2279 /* for '=' register: accept the rest of the line as an expression */
2280 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2281 {
2282 set_expr_line(vim_strsave(ea.arg));
2283 ea.arg += STRLEN(ea.arg);
2284 }
2285#endif
2286 ea.arg = skipwhite(ea.arg);
2287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 }
2289
2290 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002291 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 * count, it's a buffer name.
2293 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002294 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2295 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002296 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
2298 n = getdigits(&ea.arg);
2299 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002300 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002302 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 goto doend;
2304 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002305 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 ea.line2 = n;
2308 if (ea.addr_count == 0)
2309 ea.addr_count = 1;
2310 }
2311 else
2312 {
2313 ea.line1 = ea.line2;
2314 ea.line2 += n - 1;
2315 ++ea.addr_count;
2316 /*
2317 * Be vi compatible: no error message for out of range.
2318 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002319 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 ea.line2 = curbuf->b_ml.ml_line_count;
2321 }
2322 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002323
2324 /*
2325 * Check for flags: 'l', 'p' and '#'.
2326 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002327 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002328 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002329 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2330 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002332 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002333 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 goto doend;
2335 }
2336
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002337 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002339 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 goto doend;
2341 }
2342
2343#ifdef FEAT_EVAL
2344 /*
2345 * Skip the command when it's not going to be executed.
2346 * The commands like :if, :endif, etc. always need to be executed.
2347 * Also make an exception for commands that handle a trailing command
2348 * themselves.
2349 */
2350 if (ea.skip)
2351 {
2352 switch (ea.cmdidx)
2353 {
2354 /* commands that need evaluation */
2355 case CMD_while:
2356 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002357 case CMD_for:
2358 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 case CMD_if:
2360 case CMD_elseif:
2361 case CMD_else:
2362 case CMD_endif:
2363 case CMD_try:
2364 case CMD_catch:
2365 case CMD_finally:
2366 case CMD_endtry:
2367 case CMD_function:
2368 break;
2369
2370 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002371 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * the list at ":help :bar". */
2373 case CMD_aboveleft:
2374 case CMD_and:
2375 case CMD_belowright:
2376 case CMD_botright:
2377 case CMD_browse:
2378 case CMD_call:
2379 case CMD_confirm:
2380 case CMD_delfunction:
2381 case CMD_djump:
2382 case CMD_dlist:
2383 case CMD_dsearch:
2384 case CMD_dsplit:
2385 case CMD_echo:
2386 case CMD_echoerr:
2387 case CMD_echomsg:
2388 case CMD_echon:
2389 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002390 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_help:
2392 case CMD_hide:
2393 case CMD_ijump:
2394 case CMD_ilist:
2395 case CMD_isearch:
2396 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002397 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 case CMD_keepjumps:
2399 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002400 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 case CMD_leftabove:
2402 case CMD_let:
2403 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002404 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002406 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002407 case CMD_noautocmd:
2408 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 case CMD_perl:
2410 case CMD_psearch:
2411 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002412 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002413 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_return:
2415 case CMD_rightbelow:
2416 case CMD_ruby:
2417 case CMD_silent:
2418 case CMD_smagic:
2419 case CMD_snomagic:
2420 case CMD_substitute:
2421 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002422 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_tcl:
2424 case CMD_throw:
2425 case CMD_tilde:
2426 case CMD_topleft:
2427 case CMD_unlet:
2428 case CMD_verbose:
2429 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002430 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 break;
2432
2433 default: goto doend;
2434 }
2435 }
2436#endif
2437
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002438 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 {
2440 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2441 goto doend;
2442 }
2443
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 /*
2445 * Accept buffer name. Cannot be used at the same time with a buffer
2446 * number. Don't do this for a user command.
2447 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002448 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002449 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
2451 /*
2452 * :bdelete, :bwipeout and :bunload take several arguments, separated
2453 * by spaces: find next space (skipping over escaped characters).
2454 * The others take one argument: ignore trailing spaces.
2455 */
2456 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2457 || ea.cmdidx == CMD_bunload)
2458 p = skiptowhite_esc(ea.arg);
2459 else
2460 {
2461 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002462 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 --p;
2464 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002465 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002466 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (ea.line2 < 0) /* failed */
2468 goto doend;
2469 ea.addr_count = 1;
2470 ea.arg = skipwhite(p);
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
Bram Moolenaar2be57332018-02-13 18:05:18 +01002473 /* The :try command saves the emsg_silent flag, reset it here when
2474 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002475 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002476 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002477 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002478 if (emsg_silent < 0)
2479 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002480 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002481 }
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002484 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaar958636c2014-10-21 20:01:58 +02002487 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 {
2489 /*
2490 * Execute a user-defined command.
2491 */
2492 do_ucmd(&ea);
2493 }
2494 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002497 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 */
2499 ea.errmsg = NULL;
2500 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2501 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002502 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
2504
2505#ifdef FEAT_EVAL
2506 /*
2507 * If the command just executed called do_cmdline(), any throw or ":return"
2508 * or ":finish" encountered there must also check the cstack of the still
2509 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2510 * exception, or reanimate a returned function or finished script file and
2511 * return or finish it again.
2512 */
2513 if (need_rethrow)
2514 do_throw(cstack);
2515 else if (check_cstack)
2516 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002517 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002519 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 && current_func_returned())
2521 do_return(&ea, TRUE, FALSE, NULL);
2522 }
2523 need_rethrow = check_cstack = FALSE;
2524#endif
2525
2526doend:
2527 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002530 curwin->w_cursor.col = 0;
2531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532
2533 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2534 {
2535 if (sourcing)
2536 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002537 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002540 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002542 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544 emsg(errormsg);
2545 }
2546#ifdef FEAT_EVAL
2547 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002548 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2549 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550#endif
2551
Bram Moolenaareffed932018-08-14 13:38:17 +02002552 if (ea.verbose_save >= 0)
2553 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002554
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002555 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002557 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
Bram Moolenaareffed932018-08-14 13:38:17 +02002559 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 {
2561 /* messages could be enabled for a serious error, need to check if the
2562 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002563 if (!did_emsg || msg_silent > ea.save_msg_silent)
2564 msg_silent = ea.save_msg_silent;
2565 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (emsg_silent < 0)
2567 emsg_silent = 0;
2568 /* Restore msg_scroll, it's set by file I/O commands, even when no
2569 * message is actually displayed. */
2570 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002571
2572 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2573 * somewhere in the line. Put it back in the first column. */
2574 if (redirecting())
2575 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002578#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002579 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002580 --sandbox;
2581#endif
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2584 ea.nextcmd = NULL;
2585
2586#ifdef FEAT_EVAL
2587 --ex_nesting_level;
2588#endif
2589
2590 return ea.nextcmd;
2591}
2592#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002593 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594#endif
2595
2596/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002597 * Parse and skip over command modifiers:
2598 * - update eap->cmd
2599 * - store flags in "cmdmod".
2600 * - Set ex_pressedreturn for an empty command line.
2601 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002602 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 * - set p_verbose for ":verbose"
2604 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002605 * When "skip_only" is TRUE the global variables are not changed, except for
2606 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002607 * Return FAIL when the command is not to be executed.
2608 * May set "errormsg" to an error message.
2609 */
2610 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002611parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002612{
2613 char_u *p;
2614
2615 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2616 eap->verbose_save = -1;
2617 eap->save_msg_silent = -1;
2618
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002619 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002620 for (;;)
2621 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2623 ++eap->cmd;
2624
2625 /* in ex mode, an empty line works like :+ */
2626 if (*eap->cmd == NUL && exmode_active
2627 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2628 || getline_equal(eap->getline, eap->cookie, getexline))
2629 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2630 {
2631 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002632 if (!skip_only)
2633 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002634 }
2635
2636 /* ignore comment and empty lines */
2637 if (*eap->cmd == '"')
2638 return FAIL;
2639 if (*eap->cmd == NUL)
2640 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002641 if (!skip_only)
2642 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 return FAIL;
2644 }
2645
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 p = skip_range(eap->cmd, NULL);
2647 switch (*p)
2648 {
2649 /* When adding an entry, also modify cmd_exists(). */
2650 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2651 break;
2652 cmdmod.split |= WSP_ABOVE;
2653 continue;
2654
2655 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2656 {
2657 cmdmod.split |= WSP_BELOW;
2658 continue;
2659 }
2660 if (checkforcmd(&eap->cmd, "browse", 3))
2661 {
2662#ifdef FEAT_BROWSE_CMD
2663 cmdmod.browse = TRUE;
2664#endif
2665 continue;
2666 }
2667 if (!checkforcmd(&eap->cmd, "botright", 2))
2668 break;
2669 cmdmod.split |= WSP_BOT;
2670 continue;
2671
2672 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2673 break;
2674#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2675 cmdmod.confirm = TRUE;
2676#endif
2677 continue;
2678
2679 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2680 {
2681 cmdmod.keepmarks = TRUE;
2682 continue;
2683 }
2684 if (checkforcmd(&eap->cmd, "keepalt", 5))
2685 {
2686 cmdmod.keepalt = TRUE;
2687 continue;
2688 }
2689 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2690 {
2691 cmdmod.keeppatterns = TRUE;
2692 continue;
2693 }
2694 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2695 break;
2696 cmdmod.keepjumps = TRUE;
2697 continue;
2698
2699 case 'f': /* only accept ":filter {pat} cmd" */
2700 {
2701 char_u *reg_pat;
2702
2703 if (!checkforcmd(&p, "filter", 4)
2704 || *p == NUL || ends_excmd(*p))
2705 break;
2706 if (*p == '!')
2707 {
2708 cmdmod.filter_force = TRUE;
2709 p = skipwhite(p + 1);
2710 if (*p == NUL || ends_excmd(*p))
2711 break;
2712 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002713 if (skip_only)
2714 p = skip_vimgrep_pat(p, NULL, NULL);
2715 else
2716 // NOTE: This puts a NUL after the pattern.
2717 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002718 if (p == NULL || *p == NUL)
2719 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002720 if (!skip_only)
2721 {
2722 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002723 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002724 if (cmdmod.filter_regmatch.regprog == NULL)
2725 break;
2726 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002727 eap->cmd = p;
2728 continue;
2729 }
2730
2731 /* ":hide" and ":hide | cmd" are not modifiers */
2732 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2733 || *p == NUL || ends_excmd(*p))
2734 break;
2735 eap->cmd = p;
2736 cmdmod.hide = TRUE;
2737 continue;
2738
2739 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2740 {
2741 cmdmod.lockmarks = TRUE;
2742 continue;
2743 }
2744
2745 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2746 break;
2747 cmdmod.split |= WSP_ABOVE;
2748 continue;
2749
2750 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2751 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002752 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002753 {
2754 /* Set 'eventignore' to "all". Restore the
2755 * existing option value later. */
2756 cmdmod.save_ei = vim_strsave(p_ei);
2757 set_string_option_direct((char_u *)"ei", -1,
2758 (char_u *)"all", OPT_FREE, SID_NONE);
2759 }
2760 continue;
2761 }
2762 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2763 break;
2764 cmdmod.noswapfile = TRUE;
2765 continue;
2766
2767 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2768 break;
2769 cmdmod.split |= WSP_BELOW;
2770 continue;
2771
2772 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2773 {
2774#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002775 if (!skip_only)
2776 {
2777 if (!eap->did_sandbox)
2778 ++sandbox;
2779 eap->did_sandbox = TRUE;
2780 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002781#endif
2782 continue;
2783 }
2784 if (!checkforcmd(&eap->cmd, "silent", 3))
2785 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002786 if (!skip_only)
2787 {
2788 if (eap->save_msg_silent == -1)
2789 eap->save_msg_silent = msg_silent;
2790 ++msg_silent;
2791 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2793 {
2794 /* ":silent!", but not "silent !cmd" */
2795 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002796 if (!skip_only)
2797 {
2798 ++emsg_silent;
2799 ++eap->did_esilent;
2800 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 }
2802 continue;
2803
2804 case 't': if (checkforcmd(&p, "tab", 3))
2805 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002806 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002808 long tabnr = get_address(eap, &eap->cmd,
2809 ADDR_TABS, eap->skip,
2810 skip_only, FALSE, 1);
2811 if (tabnr == MAXLNUM)
2812 cmdmod.tab = tabpage_index(curtab) + 1;
2813 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002815 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002817 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002818 return FAIL;
2819 }
2820 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 }
2823 eap->cmd = p;
2824 continue;
2825 }
2826 if (!checkforcmd(&eap->cmd, "topleft", 2))
2827 break;
2828 cmdmod.split |= WSP_TOP;
2829 continue;
2830
2831 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2832 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002833 if (!skip_only)
2834 {
2835 if (eap->save_msg_silent == -1)
2836 eap->save_msg_silent = msg_silent;
2837 msg_silent = 0;
2838 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 continue;
2840
2841 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2842 {
2843 cmdmod.split |= WSP_VERT;
2844 continue;
2845 }
2846 if (!checkforcmd(&p, "verbose", 4))
2847 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002848 if (!skip_only)
2849 {
2850 if (eap->verbose_save < 0)
2851 eap->verbose_save = p_verbose;
2852 if (vim_isdigit(*eap->cmd))
2853 p_verbose = atoi((char *)eap->cmd);
2854 else
2855 p_verbose = 1;
2856 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 eap->cmd = p;
2858 continue;
2859 }
2860 break;
2861 }
2862
2863 return OK;
2864}
2865
2866/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002867 * Free contents of "cmdmod".
2868 */
2869 static void
2870free_cmdmod(void)
2871{
2872 if (cmdmod.save_ei != NULL)
2873 {
2874 /* Restore 'eventignore' to the value before ":noautocmd". */
2875 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2876 OPT_FREE, SID_NONE);
2877 free_string_option(cmdmod.save_ei);
2878 }
2879
2880 if (cmdmod.filter_regmatch.regprog != NULL)
2881 vim_regfree(cmdmod.filter_regmatch.regprog);
2882}
2883
2884/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002885 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002886 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002887 * Return FAIL and set "errormsg" or return OK.
2888 */
2889 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002890parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002891{
2892 int address_count = 1;
2893 linenr_T lnum;
2894
2895 // Repeat for all ',' or ';' separated addresses.
2896 for (;;)
2897 {
2898 eap->line1 = eap->line2;
2899 switch (eap->addr_type)
2900 {
2901 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002902 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002903 // default is current line number
2904 eap->line2 = curwin->w_cursor.lnum;
2905 break;
2906 case ADDR_WINDOWS:
2907 eap->line2 = CURRENT_WIN_NR;
2908 break;
2909 case ADDR_ARGUMENTS:
2910 eap->line2 = curwin->w_arg_idx + 1;
2911 if (eap->line2 > ARGCOUNT)
2912 eap->line2 = ARGCOUNT;
2913 break;
2914 case ADDR_LOADED_BUFFERS:
2915 case ADDR_BUFFERS:
2916 eap->line2 = curbuf->b_fnum;
2917 break;
2918 case ADDR_TABS:
2919 eap->line2 = CURRENT_TAB_NR;
2920 break;
2921 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002922 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002923 eap->line2 = 1;
2924 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002925 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002926#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002927 eap->line2 = qf_get_cur_idx(eap);
2928#endif
2929 break;
2930 case ADDR_QUICKFIX_VALID:
2931#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002932 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002933#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002934 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002935 case ADDR_NONE:
2936 // Will give an error later if a range is found.
2937 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002938 }
2939 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002940 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002941 eap->addr_count == 0, address_count++);
2942 if (eap->cmd == NULL) // error detected
2943 return FAIL;
2944 if (lnum == MAXLNUM)
2945 {
2946 if (*eap->cmd == '%') // '%' - all lines
2947 {
2948 ++eap->cmd;
2949 switch (eap->addr_type)
2950 {
2951 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002952 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953 eap->line1 = 1;
2954 eap->line2 = curbuf->b_ml.ml_line_count;
2955 break;
2956 case ADDR_LOADED_BUFFERS:
2957 {
2958 buf_T *buf = firstbuf;
2959
2960 while (buf->b_next != NULL
2961 && buf->b_ml.ml_mfp == NULL)
2962 buf = buf->b_next;
2963 eap->line1 = buf->b_fnum;
2964 buf = lastbuf;
2965 while (buf->b_prev != NULL
2966 && buf->b_ml.ml_mfp == NULL)
2967 buf = buf->b_prev;
2968 eap->line2 = buf->b_fnum;
2969 break;
2970 }
2971 case ADDR_BUFFERS:
2972 eap->line1 = firstbuf->b_fnum;
2973 eap->line2 = lastbuf->b_fnum;
2974 break;
2975 case ADDR_WINDOWS:
2976 case ADDR_TABS:
2977 if (IS_USER_CMDIDX(eap->cmdidx))
2978 {
2979 eap->line1 = 1;
2980 eap->line2 = eap->addr_type == ADDR_WINDOWS
2981 ? LAST_WIN_NR : LAST_TAB_NR;
2982 }
2983 else
2984 {
2985 // there is no Vim command which uses '%' and
2986 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002987 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002988 return FAIL;
2989 }
2990 break;
2991 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002992 case ADDR_UNSIGNED:
2993 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002994 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002995 return FAIL;
2996 case ADDR_ARGUMENTS:
2997 if (ARGCOUNT == 0)
2998 eap->line1 = eap->line2 = 0;
2999 else
3000 {
3001 eap->line1 = 1;
3002 eap->line2 = ARGCOUNT;
3003 }
3004 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003005 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003006#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003008 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 if (eap->line2 == 0)
3010 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003011#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003012 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003013 case ADDR_NONE:
3014 // Will give an error later if a range is found.
3015 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003016 }
3017 ++eap->addr_count;
3018 }
3019 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3020 {
3021 pos_T *fp;
3022
3023 // '*' - visual area
3024 if (eap->addr_type != ADDR_LINES)
3025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003026 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003027 return FAIL;
3028 }
3029
3030 ++eap->cmd;
3031 if (!eap->skip)
3032 {
3033 fp = getmark('<', FALSE);
3034 if (check_mark(fp) == FAIL)
3035 return FAIL;
3036 eap->line1 = fp->lnum;
3037 fp = getmark('>', FALSE);
3038 if (check_mark(fp) == FAIL)
3039 return FAIL;
3040 eap->line2 = fp->lnum;
3041 ++eap->addr_count;
3042 }
3043 }
3044 }
3045 else
3046 eap->line2 = lnum;
3047 eap->addr_count++;
3048
3049 if (*eap->cmd == ';')
3050 {
3051 if (!eap->skip)
3052 {
3053 curwin->w_cursor.lnum = eap->line2;
3054 // don't leave the cursor on an illegal line or column
3055 check_cursor();
3056 }
3057 }
3058 else if (*eap->cmd != ',')
3059 break;
3060 ++eap->cmd;
3061 }
3062
3063 // One address given: set start and end lines.
3064 if (eap->addr_count == 1)
3065 {
3066 eap->line1 = eap->line2;
3067 // ... but only implicit: really no address given
3068 if (lnum == MAXLNUM)
3069 eap->addr_count = 0;
3070 }
3071 return OK;
3072}
3073
3074/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003075 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 * If there is a match advance "pp" to the argument and return TRUE.
3077 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003078 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003079checkforcmd(
3080 char_u **pp, /* start of command */
3081 char *cmd, /* name of command */
3082 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 int i;
3085
3086 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003087 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 break;
3089 if (i >= len && !isalpha((*pp)[i]))
3090 {
3091 *pp = skipwhite(*pp + i);
3092 return TRUE;
3093 }
3094 return FALSE;
3095}
3096
3097/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003098 * Append "cmd" to the error message in IObuff.
3099 * Takes care of limiting the length and handling 0xa0, which would be
3100 * invisible otherwise.
3101 */
3102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003103append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003104{
3105 char_u *s = cmd;
3106 char_u *d;
3107
3108 STRCAT(IObuff, ": ");
3109 d = IObuff + STRLEN(IObuff);
3110 while (*s != NUL && d - IObuff < IOSIZE - 7)
3111 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003112 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003113 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003114 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003115 STRCPY(d, "<a0>");
3116 d += 4;
3117 }
3118 else
3119 MB_COPY_CHAR(s, d);
3120 }
3121 *d = NUL;
3122}
3123
3124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003127 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003128 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 * Returns NULL for an ambiguous user command.
3130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003132find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
3134 int len;
3135 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003136 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
3138 /*
3139 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003140 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 * - the 'k' command can directly be followed by any character.
3142 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003143 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003145 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 */
3147 p = eap->cmd;
3148 if (*p == 'k')
3149 {
3150 eap->cmdidx = CMD_k;
3151 ++p;
3152 }
3153 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003154 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3155 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 || p[1] == 'g'
3157 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3158 || p[1] == 'I'
3159 || (p[1] == 'r' && p[2] != 'e')))
3160 {
3161 eap->cmdidx = CMD_substitute;
3162 ++p;
3163 }
3164 else
3165 {
3166 while (ASCII_ISALPHA(*p))
3167 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003168 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003169 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003170 while (ASCII_ISALNUM(*p))
3171 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003172
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 /* check for non-alpha command */
3174 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3175 ++p;
3176 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003177 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3178 {
3179 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3180 * :delete with the 'l' flag. Same for 'p'. */
3181 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003182 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003183 break;
3184 if (i == len - 1)
3185 {
3186 --len;
3187 if (p[-1] == 'l')
3188 eap->flags |= EXFLAG_LIST;
3189 else
3190 eap->flags |= EXFLAG_PRINT;
3191 }
3192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003194 if (ASCII_ISLOWER(eap->cmd[0]))
3195 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003196 int c1 = eap->cmd[0];
3197 int c2 = eap->cmd[1];
3198
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003199 if (command_count != (int)CMD_SIZE)
3200 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003201 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003202 getout(1);
3203 }
3204
3205 /* Use a precomputed index for fast look-up in cmdnames[]
3206 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003207 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3208 if (ASCII_ISLOWER(c2))
3209 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003212 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3215 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3216 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3217 (size_t)len) == 0)
3218 {
3219#ifdef FEAT_EVAL
3220 if (full != NULL
3221 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3222 *full = TRUE;
3223#endif
3224 break;
3225 }
3226
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003227 // Look for a user defined command as a last resort. Let ":Print" be
3228 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003229 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3230 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003232 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 while (ASCII_ISALNUM(*p))
3234 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 eap->cmdidx = CMD_SIZE;
3239 }
3240
3241 return p;
3242}
3243
3244#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003245static struct cmdmod
3246{
3247 char *name;
3248 int minlen;
3249 int has_count; /* :123verbose :3tab */
3250} cmdmods[] = {
3251 {"aboveleft", 3, FALSE},
3252 {"belowright", 3, FALSE},
3253 {"botright", 2, FALSE},
3254 {"browse", 3, FALSE},
3255 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003256 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003257 {"hide", 3, FALSE},
3258 {"keepalt", 5, FALSE},
3259 {"keepjumps", 5, FALSE},
3260 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003261 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003262 {"leftabove", 5, FALSE},
3263 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003264 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003265 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003266 {"rightbelow", 6, FALSE},
3267 {"sandbox", 3, FALSE},
3268 {"silent", 3, FALSE},
3269 {"tab", 3, TRUE},
3270 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003271 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003272 {"verbose", 4, TRUE},
3273 {"vertical", 4, FALSE},
3274};
3275
3276/*
3277 * Return length of a command modifier (including optional count).
3278 * Return zero when it's not a modifier.
3279 */
3280 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003281modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003282{
3283 int i, j;
3284 char_u *p = cmd;
3285
3286 if (VIM_ISDIGIT(*cmd))
3287 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003288 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003289 {
3290 for (j = 0; p[j] != NUL; ++j)
3291 if (p[j] != cmdmods[i].name[j])
3292 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003293 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003294 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003295 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003296 }
3297 return 0;
3298}
3299
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300/*
3301 * Return > 0 if an Ex command "name" exists.
3302 * Return 2 if there is an exact match.
3303 * Return 3 if there is an ambiguous match.
3304 */
3305 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003306cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 exarg_T ea;
3309 int full = FALSE;
3310 int i;
3311 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003312 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
3314 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003315 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
3317 for (j = 0; name[j] != NUL; ++j)
3318 if (name[j] != cmdmods[i].name[j])
3319 break;
3320 if (name[j] == NUL && j >= cmdmods[i].minlen)
3321 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3322 }
3323
Bram Moolenaara9587612006-05-04 21:47:50 +00003324 /* Check built-in commands and user defined commands.
3325 * For ":2match" and ":3match" we need to skip the number. */
3326 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003328 p = find_command(&ea, &full);
3329 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003331 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3332 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003333 if (*skipwhite(p) != NUL)
3334 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3336}
3337#endif
3338
3339/*
3340 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3341 * we don't need/want deleted. Maybe this could be done better if we didn't
3342 * repeat all this stuff. The only problem is that they may not stay
3343 * perfectly compatible with each other, but then the command line syntax
3344 * probably won't change that much -- webb.
3345 */
3346 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003347set_one_cmd_context(
3348 expand_T *xp,
3349 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 char_u *p;
3352 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003353 int len = 0;
3354 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003356 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 int delim;
3358#endif
3359 int forceit = FALSE;
3360 int usefilter = FALSE; /* filter instead of file name */
3361
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003362 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 xp->xp_pattern = buff;
3364 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003365 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003368 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 */
3370 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3371 ;
3372 xp->xp_pattern = cmd;
3373
3374 if (*cmd == NUL)
3375 return NULL;
3376 if (*cmd == '"') /* ignore comment lines */
3377 {
3378 xp->xp_context = EXPAND_NOTHING;
3379 return NULL;
3380 }
3381
3382/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003383 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
3385 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 xp->xp_pattern = cmd;
3387 if (*cmd == NUL)
3388 return NULL;
3389 if (*cmd == '"')
3390 {
3391 xp->xp_context = EXPAND_NOTHING;
3392 return NULL;
3393 }
3394
3395 if (*cmd == '|' || *cmd == '\n')
3396 return cmd + 1; /* There's another command */
3397
3398 /*
3399 * Isolate the command and search for it in the command table.
3400 * Exceptions:
3401 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003402 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3404 */
3405 if (*cmd == 'k' && cmd[1] != 'e')
3406 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003407 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 p = cmd + 1;
3409 }
3410 else
3411 {
3412 p = cmd;
3413 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3414 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003415 /* a user command may contain digits */
3416 if (ASCII_ISUPPER(cmd[0]))
3417 while (ASCII_ISALNUM(*p) || *p == '*')
3418 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003419 /* for python 3.x: ":py3*" commands completion */
3420 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003421 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003422 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003423 while (ASCII_ISALPHA(*p) || *p == '*')
3424 ++p;
3425 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003426 /* check for non-alpha command */
3427 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3428 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
3433 xp->xp_context = EXPAND_UNSUCCESSFUL;
3434 return NULL;
3435 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003436 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003437 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3438 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3439 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 break;
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003443 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446
3447 /*
3448 * If the cursor is touching the command, and it ends in an alpha-numeric
3449 * character, complete the command name.
3450 */
3451 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3452 return NULL;
3453
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003454 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 {
3456 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3457 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003458 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 p = cmd + 1;
3460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3462 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003463 ea.cmd = cmd;
3464 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003465#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003467#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003469#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003470 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003471 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003472 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
3477 /* Not still touching the command and it was an illegal one */
3478 xp->xp_context = EXPAND_UNSUCCESSFUL;
3479 return NULL;
3480 }
3481
3482 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3483
3484 if (*p == '!') /* forced commands */
3485 {
3486 forceit = TRUE;
3487 ++p;
3488 }
3489
3490/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003491 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003493 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003494 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 arg = skipwhite(p);
3497
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003498 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 {
3500 if (*arg == '>') /* append */
3501 {
3502 if (*++arg == '>')
3503 ++arg;
3504 arg = skipwhite(arg);
3505 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003506 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 {
3508 ++arg;
3509 usefilter = TRUE;
3510 }
3511 }
3512
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003513 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 {
3515 usefilter = forceit; /* :r! filter if forced */
3516 if (*arg == '!') /* :r !filter */
3517 {
3518 ++arg;
3519 usefilter = TRUE;
3520 }
3521 }
3522
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003523 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 while (*arg == *cmd) /* allow any number of '>' or '<' */
3526 ++arg;
3527 arg = skipwhite(arg);
3528 }
3529
3530 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003531 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 {
3533 /* Check if we're in the +command */
3534 p = arg + 1;
3535 arg = skip_cmd_arg(arg, FALSE);
3536
3537 /* Still touching the command after '+'? */
3538 if (*arg == NUL)
3539 return p;
3540
3541 /* Skip space(s) after +command to get to the real argument */
3542 arg = skipwhite(arg);
3543 }
3544
3545 /*
3546 * Check for '|' to separate commands and '"' to start comments.
3547 * Don't do this for ":read !cmd" and ":write !cmd".
3548 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003549 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 p = arg;
3552 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003553 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 p += 2;
3555 while (*p)
3556 {
3557 if (*p == Ctrl_V)
3558 {
3559 if (p[1] != NUL)
3560 ++p;
3561 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003562 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 || *p == '|' || *p == '\n')
3564 {
3565 if (*(p - 1) != '\\')
3566 {
3567 if (*p == '|' || *p == '\n')
3568 return p + 1;
3569 return NULL; /* It's a comment */
3570 }
3571 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003572 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574 }
3575
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003576 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3577 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3578 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 return NULL;
3580
3581 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003582 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003584 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003585 while (*p && p < buff + len)
3586 {
3587 if (*p == ' ' || *p == TAB)
3588 {
3589 /* argument starts after a space */
3590 xp->xp_pattern = ++p;
3591 }
3592 else
3593 {
3594 if (*p == '\\' && *(p + 1) != NUL)
3595 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003596 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003597 }
3598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003600 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003602 int c;
3603 int in_quote = FALSE;
3604 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
3606 /*
3607 * Allow spaces within back-quotes to count as part of the argument
3608 * being expanded.
3609 */
3610 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003611 p = xp->xp_pattern;
3612 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003614 if (has_mbyte)
3615 c = mb_ptr2char(p);
3616 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003617 c = *p;
3618 if (c == '\\' && p[1] != NUL)
3619 ++p;
3620 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 {
3622 if (!in_quote)
3623 {
3624 xp->xp_pattern = p;
3625 bow = p + 1;
3626 }
3627 in_quote = !in_quote;
3628 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003629 /* An argument can contain just about everything, except
3630 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003631 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003632#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003633 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003634#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003635 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003636 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003637 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003638 while (*p != NUL)
3639 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003640 if (has_mbyte)
3641 c = mb_ptr2char(p);
3642 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003643 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003644 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003645 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003646 if (has_mbyte)
3647 len = (*mb_ptr2len)(p);
3648 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003649 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003650 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003651 }
3652 if (in_quote)
3653 bow = p;
3654 else
3655 xp->xp_pattern = p;
3656 p -= len;
3657 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003658 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660
3661 /*
3662 * If we are still inside the quotes, and we passed a space, just
3663 * expand from there.
3664 */
3665 if (bow != NULL && in_quote)
3666 xp->xp_pattern = bow;
3667 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003668
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003669 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003670 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003671 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003672#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003673 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003674#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003675 /* When still after the command name expand executables. */
3676 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003677 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 /* Check for environment variable */
3681 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003682#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 || *xp->xp_pattern == '%'
3684#endif
3685 )
3686 {
3687 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3688 if (!vim_isIDc(*p))
3689 break;
3690 if (*p == NUL)
3691 {
3692 xp->xp_context = EXPAND_ENV_VARS;
3693 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003694#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003695 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003696 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003697 compl = EXPAND_ENV_VARS;
3698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003701#if defined(FEAT_CMDL_COMPL)
3702 /* Check for user names */
3703 if (*xp->xp_pattern == '~')
3704 {
3705 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3706 ;
3707 /* Complete ~user only if it partially matches a user name.
3708 * A full match ~user<Tab> will be replaced by user's home
3709 * directory i.e. something like ~user<Tab> -> /home/user/ */
3710 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003711 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003712 {
3713 xp->xp_context = EXPAND_USER;
3714 ++xp->xp_pattern;
3715 }
3716 }
3717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 }
3719
3720/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003721 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003723 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003725 case CMD_find:
3726 case CMD_sfind:
3727 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003728 if (xp->xp_context == EXPAND_FILES)
3729 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003730 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 case CMD_cd:
3732 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003733 case CMD_tcd:
3734 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 case CMD_lcd:
3736 case CMD_lchdir:
3737 if (xp->xp_context == EXPAND_FILES)
3738 xp->xp_context = EXPAND_DIRECTORIES;
3739 break;
3740 case CMD_help:
3741 xp->xp_context = EXPAND_HELP;
3742 xp->xp_pattern = arg;
3743 break;
3744
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003745 /* Command modifiers: return the argument.
3746 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003748 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 case CMD_belowright:
3750 case CMD_botright:
3751 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003752 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003753 case CMD_cdo:
3754 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003756 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 case CMD_folddoclosed:
3758 case CMD_folddoopen:
3759 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003760 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 case CMD_keepjumps:
3762 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003763 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003764 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003766 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003768 case CMD_noautocmd:
3769 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003771 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003773 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003774 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 case CMD_topleft:
3776 case CMD_verbose:
3777 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003778 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 return arg;
3780
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003781 case CMD_filter:
3782 if (*arg != NUL)
3783 arg = skip_vimgrep_pat(arg, NULL, NULL);
3784 if (arg == NULL || *arg == NUL)
3785 {
3786 xp->xp_context = EXPAND_NOTHING;
3787 return NULL;
3788 }
3789 return skipwhite(arg);
3790
Bram Moolenaar4f688582007-07-24 12:34:30 +00003791#ifdef FEAT_CMDL_COMPL
3792# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 case CMD_match:
3794 if (*arg == NUL || !ends_excmd(*arg))
3795 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003796 /* also complete "None" */
3797 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 arg = skipwhite(skiptowhite(arg));
3799 if (*arg != NUL)
3800 {
3801 xp->xp_context = EXPAND_NOTHING;
3802 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3803 }
3804 }
3805 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003806# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808/*
3809 * All completion for the +cmdline_compl feature goes here.
3810 */
3811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003813 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
3815 case CMD_delcommand:
3816 xp->xp_context = EXPAND_USER_COMMANDS;
3817 xp->xp_pattern = arg;
3818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 case CMD_global:
3821 case CMD_vglobal:
3822 delim = *arg; /* get the delimiter */
3823 if (delim)
3824 ++arg; /* skip delimiter if there is one */
3825
3826 while (arg[0] != NUL && arg[0] != delim)
3827 {
3828 if (arg[0] == '\\' && arg[1] != NUL)
3829 ++arg;
3830 ++arg;
3831 }
3832 if (arg[0] != NUL)
3833 return arg + 1;
3834 break;
3835 case CMD_and:
3836 case CMD_substitute:
3837 delim = *arg;
3838 if (delim)
3839 {
3840 /* skip "from" part */
3841 ++arg;
3842 arg = skip_regexp(arg, delim, p_magic, NULL);
3843 }
3844 /* skip "to" part */
3845 while (arg[0] != NUL && arg[0] != delim)
3846 {
3847 if (arg[0] == '\\' && arg[1] != NUL)
3848 ++arg;
3849 ++arg;
3850 }
3851 if (arg[0] != NUL) /* skip delimiter */
3852 ++arg;
3853 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3854 ++arg;
3855 if (arg[0] != NUL)
3856 return arg;
3857 break;
3858 case CMD_isearch:
3859 case CMD_dsearch:
3860 case CMD_ilist:
3861 case CMD_dlist:
3862 case CMD_ijump:
3863 case CMD_psearch:
3864 case CMD_djump:
3865 case CMD_isplit:
3866 case CMD_dsplit:
3867 arg = skipwhite(skipdigits(arg)); /* skip count */
3868 if (*arg == '/') /* Match regexp, not just whole words */
3869 {
3870 for (++arg; *arg && *arg != '/'; arg++)
3871 if (*arg == '\\' && arg[1] != NUL)
3872 arg++;
3873 if (*arg)
3874 {
3875 arg = skipwhite(arg + 1);
3876
3877 /* Check for trailing illegal characters */
3878 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3879 xp->xp_context = EXPAND_NOTHING;
3880 else
3881 return arg;
3882 }
3883 }
3884 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_autocmd:
3887 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003889 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 case CMD_set:
3892 set_context_in_set_cmd(xp, arg, 0);
3893 break;
3894 case CMD_setglobal:
3895 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3896 break;
3897 case CMD_setlocal:
3898 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3899 break;
3900 case CMD_tag:
3901 case CMD_stag:
3902 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003903 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 case CMD_tselect:
3905 case CMD_stselect:
3906 case CMD_ptselect:
3907 case CMD_tjump:
3908 case CMD_stjump:
3909 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003910 if (*p_wop != NUL)
3911 xp->xp_context = EXPAND_TAGS_LISTFILES;
3912 else
3913 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 xp->xp_pattern = arg;
3915 break;
3916 case CMD_augroup:
3917 xp->xp_context = EXPAND_AUGROUP;
3918 xp->xp_pattern = arg;
3919 break;
3920#ifdef FEAT_SYN_HL
3921 case CMD_syntax:
3922 set_context_in_syntax_cmd(xp, arg);
3923 break;
3924#endif
3925#ifdef FEAT_EVAL
3926 case CMD_let:
3927 case CMD_if:
3928 case CMD_elseif:
3929 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003930 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 case CMD_echo:
3932 case CMD_echon:
3933 case CMD_execute:
3934 case CMD_echomsg:
3935 case CMD_echoerr:
3936 case CMD_call:
3937 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003938 case CMD_cexpr:
3939 case CMD_caddexpr:
3940 case CMD_cgetexpr:
3941 case CMD_lexpr:
3942 case CMD_laddexpr:
3943 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003944 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 break;
3946
3947 case CMD_unlet:
3948 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3949 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003950
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 xp->xp_context = EXPAND_USER_VARS;
3952 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003953
3954 if (*xp->xp_pattern == '$')
3955 {
3956 xp->xp_context = EXPAND_ENV_VARS;
3957 ++xp->xp_pattern;
3958 }
3959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 break;
3961
3962 case CMD_function:
3963 case CMD_delfunction:
3964 xp->xp_context = EXPAND_USER_FUNC;
3965 xp->xp_pattern = arg;
3966 break;
3967
3968 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003969 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 break;
3971#endif
3972 case CMD_highlight:
3973 set_context_in_highlight_cmd(xp, arg);
3974 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003975#ifdef FEAT_CSCOPE
3976 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003977 case CMD_lcscope:
3978 case CMD_scscope:
3979 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003980 break;
3981#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003982#ifdef FEAT_SIGNS
3983 case CMD_sign:
3984 set_context_in_sign_cmd(xp, arg);
3985 break;
3986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 case CMD_bdelete:
3988 case CMD_bwipeout:
3989 case CMD_bunload:
3990 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3991 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003992 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 case CMD_buffer:
3994 case CMD_sbuffer:
3995 case CMD_checktime:
3996 xp->xp_context = EXPAND_BUFFERS;
3997 xp->xp_pattern = arg;
3998 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003999
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 case CMD_USER:
4001 case CMD_USER_BUF:
4002 if (compl != EXPAND_NOTHING)
4003 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004004 // EX_XFILE: file names are handled above
4005 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004007#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if (compl == EXPAND_MENUS)
4009 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 if (compl == EXPAND_COMMANDS)
4012 return arg;
4013 if (compl == EXPAND_MAPPINGS)
4014 return set_context_in_map_cmd(xp, (char_u *)"map",
4015 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004016 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004017 p = arg;
4018 while (*p)
4019 {
4020 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004021 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004022 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004023 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004024 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004025 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 xp->xp_pattern = arg;
4028 }
4029 xp->xp_context = compl;
4030 }
4031 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004032
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 case CMD_map: case CMD_noremap:
4034 case CMD_nmap: case CMD_nnoremap:
4035 case CMD_vmap: case CMD_vnoremap:
4036 case CMD_omap: case CMD_onoremap:
4037 case CMD_imap: case CMD_inoremap:
4038 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004039 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004040 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004041 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004042 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004044 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 case CMD_unmap:
4046 case CMD_nunmap:
4047 case CMD_vunmap:
4048 case CMD_ounmap:
4049 case CMD_iunmap:
4050 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004051 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004052 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004053 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004054 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004056 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004057 case CMD_mapclear:
4058 case CMD_nmapclear:
4059 case CMD_vmapclear:
4060 case CMD_omapclear:
4061 case CMD_imapclear:
4062 case CMD_cmapclear:
4063 case CMD_lmapclear:
4064 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004065 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004066 case CMD_xmapclear:
4067 xp->xp_context = EXPAND_MAPCLEAR;
4068 xp->xp_pattern = arg;
4069 break;
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 case CMD_abbreviate: case CMD_noreabbrev:
4072 case CMD_cabbrev: case CMD_cnoreabbrev:
4073 case CMD_iabbrev: case CMD_inoreabbrev:
4074 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004075 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 case CMD_unabbreviate:
4077 case CMD_cunabbrev:
4078 case CMD_iunabbrev:
4079 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004080 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#ifdef FEAT_MENU
4082 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4083 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4084 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4085 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4086 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4087 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4088 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004089 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 case CMD_tmenu: case CMD_tunmenu:
4091 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4092 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4093#endif
4094
4095 case CMD_colorscheme:
4096 xp->xp_context = EXPAND_COLORS;
4097 xp->xp_pattern = arg;
4098 break;
4099
4100 case CMD_compiler:
4101 xp->xp_context = EXPAND_COMPILER;
4102 xp->xp_pattern = arg;
4103 break;
4104
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004105 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004106 xp->xp_context = EXPAND_OWNSYNTAX;
4107 xp->xp_pattern = arg;
4108 break;
4109
4110 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004111 xp->xp_context = EXPAND_FILETYPE;
4112 xp->xp_pattern = arg;
4113 break;
4114
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004115 case CMD_packadd:
4116 xp->xp_context = EXPAND_PACKADD;
4117 xp->xp_pattern = arg;
4118 break;
4119
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004120#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004122 p = skiptowhite(arg);
4123 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
4125 xp->xp_context = EXPAND_LANGUAGE;
4126 xp->xp_pattern = arg;
4127 }
4128 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004129 {
4130 if ( STRNCMP(arg, "messages", p - arg) == 0
4131 || STRNCMP(arg, "ctype", p - arg) == 0
4132 || STRNCMP(arg, "time", p - arg) == 0)
4133 {
4134 xp->xp_context = EXPAND_LOCALES;
4135 xp->xp_pattern = skipwhite(p);
4136 }
4137 else
4138 xp->xp_context = EXPAND_NOTHING;
4139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 break;
4141#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004142#if defined(FEAT_PROFILE)
4143 case CMD_profile:
4144 set_context_in_profile_cmd(xp, arg);
4145 break;
4146#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004147 case CMD_behave:
4148 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004149 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004150 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004152 case CMD_messages:
4153 xp->xp_context = EXPAND_MESSAGES;
4154 xp->xp_pattern = arg;
4155 break;
4156
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004157#if defined(FEAT_CMDHIST)
4158 case CMD_history:
4159 xp->xp_context = EXPAND_HISTORY;
4160 xp->xp_pattern = arg;
4161 break;
4162#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004163#if defined(FEAT_PROFILE)
4164 case CMD_syntime:
4165 xp->xp_context = EXPAND_SYNTIME;
4166 xp->xp_pattern = arg;
4167 break;
4168#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004169
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004170 case CMD_argdelete:
4171 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4172 arg = xp->xp_pattern + 1;
4173 xp->xp_context = EXPAND_ARGLIST;
4174 xp->xp_pattern = arg;
4175 break;
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177#endif /* FEAT_CMDL_COMPL */
4178
4179 default:
4180 break;
4181 }
4182 return NULL;
4183}
4184
4185/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004186 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 *
4188 * Backslashed delimiters after / or ? will be skipped, and commands will
4189 * not be expanded between /'s and ?'s or after "'".
4190 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004191 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 * Returns the "cmd" pointer advanced to beyond the range.
4193 */
4194 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004195skip_range(
4196 char_u *cmd,
4197 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004199 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004201 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004203 if (*cmd == '\\')
4204 {
4205 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4206 ++cmd;
4207 else
4208 break;
4209 }
4210 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
4212 if (*++cmd == NUL && ctx != NULL)
4213 *ctx = EXPAND_NOTHING;
4214 }
4215 else if (*cmd == '/' || *cmd == '?')
4216 {
4217 delim = *cmd++;
4218 while (*cmd != NUL && *cmd != delim)
4219 if (*cmd++ == '\\' && *cmd != NUL)
4220 ++cmd;
4221 if (*cmd == NUL && ctx != NULL)
4222 *ctx = EXPAND_NOTHING;
4223 }
4224 if (*cmd != NUL)
4225 ++cmd;
4226 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004227
4228 /* Skip ":" and white space. */
4229 while (*cmd == ':')
4230 cmd = skipwhite(cmd + 1);
4231
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return cmd;
4233}
4234
4235/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004236 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 *
4238 * Set ptr to the next character after the part that was interpreted.
4239 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004240 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 *
4242 * Return MAXLNUM when no Ex address was found.
4243 */
4244 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004245get_address(
4246 exarg_T *eap UNUSED,
4247 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004248 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004249 int skip, // only skip the address, don't use it
4250 int silent, // no errors or side effects
4251 int to_other_file, // flag: may jump to other file
4252 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004254 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 int c;
4256 int i;
4257 long n;
4258 char_u *cmd;
4259 pos_T pos;
4260 pos_T *fp;
4261 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004262 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 cmd = skipwhite(*ptr);
4265 lnum = MAXLNUM;
4266 do
4267 {
4268 switch (*cmd)
4269 {
4270 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 ++cmd;
4272 switch (addr_type)
4273 {
4274 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004275 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 lnum = curwin->w_cursor.lnum;
4277 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004278 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004279 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004280 break;
4281 case ADDR_ARGUMENTS:
4282 lnum = curwin->w_arg_idx + 1;
4283 break;
4284 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004285 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 lnum = curbuf->b_fnum;
4287 break;
4288 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004289 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004290 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004291 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004292 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004293 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004294 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004295 cmd = NULL;
4296 goto error;
4297 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004298 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004299#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004300 lnum = qf_get_cur_idx(eap);
4301#endif
4302 break;
4303 case ADDR_QUICKFIX_VALID:
4304#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004305 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004306#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004307 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004308 }
4309 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 ++cmd;
4313 switch (addr_type)
4314 {
4315 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004316 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 lnum = curbuf->b_ml.ml_line_count;
4318 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004319 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004320 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004321 break;
4322 case ADDR_ARGUMENTS:
4323 lnum = ARGCOUNT;
4324 break;
4325 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004326 buf = lastbuf;
4327 while (buf->b_ml.ml_mfp == NULL)
4328 {
4329 if (buf->b_prev == NULL)
4330 break;
4331 buf = buf->b_prev;
4332 }
4333 lnum = buf->b_fnum;
4334 break;
4335 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 lnum = lastbuf->b_fnum;
4337 break;
4338 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004339 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004340 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004341 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004342 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004343 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004344 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004345 cmd = NULL;
4346 goto error;
4347 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004348 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004349#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004350 lnum = qf_get_size(eap);
4351 if (lnum == 0)
4352 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004353#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004354 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004355 case ADDR_QUICKFIX_VALID:
4356#ifdef FEAT_QUICKFIX
4357 lnum = qf_get_valid_size(eap);
4358 if (lnum == 0)
4359 lnum = 1;
4360#endif
4361 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004362 }
4363 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
4365 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 if (*++cmd == NUL)
4367 {
4368 cmd = NULL;
4369 goto error;
4370 }
4371 if (addr_type != ADDR_LINES)
4372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004373 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004374 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004375 goto error;
4376 }
4377 if (skip)
4378 ++cmd;
4379 else
4380 {
4381 /* Only accept a mark in another file when it is
4382 * used by itself: ":'M". */
4383 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4384 ++cmd;
4385 if (fp == (pos_T *)-1)
4386 /* Jumped to another file. */
4387 lnum = curwin->w_cursor.lnum;
4388 else
4389 {
4390 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 {
4392 cmd = NULL;
4393 goto error;
4394 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 lnum = fp->lnum;
4396 }
4397 }
4398 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400 case '/':
4401 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 c = *cmd++;
4403 if (addr_type != ADDR_LINES)
4404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004405 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004406 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 goto error;
4408 }
4409 if (skip) /* skip "/pat/" */
4410 {
4411 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4412 if (*cmd == c)
4413 ++cmd;
4414 }
4415 else
4416 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004417 int flags;
4418
4419 pos = curwin->w_cursor; // save curwin->w_cursor
4420
4421 // When '/' or '?' follows another address, start from
4422 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 if (lnum != MAXLNUM)
4424 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004425
4426 // Start a forward search at the end of the line (unless
4427 // before the first line).
4428 // Start a backward search at the start of the line.
4429 // This makes sure we never match in the current
4430 // line, and can match anywhere in the
4431 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004432 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 curwin->w_cursor.col = MAXCOL;
4434 else
4435 curwin->w_cursor.col = 0;
4436 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004437 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4438 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004439 {
4440 curwin->w_cursor = pos;
4441 cmd = NULL;
4442 goto error;
4443 }
4444 lnum = curwin->w_cursor.lnum;
4445 curwin->w_cursor = pos;
4446 /* adjust command string pointer */
4447 cmd += searchcmdlen;
4448 }
4449 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004452 ++cmd;
4453 if (addr_type != ADDR_LINES)
4454 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004455 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004456 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 goto error;
4458 }
4459 if (*cmd == '&')
4460 i = RE_SUBST;
4461 else if (*cmd == '?' || *cmd == '/')
4462 i = RE_SEARCH;
4463 else
4464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004465 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 cmd = NULL;
4467 goto error;
4468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 if (!skip)
4471 {
4472 /*
4473 * When search follows another address, start from
4474 * there.
4475 */
4476 if (lnum != MAXLNUM)
4477 pos.lnum = lnum;
4478 else
4479 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 /*
4482 * Start the search just like for the above
4483 * do_search().
4484 */
4485 if (*cmd != '?')
4486 pos.col = MAXCOL;
4487 else
4488 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004489 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004490 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 *cmd == '?' ? BACKWARD : FORWARD,
4492 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004493 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 lnum = pos.lnum;
4495 else
4496 {
4497 cmd = NULL;
4498 goto error;
4499 }
4500 }
4501 ++cmd;
4502 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004505 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4506 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508
4509 for (;;)
4510 {
4511 cmd = skipwhite(cmd);
4512 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4513 break;
4514
4515 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 {
4517 switch (addr_type)
4518 {
4519 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004520 case ADDR_OTHER:
4521 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004522 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 break;
4524 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004525 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004526 break;
4527 case ADDR_ARGUMENTS:
4528 lnum = curwin->w_arg_idx + 1;
4529 break;
4530 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004531 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 lnum = curbuf->b_fnum;
4533 break;
4534 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004535 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004536 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004537 case ADDR_TABS_RELATIVE:
4538 lnum = 1;
4539 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004540 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004541#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004542 lnum = qf_get_cur_idx(eap);
4543#endif
4544 break;
4545 case ADDR_QUICKFIX_VALID:
4546#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004547 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004548#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004549 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004550 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004551 case ADDR_UNSIGNED:
4552 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004553 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004554 }
4555 }
4556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (VIM_ISDIGIT(*cmd))
4558 i = '+'; /* "number" is same as "+number" */
4559 else
4560 i = *cmd++;
4561 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4562 n = 1;
4563 else
4564 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004565
4566 if (addr_type == ADDR_TABS_RELATIVE)
4567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004568 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004569 cmd = NULL;
4570 goto error;
4571 }
4572 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004573 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004574 lnum = compute_buffer_local_count(
4575 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004577 {
4578#ifdef FEAT_FOLDING
4579 /* Relative line addressing, need to adjust for folded lines
4580 * now, but only do it after the first address. */
4581 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4582 && address_count >= 2)
4583 (void)hasFolding(lnum, NULL, &lnum);
4584#endif
4585 if (i == '-')
4586 lnum -= n;
4587 else
4588 lnum += n;
4589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
4591 } while (*cmd == '/' || *cmd == '?');
4592
4593error:
4594 *ptr = cmd;
4595 return lnum;
4596}
4597
4598/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004599 * Get flags from an Ex command argument.
4600 */
4601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004602get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004603{
4604 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4605 {
4606 if (*eap->arg == 'l')
4607 eap->flags |= EXFLAG_LIST;
4608 else if (*eap->arg == 'p')
4609 eap->flags |= EXFLAG_PRINT;
4610 else
4611 eap->flags |= EXFLAG_NR;
4612 eap->arg = skipwhite(eap->arg + 1);
4613 }
4614}
4615
4616/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 * Function called for command which is Not Implemented. NI!
4618 */
4619 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004620ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621{
4622 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004623 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624}
4625
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004626#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627/*
4628 * Function called for script command which is Not Implemented. NI!
4629 * Skips over ":perl <<EOF" constructs.
4630 */
4631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004632ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
4634 if (!eap->skip)
4635 ex_ni(eap);
4636 else
4637 vim_free(script_get(eap, eap->arg));
4638}
4639#endif
4640
4641/*
4642 * Check range in Ex command for validity.
4643 * Return NULL when valid, error message when invalid.
4644 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004645 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004646invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004648 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004649
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 if ( eap->line1 < 0
4651 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004652 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004653 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004655 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004656 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004657 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004658 {
4659 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004660 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004661#ifdef FEAT_DIFF
4662 + (eap->cmdidx == CMD_diffget)
4663#endif
4664 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004665 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004666 break;
4667 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004668 /* add 1 if ARGCOUNT is 0 */
4669 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004670 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004671 break;
4672 case ADDR_BUFFERS:
4673 if (eap->line1 < firstbuf->b_fnum
4674 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004675 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004676 break;
4677 case ADDR_LOADED_BUFFERS:
4678 buf = firstbuf;
4679 while (buf->b_ml.ml_mfp == NULL)
4680 {
4681 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004682 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004683 buf = buf->b_next;
4684 }
4685 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004686 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004687 buf = lastbuf;
4688 while (buf->b_ml.ml_mfp == NULL)
4689 {
4690 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004691 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004692 buf = buf->b_prev;
4693 }
4694 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004695 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004696 break;
4697 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004698 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004699 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004700 break;
4701 case ADDR_TABS:
4702 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004703 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004704 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004705 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004706 case ADDR_OTHER:
4707 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004708 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004709 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004710#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004711 // No error for value that is too big, will use the last entry.
4712 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004713 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004714#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004715 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004716 case ADDR_QUICKFIX_VALID:
4717#ifdef FEAT_QUICKFIX
4718 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4719 || eap->line2 < 0)
4720 return _(e_invrange);
4721#endif
4722 break;
4723 case ADDR_UNSIGNED:
4724 if (eap->line2 < 0)
4725 return _(e_invrange);
4726 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004727 case ADDR_NONE:
4728 // Will give an error elsewhere.
4729 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004730 }
4731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return NULL;
4733}
4734
4735/*
4736 * Correct the range for zero line number, if required.
4737 */
4738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004739correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004741 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 {
4743 if (eap->line1 == 0)
4744 eap->line1 = 1;
4745 if (eap->line2 == 0)
4746 eap->line2 = 1;
4747 }
4748}
4749
Bram Moolenaar748bf032005-02-02 23:04:36 +00004750#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004751/*
4752 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4753 * pattern. Otherwise return eap->arg.
4754 */
4755 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004757{
4758 char_u *p = eap->arg;
4759
Bram Moolenaara37420f2006-02-04 22:37:47 +00004760 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4761 || eap->cmdidx == CMD_vimgrepadd
4762 || eap->cmdidx == CMD_lvimgrepadd
4763 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004765 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004766 if (p == NULL)
4767 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004768 }
4769 return p;
4770}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004771
4772/*
4773 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4774 * in the command line, so that things like % get expanded.
4775 */
4776 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004777replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004778{
4779 char_u *new_cmdline;
4780 char_u *program;
4781 char_u *pos;
4782 char_u *ptr;
4783 int len;
4784 int i;
4785
4786 /*
4787 * Don't do it when ":vimgrep" is used for ":grep".
4788 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004789 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4790 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4791 || eap->cmdidx == CMD_grepadd
4792 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004793 && !grep_internal(eap->cmdidx))
4794 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004795 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4796 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004797 {
4798 if (*curbuf->b_p_gp == NUL)
4799 program = p_gp;
4800 else
4801 program = curbuf->b_p_gp;
4802 }
4803 else
4804 {
4805 if (*curbuf->b_p_mp == NUL)
4806 program = p_mp;
4807 else
4808 program = curbuf->b_p_mp;
4809 }
4810
4811 p = skipwhite(p);
4812
4813 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4814 {
4815 /* replace $* by given arguments */
4816 i = 1;
4817 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4818 ++i;
4819 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004820 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004821 if (new_cmdline == NULL)
4822 return NULL; /* out of memory */
4823 ptr = new_cmdline;
4824 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4825 {
4826 i = (int)(pos - program);
4827 STRNCPY(ptr, program, i);
4828 STRCPY(ptr += i, p);
4829 ptr += len;
4830 program = pos + 2;
4831 }
4832 STRCPY(ptr, program);
4833 }
4834 else
4835 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004836 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004837 if (new_cmdline == NULL)
4838 return NULL; /* out of memory */
4839 STRCPY(new_cmdline, program);
4840 STRCAT(new_cmdline, " ");
4841 STRCAT(new_cmdline, p);
4842 }
4843 msg_make(p);
4844
4845 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4846 vim_free(*cmdlinep);
4847 *cmdlinep = new_cmdline;
4848 p = new_cmdline;
4849 }
4850 return p;
4851}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004852#endif
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854/*
4855 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004856 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 * Return FAIL for failure, OK otherwise.
4858 */
4859 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004860expand_filename(
4861 exarg_T *eap,
4862 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004863 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 int has_wildcards; /* need to expand wildcards */
4866 char_u *repl;
4867 int srclen;
4868 char_u *p;
4869 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004870 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
Bram Moolenaar748bf032005-02-02 23:04:36 +00004872#ifdef FEAT_QUICKFIX
4873 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4874 p = skip_grep_pat(eap);
4875#else
4876 p = eap->arg;
4877#endif
4878
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 /*
4880 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4881 * the file name contains a wildcard it should not cause expanding.
4882 * (it will be expanded anyway if there is a wildcard before replacing).
4883 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004884 has_wildcards = mch_has_wildcard(p);
4885 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004887#ifdef FEAT_EVAL
4888 /* Skip over `=expr`, wildcards in it are not expanded. */
4889 if (p[0] == '`' && p[1] == '=')
4890 {
4891 p += 2;
4892 (void)skip_expr(&p);
4893 if (*p == '`')
4894 ++p;
4895 continue;
4896 }
4897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 /*
4899 * Quick check if this cannot be the start of a special string.
4900 * Also removes backslash before '%', '#' and '<'.
4901 */
4902 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4903 {
4904 ++p;
4905 continue;
4906 }
4907
4908 /*
4909 * Try to find a match at this position.
4910 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004911 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4912 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 if (*errormsgp != NULL) /* error detected */
4914 return FAIL;
4915 if (repl == NULL) /* no match found */
4916 {
4917 p += srclen;
4918 continue;
4919 }
4920
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004921 /* Wildcards won't be expanded below, the replacement is taken
4922 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4923 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4924 {
4925 char_u *l = repl;
4926
4927 repl = expand_env_save(repl);
4928 vim_free(l);
4929 }
4930
Bram Moolenaar63b92542007-03-27 14:57:09 +00004931 /* Need to escape white space et al. with a backslash.
4932 * Don't do this for:
4933 * - replacement that already has been escaped: "##"
4934 * - shell commands (may have to use quotes instead).
4935 * - non-unix systems when there is a single argument (spaces don't
4936 * separate arguments then).
4937 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004939 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 && eap->cmdidx != CMD_grep
4942 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004943 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004944 && eap->cmdidx != CMD_lgrep
4945 && eap->cmdidx != CMD_lgrepadd
4946 && eap->cmdidx != CMD_lmake
4947 && eap->cmdidx != CMD_make
4948 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004950 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951#endif
4952 )
4953 {
4954 char_u *l;
4955#ifdef BACKSLASH_IN_FILENAME
4956 /* Don't escape a backslash here, because rem_backslash() doesn't
4957 * remove it later. */
4958 static char_u *nobslash = (char_u *)" \t\"|";
4959# define ESCAPE_CHARS nobslash
4960#else
4961# define ESCAPE_CHARS escape_chars
4962#endif
4963
4964 for (l = repl; *l; ++l)
4965 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4966 {
4967 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4968 if (l != NULL)
4969 {
4970 vim_free(repl);
4971 repl = l;
4972 }
4973 break;
4974 }
4975 }
4976
4977 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004978 if ((eap->usefilter || eap->cmdidx == CMD_bang
4979 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004980 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 char_u *l;
4983
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004984 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (l != NULL)
4986 {
4987 vim_free(repl);
4988 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 }
4991
4992 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4993 vim_free(repl);
4994 if (p == NULL)
4995 return FAIL;
4996 }
4997
4998 /*
4999 * One file argument: Expand wildcards.
5000 * Don't do this with ":r !command" or ":w !command".
5001 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005002 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 /*
5005 * May do this twice:
5006 * 1. Replace environment variables.
5007 * 2. Replace any other wildcards, remove backslashes.
5008 */
5009 for (n = 1; n <= 2; ++n)
5010 {
5011 if (n == 2)
5012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 /*
5014 * Halve the number of backslashes (this is Vi compatible).
5015 * For Unix and OS/2, when wildcards are expanded, this is
5016 * done by ExpandOne() below.
5017 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005018#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (!has_wildcards)
5020#endif
5021 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023
5024 if (has_wildcards)
5025 {
5026 if (n == 1)
5027 {
5028 /*
5029 * First loop: May expand environment variables. This
5030 * can be done much faster with expand_env() than with
5031 * something else (e.g., calling a shell).
5032 * After expanding environment variables, check again
5033 * if there are still wildcards present.
5034 */
5035 if (vim_strchr(eap->arg, '$') != NULL
5036 || vim_strchr(eap->arg, '~') != NULL)
5037 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005038 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005039 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 has_wildcards = mch_has_wildcard(NameBuff);
5041 p = NameBuff;
5042 }
5043 else
5044 p = NULL;
5045 }
5046 else /* n == 2 */
5047 {
5048 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005049 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
5051 ExpandInit(&xpc);
5052 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005053 if (p_wic)
5054 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005056 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (p == NULL)
5058 return FAIL;
5059 }
5060 if (p != NULL)
5061 {
5062 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5063 p, cmdlinep);
5064 if (n == 2) /* p came from ExpandOne() */
5065 vim_free(p);
5066 }
5067 }
5068 }
5069 }
5070 return OK;
5071}
5072
5073/*
5074 * Replace part of the command line, keeping eap->cmd, eap->arg and
5075 * eap->nextcmd correct.
5076 * "src" points to the part that is to be replaced, of length "srclen".
5077 * "repl" is the replacement string.
5078 * Returns a pointer to the character after the replaced string.
5079 * Returns NULL for failure.
5080 */
5081 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005082repl_cmdline(
5083 exarg_T *eap,
5084 char_u *src,
5085 int srclen,
5086 char_u *repl,
5087 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 int len;
5090 int i;
5091 char_u *new_cmdline;
5092
5093 /*
5094 * The new command line is build in new_cmdline[].
5095 * First allocate it.
5096 * Careful: a "+cmd" argument may have been NUL terminated.
5097 */
5098 len = (int)STRLEN(repl);
5099 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005100 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005102 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 return NULL; /* out of memory! */
5104
5105 /*
5106 * Copy the stuff before the expanded part.
5107 * Copy the expanded stuff.
5108 * Copy what came after the expanded part.
5109 * Copy the next commands, if there are any.
5110 */
5111 i = (int)(src - *cmdlinep); /* length of part before match */
5112 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 mch_memmove(new_cmdline + i, repl, (size_t)len);
5115 i += len; /* remember the end of the string */
5116 STRCPY(new_cmdline + i, src + srclen);
5117 src = new_cmdline + i; /* remember where to continue */
5118
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005119 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 i = (int)STRLEN(new_cmdline) + 1;
5122 STRCPY(new_cmdline + i, eap->nextcmd);
5123 eap->nextcmd = new_cmdline + i;
5124 }
5125 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5126 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5127 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5128 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5129 vim_free(*cmdlinep);
5130 *cmdlinep = new_cmdline;
5131
5132 return src;
5133}
5134
5135/*
5136 * Check for '|' to separate commands and '"' to start comments.
5137 */
5138 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005139separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 char_u *p;
5142
Bram Moolenaar86b68352004-12-27 21:59:20 +00005143#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005144 p = skip_grep_pat(eap);
5145#else
5146 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005147#endif
5148
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005149 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 if (*p == Ctrl_V)
5152 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005153 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 ++p; /* skip CTRL-V and next char */
5155 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005156 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005157 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (*p == NUL) /* stop at NUL after CTRL-V */
5159 break;
5160 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005161
5162#ifdef FEAT_EVAL
5163 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005164 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005165 {
5166 p += 2;
5167 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005168 }
5169#endif
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 /* Check for '"': start of comment or '|': next command */
5172 /* :@" and :*" do not start a comment!
5173 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005174 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5176 || p != eap->arg)
5177 && (eap->cmdidx != CMD_redir
5178 || p != eap->arg + 1 || p[-1] != '@'))
5179 || *p == '|' || *p == '\n')
5180 {
5181 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005182 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 * AND 'b' is present in 'cpoptions'.
5184 */
5185 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005186 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005188 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 --p;
5190 }
5191 else
5192 {
5193 eap->nextcmd = check_nextcmd(p);
5194 *p = NUL;
5195 break;
5196 }
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005199
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005200 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 del_trailing_spaces(eap->arg);
5202}
5203
5204/*
5205 * get + command from ex argument
5206 */
5207 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005208getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 char_u *arg = *argp;
5211 char_u *command = NULL;
5212
5213 if (*arg == '+') /* +[command] */
5214 {
5215 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005216 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 command = dollar_command;
5218 else
5219 {
5220 command = arg;
5221 arg = skip_cmd_arg(command, TRUE);
5222 if (*arg != NUL)
5223 *arg++ = NUL; /* terminate command with NUL */
5224 }
5225
5226 arg = skipwhite(arg); /* skip over spaces */
5227 *argp = arg;
5228 }
5229 return command;
5230}
5231
5232/*
5233 * Find end of "+command" argument. Skip over "\ " and "\\".
5234 */
5235 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005236skip_cmd_arg(
5237 char_u *p,
5238 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 while (*p && !vim_isspace(*p))
5241 {
5242 if (*p == '\\' && p[1] != NUL)
5243 {
5244 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005245 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 else
5247 ++p;
5248 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005249 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251 return p;
5252}
5253
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005254 int
5255get_bad_opt(char_u *p, exarg_T *eap)
5256{
5257 if (STRICMP(p, "keep") == 0)
5258 eap->bad_char = BAD_KEEP;
5259 else if (STRICMP(p, "drop") == 0)
5260 eap->bad_char = BAD_DROP;
5261 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5262 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005263 else
5264 return FAIL;
5265 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005266}
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268/*
5269 * Get "++opt=arg" argument.
5270 * Return FAIL or OK.
5271 */
5272 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005273getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274{
5275 char_u *arg = eap->arg + 2;
5276 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005277 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279
5280 /* ":edit ++[no]bin[ary] file" */
5281 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5282 {
5283 if (*arg == 'n')
5284 {
5285 arg += 2;
5286 eap->force_bin = FORCE_NOBIN;
5287 }
5288 else
5289 eap->force_bin = FORCE_BIN;
5290 if (!checkforcmd(&arg, "binary", 3))
5291 return FAIL;
5292 eap->arg = skipwhite(arg);
5293 return OK;
5294 }
5295
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005296 /* ":read ++edit file" */
5297 if (STRNCMP(arg, "edit", 4) == 0)
5298 {
5299 eap->read_edit = TRUE;
5300 eap->arg = skipwhite(arg + 4);
5301 return OK;
5302 }
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 if (STRNCMP(arg, "ff", 2) == 0)
5305 {
5306 arg += 2;
5307 pp = &eap->force_ff;
5308 }
5309 else if (STRNCMP(arg, "fileformat", 10) == 0)
5310 {
5311 arg += 10;
5312 pp = &eap->force_ff;
5313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 else if (STRNCMP(arg, "enc", 3) == 0)
5315 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005316 if (STRNCMP(arg, "encoding", 8) == 0)
5317 arg += 8;
5318 else
5319 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 pp = &eap->force_enc;
5321 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005322 else if (STRNCMP(arg, "bad", 3) == 0)
5323 {
5324 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005325 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
5328 if (pp == NULL || *arg != '=')
5329 return FAIL;
5330
5331 ++arg;
5332 *pp = (int)(arg - eap->cmd);
5333 arg = skip_cmd_arg(arg, FALSE);
5334 eap->arg = skipwhite(arg);
5335 *arg = NUL;
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (pp == &eap->force_ff)
5338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5340 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005341 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005343 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 {
5345 /* Make 'fileencoding' lower case. */
5346 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5347 *p = TOLOWER_ASC(*p);
5348 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005349 else
5350 {
5351 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5352 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005353 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005354 return FAIL;
5355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
5357 return OK;
5358}
5359
5360/*
5361 * ":abbreviate" and friends.
5362 */
5363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005364ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 do_exmap(eap, TRUE); /* almost the same as mapping */
5367}
5368
5369/*
5370 * ":map" and friends.
5371 */
5372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005373ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374{
5375 /*
5376 * If we are sourcing .exrc or .vimrc in current directory we
5377 * print the mappings for security reasons.
5378 */
5379 if (secure)
5380 {
5381 secure = 2;
5382 msg_outtrans(eap->cmd);
5383 msg_putchar('\n');
5384 }
5385 do_exmap(eap, FALSE);
5386}
5387
5388/*
5389 * ":unmap" and friends.
5390 */
5391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005392ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394 do_exmap(eap, FALSE);
5395}
5396
5397/*
5398 * ":mapclear" and friends.
5399 */
5400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5404}
5405
5406/*
5407 * ":abclear" and friends.
5408 */
5409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005410ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411{
5412 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5413}
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005419 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 * directory for security reasons.
5421 */
5422 if (secure)
5423 {
5424 secure = 2;
5425 eap->errmsg = e_curdir;
5426 }
5427 else if (eap->cmdidx == CMD_autocmd)
5428 do_autocmd(eap->arg, eap->forceit);
5429 else
5430 do_augroup(eap->arg, eap->forceit);
5431}
5432
5433/*
5434 * ":doautocmd": Apply the automatic commands to the current buffer.
5435 */
5436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005437ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005439 char_u *arg = eap->arg;
5440 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005441 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005442
Bram Moolenaar1610d052016-06-09 22:53:01 +02005443 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5444 /* Only when there is no <nomodeline>. */
5445 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005446 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449/*
5450 * :[N]bunload[!] [N] [bufname] unload buffer
5451 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5452 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5453 */
5454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005455ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005457 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005458 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 eap->errmsg = do_bufdel(
5460 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5461 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5462 : DOBUF_UNLOAD, eap->arg,
5463 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5464}
5465
5466/*
5467 * :[N]buffer [N] to buffer N
5468 * :[N]sbuffer [N] to buffer N
5469 */
5470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005471ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005473 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005474 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (*eap->arg)
5476 eap->errmsg = e_trailing;
5477 else
5478 {
5479 if (eap->addr_count == 0) /* default is current buffer */
5480 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5481 else
5482 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005483 if (eap->do_ecmd_cmd != NULL)
5484 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486}
5487
5488/*
5489 * :[N]bmodified [N] to next mod. buffer
5490 * :[N]sbmodified [N] to next mod. buffer
5491 */
5492 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005496 if (eap->do_ecmd_cmd != NULL)
5497 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498}
5499
5500/*
5501 * :[N]bnext [N] to next buffer
5502 * :[N]sbnext [N] split and to next buffer
5503 */
5504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
5507 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005508 if (eap->do_ecmd_cmd != NULL)
5509 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510}
5511
5512/*
5513 * :[N]bNext [N] to previous buffer
5514 * :[N]bprevious [N] to previous buffer
5515 * :[N]sbNext [N] split and to previous buffer
5516 * :[N]sbprevious [N] split and to previous buffer
5517 */
5518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520{
5521 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005522 if (eap->do_ecmd_cmd != NULL)
5523 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524}
5525
5526/*
5527 * :brewind to first buffer
5528 * :bfirst to first buffer
5529 * :sbrewind split and to first buffer
5530 * :sbfirst split and to first buffer
5531 */
5532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005536 if (eap->do_ecmd_cmd != NULL)
5537 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538}
5539
5540/*
5541 * :blast to last buffer
5542 * :sblast split and to last buffer
5543 */
5544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005545ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546{
5547 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005548 if (eap->do_ecmd_cmd != NULL)
5549 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 return (c == NUL || c == '|' || c == '"' || c == '\n');
5556}
5557
5558#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5559 || defined(PROTO)
5560/*
5561 * Return the next command, after the first '|' or '\n'.
5562 * Return NULL if not found.
5563 */
5564 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005565find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566{
5567 while (*p != '|' && *p != '\n')
5568 {
5569 if (*p == NUL)
5570 return NULL;
5571 ++p;
5572 }
5573 return (p + 1);
5574}
5575#endif
5576
5577/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005578 * Check if *p is a separator between Ex commands, skipping over white space.
5579 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 */
5581 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005582check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005584 char_u *s = skipwhite(p);
5585
5586 if (*s == '|' || *s == '\n')
5587 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 else
5589 return NULL;
5590}
5591
5592/*
5593 * - if there are more files to edit
5594 * - and this is the last window
5595 * - and forceit not used
5596 * - and not repeated twice on a row
5597 * return FAIL and give error message if 'message' TRUE
5598 * return OK otherwise
5599 */
5600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005601check_more(
5602 int message, /* when FALSE check only, no messages */
5603 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5606
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005607 if (!forceit && only_one_window()
5608 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
5610 if (message)
5611 {
5612#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5613 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5614 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005615 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005617 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5618 NGETTEXT("%d more file to edit. Quit anyway?",
5619 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5621 return OK;
5622 return FAIL;
5623 }
5624#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005625 semsg(NGETTEXT("E173: %d more file to edit",
5626 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 quitmore = 2; /* next try to quit is allowed */
5628 }
5629 return FAIL;
5630 }
5631 return OK;
5632}
5633
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005634#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635/*
5636 * Function given to ExpandGeneric() to obtain the list of command names.
5637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 return cmdnames[idx].cmd_name;
5644}
5645#endif
5646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
Bram Moolenaare6850792010-05-14 15:28:44 +02005650 if (*eap->arg == NUL)
5651 {
5652#ifdef FEAT_EVAL
5653 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5654 char_u *p = NULL;
5655
5656 if (expr != NULL)
5657 {
5658 ++emsg_off;
5659 p = eval_to_string(expr, NULL, FALSE);
5660 --emsg_off;
5661 vim_free(expr);
5662 }
5663 if (p != NULL)
5664 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005665 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005666 vim_free(p);
5667 }
5668 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005669 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005670#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005671 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005672#endif
5673 }
5674 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005675 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005676
5677#ifdef FEAT_VTP
5678 else if (has_vtp_working())
5679 {
5680 // background color change requires clear + redraw
5681 update_screen(CLEAR);
5682 redrawcmd();
5683 }
5684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
5686
5687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005688ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005691 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 do_highlight(eap->arg, eap->forceit, FALSE);
5693}
5694
5695
5696/*
5697 * Call this function if we thought we were going to exit, but we won't
5698 * (because of an error). May need to restore the terminal mode.
5699 */
5700 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005701not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 exiting = FALSE;
5704 settmode(TMODE_RAW);
5705}
5706
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005707 static int
5708before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5709{
5710 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5711
5712 /* Bail out when autocommands closed the window.
5713 * Refuse to quit when the buffer in the last window is being closed (can
5714 * only happen in autocommands). */
5715 if (!win_valid(wp)
5716 || curbuf_locked()
5717 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5718 return TRUE;
5719
5720 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5721 {
5722 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5723 /* Refuse to quit when locked or when the buffer in the last window is
5724 * being closed (can only happen in autocommands). */
5725 if (curbuf_locked()
5726 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5727 return TRUE;
5728 }
5729
5730 return FALSE;
5731}
5732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005734 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005735 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 */
5737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005740 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742#ifdef FEAT_CMDWIN
5743 if (cmdwin_type != 0)
5744 {
5745 cmdwin_result = Ctrl_C;
5746 return;
5747 }
5748#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005749 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005750 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005751 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005752 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005753 return;
5754 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005755 if (eap->addr_count > 0)
5756 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005757 int wnr = eap->line2;
5758
5759 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5760 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005761 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005762 }
5763 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005764 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005765
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005766 /* Refuse to quit when locked. */
5767 if (curbuf_locked())
5768 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005769
5770 /* Trigger QuitPre and maybe ExitPre */
5771 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005772 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774#ifdef FEAT_NETBEANS_INTG
5775 netbeansForcedQuit = eap->forceit;
5776#endif
5777
5778 /*
5779 * If there are more files or windows we won't exit.
5780 */
5781 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5782 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005783 if ((!buf_hide(wp->w_buffer)
5784 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005785 | (eap->forceit ? CCGD_FORCEIT : 0)
5786 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005788 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 not_exiting();
5791 }
5792 else
5793 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005794 /* quit last window
5795 * Note: only_one_window() returns true, even so a help window is
5796 * still open. In that case only quit, if no address has been
5797 * specified. Example:
5798 * :h|wincmd w|1q - don't quit
5799 * :h|wincmd w|q - quit
5800 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005801 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005803 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005804#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005808 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 }
5810}
5811
5812/*
5813 * ":cquit".
5814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005816ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
5818 getout(1); /* this does not always pass on the exit code to the Manx
5819 compiler. why? */
5820}
5821
5822/*
5823 * ":qall": try to quit all windows
5824 */
5825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005826ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828# ifdef FEAT_CMDWIN
5829 if (cmdwin_type != 0)
5830 {
5831 if (eap->forceit)
5832 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5833 else
5834 cmdwin_result = K_XF2;
5835 return;
5836 }
5837# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005838
5839 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005840 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005841 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005842 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005843 return;
5844 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005845
5846 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005847 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005850 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 getout(0);
5852 not_exiting();
5853}
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855/*
5856 * ":close": close current window, unless it is the last one
5857 */
5858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005859ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005861 win_T *win;
5862 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005863#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005865 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005867#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005868 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005869 {
5870 if (eap->addr_count == 0)
5871 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005872 else
5873 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005874 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005875 {
5876 winnr++;
5877 if (winnr == eap->line2)
5878 break;
5879 }
5880 if (win == NULL)
5881 win = lastwin;
5882 ex_win_close(eap->forceit, win, NULL);
5883 }
5884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885}
5886
Bram Moolenaar4033c552017-09-16 20:54:51 +02005887#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005888/*
5889 * ":pclose": Close any preview window.
5890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005892ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893{
5894 win_T *win;
5895
Bram Moolenaar79648732019-07-18 21:43:07 +02005896 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005897 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005898 if (win->w_p_pvw)
5899 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005900 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005901 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005902 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005903# ifdef FEAT_TEXT_PROP
5904 // Also when 'previewpopup' is empty, it might have been cleared.
5905 popup_close_preview();
5906# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005907}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005908#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005909
Bram Moolenaarf740b292006-02-16 22:11:02 +00005910/*
5911 * Close window "win" and take care of handling closing the last window for a
5912 * modified buffer.
5913 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005915ex_win_close(
5916 int forceit,
5917 win_T *win,
5918 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919{
5920 int need_hide;
5921 buf_T *buf = win->w_buffer;
5922
5923 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005924 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005926#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 if ((p_confirm || cmdmod.confirm) && p_write)
5928 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005929 bufref_T bufref;
5930
5931 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005933 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 return;
5935 need_hide = FALSE;
5936 }
5937 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005940 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 return;
5942 }
5943 }
5944
Bram Moolenaar4033c552017-09-16 20:54:51 +02005945#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005947#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005948
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005950 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005951 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005952 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005953 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954}
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005957 * Handle the argument for a tabpage related ex command.
5958 * Returns a tabpage number.
5959 * When an error is encountered then eap->errmsg is set.
5960 */
5961 static int
5962get_tabpage_arg(exarg_T *eap)
5963{
5964 int tab_number;
5965 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5966
5967 if (eap->arg && *eap->arg != NUL)
5968 {
5969 char_u *p = eap->arg;
5970 char_u *p_save;
5971 int relative = 0; /* argument +N/-N means: go to N places to the
5972 * right/left relative to the current position. */
5973
5974 if (*p == '-')
5975 {
5976 relative = -1;
5977 p++;
5978 }
5979 else if (*p == '+')
5980 {
5981 relative = 1;
5982 p++;
5983 }
5984
5985 p_save = p;
5986 tab_number = getdigits(&p);
5987
5988 if (relative == 0)
5989 {
5990 if (STRCMP(p, "$") == 0)
5991 tab_number = LAST_TAB_NR;
5992 else if (p == p_save || *p_save == '-' || *p != NUL
5993 || tab_number > LAST_TAB_NR)
5994 {
5995 /* No numbers as argument. */
5996 eap->errmsg = e_invarg;
5997 goto theend;
5998 }
5999 }
6000 else
6001 {
6002 if (*p_save == NUL)
6003 tab_number = 1;
6004 else if (p == p_save || *p_save == '-' || *p != NUL
6005 || tab_number == 0)
6006 {
6007 /* No numbers as argument. */
6008 eap->errmsg = e_invarg;
6009 goto theend;
6010 }
6011 tab_number = tab_number * relative + tabpage_index(curtab);
6012 if (!unaccept_arg0 && relative == -1)
6013 --tab_number;
6014 }
6015 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6016 eap->errmsg = e_invarg;
6017 }
6018 else if (eap->addr_count > 0)
6019 {
6020 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006021 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006022 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006023 tab_number = 0;
6024 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006025 else
6026 {
6027 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006028 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006029 {
6030 --tab_number;
6031 if (tab_number < unaccept_arg0)
6032 eap->errmsg = e_invarg;
6033 }
6034 }
6035 }
6036 else
6037 {
6038 switch (eap->cmdidx)
6039 {
6040 case CMD_tabnext:
6041 tab_number = tabpage_index(curtab) + 1;
6042 if (tab_number > LAST_TAB_NR)
6043 tab_number = 1;
6044 break;
6045 case CMD_tabmove:
6046 tab_number = LAST_TAB_NR;
6047 break;
6048 default:
6049 tab_number = tabpage_index(curtab);
6050 }
6051 }
6052
6053theend:
6054 return tab_number;
6055}
6056
6057/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006058 * ":tabclose": close current tab page, unless it is the last one.
6059 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 */
6061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006062ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006064 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006065 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006066
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006067# ifdef FEAT_CMDWIN
6068 if (cmdwin_type != 0)
6069 cmdwin_result = K_IGNORE;
6070 else
6071# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006072 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006073 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006076 tab_number = get_tabpage_arg(eap);
6077 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006078 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006079 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006080 if (tp == NULL)
6081 {
6082 beep_flush();
6083 return;
6084 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006085 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006086 {
6087 tabpage_close_other(tp, eap->forceit);
6088 return;
6089 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006090 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006091 tabpage_close(eap->forceit);
6092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006094}
6095
6096/*
6097 * ":tabonly": close all tab pages except the current one
6098 */
6099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006100ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006101{
6102 tabpage_T *tp;
6103 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006104 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006105
6106# ifdef FEAT_CMDWIN
6107 if (cmdwin_type != 0)
6108 cmdwin_result = K_IGNORE;
6109 else
6110# endif
6111 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006112 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006113 else
6114 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006115 tab_number = get_tabpage_arg(eap);
6116 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006117 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006118 goto_tabpage(tab_number);
6119 /* Repeat this up to a 1000 times, because autocommands may
6120 * mess up the lists. */
6121 for (done = 0; done < 1000; ++done)
6122 {
6123 FOR_ALL_TABPAGES(tp)
6124 if (tp->tp_topframe != topframe)
6125 {
6126 tabpage_close_other(tp, eap->forceit);
6127 /* if we failed to close it quit */
6128 if (valid_tabpage(tp))
6129 done = 1000;
6130 /* start over, "tp" is now invalid */
6131 break;
6132 }
6133 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006134 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006135 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006136 }
6137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006141 * Close the current tab page.
6142 */
6143 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006144tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006145{
6146 /* First close all the windows but the current one. If that worked then
6147 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006148 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006149 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006150 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006151 ex_win_close(forceit, curwin, NULL);
6152# ifdef FEAT_GUI
6153 need_mouse_correct = TRUE;
6154# endif
6155}
6156
6157/*
6158 * Close tab page "tp", which is not the current tab page.
6159 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006160 * Also takes care of the tab pages line disappearing when closing the
6161 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006162 */
6163 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006164tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006165{
6166 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006167 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006168 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006169
6170 /* Limit to 1000 windows, autocommands may add a window while we close
6171 * one. OK, so I'm paranoid... */
6172 while (++done < 1000)
6173 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006174 wp = tp->tp_firstwin;
6175 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006176
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006177 /* Autocommands may delete the tab page under our fingers and we may
6178 * fail to close a window with a modified buffer. */
6179 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006180 break;
6181 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006182
Bram Moolenaar29323592016-07-24 22:04:11 +02006183 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006184
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006185 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006186 if (h != tabline_height())
6187 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006188}
6189
6190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 * ":only".
6192 */
6193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006194ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006196 win_T *wp;
6197 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198# ifdef FEAT_GUI
6199 need_mouse_correct = TRUE;
6200# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006201 if (eap->addr_count > 0)
6202 {
6203 wnr = eap->line2;
6204 for (wp = firstwin; --wnr > 0; )
6205 {
6206 if (wp->w_next == NULL)
6207 break;
6208 else
6209 wp = wp->w_next;
6210 }
6211 win_goto(wp);
6212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 close_others(TRUE, eap->forceit);
6214}
6215
6216/*
6217 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006218 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006221ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 if (eap->addr_count == 0)
6224 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006225 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227
6228 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006229ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006231 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006232 if (!eap->skip)
6233 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006234#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006235 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006236#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006237 if (eap->addr_count == 0)
6238 win_close(curwin, FALSE); /* don't free buffer */
6239 else
6240 {
6241 int winnr = 0;
6242 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006243
Bram Moolenaar2256c992016-11-15 21:17:07 +01006244 FOR_ALL_WINDOWS(win)
6245 {
6246 winnr++;
6247 if (winnr == eap->line2)
6248 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006249 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006250 if (win == NULL)
6251 win = lastwin;
6252 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 }
6255}
6256
6257/*
6258 * ":stop" and ":suspend": Suspend Vim.
6259 */
6260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006261ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 /*
6264 * Disallow suspending for "rvim".
6265 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006266 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
6268 if (!eap->forceit)
6269 autowrite_all();
6270 windgoto((int)Rows - 1, 0);
6271 out_char('\n');
6272 out_flush();
6273 stoptermcap();
6274 out_flush(); /* needed for SUN to restore xterm buffer */
6275#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006276 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277#endif
6278 ui_suspend(); /* call machine specific function */
6279#ifdef FEAT_TITLE
6280 maketitle();
6281 resettitle(); /* force updating the title */
6282#endif
6283 starttermcap();
6284 scroll_start(); /* scroll screen before redrawing */
6285 redraw_later_clear();
6286 shell_resized(); /* may have resized window */
6287 }
6288}
6289
6290/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006291 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 */
6293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006294ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
6296#ifdef FEAT_CMDWIN
6297 if (cmdwin_type != 0)
6298 {
6299 cmdwin_result = Ctrl_C;
6300 return;
6301 }
6302#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006303 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006304 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006305 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006306 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006307 return;
6308 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006309
6310 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006311 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312
6313 /*
6314 * if more files or windows we won't exit
6315 */
6316 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6317 exiting = TRUE;
6318 if ( ((eap->cmdidx == CMD_wq
6319 || curbufIsChanged())
6320 && do_write(eap) == FAIL)
6321 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006322 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 {
6324 not_exiting();
6325 }
6326 else
6327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006330 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331# ifdef FEAT_GUI
6332 need_mouse_correct = TRUE;
6333# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006334 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006335 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 }
6337}
6338
6339/*
6340 * ":print", ":list", ":number".
6341 */
6342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006343ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006345 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006346 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006347 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006349 for ( ;!got_int; ui_breakcheck())
6350 {
6351 print_line(eap->line1,
6352 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6353 || (eap->flags & EXFLAG_NR)),
6354 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6355 if (++eap->line1 > eap->line2)
6356 break;
6357 out_flush(); /* show one line at a time */
6358 }
6359 setpcmark();
6360 /* put cursor at last line */
6361 curwin->w_cursor.lnum = eap->line2;
6362 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
6364
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366}
6367
6368#ifdef FEAT_BYTEOFF
6369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006370ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371{
6372 goto_byte(eap->line2);
6373}
6374#endif
6375
6376/*
6377 * ":shell".
6378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006380ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381{
6382 do_shell(NULL, 0);
6383}
6384
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006385#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006387static int drop_busy = FALSE;
6388static int drop_filec;
6389static char_u **drop_filev = NULL;
6390static int drop_split;
6391static void (*drop_callback)(void *);
6392static void *drop_cookie;
6393
6394 static void
6395handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 exarg_T ea;
6398 int save_msg_scroll = msg_scroll;
6399
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006400 // Setting the argument list may cause screen updates and being called
6401 // recursively. Avoid that by setting drop_busy.
6402 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403
6404 /* Check whether the current buffer is changed. If so, we will need
6405 * to split the current window or data could be lost.
6406 * We don't need to check if the 'hidden' option is set, as in this
6407 * case the buffer won't be lost.
6408 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006409 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 {
6411 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006412 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 --emsg_off;
6414 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006415 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (win_split(0, 0) == FAIL)
6418 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006419 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421 /* When splitting the window, create a new alist. Otherwise the
6422 * existing one is overwritten. */
6423 alist_unlink(curwin->w_alist);
6424 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 }
6426
6427 /*
6428 * Set up the new argument list.
6429 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006430 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 /*
6433 * Move to the first file.
6434 */
6435 /* Fake up a minimal "next" command for do_argfile() */
6436 vim_memset(&ea, 0, sizeof(ea));
6437 ea.cmd = (char_u *)"next";
6438 do_argfile(&ea, 0);
6439
6440 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6441 * mode that is not needed here. */
6442 need_start_insertmode = FALSE;
6443
6444 /* Restore msg_scroll, otherwise a following command may cause scrolling
6445 * unexpectedly. The screen will be redrawn by the caller, thus
6446 * msg_scroll being set by displaying a message is irrelevant. */
6447 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006448
6449 if (drop_callback != NULL)
6450 drop_callback(drop_cookie);
6451
6452 drop_filev = NULL;
6453 drop_busy = FALSE;
6454}
6455
6456/*
6457 * Handle a file drop. The code is here because a drop is *nearly* like an
6458 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006459 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006460 * code is very similar to :args and hence needs access to a lot of the static
6461 * functions in this file.
6462 *
6463 * The "filev" list must have been allocated using alloc(), as should each item
6464 * in the list. This function takes over responsibility for freeing the "filev"
6465 * list.
6466 */
6467 void
6468handle_drop(
6469 int filec, // the number of files dropped
6470 char_u **filev, // the list of files dropped
6471 int split, // force splitting the window
6472 void (*callback)(void *), // to be called after setting the argument
6473 // list
6474 void *cookie) // argument for "callback" (allocated)
6475{
6476 // Cannot handle recursive drops, finish the pending one.
6477 if (drop_busy)
6478 {
6479 FreeWild(filec, filev);
6480 vim_free(cookie);
6481 return;
6482 }
6483
6484 // When calling handle_drop() more than once in a row we only use the last
6485 // one.
6486 if (drop_filev != NULL)
6487 {
6488 FreeWild(drop_filec, drop_filev);
6489 vim_free(drop_cookie);
6490 }
6491
6492 drop_filec = filec;
6493 drop_filev = filev;
6494 drop_split = split;
6495 drop_callback = callback;
6496 drop_cookie = cookie;
6497
6498 // Postpone this when:
6499 // - editing the command line
6500 // - not possible to change the current buffer
6501 // - updating the screen
6502 // As it may change buffers and window structures that are in use and cause
6503 // freed memory to be used.
6504 if (text_locked() || curbuf_locked() || updating_screen)
6505 return;
6506
6507 handle_drop_internal();
6508}
6509
6510/*
6511 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6512 * reset: Handle a postponed drop.
6513 */
6514 void
6515handle_any_postponed_drop(void)
6516{
6517 if (!drop_busy && drop_filev != NULL
6518 && !text_locked() && !curbuf_locked() && !updating_screen)
6519 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520}
6521#endif
6522
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523/*
6524 * Clear an argument list: free all file names and reset it to zero entries.
6525 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006526 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006527alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 while (--al->al_ga.ga_len >= 0)
6530 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6531 ga_clear(&al->al_ga);
6532}
6533
6534/*
6535 * Init an argument list.
6536 */
6537 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006538alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6541}
6542
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543/*
6544 * Remove a reference from an argument list.
6545 * Ignored when the argument list is the global one.
6546 * If the argument list is no longer used by any window, free it.
6547 */
6548 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006549alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550{
6551 if (al != &global_alist && --al->al_refcount <= 0)
6552 {
6553 alist_clear(al);
6554 vim_free(al);
6555 }
6556}
6557
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558/*
6559 * Create a new argument list and use it for the current window.
6560 */
6561 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006562alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006564 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 if (curwin->w_alist == NULL)
6566 {
6567 curwin->w_alist = &global_alist;
6568 ++global_alist.al_refcount;
6569 }
6570 else
6571 {
6572 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006573 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 alist_init(curwin->w_alist);
6575 }
6576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006578#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579/*
6580 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006581 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6582 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 */
6584 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006585alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
6587 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006588 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 char_u **new_arg_files;
6590 int new_arg_file_count;
6591 char_u *save_p_su = p_su;
6592 int i;
6593
6594 /* Don't use 'suffixes' here. This should work like the shell did the
6595 * expansion. Also, the vimrc file isn't read yet, thus the user
6596 * can't set the options. */
6597 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006598 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 if (old_arg_files != NULL)
6600 {
6601 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006602 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6603 old_arg_count = GARGCOUNT;
6604 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006606 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 && new_arg_file_count > 0)
6608 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006609 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6610 TRUE, fnum_list, fnum_len);
6611 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 }
6614 p_su = save_p_su;
6615}
6616#endif
6617
6618/*
6619 * Set the argument list for the current window.
6620 * Takes over the allocated files[] and the allocated fnames in it.
6621 */
6622 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006623alist_set(
6624 alist_T *al,
6625 int count,
6626 char_u **files,
6627 int use_curbuf,
6628 int *fnum_list,
6629 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006632 static int recursive = 0;
6633
6634 if (recursive)
6635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006636 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006637 return;
6638 }
6639 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640
6641 alist_clear(al);
6642 if (ga_grow(&al->al_ga, count) == OK)
6643 {
6644 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006645 {
6646 if (got_int)
6647 {
6648 /* When adding many buffers this can take a long time. Allow
6649 * interrupting here. */
6650 while (i < count)
6651 vim_free(files[i++]);
6652 break;
6653 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006654
6655 /* May set buffer name of a buffer previously used for the
6656 * argument list, so that it's re-used by alist_add. */
6657 if (fnum_list != NULL && i < fnum_len)
6658 buf_set_name(fnum_list[i], files[i]);
6659
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006661 ui_breakcheck();
6662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 vim_free(files);
6664 }
6665 else
6666 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006669
6670 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671}
6672
6673/*
6674 * Add file "fname" to argument list "al".
6675 * "fname" must have been allocated and "al" must have been checked for room.
6676 */
6677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006678alist_add(
6679 alist_T *al,
6680 char_u *fname,
6681 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682{
6683 if (fname == NULL) /* don't add NULL file names */
6684 return;
6685#ifdef BACKSLASH_IN_FILENAME
6686 slash_adjust(fname);
6687#endif
6688 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6689 if (set_fnum > 0)
6690 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6691 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6692 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693}
6694
6695#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6696/*
6697 * Adjust slashes in file names. Called after 'shellslash' was set.
6698 */
6699 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006700alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006704 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706 for (i = 0; i < GARGCOUNT; ++i)
6707 if (GARGLIST[i].ae_fname != NULL)
6708 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006709 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 if (wp->w_alist != &global_alist)
6711 for (i = 0; i < WARGCOUNT(wp); ++i)
6712 if (WARGLIST(wp)[i].ae_fname != NULL)
6713 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714}
6715#endif
6716
6717/*
6718 * ":preserve".
6719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006721ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006723 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 ml_preserve(curbuf, TRUE);
6725}
6726
6727/*
6728 * ":recover".
6729 */
6730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006731ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6734 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006735 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6736 | CCGD_MULTWIN
6737 | (eap->forceit ? CCGD_FORCEIT : 0)
6738 | CCGD_EXCMD)
6739
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 && (*eap->arg == NUL
6741 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006742 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 recoverymode = FALSE;
6744}
6745
6746/*
6747 * Command modifier used in a wrong way.
6748 */
6749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006750ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752 eap->errmsg = e_invcmd;
6753}
6754
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755/*
6756 * :sview [+command] file split window with new file, read-only
6757 * :split [[+command] file] split window with current or new file
6758 * :vsplit [[+command] file] split window vertically with current or new file
6759 * :new [[+command] file] split window with no or new file
6760 * :vnew [[+command] file] split vertically window with no or new file
6761 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006762 *
6763 * :tabedit open new Tab page with empty window
6764 * :tabedit [+command] file open new Tab page and edit "file"
6765 * :tabnew [[+command] file] just like :tabedit
6766 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 */
6768 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006769ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006771 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006772#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006774#endif
6775#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006777#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006778 int use_tab = eap->cmdidx == CMD_tabedit
6779 || eap->cmdidx == CMD_tabfind
6780 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006782 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006783 return;
6784
Bram Moolenaar4033c552017-09-16 20:54:51 +02006785#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788
Bram Moolenaar4033c552017-09-16 20:54:51 +02006789#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006791 * quickfix windows. But it's OK when doing ":tab split". */
6792 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 if (eap->cmdidx == CMD_split)
6795 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 if (eap->cmdidx == CMD_vsplit)
6797 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800
Bram Moolenaar4033c552017-09-16 20:54:51 +02006801#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006802 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
6804 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6805 FNAME_MESS, TRUE, curbuf->b_ffname);
6806 if (fname == NULL)
6807 goto theend;
6808 eap->arg = fname;
6809 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006810# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006811 else
6812# endif
6813#endif
6814#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 && eap->cmdidx != CMD_new)
6818 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006819 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006820# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006821 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006822# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006823 au_has_group((char_u *)"FileExplorer"))
6824 {
6825 /* No browsing supported but we do have the file explorer:
6826 * Edit the directory. */
6827 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6828 eap->arg = (char_u *)".";
6829 }
6830 else
6831 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006832 fname = do_browse(0, (char_u *)(use_tab
6833 ? _("Edit File in new tab page")
6834 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006836 if (fname == NULL)
6837 goto theend;
6838 eap->arg = fname;
6839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
6841 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006844 /*
6845 * Either open new tab page or split the window.
6846 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006847 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006848 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006849 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6850 : eap->addr_count == 0 ? 0
6851 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006852 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006853 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006854
6855 /* set the alternate buffer for the window we came from */
6856 if (curwin != old_curwin
6857 && win_valid(old_curwin)
6858 && old_curwin->w_buffer != curbuf
6859 && !cmdmod.keepalt)
6860 old_curwin->w_alt_fnum = curbuf->b_fnum;
6861 }
6862 }
6863 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6865 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 /* Reset 'scrollbind' when editing another file, but keep it when
6867 * doing ":split" without arguments. */
6868 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006869# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006873 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 else
6875 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 do_exedit(eap, old_curwin);
6877 }
6878
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006883# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884theend:
6885 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006888
6889/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006890 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006891 */
6892 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006893tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006894{
6895 exarg_T ea;
6896
6897 vim_memset(&ea, 0, sizeof(ea));
6898 ea.cmdidx = CMD_tabnew;
6899 ea.cmd = (char_u *)"tabn";
6900 ea.arg = (char_u *)"";
6901 ex_splitview(&ea);
6902}
6903
6904/*
6905 * :tabnext command
6906 */
6907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006908ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006909{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006910 int tab_number;
6911
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006912 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006913 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006914 switch (eap->cmdidx)
6915 {
6916 case CMD_tabfirst:
6917 case CMD_tabrewind:
6918 goto_tabpage(1);
6919 break;
6920 case CMD_tablast:
6921 goto_tabpage(9999);
6922 break;
6923 case CMD_tabprevious:
6924 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006925 if (eap->arg && *eap->arg != NUL)
6926 {
6927 char_u *p = eap->arg;
6928 char_u *p_save = p;
6929
6930 tab_number = getdigits(&p);
6931 if (p == p_save || *p_save == '-' || *p != NUL
6932 || tab_number == 0)
6933 {
6934 /* No numbers as argument. */
6935 eap->errmsg = e_invarg;
6936 return;
6937 }
6938 }
6939 else
6940 {
6941 if (eap->addr_count == 0)
6942 tab_number = 1;
6943 else
6944 {
6945 tab_number = eap->line2;
6946 if (tab_number < 1)
6947 {
6948 eap->errmsg = e_invrange;
6949 return;
6950 }
6951 }
6952 }
6953 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006954 break;
6955 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006956 tab_number = get_tabpage_arg(eap);
6957 if (eap->errmsg == NULL)
6958 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006959 break;
6960 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006961}
6962
6963/*
6964 * :tabmove command
6965 */
6966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006967ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006968{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006969 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006970
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006971 tab_number = get_tabpage_arg(eap);
6972 if (eap->errmsg == NULL)
6973 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006974}
6975
6976/*
6977 * :tabs command: List tabs and their contents.
6978 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006980ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006981{
6982 tabpage_T *tp;
6983 win_T *wp;
6984 int tabcount = 1;
6985
6986 msg_start();
6987 msg_scroll = TRUE;
6988 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6989 {
6990 msg_putchar('\n');
6991 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006992 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006993 out_flush(); /* output one line at a time */
6994 ui_breakcheck();
6995
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006996 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006997 wp = firstwin;
6998 else
6999 wp = tp->tp_firstwin;
7000 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7001 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007002 msg_putchar('\n');
7003 msg_putchar(wp == curwin ? '>' : ' ');
7004 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007005 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7006 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007007 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007008 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007009 else
7010 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7011 IObuff, IOSIZE, TRUE);
7012 msg_outtrans(IObuff);
7013 out_flush(); /* output one line at a time */
7014 ui_breakcheck();
7015 }
7016 }
7017}
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019/*
7020 * ":mode": Set screen mode.
7021 * If no argument given, just get the screen size and redraw.
7022 */
7023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007024ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 if (*eap->arg == NUL)
7027 shell_resized();
7028 else
7029 mch_screenmode(eap->arg);
7030}
7031
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032/*
7033 * ":resize".
7034 * set, increment or decrement current window height
7035 */
7036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007037ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 int n;
7040 win_T *wp = curwin;
7041
7042 if (eap->addr_count > 0)
7043 {
7044 n = eap->line2;
7045 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7046 ;
7047 }
7048
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007049# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007051# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if (cmdmod.split & WSP_VERT)
7054 {
7055 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007056 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7058 n = 9999;
7059 win_setwidth_win((int)n, wp);
7060 }
7061 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
7063 if (*eap->arg == '-' || *eap->arg == '+')
7064 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007065 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 n = 9999;
7067 win_setheight_win((int)n, wp);
7068 }
7069}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
7071/*
7072 * ":find [+command] <file>" command.
7073 */
7074 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007075ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076{
7077#ifdef FEAT_SEARCHPATH
7078 char_u *fname;
7079 int count;
7080
7081 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7082 TRUE, curbuf->b_ffname);
7083 if (eap->addr_count > 0)
7084 {
7085 /* Repeat finding the file "count" times. This matters when it
7086 * appears several times in the path. */
7087 count = eap->line2;
7088 while (fname != NULL && --count > 0)
7089 {
7090 vim_free(fname);
7091 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7092 FALSE, curbuf->b_ffname);
7093 }
7094 }
7095
7096 if (fname != NULL)
7097 {
7098 eap->arg = fname;
7099#endif
7100 do_exedit(eap, NULL);
7101#ifdef FEAT_SEARCHPATH
7102 vim_free(fname);
7103 }
7104#endif
7105}
7106
7107/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007108 * ":open" simulation: for now just work like ":visual".
7109 */
7110 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007111ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007112{
7113 regmatch_T regmatch;
7114 char_u *p;
7115
7116 curwin->w_cursor.lnum = eap->line2;
7117 beginline(BL_SOL | BL_FIX);
7118 if (*eap->arg == '/')
7119 {
7120 /* ":open /pattern/": put cursor in column found with pattern */
7121 ++eap->arg;
7122 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7123 *p = NUL;
7124 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7125 if (regmatch.regprog != NULL)
7126 {
7127 regmatch.rm_ic = p_ic;
7128 p = ml_get_curline();
7129 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007130 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007131 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007132 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007133 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007134 }
7135 /* Move to the NUL, ignore any other arguments. */
7136 eap->arg += STRLEN(eap->arg);
7137 }
7138 check_cursor();
7139
7140 eap->cmdidx = CMD_visual;
7141 do_exedit(eap, NULL);
7142}
7143
7144/*
7145 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 */
7147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007148ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 do_exedit(eap, NULL);
7151}
7152
7153/*
7154 * ":edit <file>" command and alikes.
7155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007157do_exedit(
7158 exarg_T *eap,
7159 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007163 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007165 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007166 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 /*
7168 * ":vi" command ends Ex mode.
7169 */
7170 if (exmode_active && (eap->cmdidx == CMD_visual
7171 || eap->cmdidx == CMD_view))
7172 {
7173 exmode_active = FALSE;
7174 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007175 {
7176 /* Special case: ":global/pat/visual\NLvi-commands" */
7177 if (global_busy)
7178 {
7179 int rd = RedrawingDisabled;
7180 int nwr = no_wait_return;
7181 int ms = msg_scroll;
7182#ifdef FEAT_GUI
7183 int he = hold_gui_events;
7184#endif
7185
7186 if (eap->nextcmd != NULL)
7187 {
7188 stuffReadbuff(eap->nextcmd);
7189 eap->nextcmd = NULL;
7190 }
7191
7192 if (exmode_was != EXMODE_VIM)
7193 settmode(TMODE_RAW);
7194 RedrawingDisabled = 0;
7195 no_wait_return = 0;
7196 need_wait_return = FALSE;
7197 msg_scroll = 0;
7198#ifdef FEAT_GUI
7199 hold_gui_events = 0;
7200#endif
7201 must_redraw = CLEAR;
7202
7203 main_loop(FALSE, TRUE);
7204
7205 RedrawingDisabled = rd;
7206 no_wait_return = nwr;
7207 msg_scroll = ms;
7208#ifdef FEAT_GUI
7209 hold_gui_events = he;
7210#endif
7211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
7215
7216 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007217 || eap->cmdidx == CMD_tabnew
7218 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007219 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007221 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 setpcmark();
7223 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007224 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7225 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007227 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 || *eap->arg != NUL
7229#ifdef FEAT_BROWSE
7230 || cmdmod.browse
7231#endif
7232 )
7233 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007234 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7235 * can bring us here, others are stopped earlier. */
7236 if (*eap->arg != NUL && curbuf_locked())
7237 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 n = readonlymode;
7240 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7241 readonlymode = TRUE;
7242 else if (eap->cmdidx == CMD_enew)
7243 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7244 empty buffer */
7245 setpcmark();
7246 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7247 NULL, eap,
7248 /* ":edit" goes to first line if Vi compatible */
7249 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7250 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7251 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007252 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007254 /* after a split we can use an existing buffer */
7255 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007257 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 {
7259 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 if (old_curwin != NULL)
7261 {
7262 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007263 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007265#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007266 cleanup_T cs;
7267
7268 /* Reset the error/interrupt/exception state here so that
7269 * aborting() returns FALSE when closing a window. */
7270 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007271#endif
7272#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007274#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007275 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007276
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007277#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007278 /* Restore the error/interrupt/exception state if not
7279 * discarded by a new aborting error, interrupt, or
7280 * uncaught exception. */
7281 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 }
7284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 }
7286 else if (readonlymode && curbuf->b_nwindows == 1)
7287 {
7288 /* When editing an already visited buffer, 'readonly' won't be set
7289 * but the previous value is kept. With ":view" and ":sview" we
7290 * want the file to be readonly, except when another window is
7291 * editing the same buffer. */
7292 curbuf->b_p_ro = TRUE;
7293 }
7294 readonlymode = n;
7295 }
7296 else
7297 {
7298 if (eap->do_ecmd_cmd != NULL)
7299 do_cmdline_cmd(eap->do_ecmd_cmd);
7300#ifdef FEAT_TITLE
7301 n = curwin->w_arg_idx_invalid;
7302#endif
7303 check_arg_idx(curwin);
7304#ifdef FEAT_TITLE
7305 if (n != curwin->w_arg_idx_invalid)
7306 maketitle();
7307#endif
7308 }
7309
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 /*
7311 * if ":split file" worked, set alternate file name in old window to new
7312 * file
7313 */
7314 if (old_curwin != NULL
7315 && *eap->arg != NUL
7316 && curwin != old_curwin
7317 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007318 && old_curwin->w_buffer != curbuf
7319 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
7322 ex_no_reprint = TRUE;
7323}
7324
7325#ifndef FEAT_GUI
7326/*
7327 * ":gui" and ":gvim" when there is no GUI.
7328 */
7329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007330ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 eap->errmsg = e_nogvim;
7333}
7334#endif
7335
Bram Moolenaar4f974752019-02-17 17:44:42 +01007336#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007338ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
7340 gui_make_tearoff(eap->arg);
7341}
7342#endif
7343
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007344#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7345 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007347ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007349# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7350 if (gui.in_use)
7351 gui_make_popup(eap->arg, eap->forceit);
7352# ifdef FEAT_TERM_POPUP_MENU
7353 else
7354# endif
7355# endif
7356# ifdef FEAT_TERM_POPUP_MENU
7357 pum_make_popup(eap->arg, eap->forceit);
7358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359}
7360#endif
7361
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007363ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364{
7365 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007366 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007368 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369}
7370
7371/*
7372 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7373 * offset.
7374 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007377ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007380 win_T *save_curwin = curwin;
7381 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 long topline;
7383 long y;
7384 linenr_T old_linenr = curwin->w_cursor.lnum;
7385
7386 setpcmark();
7387
7388 /*
7389 * determine max topline
7390 */
7391 if (curwin->w_p_scb)
7392 {
7393 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007394 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
7396 if (wp->w_p_scb && wp->w_buffer)
7397 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007398 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 if (topline > y)
7400 topline = y;
7401 }
7402 }
7403 if (topline < 1)
7404 topline = 1;
7405 }
7406 else
7407 {
7408 topline = 1;
7409 }
7410
7411
7412 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007413 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007415 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 {
7417 if (curwin->w_p_scb)
7418 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007419 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 y = topline - curwin->w_topline;
7421 if (y > 0)
7422 scrollup(y, TRUE);
7423 else
7424 scrolldown(-y, TRUE);
7425 curwin->w_scbind_pos = topline;
7426 redraw_later(VALID);
7427 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007431 curwin = save_curwin;
7432 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (curwin->w_p_scb)
7434 {
7435 did_syncbind = TRUE;
7436 checkpcmark();
7437 if (old_linenr != curwin->w_cursor.lnum)
7438 {
7439 char_u ctrl_o[2];
7440
7441 ctrl_o[0] = Ctrl_O;
7442 ctrl_o[1] = 0;
7443 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7444 }
7445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446}
7447
7448
7449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007450ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007452 int i;
7453 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7454 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
7456 if (eap->usefilter) /* :r!cmd */
7457 do_bang(1, eap, FALSE, FALSE, TRUE);
7458 else
7459 {
7460 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7461 return;
7462
7463#ifdef FEAT_BROWSE
7464 if (cmdmod.browse)
7465 {
7466 char_u *browseFile;
7467
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007468 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 NULL, NULL, NULL, curbuf);
7470 if (browseFile != NULL)
7471 {
7472 i = readfile(browseFile, NULL,
7473 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7474 vim_free(browseFile);
7475 }
7476 else
7477 i = OK;
7478 }
7479 else
7480#endif
7481 if (*eap->arg == NUL)
7482 {
7483 if (check_fname() == FAIL) /* check for no file name */
7484 return;
7485 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7486 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7487 }
7488 else
7489 {
7490 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7491 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7492 i = readfile(eap->arg, NULL,
7493 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7494
7495 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007496 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007498#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 if (!aborting())
7500#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007501 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 }
7503 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007504 {
7505 if (empty && exmode_active)
7506 {
7507 /* Delete the empty line that remains. Historically ex does
7508 * this but vi doesn't. */
7509 if (eap->line2 == 0)
7510 lnum = curbuf->b_ml.ml_line_count;
7511 else
7512 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007513 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007514 {
7515 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007516 if (curwin->w_cursor.lnum > 1
7517 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007518 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007519 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007520 }
7521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 }
7525}
7526
Bram Moolenaarea408852005-06-25 22:49:46 +00007527static char_u *prev_dir = NULL;
7528
7529#if defined(EXITFREE) || defined(PROTO)
7530 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007531free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007532{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007533 VIM_CLEAR(prev_dir);
7534 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007535}
7536#endif
7537
Bram Moolenaarf4258302013-06-02 18:20:17 +02007538/*
7539 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007540 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7541 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007542 */
7543 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007544post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007545{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007546 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007547 // Clear tab local directory for both :cd and :tcd
7548 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007549 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007550 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007551 {
7552 /* If still in global directory, need to remember current
7553 * directory as global directory. */
7554 if (globaldir == NULL && prev_dir != NULL)
7555 globaldir = vim_strsave(prev_dir);
7556 /* Remember this local directory for the window. */
7557 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007558 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007559 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007560 curtab->tp_localdir = vim_strsave(NameBuff);
7561 else
7562 curwin->w_localdir = vim_strsave(NameBuff);
7563 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007564 }
7565 else
7566 {
7567 /* We are now in the global directory, no need to remember its
7568 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007569 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007570 }
7571
7572 shorten_fnames(TRUE);
7573}
7574
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007575/*
7576 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7577 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7578 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7579 * Otherwise changes the global directory.
7580 * Returns TRUE if the directory is successfully changed.
7581 */
7582 int
7583changedir_func(
7584 char_u *new_dir,
7585 int forceit,
7586 cdscope_T scope)
7587{
7588 char_u *tofree;
7589 int dir_differs;
7590 int retval = FALSE;
7591
7592 if (allbuf_locked())
7593 return FALSE;
7594
7595 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7596 {
7597 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7598 return FALSE;
7599 }
7600
7601 // ":cd -": Change to previous directory
7602 if (STRCMP(new_dir, "-") == 0)
7603 {
7604 if (prev_dir == NULL)
7605 {
7606 emsg(_("E186: No previous directory"));
7607 return FALSE;
7608 }
7609 new_dir = prev_dir;
7610 }
7611
7612 // Save current directory for next ":cd -"
7613 tofree = prev_dir;
7614 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7615 prev_dir = vim_strsave(NameBuff);
7616 else
7617 prev_dir = NULL;
7618
7619#if defined(UNIX) || defined(VMS)
7620 // for UNIX ":cd" means: go to home directory
7621 if (*new_dir == NUL)
7622 {
7623 // use NameBuff for home directory name
7624# ifdef VMS
7625 char_u *p;
7626
7627 p = mch_getenv((char_u *)"SYS$LOGIN");
7628 if (p == NULL || *p == NUL) // empty is the same as not set
7629 NameBuff[0] = NUL;
7630 else
7631 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7632# else
7633 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7634# endif
7635 new_dir = NameBuff;
7636 }
7637#endif
7638 dir_differs = new_dir == NULL || prev_dir == NULL
7639 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7640 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7641 emsg(_(e_failed));
7642 else
7643 {
7644 char_u *acmd_fname;
7645
7646 post_chdir(scope);
7647
7648 if (dir_differs)
7649 {
7650 if (scope == CDSCOPE_WINDOW)
7651 acmd_fname = (char_u *)"window";
7652 else if (scope == CDSCOPE_TABPAGE)
7653 acmd_fname = (char_u *)"tabpage";
7654 else
7655 acmd_fname = (char_u *)"global";
7656 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7657 curbuf);
7658 }
7659 retval = TRUE;
7660 }
7661 vim_free(tofree);
7662
7663 return retval;
7664}
Bram Moolenaarea408852005-06-25 22:49:46 +00007665
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007667 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007670ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007672 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673
7674 new_dir = eap->arg;
7675#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007676 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 if (*new_dir == NUL)
7678 ex_pwd(NULL);
7679 else
7680#endif
7681 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007682 cdscope_T scope = CDSCOPE_GLOBAL;
7683
7684 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7685 scope = CDSCOPE_WINDOW;
7686 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7687 scope = CDSCOPE_TABPAGE;
7688
7689 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007690 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007691 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007692 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 ex_pwd(eap);
7694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 }
7696}
7697
7698/*
7699 * ":pwd".
7700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007702ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
7704 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7705 {
7706#ifdef BACKSLASH_IN_FILENAME
7707 slash_adjust(NameBuff);
7708#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007709 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
7711 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007712 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713}
7714
7715/*
7716 * ":=".
7717 */
7718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007719ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007721 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007722 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
7725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007726ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007728 int n;
7729 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730
7731 if (cursor_valid())
7732 {
7733 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7734 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007735 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007737
7738 len = eap->line2;
7739 switch (*eap->arg)
7740 {
7741 case 'm': break;
7742 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007743 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007744 }
7745 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746}
7747
7748/*
7749 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7750 */
7751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007752do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753{
7754 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007755 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756
7757 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007758 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007759 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007761 wait_now = msec - done > 1000L ? 1000L : msec - done;
7762#ifdef FEAT_TIMERS
7763 {
7764 long due_time = check_due_timer();
7765
7766 if (due_time > 0 && due_time < wait_now)
7767 wait_now = due_time;
7768 }
7769#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007770#ifdef FEAT_JOB_CHANNEL
7771 if (has_any_channel() && wait_now > 100L)
7772 wait_now = 100L;
7773#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007774 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007775#ifdef FEAT_JOB_CHANNEL
7776 if (has_any_channel())
7777 ui_breakcheck_force(TRUE);
7778 else
7779#endif
7780 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007781#ifdef MESSAGE_QUEUE
7782 /* Process the netbeans and clientserver messages that may have been
7783 * received in the call to ui_breakcheck() when the GUI is in use. This
7784 * may occur when running a test case. */
7785 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007788
7789 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7790 // input buffer, otherwise a following call to input() fails.
7791 if (got_int)
7792 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793}
7794
7795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007796do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 int mode;
7799 char_u *cmdp;
7800
7801 cmdp = eap->cmd;
7802 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7803
7804 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7805 eap->arg, mode, isabbrev))
7806 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007807 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007809 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 break;
7811 }
7812}
7813
7814/*
7815 * ":winsize" command (obsolete).
7816 */
7817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007818ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
7820 int w, h;
7821 char_u *arg = eap->arg;
7822 char_u *p;
7823
7824 w = getdigits(&arg);
7825 arg = skipwhite(arg);
7826 p = arg;
7827 h = getdigits(&arg);
7828 if (*p != NUL && *arg == NUL)
7829 set_shellsize(w, h, TRUE);
7830 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007831 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832}
7833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007835ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
7837 int xchar = NUL;
7838 char_u *p;
7839
7840 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7841 {
7842 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7843 if (eap->arg[1] == NUL)
7844 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007845 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 return;
7847 }
7848 xchar = eap->arg[1];
7849 p = eap->arg + 2;
7850 }
7851 else
7852 p = eap->arg + 1;
7853
7854 eap->nextcmd = check_nextcmd(p);
7855 p = skipwhite(p);
7856 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007857 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007858 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 {
7860 /* Pass flags on for ":vertical wincmd ]". */
7861 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007862 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7864 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007865 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 }
7867}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868
Bram Moolenaar843ee412004-06-30 16:16:41 +00007869#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870/*
7871 * ":winpos".
7872 */
7873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007874ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 int x, y;
7877 char_u *arg = eap->arg;
7878 char_u *p;
7879
7880 if (*arg == NUL)
7881 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007882# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007883# ifdef VIMDLL
7884 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7885 mch_get_winpos(&x, &y) != FAIL)
7886# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007888# else
7889 if (mch_get_winpos(&x, &y) != FAIL)
7890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {
7892 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007893 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 }
7895 else
7896# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007897 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
7899 else
7900 {
7901 x = getdigits(&arg);
7902 arg = skipwhite(arg);
7903 p = arg;
7904 y = getdigits(&arg);
7905 if (*p == NUL || *arg != NUL)
7906 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007907 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 return;
7909 }
7910# ifdef FEAT_GUI
7911 if (gui.in_use)
7912 gui_mch_set_winpos(x, y);
7913 else if (gui.starting)
7914 {
7915 /* Remember the coordinates for when the window is opened. */
7916 gui_win_x = x;
7917 gui_win_y = y;
7918 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007919# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 else
7921# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007922# endif
7923# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007924 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925# endif
7926# ifdef HAVE_TGETENT
7927 if (*T_CWP)
7928 term_set_winpos(x, y);
7929# endif
7930 }
7931}
7932#endif
7933
7934/*
7935 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7936 */
7937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007938ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939{
7940 oparg_T oa;
7941
7942 clear_oparg(&oa);
7943 oa.regname = eap->regname;
7944 oa.start.lnum = eap->line1;
7945 oa.end.lnum = eap->line2;
7946 oa.line_count = eap->line2 - eap->line1 + 1;
7947 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7950 {
7951 setpcmark();
7952 curwin->w_cursor.lnum = eap->line1;
7953 beginline(BL_SOL | BL_FIX);
7954 }
7955
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007956 if (VIsual_active)
7957 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007958
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 switch (eap->cmdidx)
7960 {
7961 case CMD_delete:
7962 oa.op_type = OP_DELETE;
7963 op_delete(&oa);
7964 break;
7965
7966 case CMD_yank:
7967 oa.op_type = OP_YANK;
7968 (void)op_yank(&oa, FALSE, TRUE);
7969 break;
7970
7971 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007972 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007974 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7975#else
7976 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007978 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 oa.op_type = OP_RSHIFT;
7980 else
7981 oa.op_type = OP_LSHIFT;
7982 op_shift(&oa, FALSE, eap->amount);
7983 break;
7984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007986 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987}
7988
7989/*
7990 * ":put".
7991 */
7992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007993ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
7995 /* ":0put" works like ":1put!". */
7996 if (eap->line2 == 0)
7997 {
7998 eap->line2 = 1;
7999 eap->forceit = TRUE;
8000 }
8001 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008002 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8003 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004}
8005
8006/*
8007 * Handle ":copy" and ":move".
8008 */
8009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008010ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
8012 long n;
8013
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008014 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (eap->arg == NULL) /* error detected */
8016 {
8017 eap->nextcmd = NULL;
8018 return;
8019 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008020 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021
8022 /*
8023 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8024 */
8025 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008027 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 return;
8029 }
8030
8031 if (eap->cmdidx == CMD_move)
8032 {
8033 if (do_move(eap->line1, eap->line2, n) == FAIL)
8034 return;
8035 }
8036 else
8037 ex_copy(eap->line1, eap->line2, n);
8038 u_clearline();
8039 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008040 ex_may_print(eap);
8041}
8042
8043/*
8044 * Print the current line if flags were given to the Ex command.
8045 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008046 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008047ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008048{
8049 if (eap->flags != 0)
8050 {
8051 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8052 (eap->flags & EXFLAG_LIST));
8053 ex_no_reprint = TRUE;
8054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055}
8056
8057/*
8058 * ":smagic" and ":snomagic".
8059 */
8060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008061ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 int magic_save = p_magic;
8064
8065 p_magic = (eap->cmdidx == CMD_smagic);
8066 do_sub(eap);
8067 p_magic = magic_save;
8068}
8069
8070/*
8071 * ":join".
8072 */
8073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008074ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 curwin->w_cursor.lnum = eap->line1;
8077 if (eap->line1 == eap->line2)
8078 {
8079 if (eap->addr_count >= 2) /* :2,2join does nothing */
8080 return;
8081 if (eap->line2 == curbuf->b_ml.ml_line_count)
8082 {
8083 beep_flush();
8084 return;
8085 }
8086 ++eap->line2;
8087 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008088 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008090 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091}
8092
8093/*
8094 * ":[addr]@r" or ":[addr]*r": execute register
8095 */
8096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008097ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008100 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101
8102 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008103 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
8105#ifdef USE_ON_FLY_SCROLL
8106 dont_scroll = TRUE; /* disallow scrolling here */
8107#endif
8108
8109 /* get the register name. No name means to use the previous one */
8110 c = *eap->arg;
8111 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8112 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008113 /* Put the register in the typeahead buffer with the "silent" flag. */
8114 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8115 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008116 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 else
8120 {
8121 int save_efr = exec_from_reg;
8122
8123 exec_from_reg = TRUE;
8124
8125 /*
8126 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008127 * Continue until the stuff buffer is empty and all added characters
8128 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008130 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8132
8133 exec_from_reg = save_efr;
8134 }
8135}
8136
8137/*
8138 * ":!".
8139 */
8140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008141ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8144}
8145
8146/*
8147 * ":undo".
8148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008150ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008152 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008153 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008154 else
8155 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156}
8157
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008158#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008160ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008161{
8162 char_u hash[UNDO_HASH_SIZE];
8163
8164 u_compute_hash(hash);
8165 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8166}
8167
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008169ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008170{
8171 char_u hash[UNDO_HASH_SIZE];
8172
8173 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008174 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008175}
8176#endif
8177
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178/*
8179 * ":redo".
8180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008182ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183{
8184 u_redo(1);
8185}
8186
8187/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008188 * ":earlier" and ":later".
8189 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008191ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008192{
8193 long count = 0;
8194 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008195 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008196 char_u *p = eap->arg;
8197
8198 if (*p == NUL)
8199 count = 1;
8200 else if (isdigit(*p))
8201 {
8202 count = getdigits(&p);
8203 switch (*p)
8204 {
8205 case 's': ++p; sec = TRUE; break;
8206 case 'm': ++p; sec = TRUE; count *= 60; break;
8207 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008208 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8209 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008210 }
8211 }
8212
8213 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008214 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008215 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008216 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8217 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008218}
8219
8220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 * ":redir": start/stop redirection.
8222 */
8223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008224ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225{
8226 char *mode;
8227 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008228 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
Bram Moolenaarba768492016-07-08 15:32:54 +02008230#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008231 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008233 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008234 return;
8235 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008236#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 if (STRICMP(eap->arg, "END") == 0)
8239 close_redir();
8240 else
8241 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008242 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008244 ++arg;
8245 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008247 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 mode = "a";
8249 }
8250 else
8251 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008252 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
8254 close_redir();
8255
8256 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008257 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 if (fname == NULL)
8259 return;
8260#ifdef FEAT_BROWSE
8261 if (cmdmod.browse)
8262 {
8263 char_u *browseFile;
8264
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008265 browseFile = do_browse(BROWSE_SAVE,
8266 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008267 fname, NULL, NULL,
8268 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 if (browseFile == NULL)
8270 return; /* operation cancelled */
8271 vim_free(fname);
8272 fname = browseFile;
8273 eap->forceit = TRUE; /* since dialog already asked */
8274 }
8275#endif
8276
8277 redir_fd = open_exfile(fname, eap->forceit, mode);
8278 vim_free(fname);
8279 }
8280#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008281 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
8283 /* redirect to a register a-z (resp. A-Z for appending) */
8284 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008285 ++arg;
8286 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008288 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008289 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008291 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008293 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008294 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008295 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008296 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008298 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008299 if (*arg == '>')
8300 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008301 /* Make register empty when not using @A-@Z and the
8302 * command is valid. */
8303 if (*arg == NUL && !isupper(redir_reg))
8304 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008306 }
8307 if (*arg != NUL)
8308 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008309 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008310 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008311 }
8312 }
8313 else if (*arg == '=' && arg[1] == '>')
8314 {
8315 int append;
8316
8317 /* redirect to a variable */
8318 close_redir();
8319 arg += 2;
8320
8321 if (*arg == '>')
8322 {
8323 ++arg;
8324 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 }
8326 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008327 append = FALSE;
8328
8329 if (var_redir_start(skipwhite(arg), append) == OK)
8330 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 }
8332#endif
8333
8334 /* TODO: redirect to a buffer */
8335
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008337 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008339
8340 /* Make sure redirection is not off. Can happen for cmdline completion
8341 * that indirectly invokes a command to catch its output. */
8342 if (redir_fd != NULL
8343#ifdef FEAT_EVAL
8344 || redir_reg || redir_vname
8345#endif
8346 )
8347 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348}
8349
8350/*
8351 * ":redraw": force redraw
8352 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008353 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008354ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355{
8356 int r = RedrawingDisabled;
8357 int p = p_lz;
8358
8359 RedrawingDisabled = 0;
8360 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008361 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008363 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364#ifdef FEAT_TITLE
8365 if (need_maketitle)
8366 maketitle();
8367#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008368#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8369# ifdef VIMDLL
8370 if (!gui.in_use)
8371# endif
8372 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 RedrawingDisabled = r;
8375 p_lz = p;
8376
8377 /* Reset msg_didout, so that a message that's there is overwritten. */
8378 msg_didout = FALSE;
8379 msg_col = 0;
8380
Bram Moolenaar56667a52013-07-17 11:54:28 +02008381 /* No need to wait after an intentional redraw. */
8382 need_wait_return = FALSE;
8383
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 out_flush();
8385}
8386
8387/*
8388 * ":redrawstatus": force redraw of status line(s)
8389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008391ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 int r = RedrawingDisabled;
8394 int p = p_lz;
8395
8396 RedrawingDisabled = 0;
8397 p_lz = FALSE;
8398 if (eap->forceit)
8399 status_redraw_all();
8400 else
8401 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008402 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 RedrawingDisabled = r;
8404 p_lz = p;
8405 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406}
8407
Bram Moolenaare12bab32019-01-08 22:02:56 +01008408/*
8409 * ":redrawtabline": force redraw of the tabline
8410 */
8411 static void
8412ex_redrawtabline(exarg_T *eap UNUSED)
8413{
8414 int r = RedrawingDisabled;
8415 int p = p_lz;
8416
8417 RedrawingDisabled = 0;
8418 p_lz = FALSE;
8419
8420 draw_tabline();
8421
8422 RedrawingDisabled = r;
8423 p_lz = p;
8424 out_flush();
8425}
8426
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008428close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429{
8430 if (redir_fd != NULL)
8431 {
8432 fclose(redir_fd);
8433 redir_fd = NULL;
8434 }
8435#ifdef FEAT_EVAL
8436 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008437 if (redir_vname)
8438 {
8439 var_redir_stop();
8440 redir_vname = 0;
8441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442#endif
8443}
8444
8445#if defined(FEAT_SESSION) && defined(USE_CRNL)
8446# define MKSESSION_NL
8447static int mksession_nl = FALSE; /* use NL only in put_eol() */
8448#endif
8449
8450/*
8451 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8452 */
8453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008454ex_mkrc(
8455 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
8457 FILE *fd;
8458 int failed = FALSE;
8459 char_u *fname;
8460#ifdef FEAT_BROWSE
8461 char_u *browseFile = NULL;
8462#endif
8463#ifdef FEAT_SESSION
8464 int view_session = FALSE;
8465 int using_vdir = FALSE; /* using 'viewdir'? */
8466 char_u *viewFile = NULL;
8467 unsigned *flagp;
8468#endif
8469
8470 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8471 {
8472#ifdef FEAT_SESSION
8473 view_session = TRUE;
8474#else
8475 ex_ni(eap);
8476 return;
8477#endif
8478 }
8479
8480#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008481 /* Use the short file name until ":lcd" is used. We also don't use the
8482 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008483 did_lcd = FALSE;
8484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8486 if (eap->cmdidx == CMD_mkview
8487 && (*eap->arg == NUL
8488 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8489 {
8490 eap->forceit = TRUE;
8491 fname = get_view_file(*eap->arg);
8492 if (fname == NULL)
8493 return;
8494 viewFile = fname;
8495 using_vdir = TRUE;
8496 }
8497 else
8498#endif
8499 if (*eap->arg != NUL)
8500 fname = eap->arg;
8501 else if (eap->cmdidx == CMD_mkvimrc)
8502 fname = (char_u *)VIMRC_FILE;
8503#ifdef FEAT_SESSION
8504 else if (eap->cmdidx == CMD_mksession)
8505 fname = (char_u *)SESSION_FILE;
8506#endif
8507 else
8508 fname = (char_u *)EXRC_FILE;
8509
8510#ifdef FEAT_BROWSE
8511 if (cmdmod.browse)
8512 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008513 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514# ifdef FEAT_SESSION
8515 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8516 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8517# endif
8518 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008519 fname, (char_u *)"vim", NULL,
8520 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 if (browseFile == NULL)
8522 goto theend;
8523 fname = browseFile;
8524 eap->forceit = TRUE; /* since dialog already asked */
8525 }
8526#endif
8527
8528#if defined(FEAT_SESSION) && defined(vim_mkdir)
8529 /* When using 'viewdir' may have to create the directory. */
8530 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008531 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532#endif
8533
8534 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8535 if (fd != NULL)
8536 {
8537#ifdef FEAT_SESSION
8538 if (eap->cmdidx == CMD_mkview)
8539 flagp = &vop_flags;
8540 else
8541 flagp = &ssop_flags;
8542#endif
8543
8544#ifdef MKSESSION_NL
8545 /* "unix" in 'sessionoptions': use NL line separator */
8546 if (view_session && (*flagp & SSOP_UNIX))
8547 mksession_nl = TRUE;
8548#endif
8549
8550 /* Write the version command for :mkvimrc */
8551 if (eap->cmdidx == CMD_mkvimrc)
8552 (void)put_line(fd, "version 6.0");
8553
8554#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008555 if (eap->cmdidx == CMD_mksession)
8556 {
8557 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8558 failed = TRUE;
8559 }
8560
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 if (eap->cmdidx != CMD_mkview)
8562#endif
8563 {
8564 /* Write setting 'compatible' first, because it has side effects.
8565 * For that same reason only do it when needed. */
8566 if (p_cp)
8567 (void)put_line(fd, "if !&cp | set cp | endif");
8568 else
8569 (void)put_line(fd, "if &cp | set nocp | endif");
8570 }
8571
8572#ifdef FEAT_SESSION
8573 if (!view_session
8574 || (eap->cmdidx == CMD_mksession
8575 && (*flagp & SSOP_OPTIONS)))
8576#endif
8577 failed |= (makemap(fd, NULL) == FAIL
8578 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8579
8580#ifdef FEAT_SESSION
8581 if (!failed && view_session)
8582 {
8583 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8584 failed = TRUE;
8585 if (eap->cmdidx == CMD_mksession)
8586 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008587 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588
Bram Moolenaard9462e32011-04-11 21:35:11 +02008589 dirnow = alloc(MAXPATHL);
8590 if (dirnow == NULL)
8591 failed = TRUE;
8592 else
8593 {
8594 /*
8595 * Change to session file's dir.
8596 */
8597 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008599 *dirnow = NUL;
8600 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8601 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008602 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008603 shorten_fnames(TRUE);
8604 }
8605 else if (*dirnow != NUL
8606 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8607 {
8608 if (mch_chdir((char *)globaldir) == 0)
8609 shorten_fnames(TRUE);
8610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
Bram Moolenaard9462e32011-04-11 21:35:11 +02008612 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613
Bram Moolenaard9462e32011-04-11 21:35:11 +02008614 /* restore original dir */
8615 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008617 {
8618 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008619 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008620 shorten_fnames(TRUE);
8621 }
8622 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 }
8624 }
8625 else
8626 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008627 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8628 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 }
8630 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8631 == FAIL)
8632 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008633# ifdef FEAT_SEARCH_EXTRA
8634 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8635 failed = TRUE;
8636# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008637 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8638 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008639 if (eap->cmdidx == CMD_mksession)
8640 {
8641 if (put_line(fd, "unlet SessionLoad") == FAIL)
8642 failed = TRUE;
8643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008646 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8647 failed = TRUE;
8648
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 failed |= fclose(fd);
8650
8651 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008652 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8654 else if (eap->cmdidx == CMD_mksession)
8655 {
8656 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008657 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658
Bram Moolenaard9462e32011-04-11 21:35:11 +02008659 tbuf = alloc(MAXPATHL);
8660 if (tbuf != NULL)
8661 {
8662 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8663 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8664 vim_free(tbuf);
8665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 }
8667#endif
8668#ifdef MKSESSION_NL
8669 mksession_nl = FALSE;
8670#endif
8671 }
8672
8673#ifdef FEAT_BROWSE
8674theend:
8675 vim_free(browseFile);
8676#endif
8677#ifdef FEAT_SESSION
8678 vim_free(viewFile);
8679#endif
8680}
8681
Bram Moolenaardf177f62005-02-22 08:39:57 +00008682#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8683 || defined(PROTO)
8684 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008685vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008686{
8687 if (vim_mkdir(name, prot) != 0)
8688 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008689 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008690 return FAIL;
8691 }
8692 return OK;
8693}
8694#endif
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696/*
8697 * Open a file for writing for an Ex command, with some checks.
8698 * Return file descriptor, or NULL on failure.
8699 */
8700 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008701open_exfile(
8702 char_u *fname,
8703 int forceit,
8704 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705{
8706 FILE *fd;
8707
8708#ifdef UNIX
8709 /* with Unix it is possible to open a directory */
8710 if (mch_isdir(fname))
8711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008712 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 return NULL;
8714 }
8715#endif
8716 if (!forceit && *mode != 'a' && vim_fexists(fname))
8717 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008718 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 return NULL;
8720 }
8721
8722 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008723 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 return fd;
8726}
8727
8728/*
8729 * ":mark" and ":k".
8730 */
8731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008732ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734 pos_T pos;
8735
8736 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008737 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008739 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 else
8741 {
8742 pos = curwin->w_cursor; /* save curwin->w_cursor */
8743 curwin->w_cursor.lnum = eap->line2;
8744 beginline(BL_WHITE | BL_FIX);
8745 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008746 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8748 }
8749}
8750
8751/*
8752 * Update w_topline, w_leftcol and the cursor position.
8753 */
8754 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008755update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756{
8757 check_cursor(); /* put cursor on valid line */
8758 update_topline();
8759 if (!curwin->w_p_wrap)
8760 validate_cursor();
8761 update_curswant();
8762}
8763
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008765 * Save the current State and go to Normal mode.
8766 * Return TRUE if the typeahead could be saved.
8767 */
8768 int
8769save_current_state(save_state_T *sst)
8770{
8771 sst->save_msg_scroll = msg_scroll;
8772 sst->save_restart_edit = restart_edit;
8773 sst->save_msg_didout = msg_didout;
8774 sst->save_State = State;
8775 sst->save_insertmode = p_im;
8776 sst->save_finish_op = finish_op;
8777 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008778 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008779
8780 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8781 restart_edit = 0; /* don't go to Insert mode */
8782 p_im = FALSE; /* don't use 'insertmode' */
8783
8784 /*
8785 * Save the current typeahead. This is required to allow using ":normal"
8786 * from an event handler and makes sure we don't hang when the argument
8787 * ends with half a command.
8788 */
8789 save_typeahead(&sst->tabuf);
8790 return sst->tabuf.typebuf_valid;
8791}
8792
8793 void
8794restore_current_state(save_state_T *sst)
8795{
8796 /* Restore the previous typeahead. */
8797 restore_typeahead(&sst->tabuf);
8798
8799 msg_scroll = sst->save_msg_scroll;
8800 restart_edit = sst->save_restart_edit;
8801 p_im = sst->save_insertmode;
8802 finish_op = sst->save_finish_op;
8803 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008804 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008805 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8806
8807 /* Restore the state (needed when called from a function executed for
8808 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8809 State = sst->save_State;
8810#ifdef CURSOR_SHAPE
8811 ui_cursor_shape(); /* may show different cursor shape */
8812#endif
8813}
8814
8815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 * ":normal[!] {commands}": Execute normal mode commands.
8817 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008818 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008819ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008821 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 char_u *arg = NULL;
8823 int l;
8824 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008826 if (ex_normal_lock > 0)
8827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008828 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008829 return;
8830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 if (ex_normal_busy >= p_mmd)
8832 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008833 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 return;
8835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 /*
8838 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8839 * this for the K_SPECIAL leading byte, otherwise special keys will not
8840 * work.
8841 */
8842 if (has_mbyte)
8843 {
8844 int len = 0;
8845
8846 /* Count the number of characters to be escaped. */
8847 for (p = eap->arg; *p != NUL; ++p)
8848 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008849#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 if (*p == CSI) /* leadbyte CSI */
8851 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008852#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008853 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008855#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 )
8859 len += 2;
8860 }
8861 if (len > 0)
8862 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008863 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 if (arg != NULL)
8865 {
8866 len = 0;
8867 for (p = eap->arg; *p != NUL; ++p)
8868 {
8869 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008870#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 if (*p == CSI)
8872 {
8873 arg[len++] = KS_EXTRA;
8874 arg[len++] = (int)KE_CSI;
8875 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008876#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008877 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 {
8879 arg[len++] = *++p;
8880 if (*p == K_SPECIAL)
8881 {
8882 arg[len++] = KS_SPECIAL;
8883 arg[len++] = KE_FILLER;
8884 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008885#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 else if (*p == CSI)
8887 {
8888 arg[len++] = KS_EXTRA;
8889 arg[len++] = (int)KE_CSI;
8890 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 }
8893 arg[len] = NUL;
8894 }
8895 }
8896 }
8897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008899 ++ex_normal_busy;
8900 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 /*
8903 * Repeat the :normal command for each line in the range. When no
8904 * range given, execute it just once, without positioning the cursor
8905 * first.
8906 */
8907 do
8908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 if (eap->addr_count != 0)
8910 {
8911 curwin->w_cursor.lnum = eap->line1++;
8912 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008913 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 }
8915
Bram Moolenaar13505972019-01-24 15:04:48 +01008916 exec_normal_cmd(arg != NULL
8917 ? arg
8918 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 }
8920 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8921 }
8922
8923 /* Might not return to the main loop when in an event handler. */
8924 update_topline_cursor();
8925
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008926 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008928#ifdef FEAT_MOUSE
8929 setmouse();
8930#endif
8931#ifdef CURSOR_SHAPE
8932 ui_cursor_shape(); /* may show different cursor shape */
8933#endif
8934
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936}
8937
8938/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008939 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 */
8941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008942ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008944 if (eap->forceit)
8945 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008946 /* cursor line can be zero on startup */
8947 if (!curwin->w_cursor.lnum)
8948 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008949 coladvance((colnr_T)MAXCOL);
8950 curwin->w_curswant = MAXCOL;
8951 curwin->w_set_curswant = FALSE;
8952 }
8953
Bram Moolenaara40c5002005-01-09 21:16:21 +00008954 /* Ignore the command when already in Insert mode. Inserting an
8955 * expression register that invokes a function can do this. */
8956 if (State & INSERT)
8957 return;
8958
Bram Moolenaar64969662005-12-14 21:59:55 +00008959 if (eap->cmdidx == CMD_startinsert)
8960 restart_edit = 'a';
8961 else if (eap->cmdidx == CMD_startreplace)
8962 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008964 restart_edit = 'V';
8965
8966 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008968 if (eap->cmdidx == CMD_startinsert)
8969 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 curwin->w_curswant = 0; /* avoid MAXCOL */
8971 }
8972}
8973
8974/*
8975 * ":stopinsert"
8976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008978ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 restart_edit = 0;
8981 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008982 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008985/*
8986 * Execute normal mode command "cmd".
8987 * "remap" can be REMAP_NONE or REMAP_YES.
8988 */
8989 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008990exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008991{
Bram Moolenaar25281632016-01-21 23:32:32 +01008992 /* Stuff the argument into the typeahead buffer. */
8993 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008994 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008995}
Bram Moolenaar25281632016-01-21 23:32:32 +01008996
Bram Moolenaar25281632016-01-21 23:32:32 +01008997/*
8998 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008999 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01009000 */
9001 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009002exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009003{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009004 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009005 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009006
Bram Moolenaar905dd902019-04-07 14:21:47 +02009007 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9008 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009009 clear_oparg(&oa);
9010 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009011 while ((!stuff_empty()
9012 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009013 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009014 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009015 {
9016 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009017#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009018 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009019 && oa.op_type == OP_NOP && oa.regname == NUL
9020 && !VIsual_active)
9021 {
9022 /* If terminal_loop() returns OK we got a key that is handled
9023 * in Normal model. With FAIL we first need to position the
9024 * cursor and the screen needs to be redrawn. */
9025 if (terminal_loop(TRUE) == OK)
9026 normal_cmd(&oa, TRUE);
9027 }
9028 else
9029#endif
9030 /* execute a Normal mode cmd */
9031 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009032 }
9033}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009034
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035#ifdef FEAT_FIND_ID
9036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009037ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
9039 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9040 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9041 (linenr_T)1, (linenr_T)MAXLNUM);
9042}
9043
Bram Moolenaar4033c552017-09-16 20:54:51 +02009044#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045/*
9046 * ":psearch"
9047 */
9048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009049ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
9051 g_do_tagpreview = p_pvh;
9052 ex_findpat(eap);
9053 g_do_tagpreview = 0;
9054}
9055#endif
9056
9057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009058ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059{
9060 int whole = TRUE;
9061 long n;
9062 char_u *p;
9063 int action;
9064
9065 switch (cmdnames[eap->cmdidx].cmd_name[2])
9066 {
9067 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9068 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9069 action = ACTION_GOTO;
9070 else
9071 action = ACTION_SHOW;
9072 break;
9073 case 'i': /* ":ilist" and ":dlist" */
9074 action = ACTION_SHOW_ALL;
9075 break;
9076 case 'u': /* ":ijump" and ":djump" */
9077 action = ACTION_GOTO;
9078 break;
9079 default: /* ":isplit" and ":dsplit" */
9080 action = ACTION_SPLIT;
9081 break;
9082 }
9083
9084 n = 1;
9085 if (vim_isdigit(*eap->arg)) /* get count */
9086 {
9087 n = getdigits(&eap->arg);
9088 eap->arg = skipwhite(eap->arg);
9089 }
9090 if (*eap->arg == '/') /* Match regexp, not just whole words */
9091 {
9092 whole = FALSE;
9093 ++eap->arg;
9094 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9095 if (*p)
9096 {
9097 *p++ = NUL;
9098 p = skipwhite(p);
9099
9100 /* Check for trailing illegal characters */
9101 if (!ends_excmd(*p))
9102 eap->errmsg = e_trailing;
9103 else
9104 eap->nextcmd = check_nextcmd(p);
9105 }
9106 }
9107 if (!eap->skip)
9108 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9109 whole, !eap->forceit,
9110 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9111 n, action, eap->line1, eap->line2);
9112}
9113#endif
9114
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115
Bram Moolenaar4033c552017-09-16 20:54:51 +02009116#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117/*
9118 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9119 */
9120 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009121ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009123 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9125}
9126
9127/*
9128 * ":pedit"
9129 */
9130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009131ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
9133 win_T *curwin_save = curwin;
9134
9135 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009136 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009137 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 do_exedit(eap, NULL);
9139 keep_help_flag = FALSE;
9140 if (curwin != curwin_save && win_valid(curwin_save))
9141 {
9142 /* Return cursor to where we were */
9143 validate_cursor();
9144 redraw_later(VALID);
9145 win_enter(curwin_save, TRUE);
9146 }
9147 g_do_tagpreview = 0;
9148}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150
9151/*
9152 * ":stag", ":stselect" and ":stjump".
9153 */
9154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009155ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157 postponed_split = -1;
9158 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009159 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9161 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009162 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164
9165/*
9166 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9167 */
9168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009169ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
9171 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9172}
9173
9174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009175ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176{
9177 int cmd;
9178
9179 switch (name[1])
9180 {
9181 case 'j': cmd = DT_JUMP; /* ":tjump" */
9182 break;
9183 case 's': cmd = DT_SELECT; /* ":tselect" */
9184 break;
9185 case 'p': cmd = DT_PREV; /* ":tprevious" */
9186 break;
9187 case 'N': cmd = DT_PREV; /* ":tNext" */
9188 break;
9189 case 'n': cmd = DT_NEXT; /* ":tnext" */
9190 break;
9191 case 'o': cmd = DT_POP; /* ":pop" */
9192 break;
9193 case 'f': /* ":tfirst" */
9194 case 'r': cmd = DT_FIRST; /* ":trewind" */
9195 break;
9196 case 'l': cmd = DT_LAST; /* ":tlast" */
9197 break;
9198 default: /* ":tag" */
9199#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009200 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009202 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 return;
9204 }
9205#endif
9206 cmd = DT_TAG;
9207 break;
9208 }
9209
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009210 if (name[0] == 'l')
9211 {
9212#ifndef FEAT_QUICKFIX
9213 ex_ni(eap);
9214 return;
9215#else
9216 cmd = DT_LTAG;
9217#endif
9218 }
9219
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9221 eap->forceit, TRUE);
9222}
9223
9224/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009225 * Check "str" for starting with a special cmdline variable.
9226 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9227 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9228 */
9229 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009230find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009231{
9232 int len;
9233 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009234 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009235 "%",
9236#define SPEC_PERC 0
9237 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009238#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009239 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009240#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009241 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009242#define SPEC_CCWORD (SPEC_CWORD + 1)
9243 "<cexpr>", /* expr under cursor */
9244#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009245 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009246#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009247 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009248#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009249 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009250#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009251 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009252#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009253 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009254#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009255 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009256#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009257 "<sflnum>", /* script file line number */
9258#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009259#ifdef FEAT_CLIENTSERVER
9260 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009261# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009262#endif
9263 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009264
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009265 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009266 {
9267 len = (int)STRLEN(spec_str[i]);
9268 if (STRNCMP(src, spec_str[i], len) == 0)
9269 {
9270 *usedlen = len;
9271 return i;
9272 }
9273 }
9274 return -1;
9275}
9276
9277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 * Evaluate cmdline variables.
9279 *
9280 * change '%' to curbuf->b_ffname
9281 * '#' to curwin->w_altfile
9282 * '<cword>' to word under the cursor
9283 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02009284 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 * '<cfile>' to path name under the cursor
9286 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009287 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 * '<afile>' to file name for autocommand
9289 * '<abuf>' to buffer number for autocommand
9290 * '<amatch>' to matching name for autocommand
9291 *
9292 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9293 * "" for error without a message) and NULL is returned.
9294 * Returns an allocated string if a valid match was found.
9295 * Returns NULL if no match was found. "usedlen" then still contains the
9296 * number of characters to skip.
9297 */
9298 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009299eval_vars(
9300 char_u *src, /* pointer into commandline */
9301 char_u *srcstart, /* beginning of valid memory for src */
9302 int *usedlen, /* characters after src that are used */
9303 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009304 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009305 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009306 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307{
9308 int i;
9309 char_u *s;
9310 char_u *result;
9311 char_u *resultbuf = NULL;
9312 int resultlen;
9313 buf_T *buf;
9314 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9315 int spec_idx;
9316#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009317 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 int skip_mod = FALSE;
9319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321
9322 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009323 if (escaped != NULL)
9324 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325
9326 /*
9327 * Check if there is something to do.
9328 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009329 spec_idx = find_cmdline_var(src, usedlen);
9330 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 {
9332 *usedlen = 1;
9333 return NULL;
9334 }
9335
9336 /*
9337 * Skip when preceded with a backslash "\%" and "\#".
9338 * Note: In "\\%" the % is also not recognized!
9339 */
9340 if (src > srcstart && src[-1] == '\\')
9341 {
9342 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009343 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 return NULL;
9345 }
9346
9347 /*
9348 * word or WORD under cursor
9349 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009350 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9351 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009353 resultlen = find_ident_under_cursor(&result,
9354 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9355 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9356 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 if (resultlen == 0)
9358 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009359 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 return NULL;
9361 }
9362 }
9363
9364 /*
9365 * '#': Alternate file name
9366 * '%': Current file name
9367 * File name under the cursor
9368 * File name for autocommand
9369 * and following modifiers
9370 */
9371 else
9372 {
9373 switch (spec_idx)
9374 {
9375 case SPEC_PERC: /* '%': current file */
9376 if (curbuf->b_fname == NULL)
9377 {
9378 result = (char_u *)"";
9379 valid = 0; /* Must have ":p:h" to be valid */
9380 }
9381 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009384#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009385 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009386#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 break;
9389
9390 case SPEC_HASH: /* '#' or "#99": alternate file */
9391 if (src[1] == '#') /* "##": the argument list */
9392 {
9393 result = arg_all();
9394 resultbuf = result;
9395 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009396 if (escaped != NULL)
9397 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398#ifdef FEAT_MODIFY_FNAME
9399 skip_mod = TRUE;
9400#endif
9401 break;
9402 }
9403 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009404 if (*s == '<') /* "#<99" uses v:oldfiles */
9405 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009407 if (s == src + 2 && src[1] == '-')
9408 /* just a minus sign, don't skip over it */
9409 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 *usedlen = (int)(s - src); /* length of what we expand */
9411
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009412 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009414 if (*usedlen < 2)
9415 {
9416 /* Should we give an error message for #<text? */
9417 *usedlen = 1;
9418 return NULL;
9419 }
9420#ifdef FEAT_EVAL
9421 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9422 (long)i);
9423 if (result == NULL)
9424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009425 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009426 return NULL;
9427 }
9428#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009429 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 }
9433 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009434 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009435 if (i == 0 && src[1] == '<' && *usedlen > 1)
9436 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009437 buf = buflist_findnr(i);
9438 if (buf == NULL)
9439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009440 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009441 return NULL;
9442 }
9443 if (lnump != NULL)
9444 *lnump = ECMD_LAST;
9445 if (buf->b_fname == NULL)
9446 {
9447 result = (char_u *)"";
9448 valid = 0; /* Must have ":p:h" to be valid */
9449 }
9450 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009451 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009452 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009453#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009454 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009455#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009456 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 break;
9459
9460#ifdef FEAT_SEARCHPATH
9461 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009462 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 if (result == NULL)
9464 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009465 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 return NULL;
9467 }
9468 resultbuf = result; /* remember allocated string */
9469 break;
9470#endif
9471
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 case SPEC_AFILE: /* file name for autocommand */
9473 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009474 if (result != NULL && !autocmd_fname_full)
9475 {
9476 /* Still need to turn the fname into a full path. It is
9477 * postponed to avoid a delay when <afile> is not used. */
9478 autocmd_fname_full = TRUE;
9479 result = FullName_save(autocmd_fname, FALSE);
9480 vim_free(autocmd_fname);
9481 autocmd_fname = result;
9482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 if (result == NULL)
9484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009485 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 return NULL;
9487 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009488 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 break;
9490
9491 case SPEC_ABUF: /* buffer number for autocommand */
9492 if (autocmd_bufnr <= 0)
9493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009494 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 return NULL;
9496 }
9497 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9498 result = strbuf;
9499 break;
9500
9501 case SPEC_AMATCH: /* match name for autocommand */
9502 result = autocmd_match;
9503 if (result == NULL)
9504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009505 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 return NULL;
9507 }
9508 break;
9509
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 case SPEC_SFILE: /* file name for ":so" command */
9511 result = sourcing_name;
9512 if (result == NULL)
9513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009514 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515 return NULL;
9516 }
9517 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009518
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009519 case SPEC_SLNUM: /* line in file for ":so" command */
9520 if (sourcing_name == NULL || sourcing_lnum == 0)
9521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009522 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009523 return NULL;
9524 }
9525 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9526 result = strbuf;
9527 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009528
9529#ifdef FEAT_EVAL
9530 case SPEC_SFLNUM: /* line in script file */
9531 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9532 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009533 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009534 return NULL;
9535 }
9536 sprintf((char *)strbuf, "%ld",
9537 (long)(current_sctx.sc_lnum + sourcing_lnum));
9538 result = strbuf;
9539 break;
9540#endif
9541
9542#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009544 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9545 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 result = strbuf;
9547 break;
9548#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009549
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009550 default:
9551 result = (char_u *)""; /* avoid gcc warning */
9552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 }
9554
9555 resultlen = (int)STRLEN(result); /* length of new string */
9556 if (src[*usedlen] == '<') /* remove the file name extension */
9557 {
9558 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 resultlen = (int)(s - result);
9561 }
9562#ifdef FEAT_MODIFY_FNAME
9563 else if (!skip_mod)
9564 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009565 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566 &resultlen);
9567 if (result == NULL)
9568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009569 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 return NULL;
9571 }
9572 }
9573#endif
9574 }
9575
9576 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9577 {
9578 if (valid != VALID_HEAD + VALID_PATH)
9579 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009580 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009582 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583 result = NULL;
9584 }
9585 else
9586 result = vim_strnsave(result, resultlen);
9587 vim_free(resultbuf);
9588 return result;
9589}
9590
9591/*
9592 * Concatenate all files in the argument list, separated by spaces, and return
9593 * it in one allocated string.
9594 * Spaces and backslashes in the file names are escaped with a backslash.
9595 * Returns NULL when out of memory.
9596 */
9597 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009598arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599{
9600 int len;
9601 int idx;
9602 char_u *retval = NULL;
9603 char_u *p;
9604
9605 /*
9606 * Do this loop two times:
9607 * first time: compute the total length
9608 * second time: concatenate the names
9609 */
9610 for (;;)
9611 {
9612 len = 0;
9613 for (idx = 0; idx < ARGCOUNT; ++idx)
9614 {
9615 p = alist_name(&ARGLIST[idx]);
9616 if (p != NULL)
9617 {
9618 if (len > 0)
9619 {
9620 /* insert a space in between names */
9621 if (retval != NULL)
9622 retval[len] = ' ';
9623 ++len;
9624 }
9625 for ( ; *p != NUL; ++p)
9626 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009627 if (*p == ' '
9628#ifndef BACKSLASH_IN_FILENAME
9629 || *p == '\\'
9630#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009631 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 {
9633 /* insert a backslash */
9634 if (retval != NULL)
9635 retval[len] = '\\';
9636 ++len;
9637 }
9638 if (retval != NULL)
9639 retval[len] = *p;
9640 ++len;
9641 }
9642 }
9643 }
9644
9645 /* second time: break here */
9646 if (retval != NULL)
9647 {
9648 retval[len] = NUL;
9649 break;
9650 }
9651
9652 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009653 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 if (retval == NULL)
9655 break;
9656 }
9657
9658 return retval;
9659}
9660
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661/*
9662 * Expand the <sfile> string in "arg".
9663 *
9664 * Returns an allocated string, or NULL for any error.
9665 */
9666 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009667expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009669 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 int len;
9671 char_u *result;
9672 char_u *newres;
9673 char_u *repl;
9674 int srclen;
9675 char_u *p;
9676
9677 result = vim_strsave(arg);
9678 if (result == NULL)
9679 return NULL;
9680
9681 for (p = result; *p; )
9682 {
9683 if (STRNCMP(p, "<sfile>", 7) != 0)
9684 ++p;
9685 else
9686 {
9687 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009688 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 if (errormsg != NULL)
9690 {
9691 if (*errormsg)
9692 emsg(errormsg);
9693 vim_free(result);
9694 return NULL;
9695 }
9696 if (repl == NULL) /* no match (cannot happen) */
9697 {
9698 p += srclen;
9699 continue;
9700 }
9701 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9702 newres = alloc(len);
9703 if (newres == NULL)
9704 {
9705 vim_free(repl);
9706 vim_free(result);
9707 return NULL;
9708 }
9709 mch_memmove(newres, result, (size_t)(p - result));
9710 STRCPY(newres + (p - result), repl);
9711 len = (int)STRLEN(newres);
9712 STRCAT(newres, p + srclen);
9713 vim_free(repl);
9714 vim_free(result);
9715 result = newres;
9716 p = newres + len; /* continue after the match */
9717 }
9718 }
9719
9720 return result;
9721}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722
9723#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009724static int ses_winsizes(FILE *fd, int restore_size,
9725 win_T *tab_firstwin);
9726static int ses_win_rec(FILE *fd, frame_T *fr);
9727static frame_T *ses_skipframe(frame_T *fr);
9728static int ses_do_frame(frame_T *fr);
9729static int ses_do_win(win_T *wp);
9730static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9731static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009732static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733
9734/*
9735 * Write openfile commands for the current buffers to an .exrc file.
9736 * Return FAIL on error, OK otherwise.
9737 */
9738 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009739makeopens(
9740 FILE *fd,
9741 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742{
9743 buf_T *buf;
9744 int only_save_windows = TRUE;
9745 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 int restore_size = TRUE;
9747 win_T *wp;
9748 char_u *sname;
9749 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009750 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009751 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009752 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009753 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009754 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009755 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756
9757 if (ssop_flags & SSOP_BUFFERS)
9758 only_save_windows = FALSE; /* Save ALL buffers */
9759
9760 /*
9761 * Begin by setting the this_session variable, and then other
9762 * sessionable variables.
9763 */
9764#ifdef FEAT_EVAL
9765 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9766 return FAIL;
9767 if (ssop_flags & SSOP_GLOBALS)
9768 if (store_session_globals(fd) == FAIL)
9769 return FAIL;
9770#endif
9771
9772 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009773 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774 */
9775 if (put_line(fd, "silent only") == FAIL)
9776 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009777 if ((ssop_flags & SSOP_TABPAGES)
9778 && put_line(fd, "silent tabonly") == FAIL)
9779 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780
9781 /*
9782 * Now a :cd command to the session directory or the current directory
9783 */
9784 if (ssop_flags & SSOP_SESDIR)
9785 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009786 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9787 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 return FAIL;
9789 }
9790 else if (ssop_flags & SSOP_CURDIR)
9791 {
9792 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9793 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009794 || fputs("cd ", fd) < 0
9795 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9796 || put_eol(fd) == FAIL)
9797 {
9798 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 vim_free(sname);
9802 }
9803
9804 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009805 * If there is an empty, unnamed buffer we will wipe it out later.
9806 * Remember the buffer number.
9807 */
9808 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9809 return FAIL;
9810 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9811 return FAIL;
9812 if (put_line(fd, "endif") == FAIL)
9813 return FAIL;
9814
9815 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 * Now save the current files, current buffer first.
9817 */
9818 if (put_line(fd, "set shortmess=aoO") == FAIL)
9819 return FAIL;
9820
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009822 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9824 return FAIL;
9825
9826 if (ssop_flags & SSOP_RESIZE)
9827 {
9828 /* Note: after the restore we still check it worked!*/
9829 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9830 || put_eol(fd) == FAIL)
9831 return FAIL;
9832 }
9833
9834#ifdef FEAT_GUI
9835 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9836 {
9837 int x, y;
9838
9839 if (gui_mch_get_winpos(&x, &y) == OK)
9840 {
9841 /* Note: after the restore we still check it worked!*/
9842 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9843 return FAIL;
9844 }
9845 }
9846#endif
9847
9848 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009849 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9850 * will be displayed when creating the next tab. That resizes the windows
9851 * in the first tab, which may cause problems. Set 'showtabline' to 2
9852 * temporarily to avoid that.
9853 */
9854 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9855 {
9856 if (put_line(fd, "set stal=2") == FAIL)
9857 return FAIL;
9858 restore_stal = TRUE;
9859 }
9860
9861 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009862 * May repeat putting Windows for each tab, when "tabpages" is in
9863 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009864 * Don't use goto_tabpage(), it may change directory and trigger
9865 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009867 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009868 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009869 if ((ssop_flags & SSOP_TABPAGES))
9870 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009871 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009872
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009873 // Similar to ses_win_rec() below, populate the tab pages first so
9874 // later local options won't be copied to the new tabs.
9875 FOR_ALL_TABPAGES(tp)
9876 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009877 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009878 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009879 return FAIL;
9880 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009881 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009883 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009884 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009885 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009886
Bram Moolenaar18144c82006-04-12 21:52:12 +00009887 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009889 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009890
9891 if (tp == NULL)
9892 break; /* done all tab pages */
9893 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009894 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009895 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009896 tab_topframe = topframe;
9897 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009898 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009899 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009900 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009901 tab_topframe = tp->tp_topframe;
9902 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009903 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009904 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906
Bram Moolenaar18144c82006-04-12 21:52:12 +00009907 /*
9908 * Before creating the window layout, try loading one file. If this
9909 * is aborted we don't end up with a number of useless windows.
9910 * This may have side effects! (e.g., compressed or network file).
9911 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009912 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009913 {
9914 if (ses_do_win(wp)
9915 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009916 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009917#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02009918 && !bt_nofilename(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009919#endif
9920 )
9921 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009922 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9923 return FAIL;
9924 need_tabnext = FALSE;
9925
9926 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009927 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9928 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009929 return FAIL;
9930 if (!wp->w_arg_idx_invalid)
9931 edited_win = wp;
9932 break;
9933 }
9934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009936 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009937 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009938 return FAIL;
9939
Bram Moolenaar18144c82006-04-12 21:52:12 +00009940 /*
9941 * Save current window layout.
9942 */
9943 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009945 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009947 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9948 return FAIL;
9949 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9950 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951
Bram Moolenaar18144c82006-04-12 21:52:12 +00009952 /*
9953 * Check if window sizes can be restored (no windows omitted).
9954 * Remember the window number of the current window after restoring.
9955 */
9956 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009957 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009958 {
9959 if (ses_do_win(wp))
9960 ++nr;
9961 else
9962 restore_size = FALSE;
9963 if (curwin == wp)
9964 cnr = nr;
9965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966
Bram Moolenaar18144c82006-04-12 21:52:12 +00009967 /* Go to the first window. */
9968 if (put_line(fd, "wincmd t") == FAIL)
9969 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009970
Bram Moolenaar18144c82006-04-12 21:52:12 +00009971 /*
9972 * If more than one window, see if sizes can be restored.
9973 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9974 * resized when moving between windows.
9975 * Do this before restoring the view, so that the topline and the
9976 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009977 * winminheight and winminwidth need to be set to avoid an error if the
9978 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009979 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009980 if (put_line(fd, "set winminheight=0") == FAIL
9981 || put_line(fd, "set winheight=1") == FAIL
9982 || put_line(fd, "set winminwidth=0") == FAIL
9983 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009984 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009985 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009986 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009988 // Restore the tab-local working directory if specified
9989 // Do this before the windows, so that the window-local directory can
9990 // override the tab-local directory.
9991 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9992 {
9993 if (fputs("tcd ", fd) < 0
9994 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9995 || put_eol(fd) == FAIL)
9996 return FAIL;
9997 did_lcd = TRUE;
9998 }
9999
Bram Moolenaar18144c82006-04-12 21:52:12 +000010000 /*
10001 * Restore the view of the window (options, file, cursor, etc.).
10002 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010003 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010004 {
10005 if (!ses_do_win(wp))
10006 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010007 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10008 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010009 return FAIL;
10010 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10011 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010012 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010013 }
10014
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010015 /* The argument index in the first tab page is zero, need to set it in
10016 * each window. For further tab pages it's the window where we do
10017 * "tabedit". */
10018 cur_arg_idx = next_arg_idx;
10019
Bram Moolenaar18144c82006-04-12 21:52:12 +000010020 /*
10021 * Restore cursor to the current window if it's not the first one.
10022 */
10023 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10024 || put_eol(fd) == FAIL))
10025 return FAIL;
10026
10027 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010028 * Restore window sizes again after jumping around in windows, because
10029 * the current window has a minimum size while others may not.
10030 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010031 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010032 return FAIL;
10033
Bram Moolenaar18144c82006-04-12 21:52:12 +000010034 /* Don't continue in another tab page when doing only the current one
10035 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010036 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010037 break;
10038 }
10039
10040 if (ssop_flags & SSOP_TABPAGES)
10041 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010042 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10043 || put_eol(fd) == FAIL)
10044 return FAIL;
10045 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010046 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10047 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010048
Bram Moolenaard39e2752019-01-26 20:07:38 +010010049 // Now put the remaining buffers into the buffer list.
10050 // This is near the end, so that when 'hidden' is set we don't create extra
10051 // buffers. If the buffer was already created with another command the
10052 // ":badd" will have no effect.
10053 FOR_ALL_BUFFERS(buf)
10054 {
10055 if (!(only_save_windows && buf->b_nwindows == 0)
10056 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10057#ifdef FEAT_TERMINAL
10058 // Skip terminal buffers: finished ones are not useful, others
10059 // will be resurrected and result in a new buffer.
10060 && !bt_terminal(buf)
10061#endif
10062 && buf->b_fname != NULL
10063 && buf->b_p_bl)
10064 {
10065 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10066 : buf->b_wininfo->wi_fpos.lnum) < 0
10067 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
10068 return FAIL;
10069 }
10070 }
10071
Bram Moolenaar9c102382006-05-03 21:26:49 +000010072 /*
10073 * Wipe out an empty unnamed buffer we started in.
10074 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020010075 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010076 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010077 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010078 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010079 return FAIL;
10080 if (put_line(fd, "endif") == FAIL)
10081 return FAIL;
10082 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10083 return FAIL;
10084
10085 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10086 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10087 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10088 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010010089 /* Re-apply 'winminheight' and 'winminwidth'. */
10090 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
10091 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
10092 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
10094 /*
10095 * Lastly, execute the x.vim file if it exists.
10096 */
10097 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10098 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010099 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 || put_line(fd, "endif") == FAIL)
10101 return FAIL;
10102
10103 return OK;
10104}
10105
10106 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010107ses_winsizes(
10108 FILE *fd,
10109 int restore_size,
10110 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112 int n = 0;
10113 win_T *wp;
10114
10115 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10116 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010117 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 {
10119 if (!ses_do_win(wp))
10120 continue;
10121 ++n;
10122
10123 /* restore height when not full height */
10124 if (wp->w_height + wp->w_status_height < topframe->fr_height
10125 && (fprintf(fd,
10126 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10127 n, (long)wp->w_height, Rows / 2, Rows) < 0
10128 || put_eol(fd) == FAIL))
10129 return FAIL;
10130
10131 /* restore width when not full width */
10132 if (wp->w_width < Columns && (fprintf(fd,
10133 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10134 n, (long)wp->w_width, Columns / 2, Columns) < 0
10135 || put_eol(fd) == FAIL))
10136 return FAIL;
10137 }
10138 }
10139 else
10140 {
10141 /* Just equalise window sizes */
10142 if (put_line(fd, "wincmd =") == FAIL)
10143 return FAIL;
10144 }
10145 return OK;
10146}
10147
10148/*
10149 * Write commands to "fd" to recursively create windows for frame "fr",
10150 * horizontally and vertically split.
10151 * After the commands the last window in the frame is the current window.
10152 * Returns FAIL when writing the commands to "fd" fails.
10153 */
10154 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010155ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156{
10157 frame_T *frc;
10158 int count = 0;
10159
10160 if (fr->fr_layout != FR_LEAF)
10161 {
10162 /* Find first frame that's not skipped and then create a window for
10163 * each following one (first frame is already there). */
10164 frc = ses_skipframe(fr->fr_child);
10165 if (frc != NULL)
10166 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10167 {
10168 /* Make window as big as possible so that we have lots of room
10169 * to split. */
10170 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10171 || put_line(fd, fr->fr_layout == FR_COL
10172 ? "split" : "vsplit") == FAIL)
10173 return FAIL;
10174 ++count;
10175 }
10176
10177 /* Go back to the first window. */
10178 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10179 ? "%dwincmd k" : "%dwincmd h", count) < 0
10180 || put_eol(fd) == FAIL))
10181 return FAIL;
10182
10183 /* Recursively create frames/windows in each window of this column or
10184 * row. */
10185 frc = ses_skipframe(fr->fr_child);
10186 while (frc != NULL)
10187 {
10188 ses_win_rec(fd, frc);
10189 frc = ses_skipframe(frc->fr_next);
10190 /* Go to next window. */
10191 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10192 return FAIL;
10193 }
10194 }
10195 return OK;
10196}
10197
10198/*
10199 * Skip frames that don't contain windows we want to save in the Session.
10200 * Returns NULL when there none.
10201 */
10202 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010203ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204{
10205 frame_T *frc;
10206
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010207 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 if (ses_do_frame(frc))
10209 break;
10210 return frc;
10211}
10212
10213/*
10214 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10215 * the Session.
10216 */
10217 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010218ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219{
10220 frame_T *frc;
10221
10222 if (fr->fr_layout == FR_LEAF)
10223 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010224 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 if (ses_do_frame(frc))
10226 return TRUE;
10227 return FALSE;
10228}
10229
10230/*
10231 * Return non-zero if window "wp" is to be stored in the Session.
10232 */
10233 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010234ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010236#ifdef FEAT_TERMINAL
10237 if (bt_terminal(wp->w_buffer))
10238 return !term_is_finished(wp->w_buffer)
10239 && (ssop_flags & SSOP_TERMINAL)
10240 && term_should_restore(wp->w_buffer);
10241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 if (wp->w_buffer->b_fname == NULL
10243#ifdef FEAT_QUICKFIX
10244 /* When 'buftype' is "nofile" can't restore the window contents. */
Bram Moolenaar26910de2019-06-15 19:37:15 +020010245 || bt_nofilename(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#endif
10247 )
10248 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010249 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 return (ssop_flags & SSOP_HELP);
10251 return TRUE;
10252}
10253
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010254 static int
10255put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10256{
10257 int r;
10258
10259 if (wp->w_curswant == MAXCOL)
10260 r = fprintf(fd, "%snormal! $", spaces);
10261 else
10262 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10263 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10264}
10265
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266/*
10267 * Write commands to "fd" to restore the view of a window.
10268 * Caller must make sure 'scrolloff' is zero.
10269 */
10270 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010271put_view(
10272 FILE *fd,
10273 win_T *wp,
10274 int add_edit, /* add ":edit" command to view */
10275 unsigned *flagp, /* vop_flags or ssop_flags */
10276 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010277 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278{
10279 win_T *save_curwin;
10280 int f;
10281 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010282 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283
10284 /* Always restore cursor position for ":mksession". For ":mkview" only
10285 * when 'viewoptions' contains "cursor". */
10286 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10287
10288 /*
10289 * Local argument list.
10290 */
10291 if (wp->w_alist == &global_alist)
10292 {
10293 if (put_line(fd, "argglobal") == FAIL)
10294 return FAIL;
10295 }
10296 else
10297 {
10298 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10299 flagp == &vop_flags
10300 || !(*flagp & SSOP_CURDIR)
10301 || wp->w_localdir != NULL, flagp) == FAIL)
10302 return FAIL;
10303 }
10304
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010305 /* Only when part of a session: restore the argument index. Some
10306 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010307 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010308 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010310 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 || put_eol(fd) == FAIL)
10312 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010313 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 }
10315
10316 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010317 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010319# ifdef FEAT_TERMINAL
10320 if (bt_terminal(wp->w_buffer))
10321 {
10322 if (term_write_session(fd, wp) == FAIL)
10323 return FAIL;
10324 }
10325 else
10326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 /*
10328 * Load the file.
10329 */
10330 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010331# ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +020010332 && !bt_nofilename(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010334 )
10335 {
10336 /*
10337 * Editing a file in this buffer: use ":edit file".
10338 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010339 *
10340 * Note, if a buffer for that file already exists, use :badd to
10341 * edit that buffer, to not lose folding information (:edit resets
10342 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010344 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010345 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010346 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010347 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10348 || fputs(" | else | edit ", fd) < 0
10349 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10350 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010351 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 return FAIL;
10353 }
10354 else
10355 {
10356 /* No file in this buffer, just make it empty. */
10357 if (put_line(fd, "enew") == FAIL)
10358 return FAIL;
10359#ifdef FEAT_QUICKFIX
10360 if (wp->w_buffer->b_ffname != NULL)
10361 {
10362 /* The buffer does have a name, but it's not a file name. */
10363 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010364 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 return FAIL;
10366 }
10367#endif
10368 do_cursor = FALSE;
10369 }
10370 }
10371
10372 /*
10373 * Local mappings and abbreviations.
10374 */
10375 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10376 && makemap(fd, wp->w_buffer) == FAIL)
10377 return FAIL;
10378
10379 /*
10380 * Local options. Need to go to the window temporarily.
10381 * Store only local values when using ":mkview" and when ":mksession" is
10382 * used and 'sessionoptions' doesn't include "options".
10383 * Some folding options are always stored when "folds" is included,
10384 * otherwise the folds would not be restored correctly.
10385 */
10386 save_curwin = curwin;
10387 curwin = wp;
10388 curbuf = curwin->w_buffer;
10389 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10390 f = makeset(fd, OPT_LOCAL,
10391 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10392#ifdef FEAT_FOLDING
10393 else if (*flagp & SSOP_FOLDS)
10394 f = makefoldset(fd);
10395#endif
10396 else
10397 f = OK;
10398 curwin = save_curwin;
10399 curbuf = curwin->w_buffer;
10400 if (f == FAIL)
10401 return FAIL;
10402
10403#ifdef FEAT_FOLDING
10404 /*
10405 * Save Folds when 'buftype' is empty and for help files.
10406 */
10407 if ((*flagp & SSOP_FOLDS)
10408 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010409 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410 {
10411 if (put_folds(fd, wp) == FAIL)
10412 return FAIL;
10413 }
10414#endif
10415
10416 /*
10417 * Set the cursor after creating folds, since that moves the cursor.
10418 */
10419 if (do_cursor)
10420 {
10421
10422 /* Restore the cursor line in the file and relatively in the
10423 * window. Don't use "G", it changes the jumplist. */
10424 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10425 (long)wp->w_cursor.lnum,
10426 (long)(wp->w_cursor.lnum - wp->w_topline),
10427 (long)wp->w_height / 2, (long)wp->w_height) < 0
10428 || put_eol(fd) == FAIL
10429 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10430 || put_line(fd, "exe s:l") == FAIL
10431 || put_line(fd, "normal! zt") == FAIL
10432 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10433 || put_eol(fd) == FAIL)
10434 return FAIL;
10435 /* Restore the cursor column and left offset when not wrapping. */
10436 if (wp->w_cursor.col == 0)
10437 {
10438 if (put_line(fd, "normal! 0") == FAIL)
10439 return FAIL;
10440 }
10441 else
10442 {
10443 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10444 {
10445 if (fprintf(fd,
10446 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010447 (long)wp->w_virtcol + 1,
10448 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 (long)wp->w_width / 2, (long)wp->w_width) < 0
10450 || put_eol(fd) == FAIL
10451 || put_line(fd, "if s:c > 0") == FAIL
10452 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010453 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10454 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 || put_eol(fd) == FAIL
10456 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010457 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 || put_line(fd, "endif") == FAIL)
10459 return FAIL;
10460 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010461 else if (put_view_curpos(fd, wp, "") == FAIL)
10462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 }
10464 }
10465
10466 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010467 * Local directory, if the current flag is not view options or the "curdir"
10468 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010470 if (wp->w_localdir != NULL
10471 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
10473 if (fputs("lcd ", fd) < 0
10474 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10475 || put_eol(fd) == FAIL)
10476 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010477 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 }
10479
10480 return OK;
10481}
10482
10483/*
10484 * Write an argument list to the session file.
10485 * Returns FAIL if writing fails.
10486 */
10487 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010488ses_arglist(
10489 FILE *fd,
10490 char *cmd,
10491 garray_T *gap,
10492 int fullname, /* TRUE: use full path name */
10493 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494{
10495 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010496 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 char_u *s;
10498
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010499 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10500 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010501 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502 return FAIL;
10503 for (i = 0; i < gap->ga_len; ++i)
10504 {
10505 /* NULL file names are skipped (only happens when out of memory). */
10506 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10507 if (s != NULL)
10508 {
10509 if (fullname)
10510 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010511 buf = alloc(MAXPATHL);
10512 if (buf != NULL)
10513 {
10514 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10515 s = buf;
10516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010518 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010519 || ses_put_fname(fd, s, flagp) == FAIL
10520 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010521 {
10522 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010524 }
10525 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 }
10527 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010528 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529}
10530
10531/*
10532 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010533 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 * Returns FAIL if writing fails.
10535 */
10536 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010537ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538{
10539 char_u *name;
10540
10541 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010542 * the session file will be sourced.
10543 * Don't do this for ":mkview", we don't know the current directory.
10544 * Don't do this after ":lcd", we don't keep track of what the current
10545 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 if (buf->b_sfname != NULL
10547 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010548 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010549#ifdef FEAT_AUTOCHDIR
10550 && !p_acd
10551#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010552 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 name = buf->b_sfname;
10554 else
10555 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010556 if (ses_put_fname(fd, name, flagp) == FAIL
10557 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558 return FAIL;
10559 return OK;
10560}
10561
10562/*
10563 * Write a file name to the session file.
10564 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10565 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010566 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 */
10568 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010569ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570{
10571 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010572 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
10575 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010576 if (sname == NULL)
10577 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010579 if (*flagp & SSOP_SLASH)
10580 {
10581 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010582 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010583 if (*p == '\\')
10584 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010586
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010587 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010588 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010590 if (p == NULL)
10591 return FAIL;
10592
10593 /* write the result */
10594 if (fputs((char *)p, fd) < 0)
10595 retval = FAIL;
10596
10597 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 return retval;
10599}
10600
10601/*
10602 * ":loadview [nr]"
10603 */
10604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010605ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606{
10607 char_u *fname;
10608
10609 fname = get_view_file(*eap->arg);
10610 if (fname != NULL)
10611 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010612 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 vim_free(fname);
10614 }
10615}
10616
10617/*
10618 * Get the name of the view file for the current buffer.
10619 */
10620 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010621get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623 int len = 0;
10624 char_u *p, *s;
10625 char_u *retval;
10626 char_u *sname;
10627
10628 if (curbuf->b_ffname == NULL)
10629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010630 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 return NULL;
10632 }
10633 sname = home_replace_save(NULL, curbuf->b_ffname);
10634 if (sname == NULL)
10635 return NULL;
10636
10637 /*
10638 * We want a file name without separators, because we're not going to make
10639 * a directory.
10640 * "normal" path separator -> "=+"
10641 * "=" -> "=="
10642 * ":" path separator -> "=-"
10643 */
10644 for (p = sname; *p; ++p)
10645 if (*p == '=' || vim_ispathsep(*p))
10646 ++len;
Bram Moolenaar964b3742019-05-24 18:54:09 +020010647 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 if (retval != NULL)
10649 {
10650 STRCPY(retval, p_vdir);
10651 add_pathsep(retval);
10652 s = retval + STRLEN(retval);
10653 for (p = sname; *p; ++p)
10654 {
10655 if (*p == '=')
10656 {
10657 *s++ = '=';
10658 *s++ = '=';
10659 }
10660 else if (vim_ispathsep(*p))
10661 {
10662 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010663#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 if (*p == ':')
10665 *s++ = '-';
10666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010668 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 }
10670 else
10671 *s++ = *p;
10672 }
10673 *s++ = '=';
10674 *s++ = c;
10675 STRCPY(s, ".vim");
10676 }
10677
10678 vim_free(sname);
10679 return retval;
10680}
10681
10682#endif /* FEAT_SESSION */
10683
10684/*
10685 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10686 * Return FAIL for a write error.
10687 */
10688 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010689put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690{
10691 if (
10692#ifdef USE_CRNL
10693 (
10694# ifdef MKSESSION_NL
10695 !mksession_nl &&
10696# endif
10697 (putc('\r', fd) < 0)) ||
10698#endif
10699 (putc('\n', fd) < 0))
10700 return FAIL;
10701 return OK;
10702}
10703
10704/*
10705 * Write a line to "fd".
10706 * Return FAIL for a write error.
10707 */
10708 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010709put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710{
10711 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10712 return FAIL;
10713 return OK;
10714}
10715
10716#ifdef FEAT_VIMINFO
10717/*
10718 * ":rviminfo" and ":wviminfo".
10719 */
10720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010721ex_viminfo(
10722 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723{
10724 char_u *save_viminfo;
10725
10726 save_viminfo = p_viminfo;
10727 if (*p_viminfo == NUL)
10728 p_viminfo = (char_u *)"'100";
10729 if (eap->cmdidx == CMD_rviminfo)
10730 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010731 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10732 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010733 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 }
10735 else
10736 write_viminfo(eap->arg, eap->forceit);
10737 p_viminfo = save_viminfo;
10738}
10739#endif
10740
10741#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010742/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010743 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010744 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010747dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 if (fname == NULL)
10750 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010751 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752}
10753#endif
10754
10755/*
10756 * ":behave {mswin,xterm}"
10757 */
10758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010759ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760{
10761 if (STRCMP(eap->arg, "mswin") == 0)
10762 {
10763 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10764 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10765 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10766 set_option_value((char_u *)"keymodel", 0L,
10767 (char_u *)"startsel,stopsel", 0);
10768 }
10769 else if (STRCMP(eap->arg, "xterm") == 0)
10770 {
10771 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10772 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10773 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10774 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10775 }
10776 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010777 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778}
10779
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010780#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10781/*
10782 * Function given to ExpandGeneric() to obtain the possible arguments of the
10783 * ":behave {mswin,xterm}" command.
10784 */
10785 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010786get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010787{
10788 if (idx == 0)
10789 return (char_u *)"mswin";
10790 if (idx == 1)
10791 return (char_u *)"xterm";
10792 return NULL;
10793}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010794
10795/*
10796 * Function given to ExpandGeneric() to obtain the possible arguments of the
10797 * ":messages {clear}" command.
10798 */
10799 char_u *
10800get_messages_arg(expand_T *xp UNUSED, int idx)
10801{
10802 if (idx == 0)
10803 return (char_u *)"clear";
10804 return NULL;
10805}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010806#endif
10807
Bram Moolenaar113e1072019-01-20 15:30:40 +010010808#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010809 char_u *
10810get_mapclear_arg(expand_T *xp UNUSED, int idx)
10811{
10812 if (idx == 0)
10813 return (char_u *)"<buffer>";
10814 return NULL;
10815}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010816#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010817
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818static int filetype_detect = FALSE;
10819static int filetype_plugin = FALSE;
10820static int filetype_indent = FALSE;
10821
10822/*
10823 * ":filetype [plugin] [indent] {on,off,detect}"
10824 * on: Load the filetype.vim file to install autocommands for file types.
10825 * off: Load the ftoff.vim file to remove all autocommands for file types.
10826 * plugin on: load filetype.vim and ftplugin.vim
10827 * plugin off: load ftplugof.vim
10828 * indent on: load filetype.vim and indent.vim
10829 * indent off: load indoff.vim
10830 */
10831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010832ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834 char_u *arg = eap->arg;
10835 int plugin = FALSE;
10836 int indent = FALSE;
10837
10838 if (*eap->arg == NUL)
10839 {
10840 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010841 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 filetype_detect ? "ON" : "OFF",
10843 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10844 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10845 return;
10846 }
10847
10848 /* Accept "plugin" and "indent" in any order. */
10849 for (;;)
10850 {
10851 if (STRNCMP(arg, "plugin", 6) == 0)
10852 {
10853 plugin = TRUE;
10854 arg = skipwhite(arg + 6);
10855 continue;
10856 }
10857 if (STRNCMP(arg, "indent", 6) == 0)
10858 {
10859 indent = TRUE;
10860 arg = skipwhite(arg + 6);
10861 continue;
10862 }
10863 break;
10864 }
10865 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10866 {
10867 if (*arg == 'o' || !filetype_detect)
10868 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010869 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 filetype_detect = TRUE;
10871 if (plugin)
10872 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010873 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 filetype_plugin = TRUE;
10875 }
10876 if (indent)
10877 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010878 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 filetype_indent = TRUE;
10880 }
10881 }
10882 if (*arg == 'd')
10883 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010884 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010885 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 }
10887 }
10888 else if (STRCMP(arg, "off") == 0)
10889 {
10890 if (plugin || indent)
10891 {
10892 if (plugin)
10893 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010894 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 filetype_plugin = FALSE;
10896 }
10897 if (indent)
10898 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010899 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 filetype_indent = FALSE;
10901 }
10902 }
10903 else
10904 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010905 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 filetype_detect = FALSE;
10907 }
10908 }
10909 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010910 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911}
10912
10913/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010914 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 */
10916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010917ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918{
10919 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010920 {
10921 char_u *arg = eap->arg;
10922
10923 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10924 arg += 9;
10925
10926 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10927 if (arg != eap->arg)
10928 did_filetype = FALSE;
10929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010933ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934{
10935#ifdef FEAT_DIGRAPHS
10936 if (*eap->arg != NUL)
10937 putdigraph(eap->arg);
10938 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010939 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010941 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942#endif
10943}
10944
10945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010946ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947{
10948 int flags = 0;
10949
10950 if (eap->cmdidx == CMD_setlocal)
10951 flags = OPT_LOCAL;
10952 else if (eap->cmdidx == CMD_setglobal)
10953 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010954#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 if (cmdmod.browse && flags == 0)
10956 ex_options(eap);
10957 else
10958#endif
10959 (void)do_set(eap->arg, flags);
10960}
10961
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010962#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10963 void
10964set_no_hlsearch(int flag)
10965{
10966 no_hlsearch = flag;
10967# ifdef FEAT_EVAL
10968 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10969# endif
10970}
10971
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972/*
10973 * ":nohlsearch"
10974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010976ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010978 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010979 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
10982/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010983 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 * Sets nextcmd to the start of the next command, if any. Also called when
10985 * skipping commands to find the next command.
10986 */
10987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010988ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989{
10990 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010991 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992 char_u *end;
10993 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010994 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010995
10996 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010997 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010998 else
10999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011000 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011001 return;
11002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003
11004 /* First clear any old pattern. */
11005 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011006 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011007
11008 if (ends_excmd(*eap->arg))
11009 end = eap->arg;
11010 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010011011 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011012 end = eap->arg + 4;
11013 else
11014 {
11015 p = skiptowhite(eap->arg);
11016 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011017 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 p = skipwhite(p);
11019 if (*p == NUL)
11020 {
11021 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011022 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011023 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 return;
11025 }
11026 end = skip_regexp(p + 1, *p, TRUE, NULL);
11027 if (!eap->skip)
11028 {
11029 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11030 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011031 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 eap->errmsg = e_trailing;
11033 return;
11034 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011035 if (*end != *p)
11036 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011037 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011038 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011039 return;
11040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041
11042 c = *end;
11043 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011044 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011045 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011046 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 }
11048 }
11049 eap->nextcmd = find_nextcmd(end);
11050}
11051#endif
11052
11053#ifdef FEAT_CRYPT
11054/*
11055 * ":X": Get crypt key
11056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011058ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011060 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011061 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062}
11063#endif
11064
11065#ifdef FEAT_FOLDING
11066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011067ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068{
11069 if (foldManualAllowed(TRUE))
11070 foldCreate(eap->line1, eap->line2);
11071}
11072
11073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011074ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075{
11076 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11077 eap->forceit, FALSE);
11078}
11079
11080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011081ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082{
11083 linenr_T lnum;
11084
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011085#ifdef FEAT_CLIPBOARD
11086 start_global_changes();
11087#endif
11088
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089 /* First set the marks for all lines closed/open. */
11090 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11091 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11092 ml_setmarked(lnum);
11093
11094 /* Execute the command on the marked lines. */
11095 global_exe(eap->arg);
11096 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011097#ifdef FEAT_CLIPBOARD
11098 end_global_changes();
11099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100}
11101#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011102
Bram Moolenaar39665952018-08-15 20:59:48 +020011103#ifdef FEAT_QUICKFIX
11104/*
11105 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11106 * instead of a quickfix command.
11107 */
11108 int
11109is_loclist_cmd(int cmdidx)
11110{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011111 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011112 return FALSE;
11113 return cmdnames[cmdidx].cmd_name[0] == 'l';
11114}
11115#endif
11116
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011117# if defined(FEAT_TIMERS) || defined(PROTO)
11118 int
11119get_pressedreturn(void)
11120{
11121 return ex_pressedreturn;
11122}
11123
11124 void
11125set_pressedreturn(int val)
11126{
11127 ex_pressedreturn = val;
11128}
11129#endif