blob: d8249186020fc9c4e0c402b1d031cc520f1c0ffb [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
Bram Moolenaardefa0672019-07-21 19:25:37 +0200321#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_viminfo ex_ni
323#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_filetype(exarg_T *eap);
326static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000328# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329# define ex_diffpatch ex_ni
330# define ex_diffgetput ex_ni
331# define ex_diffsplit ex_ni
332# define ex_diffthis ex_ni
333# define ex_diffupdate ex_ni
334#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_digraphs(exarg_T *eap);
336static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100337#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338# define ex_options ex_ni
339#endif
340#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_nohlsearch(exarg_T *eap);
342static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_nohlsearch ex_ni
345# define ex_match ex_ni
346#endif
347#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#else
350# define ex_X ex_ni
351#endif
352#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static void ex_fold(exarg_T *eap);
354static void ex_foldopen(exarg_T *eap);
355static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#else
357# define ex_fold ex_ni
358# define ex_foldopen ex_ni
359# define ex_folddo ex_ni
360#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100361#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362# define ex_language ex_ni
363#endif
364#ifndef FEAT_SIGNS
365# define ex_sign ex_ni
366#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000367#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200368# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000369# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200370# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifndef FEAT_EVAL
374# define ex_debug ex_ni
375# define ex_breakadd ex_ni
376# define ex_debuggreedy ex_ni
377# define ex_breakdel ex_ni
378# define ex_breaklist ex_ni
379#endif
380
381#ifndef FEAT_CMDHIST
382# define ex_history ex_ni
383#endif
384#ifndef FEAT_JUMPLIST
385# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200386# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_changes ex_ni
388#endif
389
Bram Moolenaar05159a02005-02-26 23:04:13 +0000390#ifndef FEAT_PROFILE
391# define ex_profile ex_ni
392#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200393#ifndef FEAT_TERMINAL
394# define ex_terminal ex_ni
395#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200396#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
397# define ex_xrestore ex_ni
398#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200399#if !defined(FEAT_TEXT_PROP)
400# define ex_popupclear ex_ni
401#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000402
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403/*
404 * Declare cmdnames[].
405 */
406#define DO_DECLARE_EXCMD
407#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200408#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410static char_u dollar_command[2] = {'$', 0};
411
412
413#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000414/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415typedef struct
416{
417 char_u *line; /* command line */
418 linenr_T lnum; /* sourcing_lnum of the line */
419} wcmd_T;
420
421/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000422 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000424 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000426struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427{
428 garray_T *lines_gap; /* growarray with line info */
429 int current_line; /* last read line from growarray */
430 int repeating; /* TRUE when looping a second time */
431 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200432 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 void *cookie;
434};
435
Bram Moolenaare96a2492019-06-25 04:12:16 +0200436static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100437static int store_loop_line(garray_T *gap, char_u *line);
438static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000439
440/* Struct to save a few things while debugging. Used in do_cmdline() only. */
441struct dbg_stuff
442{
443 int trylevel;
444 int force_abort;
445 except_T *caught_stack;
446 char_u *vv_exception;
447 char_u *vv_throwpoint;
448 int did_emsg;
449 int got_int;
450 int did_throw;
451 int need_rethrow;
452 int check_cstack;
453 except_T *current_exception;
454};
455
Bram Moolenaared203462004-06-16 11:19:22 +0000456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100457save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000458{
459 dsp->trylevel = trylevel; trylevel = 0;
460 dsp->force_abort = force_abort; force_abort = FALSE;
461 dsp->caught_stack = caught_stack; caught_stack = NULL;
462 dsp->vv_exception = v_exception(NULL);
463 dsp->vv_throwpoint = v_throwpoint(NULL);
464
465 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
466 dsp->did_emsg = did_emsg; did_emsg = FALSE;
467 dsp->got_int = got_int; got_int = FALSE;
468 dsp->did_throw = did_throw; did_throw = FALSE;
469 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
470 dsp->check_cstack = check_cstack; check_cstack = FALSE;
471 dsp->current_exception = current_exception; current_exception = NULL;
472}
473
474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100475restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000476{
477 suppress_errthrow = FALSE;
478 trylevel = dsp->trylevel;
479 force_abort = dsp->force_abort;
480 caught_stack = dsp->caught_stack;
481 (void)v_exception(dsp->vv_exception);
482 (void)v_throwpoint(dsp->vv_throwpoint);
483 did_emsg = dsp->did_emsg;
484 got_int = dsp->got_int;
485 did_throw = dsp->did_throw;
486 need_rethrow = dsp->need_rethrow;
487 check_cstack = dsp->check_cstack;
488 current_exception = dsp->current_exception;
489}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#endif
491
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492/*
493 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
494 * command is given.
495 */
496 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100497do_exmode(
498 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 int save_msg_scroll;
501 int prev_msg_row;
502 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100503 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000504
505 if (improved)
506 exmode_active = EXMODE_VIM;
507 else
508 exmode_active = EXMODE_NORMAL;
509 State = NORMAL;
510
511 /* When using ":global /pat/ visual" and then "Q" we return to continue
512 * the :global command. */
513 if (global_busy)
514 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515
516 save_msg_scroll = msg_scroll;
517 ++RedrawingDisabled; /* don't redisplay the window */
518 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519#ifdef FEAT_GUI
520 /* Ignore scrollbar and mouse events in Ex mode */
521 ++hold_gui_events;
522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
Bram Moolenaar32526b32019-01-19 17:43:09 +0100524 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 while (exmode_active)
526 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000527 /* Check for a ":normal" command and no more characters left. */
528 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
529 {
530 exmode_active = FALSE;
531 break;
532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 msg_scroll = TRUE;
534 need_wait_return = FALSE;
535 ex_pressedreturn = FALSE;
536 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100537 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 prev_msg_row = msg_row;
539 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 if (improved)
541 {
542 cmdline_row = msg_row;
543 do_cmdline(NULL, getexline, NULL, 0);
544 }
545 else
546 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
547 lines_left = Rows - 1;
548
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100550 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100553 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000556 if (ex_pressedreturn)
557 {
558 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000559 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000560 msg_row = prev_msg_row;
561 if (prev_msg_row == Rows - 1)
562 msg_row--;
563 }
564 msg_col = 0;
565 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
566 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000569 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
570 {
571 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100572 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000573 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100574 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 }
577
578#ifdef FEAT_GUI
579 --hold_gui_events;
580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 --RedrawingDisabled;
582 --no_wait_return;
583 update_screen(CLEAR);
584 need_wait_return = FALSE;
585 msg_scroll = save_msg_scroll;
586}
587
588/*
589 * Execute a simple command line. Used for translated commands like "*".
590 */
591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593{
594 return do_cmdline(cmd, NULL, NULL,
595 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
596}
597
598/*
599 * do_cmdline(): execute one Ex command line
600 *
601 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100602 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * 2. Split up in parts separated with '|'.
604 *
605 * This function can be called recursively!
606 *
607 * flags:
608 * DOCMD_VERBOSE - The command will be included in the error message.
609 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100610 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 * DOCMD_KEYTYPED - Don't reset KeyTyped.
612 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
613 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
614 *
615 * return FAIL if cmdline could not be executed, OK otherwise
616 */
617 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618do_cmdline(
619 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200620 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100621 void *cookie, /* argument for fgetline() */
622 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 char_u *next_cmdline; /* next cmd to execute */
625 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100626 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 static int recursive = 0; /* recursive depth */
628 int msg_didout_before_start = 0;
629 int count = 0; /* line number count */
630 int did_inc = FALSE; /* incremented RedrawingDisabled */
631 int retval = OK;
632#ifdef FEAT_EVAL
633 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000634 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 int current_line = 0; /* active line in lines_ga */
636 char_u *fname = NULL; /* function or script name */
637 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
638 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000639 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 int initial_trylevel;
641 struct msglist **saved_msg_list = NULL;
642 struct msglist *private_msg_list;
643
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100644 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200645 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000647 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000649 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100651# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652# define cmd_cookie cookie
653#endif
654 static int call_depth = 0; /* recursiveness */
655
656#ifdef FEAT_EVAL
657 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
658 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000659 * This ensures that the do_errthrow() call in do_one_cmd() does not
660 * combine the messages stored by an earlier invocation of do_one_cmd()
661 * with the command name of the later one. This would happen when
662 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 saved_msg_list = msg_list;
664 msg_list = &private_msg_list;
665 private_msg_list = NULL;
666#endif
667
668 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100669 * here. The value of 200 allows nested function calls, ":source", etc.
670 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100671 if (call_depth >= 200
672#ifdef FEAT_EVAL
673 && call_depth >= p_mfd
674#endif
675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100677 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_EVAL
679 /* When converting to an exception, we do not include the command name
680 * since this is not an error of the specific command. */
681 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
682 msg_list = saved_msg_list;
683#endif
684 return FAIL;
685 }
686 ++call_depth;
687
688#ifdef FEAT_EVAL
689 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 cstack.cs_trylevel = 0;
692 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000693 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
695
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
698 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ++ex_nesting_level;
702
703 /* Get the function or script name and the address where the next breakpoint
704 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000705 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = func_name(real_cookie);
708 breakpoint = func_breakpoint(real_cookie);
709 dbg_tick = func_dbg_tick(real_cookie);
710 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100711 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
713 fname = sourcing_name;
714 breakpoint = source_breakpoint(real_cookie);
715 dbg_tick = source_dbg_tick(real_cookie);
716 }
717
718 /*
719 * Initialize "force_abort" and "suppress_errthrow" at the top level.
720 */
721 if (!recursive)
722 {
723 force_abort = FALSE;
724 suppress_errthrow = FALSE;
725 }
726
727 /*
728 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000729 * exception handling (used when debugging). Otherwise clear it to avoid
730 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000732 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000733 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000734 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200735 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737 initial_trylevel = trylevel;
738
739 /*
740 * "did_throw" will be set to TRUE when an exception is being thrown.
741 */
742 did_throw = FALSE;
743#endif
744 /*
745 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000746 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * If force_abort is set, we cancel everything.
748 */
749 did_emsg = FALSE;
750
751 /*
752 * KeyTyped is only set when calling vgetc(). Reset it here when not
753 * calling vgetc() (sourced command lines).
754 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 if (!(flags & DOCMD_KEYTYPED)
756 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 KeyTyped = FALSE;
758
759 /*
760 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000761 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 * - for multiple commands on one line, separated with '|'
763 * - when repeating until there are no more lines (for ":source")
764 */
765 next_cmdline = cmdline;
766 do
767 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#endif
771
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000772 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (next_cmdline == NULL
774#ifdef FEAT_EVAL
775 && !force_abort
776 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#endif
779 )
780 did_emsg = FALSE;
781
782 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000783 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 * 3. If a line is given: Make a copy, so we can mess with it.
786 */
787
788#ifdef FEAT_EVAL
789 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
792 /* Each '|' separated command is stored separately in lines_ga, to
793 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100794 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
796 /* Check if a function has returned or, unless it has an unclosed
797 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000801 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802 func_line_end(real_cookie);
803# endif
804 if (func_has_ended(real_cookie))
805 {
806 retval = FAIL;
807 break;
808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000811 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 script_line_end();
814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
819 retval = FAIL;
820 break;
821 }
822
823 /* If breakpoints have been added/deleted need to check for it. */
824 if (breakpoint != NULL && dbg_tick != NULL
825 && *dbg_tick != debug_tick)
826 {
827 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 fname, sourcing_lnum);
830 *dbg_tick = debug_tick;
831 }
832
833 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
834 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
835
836 /* Did we encounter a breakpoint? */
837 if (breakpoint != NULL && *breakpoint != 0
838 && *breakpoint <= sourcing_lnum)
839 {
840 dbg_breakpoint(fname, sourcing_lnum);
841 /* Find next breakpoint. */
842 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 fname, sourcing_lnum);
845 *dbg_tick = debug_tick;
846 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000848 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 {
850 if (getline_is_func)
851 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853 script_line_start();
854 }
855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000858 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000860 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 * below, so that it stores lines in or reads them from
863 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 * while/for loop. */
865 cmd_getline = get_loop_line;
866 cmd_cookie = (void *)&cmd_loop_cookie;
867 cmd_loop_cookie.lines_gap = &lines_ga;
868 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000870 cmd_loop_cookie.cookie = cookie;
871 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 else
874 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 cmd_cookie = cookie;
877 }
878#endif
879
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100880 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 if (next_cmdline == NULL)
882 {
883 /*
884 * Need to set msg_didout for the first line after an ":if",
885 * otherwise the ":if" will be overwritten.
886 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100887 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100889 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890#ifdef FEAT_EVAL
891 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
892#else
893 0
894#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200895 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 {
897 /* Don't call wait_return for aborted command line. The NULL
898 * returned for the end of a sourced file or executed function
899 * doesn't do this. */
900 if (KeyTyped && !(flags & DOCMD_REPEAT))
901 need_wait_return = FALSE;
902 retval = FAIL;
903 break;
904 }
905 used_getline = TRUE;
906
907 /*
908 * Keep the first typed line. Clear it when more lines are typed.
909 */
910 if (flags & DOCMD_KEEPLINE)
911 {
912 vim_free(repeat_cmdline);
913 if (count == 0)
914 repeat_cmdline = vim_strsave(next_cmdline);
915 else
916 repeat_cmdline = NULL;
917 }
918 }
919
920 /* 3. Make a copy of the command so we can mess with it. */
921 else if (cmdline_copy == NULL)
922 {
923 next_cmdline = vim_strsave(next_cmdline);
924 if (next_cmdline == NULL)
925 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100926 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 retval = FAIL;
928 break;
929 }
930 }
931 cmdline_copy = next_cmdline;
932
933#ifdef FEAT_EVAL
934 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 * Save the current line when inside a ":while" or ":for", and when
936 * the command looks like a ":while" or ":for", because we may need it
937 * later. When there is a '|' and another command, it is stored
938 * separately, because we need to be able to jump back to it from an
939 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000941 if (current_line == lines_ga.ga_len
942 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000944 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 {
946 retval = FAIL;
947 break;
948 }
949 }
950 did_endif = FALSE;
951#endif
952
953 if (count++ == 0)
954 {
955 /*
956 * All output from the commands is put below each other, without
957 * waiting for a return. Don't do this when executing commands
958 * from a script or when being called recursive (e.g. for ":e
959 * +command file").
960 */
961 if (!(flags & DOCMD_NOWAIT) && !recursive)
962 {
963 msg_didout_before_start = msg_didout;
964 msg_didany = FALSE; /* no output yet */
965 msg_start();
966 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200967 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 ++RedrawingDisabled;
969 did_inc = TRUE;
970 }
971 }
972
973 if (p_verbose >= 15 && sourcing_name != NULL)
974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000976 verbose_enter_scroll();
977
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100978 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000980 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100981 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000982
983 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 --no_wait_return;
985 }
986
987 /*
988 * 2. Execute one '|' separated command.
989 * do_one_cmd() will return NULL if there is no trailing '|'.
990 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
991 */
992 ++recursive;
993 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
994#ifdef FEAT_EVAL
995 &cstack,
996#endif
997 cmd_getline, cmd_cookie);
998 --recursive;
999
1000#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001001 if (cmd_cookie == (void *)&cmd_loop_cookie)
1002 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001004 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#endif
1006
1007 if (next_cmdline == NULL)
1008 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001009 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010#ifdef FEAT_CMDHIST
1011 /*
1012 * If the command was typed, remember it for the ':' register.
1013 * Do this AFTER executing the command to make :@: work.
1014 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001015 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 && new_last_cmdline != NULL)
1017 {
1018 vim_free(last_cmdline);
1019 last_cmdline = new_last_cmdline;
1020 new_last_cmdline = NULL;
1021 }
1022#endif
1023 }
1024 else
1025 {
1026 /* need to copy the command after the '|' to cmdline_copy, for the
1027 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001028 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 next_cmdline = cmdline_copy;
1030 }
1031
1032
1033#ifdef FEAT_EVAL
1034 /* reset did_emsg for a function that is not aborted by an error */
1035 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001036 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 && !func_has_abort(real_cookie))
1038 did_emsg = FALSE;
1039
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 ++current_line;
1043
1044 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 * An ":endwhile", ":endfor" and ":continue" is handled here.
1046 * If we were executing commands, jump back to the ":while" or
1047 * ":for".
1048 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 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 /* Jump back to the matching ":while" or ":for". Be careful
1055 * not to use a cs_line[] from an entry that isn't a ":while"
1056 * or ":for": It would make "current_line" invalid and can
1057 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (!did_emsg && !got_int && !did_throw
1059 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 && (cstack.cs_flags[cstack.cs_idx]
1061 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 && cstack.cs_line[cstack.cs_idx] >= 0
1063 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1064 {
1065 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 /* remember we jumped there */
1067 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 line_breakcheck(); /* check if CTRL-C typed */
1069
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 /* Check for the next breakpoint at or after the ":while"
1071 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (breakpoint != NULL)
1073 {
1074 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001075 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 fname,
1077 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1078 *dbg_tick = debug_tick;
1079 }
1080 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001085 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1086 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
1088 }
1089
1090 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1097 }
1098 }
1099
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001100 /* Check for the next breakpoint after a watchexpression */
1101 if (breakpoint != NULL && has_watchexpr())
1102 {
1103 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1104 *dbg_tick = debug_tick;
1105 }
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 /*
1108 * When not inside any ":while" loop, clear remembered lines.
1109 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
1112 if (lines_ga.ga_len > 0)
1113 {
1114 sourcing_lnum =
1115 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1116 free_cmdlines(&lines_ga);
1117 }
1118 current_line = 0;
1119 }
1120
1121 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1123 * being restored at the ":endtry". Reset them here and set the
1124 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1125 * This includes the case where a missing ":endif", ":endwhile" or
1126 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001128 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 cstack.cs_lflags &= ~CSL_HAD_FINA;
1131 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1132 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 did_throw ? (void *)current_exception : NULL);
1134 did_emsg = got_int = did_throw = FALSE;
1135 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1136 }
1137
1138 /* Update global "trylevel" for recursive calls to do_cmdline() from
1139 * within this loop. */
1140 trylevel = initial_trylevel + cstack.cs_trylevel;
1141
1142 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001143 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 * files) is aborted because of an error, an interrupt, or an uncaught
1145 * exception, cancel everything. If it is left normally, reset
1146 * force_abort to get the non-EH compatible abortion behavior for
1147 * the rest of the script.
1148 */
1149 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1150 force_abort = FALSE;
1151
1152 /* Convert an interrupt to an exception if appropriate. */
1153 (void)do_intthrow(&cstack);
1154#endif /* FEAT_EVAL */
1155
1156 }
1157 /*
1158 * Continue executing command lines when:
1159 * - no CTRL-C typed, no aborting error, no exception thrown or try
1160 * conditionals need to be checked for executing finally clauses or
1161 * catching an interrupt exception
1162 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001163 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 * looping for ":source" command or function call.
1165 */
1166 while (!((got_int
1167#ifdef FEAT_EVAL
1168 || (did_emsg && force_abort) || did_throw
1169#endif
1170 )
1171#ifdef FEAT_EVAL
1172 && cstack.cs_trylevel == 0
1173#endif
1174 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001175 && !(did_emsg
1176#ifdef FEAT_EVAL
1177 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001178 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001179 * the :endtry to be missed. */
1180 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1181#endif
1182 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001183 && (getline_equal(fgetline, cookie, getexmodeline)
1184 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 && (next_cmdline != NULL
1186#ifdef FEAT_EVAL
1187 || cstack.cs_idx >= 0
1188#endif
1189 || (flags & DOCMD_REPEAT)));
1190
1191 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001192 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193#ifdef FEAT_EVAL
1194 free_cmdlines(&lines_ga);
1195 ga_clear(&lines_ga);
1196
1197 if (cstack.cs_idx >= 0)
1198 {
1199 /*
1200 * If a sourced file or executed function ran to its end, report the
1201 * unclosed conditional.
1202 */
1203 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001204 && ((getline_equal(fgetline, cookie, getsourceline)
1205 && !source_finished(fgetline, cookie))
1206 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 && !func_has_ended(real_cookie))))
1208 {
1209 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001210 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001212 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001214 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001216 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218
1219 /*
1220 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1221 * ":endtry" in a sourced file or executed function. If the try
1222 * conditional is in its finally clause, ignore anything pending.
1223 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001224 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 */
1226 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001227 {
1228 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1229
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001230 if (idx >= 0)
1231 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001232 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1233 &cstack.cs_looplevel);
1234 }
1235 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 trylevel = initial_trylevel;
1237 }
1238
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001239 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1240 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001242 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 ? (char_u *)"endfunction" : (char_u *)NULL);
1244
1245 if (trylevel == 0)
1246 {
1247 /*
1248 * When an exception is being thrown out of the outermost try
1249 * conditional, discard the uncaught exception, disable the conversion
1250 * of interrupts or errors to exceptions, and ensure that no more
1251 * commands are executed.
1252 */
1253 if (did_throw)
1254 {
1255 void *p = NULL;
1256 char_u *saved_sourcing_name;
1257 int saved_sourcing_lnum;
1258 struct msglist *messages = NULL, *next;
1259
1260 /*
1261 * If the uncaught exception is a user exception, report it as an
1262 * error. If it is an error exception, display the saved error
1263 * message now. For an interrupt exception, do nothing; the
1264 * interrupt message is given elsewhere.
1265 */
1266 switch (current_exception->type)
1267 {
1268 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001269 vim_snprintf((char *)IObuff, IOSIZE,
1270 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 current_exception->value);
1272 p = vim_strsave(IObuff);
1273 break;
1274 case ET_ERROR:
1275 messages = current_exception->messages;
1276 current_exception->messages = NULL;
1277 break;
1278 case ET_INTERRUPT:
1279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281
1282 saved_sourcing_name = sourcing_name;
1283 saved_sourcing_lnum = sourcing_lnum;
1284 sourcing_name = current_exception->throw_name;
1285 sourcing_lnum = current_exception->throw_lnum;
1286 current_exception->throw_name = NULL;
1287
1288 discard_current_exception(); /* uses IObuff if 'verbose' */
1289 suppress_errthrow = TRUE;
1290 force_abort = TRUE;
1291
1292 if (messages != NULL)
1293 {
1294 do
1295 {
1296 next = messages->next;
1297 emsg(messages->msg);
1298 vim_free(messages->msg);
1299 vim_free(messages);
1300 messages = next;
1301 }
1302 while (messages != NULL);
1303 }
1304 else if (p != NULL)
1305 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001306 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 vim_free(p);
1308 }
1309 vim_free(sourcing_name);
1310 sourcing_name = saved_sourcing_name;
1311 sourcing_lnum = saved_sourcing_lnum;
1312 }
1313
1314 /*
1315 * On an interrupt or an aborting error not converted to an exception,
1316 * disable the conversion of errors to exceptions. (Interrupts are not
1317 * converted any more, here.) This enables also the interrupt message
1318 * when force_abort is set and did_emsg unset in case of an interrupt
1319 * from a finally clause after an error.
1320 */
1321 else if (got_int || (did_emsg && force_abort))
1322 suppress_errthrow = TRUE;
1323 }
1324
1325 /*
1326 * The current cstack will be freed when do_cmdline() returns. An uncaught
1327 * exception will have to be rethrown in the previous cstack. If a function
1328 * has just returned or a script file was just finished and the previous
1329 * cstack belongs to the same function or, respectively, script file, it
1330 * will have to be checked for finally clauses to be executed due to the
1331 * ":return" or ":finish". This is done in do_one_cmd().
1332 */
1333 if (did_throw)
1334 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001335 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 && ex_nesting_level > func_level(real_cookie) + 1))
1339 {
1340 if (!did_throw)
1341 check_cstack = TRUE;
1342 }
1343 else
1344 {
1345 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001346 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 --ex_nesting_level;
1348 /*
1349 * Go to debug mode when returning from a function in which we are
1350 * single-stepping.
1351 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001352 if ((getline_equal(fgetline, cookie, getsourceline)
1353 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001355 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 ? (char_u *)_("End of sourced file")
1357 : (char_u *)_("End of function"));
1358 }
1359
1360 /*
1361 * Restore the exception environment (done after returning from the
1362 * debugger).
1363 */
1364 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001365 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367 msg_list = saved_msg_list;
1368#endif /* FEAT_EVAL */
1369
1370 /*
1371 * If there was too much output to fit on the command line, ask the user to
1372 * hit return before redrawing the screen. With the ":global" command we do
1373 * this only once after the command is finished.
1374 */
1375 if (did_inc)
1376 {
1377 --RedrawingDisabled;
1378 --no_wait_return;
1379 msg_scroll = FALSE;
1380
1381 /*
1382 * When just finished an ":if"-":else" which was typed, no need to
1383 * wait for hit-return. Also for an error situation.
1384 */
1385 if (retval == FAIL
1386#ifdef FEAT_EVAL
1387 || (did_endif && KeyTyped && !did_emsg)
1388#endif
1389 )
1390 {
1391 need_wait_return = FALSE;
1392 msg_didany = FALSE; /* don't wait when restarting edit */
1393 }
1394 else if (need_wait_return)
1395 {
1396 /*
1397 * The msg_start() above clears msg_didout. The wait_return we do
1398 * here should not overwrite the command that may be shown before
1399 * doing that.
1400 */
1401 msg_didout |= msg_didout_before_start;
1402 wait_return(FALSE);
1403 }
1404 }
1405
Bram Moolenaar2a942252012-11-28 23:03:07 +01001406#ifdef FEAT_EVAL
1407 did_endif = FALSE; /* in case do_cmdline used recursively */
1408#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /*
1410 * Reset if_level, in case a sourced script file contains more ":if" than
1411 * ":endif" (could be ":if x | foo | endif").
1412 */
1413 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001414#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 --call_depth;
1417 return retval;
1418}
1419
1420#ifdef FEAT_EVAL
1421/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001422 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 */
1424 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001425get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001427 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 wcmd_T *wp;
1429 char_u *line;
1430
1431 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1432 {
1433 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001434 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001436 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001438 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001440 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001441 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 ++cp->current_line;
1443
1444 return line;
1445 }
1446
1447 KeyTyped = FALSE;
1448 ++cp->current_line;
1449 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1450 sourcing_lnum = wp->lnum;
1451 return vim_strsave(wp->line);
1452}
1453
1454/*
1455 * Store a line in "gap" so that a ":while" loop can execute it again.
1456 */
1457 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001458store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 if (ga_grow(gap, 1) == FAIL)
1461 return FAIL;
1462 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1463 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1464 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 return OK;
1466}
1467
1468/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001469 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 */
1471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001472free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
1474 while (gap->ga_len > 0)
1475 {
1476 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1477 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 }
1479}
1480#endif
1481
1482/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001483 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1484 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001487getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001488 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001489 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
1492#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001493 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
Bram Moolenaar89d40322006-08-29 15:30:07 +00001496 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001497 * function that's originally used to obtain the lines. This may be
1498 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001499 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001500 cp = (struct loop_cookie *)cookie;
1501 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 gp = cp->getline;
1504 cp = cp->cookie;
1505 }
1506 return gp == func;
1507#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001508 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509#endif
1510}
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001513 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * getline function. Otherwise return "cookie".
1515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001517getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001518 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001519 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
Bram Moolenaar13505972019-01-24 15:04:48 +01001521#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001522 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001526 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001528 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001529 cp = (struct loop_cookie *)cookie;
1530 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 {
1532 gp = cp->getline;
1533 cp = cp->cookie;
1534 }
1535 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001536#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001539}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001541
1542/*
1543 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1544 * ":bwipeout", etc.
1545 * Returns the buffer number.
1546 */
1547 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001548compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001549{
1550 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001551 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001552 int count = offset;
1553
1554 buf = firstbuf;
1555 while (buf->b_next != NULL && buf->b_fnum < lnum)
1556 buf = buf->b_next;
1557 while (count != 0)
1558 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001559 count += (offset < 0) ? 1 : -1;
1560 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1561 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001562 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001563 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001564 if (addr_type == ADDR_LOADED_BUFFERS)
1565 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001566 while (buf->b_ml.ml_mfp == NULL)
1567 {
1568 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1569 if (nextbuf == NULL)
1570 break;
1571 buf = nextbuf;
1572 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001573 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001574 /* we might have gone too far, last buffer is not loadedd */
1575 if (addr_type == ADDR_LOADED_BUFFERS)
1576 while (buf->b_ml.ml_mfp == NULL)
1577 {
1578 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1579 if (nextbuf == NULL)
1580 break;
1581 buf = nextbuf;
1582 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001583 return buf->b_fnum;
1584}
1585
Bram Moolenaarb7316892019-05-01 18:08:42 +02001586/*
1587 * Return the window number of "win".
1588 * When "win" is NULL return the number of windows.
1589 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001590 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001591current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001592{
1593 win_T *wp;
1594 int nr = 0;
1595
Bram Moolenaar29323592016-07-24 22:04:11 +02001596 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001597 {
1598 ++nr;
1599 if (wp == win)
1600 break;
1601 }
1602 return nr;
1603}
1604
1605 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001606current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001607{
1608 tabpage_T *tp;
1609 int nr = 0;
1610
Bram Moolenaar29323592016-07-24 22:04:11 +02001611 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001612 {
1613 ++nr;
1614 if (tp == tab)
1615 break;
1616 }
1617 return nr;
1618}
1619
1620# define CURRENT_WIN_NR current_win_nr(curwin)
1621# define LAST_WIN_NR current_win_nr(NULL)
1622# define CURRENT_TAB_NR current_tab_nr(curtab)
1623# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625/*
1626 * Execute one Ex command.
1627 *
1628 * If 'sourcing' is TRUE, the command will be included in the error message.
1629 *
1630 * 1. skip comment lines and leading space
1631 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001632 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001634 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001635 * 6. parse arguments
1636 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001638 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 *
1640 * This function may be called recursively!
1641 */
1642#if (_MSC_VER == 1200)
1643/*
Bram Moolenaared203462004-06-16 11:19:22 +00001644 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001646 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#endif
1648 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001649do_one_cmd(
1650 char_u **cmdlinep,
1651 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001653 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001655 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657{
1658 char_u *p;
1659 linenr_T lnum;
1660 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001661 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001662 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001664 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001666 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001668 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 vim_memset(&ea, 0, sizeof(ea));
1671 ea.line1 = 1;
1672 ea.line2 = 1;
1673#ifdef FEAT_EVAL
1674 ++ex_nesting_level;
1675#endif
1676
Bram Moolenaar21691f82012-12-05 19:13:18 +01001677 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (quitmore
1679#ifdef FEAT_EVAL
1680 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001681 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001682#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001683 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001684 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 --quitmore;
1686
1687 /*
1688 * Reset browse, confirm, etc.. They are restored when returning, for
1689 * recursive calls.
1690 */
1691 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001693 /* "#!anything" is handled like a comment. */
1694 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1695 goto doend;
1696
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001697/*
1698 * 1. Skip comment lines and leading white space and colons.
1699 * 2. Handle command modifiers.
1700 */
1701 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001703 ea.cmdlinep = cmdlinep;
1704 ea.getline = fgetline;
1705 ea.cookie = cookie;
1706#ifdef FEAT_EVAL
1707 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001709 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001710 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001712 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714#ifdef FEAT_EVAL
1715 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1716 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1717#else
1718 ea.skip = (if_level > 0);
1719#endif
1720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001722 * 3. Skip over the range to find the command. Let "p" point to after it.
1723 *
1724 * We need the command to know what kind of range it uses.
1725 */
1726 cmd = ea.cmd;
1727 ea.cmd = skip_range(ea.cmd, NULL);
1728 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1729 ea.cmd = skipwhite(ea.cmd + 1);
1730 p = find_command(&ea, NULL);
1731
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001732#ifdef FEAT_EVAL
1733# ifdef FEAT_PROFILE
1734 // Count this line for profiling if skip is TRUE.
1735 if (do_profiling == PROF_YES
1736 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1737 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1738 {
1739 int skip = did_emsg || got_int || did_throw;
1740
1741 if (ea.cmdidx == CMD_catch)
1742 skip = !skip && !(cstack->cs_idx >= 0
1743 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1744 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1745 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1746 skip = skip || !(cstack->cs_idx >= 0
1747 && !(cstack->cs_flags[cstack->cs_idx]
1748 & (CSF_ACTIVE | CSF_TRUE)));
1749 else if (ea.cmdidx == CMD_finally)
1750 skip = FALSE;
1751 else if (ea.cmdidx != CMD_endif
1752 && ea.cmdidx != CMD_endfor
1753 && ea.cmdidx != CMD_endtry
1754 && ea.cmdidx != CMD_endwhile)
1755 skip = ea.skip;
1756
1757 if (!skip)
1758 {
1759 if (getline_equal(fgetline, cookie, get_func_line))
1760 func_line_exec(getline_cookie(fgetline, cookie));
1761 else if (getline_equal(fgetline, cookie, getsourceline))
1762 script_line_exec();
1763 }
1764 }
1765# endif
1766
1767 /* May go to debug mode. If this happens and the ">quit" debug command is
1768 * used, throw an interrupt exception and skip the next command. */
1769 dbg_check_breakpoint(&ea);
1770 if (!ea.skip && got_int)
1771 {
1772 ea.skip = TRUE;
1773 (void)do_intthrow(cstack);
1774 }
1775#endif
1776
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001777/*
1778 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 *
1780 * where 'addr' is:
1781 *
1782 * % (entire file)
1783 * $ [+-NUM]
1784 * 'x [+-NUM] (where x denotes a currently defined mark)
1785 * . [+-NUM]
1786 * [+-NUM]..
1787 * NUM
1788 *
1789 * The ea.cmd pointer is updated to point to the first character following the
1790 * range spec. If an initial address is found, but no second, the upper bound
1791 * is equal to the lower.
1792 */
1793
Bram Moolenaar25190db2019-05-04 15:05:28 +02001794 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001795 if (!IS_USER_CMDIDX(ea.cmdidx))
1796 {
1797 if (ea.cmdidx != CMD_SIZE)
1798 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1799 else
1800 ea.addr_type = ADDR_LINES;
1801
Bram Moolenaar25190db2019-05-04 15:05:28 +02001802 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001803 if (ea.cmdidx == CMD_wincmd && p != NULL)
1804 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001805#ifdef FEAT_QUICKFIX
1806 // :.cc in quickfix window uses line number
1807 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1808 ea.addr_type = ADDR_OTHER;
1809#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001810 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001811
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001812 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001813 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001814 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001817 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 */
1819
1820 /*
1821 * Skip ':' and any white space
1822 */
1823 ea.cmd = skipwhite(ea.cmd);
1824 while (*ea.cmd == ':')
1825 ea.cmd = skipwhite(ea.cmd + 1);
1826
1827 /*
1828 * If we got a line, but no command, then go to the line.
1829 * If we find a '|' or '\n' we set ea.nextcmd.
1830 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001831 if (*ea.cmd == NUL || *ea.cmd == '"'
1832 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
1834 /*
1835 * strange vi behaviour:
1836 * ":3" jumps to line 3
1837 * ":3|..." prints line 3
1838 * ":|" prints current line
1839 */
1840 if (ea.skip) /* skip this if inside :if */
1841 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001842 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 {
1844 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001845 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if ((errormsg = invalid_range(&ea)) == NULL)
1847 {
1848 correct_range(&ea);
1849 ex_print(&ea);
1850 }
1851 }
1852 else if (ea.addr_count != 0)
1853 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001854 if (ea.line2 > curbuf->b_ml.ml_line_count)
1855 {
1856 /* With '-' in 'cpoptions' a line number past the file is an
1857 * error, otherwise put it at the end of the file. */
1858 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1859 ea.line2 = -1;
1860 else
1861 ea.line2 = curbuf->b_ml.ml_line_count;
1862 }
1863
1864 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001865 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 else
1867 {
1868 if (ea.line2 == 0)
1869 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 else
1871 curwin->w_cursor.lnum = ea.line2;
1872 beginline(BL_SOL | BL_FIX);
1873 }
1874 }
1875 goto doend;
1876 }
1877
Bram Moolenaard5005162014-08-22 23:05:54 +02001878 /* If this looks like an undefined user command and there are CmdUndefined
1879 * autocommands defined, trigger the matching autocommands. */
1880 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1881 && ASCII_ISUPPER(*ea.cmd)
1882 && has_cmdundefined())
1883 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001884 int ret;
1885
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001886 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001887 while (ASCII_ISALNUM(*p))
1888 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001889 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001890 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1891 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001892 /* If the autocommands did something and didn't cause an error, try
1893 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001894 p = (ret
1895#ifdef FEAT_EVAL
1896 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001897#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001898 ) ? find_command(&ea, NULL) : ea.cmd;
1899 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (p == NULL)
1902 {
1903 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001904 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 goto doend;
1906 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001907 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001908 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1909 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 {
1911 errormsg = uc_fun_cmd();
1912 goto doend;
1913 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (ea.cmdidx == CMD_SIZE)
1916 {
1917 if (!ea.skip)
1918 {
1919 STRCPY(IObuff, _("E492: Not an editor command"));
1920 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001921 {
1922 /* If the modifier was parsed OK the error must be in the
1923 * following command */
1924 if (after_modifier != NULL)
1925 append_command(after_modifier);
1926 else
1927 append_command(*cmdlinep);
1928 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001929 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001930 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 }
1932 goto doend;
1933 }
1934
Bram Moolenaar958636c2014-10-21 20:01:58 +02001935 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1936 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001937#ifdef HAVE_EX_SCRIPT_NI
1938 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1939#endif
1940 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
1942#ifndef FEAT_EVAL
1943 /*
1944 * When the expression evaluation is disabled, recognize the ":if" and
1945 * ":endif" commands and ignore everything in between it.
1946 */
1947 if (ea.cmdidx == CMD_if)
1948 ++if_level;
1949 if (if_level)
1950 {
1951 if (ea.cmdidx == CMD_endif)
1952 --if_level;
1953 goto doend;
1954 }
1955
1956#endif
1957
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001958 /* forced commands */
1959 if (*p == '!' && ea.cmdidx != CMD_substitute
1960 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
1962 ++p;
1963 ea.forceit = TRUE;
1964 }
1965 else
1966 ea.forceit = FALSE;
1967
1968/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001969 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001971 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001972 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
1974 if (!ea.skip)
1975 {
1976#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001977 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001979 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001980 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 goto doend;
1982 }
1983#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001984 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001985 {
1986 errormsg = _("E981: Command not allowed in rvim");
1987 goto doend;
1988 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001989 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
1991 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001992 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 goto doend;
1994 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001995
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001996 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001997 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001999 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002000 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 goto doend;
2002 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002003
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002004 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002005 * Do allow ":checktime" (it is postponed).
2006 * Do allow ":edit" (check for an argument later).
2007 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002008 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002009 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002010 && ea.cmdidx != CMD_edit
2011 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002012 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002013 && curbuf_locked())
2014 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002016 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
2018 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002019 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 goto doend;
2021 }
2022 }
2023
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002024 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 goto doend;
2028 }
2029
2030 /*
2031 * Don't complain about the range if it is not used
2032 * (could happen if line_count is accidentally set to 0).
2033 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002034 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
2036 /*
2037 * If the range is backwards, ask for confirmation and, if given, swap
2038 * ea.line1 & ea.line2 so it's forwards again.
2039 * When global command is busy, don't ask, will fail below.
2040 */
2041 if (!global_busy && ea.line1 > ea.line2)
2042 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002043 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002045 if (sourcing || exmode_active)
2046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002047 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002048 goto doend;
2049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 if (ask_yesno((char_u *)
2051 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002052 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 }
2054 lnum = ea.line1;
2055 ea.line1 = ea.line2;
2056 ea.line2 = lnum;
2057 }
2058 if ((errormsg = invalid_range(&ea)) != NULL)
2059 goto doend;
2060 }
2061
Bram Moolenaarb7316892019-05-01 18:08:42 +02002062 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2063 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 ea.line2 = 1;
2065
2066 correct_range(&ea);
2067
2068#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002069 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002070 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
2072 /* Put the first line at the start of a closed fold, put the last line
2073 * at the end of a closed fold. */
2074 (void)hasFolding(ea.line1, &ea.line1, NULL);
2075 (void)hasFolding(ea.line2, NULL, &ea.line2);
2076 }
2077#endif
2078
2079#ifdef FEAT_QUICKFIX
2080 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002081 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 * option here, so things like % get expanded.
2083 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002084 p = replace_makeprg(&ea, p, cmdlinep);
2085 if (p == NULL)
2086 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087#endif
2088
2089 /*
2090 * Skip to start of argument.
2091 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2092 */
2093 if (ea.cmdidx == CMD_bang)
2094 ea.arg = p;
2095 else
2096 ea.arg = skipwhite(p);
2097
Bram Moolenaar379fb762018-08-30 15:58:28 +02002098 // ":file" cannot be run with an argument when "curbuf_lock" is set
2099 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2100 goto doend;
2101
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 /*
2103 * Check for "++opt=val" argument.
2104 * Must be first, allow ":w ++enc=utf8 !cmd"
2105 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002106 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2108 if (getargopt(&ea) == FAIL && !ni)
2109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 goto doend;
2112 }
2113
2114 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2115 {
2116 if (*ea.arg == '>') /* append */
2117 {
2118 if (*++ea.arg != '>') /* typed wrong */
2119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002120 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 goto doend;
2122 }
2123 ea.arg = skipwhite(ea.arg + 1);
2124 ea.append = TRUE;
2125 }
2126 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2127 {
2128 ++ea.arg;
2129 ea.usefilter = TRUE;
2130 }
2131 }
2132
2133 if (ea.cmdidx == CMD_read)
2134 {
2135 if (ea.forceit)
2136 {
2137 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2138 ea.forceit = FALSE;
2139 }
2140 else if (*ea.arg == '!') /* :r !filter */
2141 {
2142 ++ea.arg;
2143 ea.usefilter = TRUE;
2144 }
2145 }
2146
2147 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2148 {
2149 ea.amount = 1;
2150 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2151 {
2152 ++ea.arg;
2153 ++ea.amount;
2154 }
2155 ea.arg = skipwhite(ea.arg);
2156 }
2157
2158 /*
2159 * Check for "+command" argument, before checking for next command.
2160 * Don't do this for ":read !cmd" and ":write !cmd".
2161 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002162 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2164
2165 /*
2166 * Check for '|' to separate commands and '"' to start comments.
2167 * Don't do this for ":read !cmd" and ":write !cmd".
2168 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002169 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 separate_nextcmd(&ea);
2171
2172 /*
2173 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002174 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2175 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002177 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002178 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002179 || ea.cmdidx == CMD_global
2180 || ea.cmdidx == CMD_vglobal
2181 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
2183 for (p = ea.arg; *p; ++p)
2184 {
2185 /* Remove one backslash before a newline, so that it's possible to
2186 * pass a newline to the shell and also a newline that is preceded
2187 * with a backslash. This makes it impossible to end a shell
2188 * command in a backslash, but that doesn't appear useful.
2189 * Halving the number of backslashes is incompatible with previous
2190 * versions. */
2191 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002192 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 else if (*p == '\n')
2194 {
2195 ea.nextcmd = p + 1;
2196 *p = NUL;
2197 break;
2198 }
2199 }
2200 }
2201
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002202 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002204 buf_T *buf;
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002207 switch (ea.addr_type)
2208 {
2209 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002210 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002211 ea.line2 = curbuf->b_ml.ml_line_count;
2212 break;
2213 case ADDR_LOADED_BUFFERS:
2214 buf = firstbuf;
2215 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2216 buf = buf->b_next;
2217 ea.line1 = buf->b_fnum;
2218 buf = lastbuf;
2219 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2220 buf = buf->b_prev;
2221 ea.line2 = buf->b_fnum;
2222 break;
2223 case ADDR_BUFFERS:
2224 ea.line1 = firstbuf->b_fnum;
2225 ea.line2 = lastbuf->b_fnum;
2226 break;
2227 case ADDR_WINDOWS:
2228 ea.line2 = LAST_WIN_NR;
2229 break;
2230 case ADDR_TABS:
2231 ea.line2 = LAST_TAB_NR;
2232 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002233 case ADDR_TABS_RELATIVE:
2234 ea.line2 = 1;
2235 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002236 case ADDR_ARGUMENTS:
2237 if (ARGCOUNT == 0)
2238 ea.line1 = ea.line2 = 0;
2239 else
2240 ea.line2 = ARGCOUNT;
2241 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002242 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002243#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002244 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002245 if (ea.line2 == 0)
2246 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002247#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002248 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002249 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002250 case ADDR_UNSIGNED:
2251 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002252 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002253 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256
2257 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002258 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002260 /* Do not allow register = for user commands */
2261 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002262 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002264#ifndef FEAT_CLIPBOARD
2265 /* check these explicitly for a more specific error message */
2266 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002268 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002269 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002272 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2273 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002274 {
2275 ea.regname = *ea.arg++;
2276#ifdef FEAT_EVAL
2277 /* for '=' register: accept the rest of the line as an expression */
2278 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2279 {
2280 set_expr_line(vim_strsave(ea.arg));
2281 ea.arg += STRLEN(ea.arg);
2282 }
2283#endif
2284 ea.arg = skipwhite(ea.arg);
2285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287
2288 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002289 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 * count, it's a buffer name.
2291 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002292 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2293 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002294 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 n = getdigits(&ea.arg);
2297 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002298 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002300 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 goto doend;
2302 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002303 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 ea.line2 = n;
2306 if (ea.addr_count == 0)
2307 ea.addr_count = 1;
2308 }
2309 else
2310 {
2311 ea.line1 = ea.line2;
2312 ea.line2 += n - 1;
2313 ++ea.addr_count;
2314 /*
2315 * Be vi compatible: no error message for out of range.
2316 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002317 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 ea.line2 = curbuf->b_ml.ml_line_count;
2319 }
2320 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002321
2322 /*
2323 * Check for flags: 'l', 'p' and '#'.
2324 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002325 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002326 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002327 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2328 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002330 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002331 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 goto doend;
2333 }
2334
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002335 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002337 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 goto doend;
2339 }
2340
2341#ifdef FEAT_EVAL
2342 /*
2343 * Skip the command when it's not going to be executed.
2344 * The commands like :if, :endif, etc. always need to be executed.
2345 * Also make an exception for commands that handle a trailing command
2346 * themselves.
2347 */
2348 if (ea.skip)
2349 {
2350 switch (ea.cmdidx)
2351 {
2352 /* commands that need evaluation */
2353 case CMD_while:
2354 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002355 case CMD_for:
2356 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 case CMD_if:
2358 case CMD_elseif:
2359 case CMD_else:
2360 case CMD_endif:
2361 case CMD_try:
2362 case CMD_catch:
2363 case CMD_finally:
2364 case CMD_endtry:
2365 case CMD_function:
2366 break;
2367
2368 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002369 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 * the list at ":help :bar". */
2371 case CMD_aboveleft:
2372 case CMD_and:
2373 case CMD_belowright:
2374 case CMD_botright:
2375 case CMD_browse:
2376 case CMD_call:
2377 case CMD_confirm:
2378 case CMD_delfunction:
2379 case CMD_djump:
2380 case CMD_dlist:
2381 case CMD_dsearch:
2382 case CMD_dsplit:
2383 case CMD_echo:
2384 case CMD_echoerr:
2385 case CMD_echomsg:
2386 case CMD_echon:
2387 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002388 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 case CMD_help:
2390 case CMD_hide:
2391 case CMD_ijump:
2392 case CMD_ilist:
2393 case CMD_isearch:
2394 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002395 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 case CMD_keepjumps:
2397 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002398 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 case CMD_leftabove:
2400 case CMD_let:
2401 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002402 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002404 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002405 case CMD_noautocmd:
2406 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 case CMD_perl:
2408 case CMD_psearch:
2409 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002410 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002411 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 case CMD_return:
2413 case CMD_rightbelow:
2414 case CMD_ruby:
2415 case CMD_silent:
2416 case CMD_smagic:
2417 case CMD_snomagic:
2418 case CMD_substitute:
2419 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002420 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 case CMD_tcl:
2422 case CMD_throw:
2423 case CMD_tilde:
2424 case CMD_topleft:
2425 case CMD_unlet:
2426 case CMD_verbose:
2427 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002428 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 break;
2430
2431 default: goto doend;
2432 }
2433 }
2434#endif
2435
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002436 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 {
2438 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2439 goto doend;
2440 }
2441
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 /*
2443 * Accept buffer name. Cannot be used at the same time with a buffer
2444 * number. Don't do this for a user command.
2445 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002446 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002447 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 {
2449 /*
2450 * :bdelete, :bwipeout and :bunload take several arguments, separated
2451 * by spaces: find next space (skipping over escaped characters).
2452 * The others take one argument: ignore trailing spaces.
2453 */
2454 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2455 || ea.cmdidx == CMD_bunload)
2456 p = skiptowhite_esc(ea.arg);
2457 else
2458 {
2459 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002460 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 --p;
2462 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002463 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002464 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 if (ea.line2 < 0) /* failed */
2466 goto doend;
2467 ea.addr_count = 1;
2468 ea.arg = skipwhite(p);
2469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
Bram Moolenaar2be57332018-02-13 18:05:18 +01002471 /* The :try command saves the emsg_silent flag, reset it here when
2472 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002473 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002474 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002475 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002476 if (emsg_silent < 0)
2477 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002478 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002479 }
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002482 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
Bram Moolenaar958636c2014-10-21 20:01:58 +02002485 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 /*
2488 * Execute a user-defined command.
2489 */
2490 do_ucmd(&ea);
2491 }
2492 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 {
2494 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002495 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 */
2497 ea.errmsg = NULL;
2498 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2499 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002500 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
2502
2503#ifdef FEAT_EVAL
2504 /*
2505 * If the command just executed called do_cmdline(), any throw or ":return"
2506 * or ":finish" encountered there must also check the cstack of the still
2507 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2508 * exception, or reanimate a returned function or finished script file and
2509 * return or finish it again.
2510 */
2511 if (need_rethrow)
2512 do_throw(cstack);
2513 else if (check_cstack)
2514 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002515 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002517 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 && current_func_returned())
2519 do_return(&ea, TRUE, FALSE, NULL);
2520 }
2521 need_rethrow = check_cstack = FALSE;
2522#endif
2523
2524doend:
2525 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002528 curwin->w_cursor.col = 0;
2529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
2531 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2532 {
2533 if (sourcing)
2534 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002535 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
2537 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002538 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002540 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542 emsg(errormsg);
2543 }
2544#ifdef FEAT_EVAL
2545 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002546 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2547 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548#endif
2549
Bram Moolenaareffed932018-08-14 13:38:17 +02002550 if (ea.verbose_save >= 0)
2551 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002552
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002553 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002555 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaareffed932018-08-14 13:38:17 +02002557 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 /* messages could be enabled for a serious error, need to check if the
2560 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002561 if (!did_emsg || msg_silent > ea.save_msg_silent)
2562 msg_silent = ea.save_msg_silent;
2563 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 if (emsg_silent < 0)
2565 emsg_silent = 0;
2566 /* Restore msg_scroll, it's set by file I/O commands, even when no
2567 * message is actually displayed. */
2568 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002569
2570 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2571 * somewhere in the line. Put it back in the first column. */
2572 if (redirecting())
2573 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 }
2575
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002576#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002577 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002578 --sandbox;
2579#endif
2580
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2582 ea.nextcmd = NULL;
2583
2584#ifdef FEAT_EVAL
2585 --ex_nesting_level;
2586#endif
2587
2588 return ea.nextcmd;
2589}
2590#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002591 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#endif
2593
2594/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002595 * Parse and skip over command modifiers:
2596 * - update eap->cmd
2597 * - store flags in "cmdmod".
2598 * - Set ex_pressedreturn for an empty command line.
2599 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002600 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002601 * - set p_verbose for ":verbose"
2602 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002603 * When "skip_only" is TRUE the global variables are not changed, except for
2604 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002605 * Return FAIL when the command is not to be executed.
2606 * May set "errormsg" to an error message.
2607 */
2608 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002609parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002610{
2611 char_u *p;
2612
2613 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2614 eap->verbose_save = -1;
2615 eap->save_msg_silent = -1;
2616
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002617 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002618 for (;;)
2619 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002620 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2621 ++eap->cmd;
2622
2623 /* in ex mode, an empty line works like :+ */
2624 if (*eap->cmd == NUL && exmode_active
2625 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2626 || getline_equal(eap->getline, eap->cookie, getexline))
2627 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2628 {
2629 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002630 if (!skip_only)
2631 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 }
2633
2634 /* ignore comment and empty lines */
2635 if (*eap->cmd == '"')
2636 return FAIL;
2637 if (*eap->cmd == NUL)
2638 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002639 if (!skip_only)
2640 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002641 return FAIL;
2642 }
2643
Bram Moolenaareffed932018-08-14 13:38:17 +02002644 p = skip_range(eap->cmd, NULL);
2645 switch (*p)
2646 {
2647 /* When adding an entry, also modify cmd_exists(). */
2648 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2649 break;
2650 cmdmod.split |= WSP_ABOVE;
2651 continue;
2652
2653 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2654 {
2655 cmdmod.split |= WSP_BELOW;
2656 continue;
2657 }
2658 if (checkforcmd(&eap->cmd, "browse", 3))
2659 {
2660#ifdef FEAT_BROWSE_CMD
2661 cmdmod.browse = TRUE;
2662#endif
2663 continue;
2664 }
2665 if (!checkforcmd(&eap->cmd, "botright", 2))
2666 break;
2667 cmdmod.split |= WSP_BOT;
2668 continue;
2669
2670 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2671 break;
2672#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2673 cmdmod.confirm = TRUE;
2674#endif
2675 continue;
2676
2677 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2678 {
2679 cmdmod.keepmarks = TRUE;
2680 continue;
2681 }
2682 if (checkforcmd(&eap->cmd, "keepalt", 5))
2683 {
2684 cmdmod.keepalt = TRUE;
2685 continue;
2686 }
2687 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2688 {
2689 cmdmod.keeppatterns = TRUE;
2690 continue;
2691 }
2692 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2693 break;
2694 cmdmod.keepjumps = TRUE;
2695 continue;
2696
2697 case 'f': /* only accept ":filter {pat} cmd" */
2698 {
2699 char_u *reg_pat;
2700
2701 if (!checkforcmd(&p, "filter", 4)
2702 || *p == NUL || ends_excmd(*p))
2703 break;
2704 if (*p == '!')
2705 {
2706 cmdmod.filter_force = TRUE;
2707 p = skipwhite(p + 1);
2708 if (*p == NUL || ends_excmd(*p))
2709 break;
2710 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002711 if (skip_only)
2712 p = skip_vimgrep_pat(p, NULL, NULL);
2713 else
2714 // NOTE: This puts a NUL after the pattern.
2715 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002716 if (p == NULL || *p == NUL)
2717 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002718 if (!skip_only)
2719 {
2720 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002721 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002722 if (cmdmod.filter_regmatch.regprog == NULL)
2723 break;
2724 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002725 eap->cmd = p;
2726 continue;
2727 }
2728
2729 /* ":hide" and ":hide | cmd" are not modifiers */
2730 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2731 || *p == NUL || ends_excmd(*p))
2732 break;
2733 eap->cmd = p;
2734 cmdmod.hide = TRUE;
2735 continue;
2736
2737 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2738 {
2739 cmdmod.lockmarks = TRUE;
2740 continue;
2741 }
2742
2743 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2744 break;
2745 cmdmod.split |= WSP_ABOVE;
2746 continue;
2747
2748 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2749 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002750 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 {
2752 /* Set 'eventignore' to "all". Restore the
2753 * existing option value later. */
2754 cmdmod.save_ei = vim_strsave(p_ei);
2755 set_string_option_direct((char_u *)"ei", -1,
2756 (char_u *)"all", OPT_FREE, SID_NONE);
2757 }
2758 continue;
2759 }
2760 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2761 break;
2762 cmdmod.noswapfile = TRUE;
2763 continue;
2764
2765 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2766 break;
2767 cmdmod.split |= WSP_BELOW;
2768 continue;
2769
2770 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2771 {
2772#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002773 if (!skip_only)
2774 {
2775 if (!eap->did_sandbox)
2776 ++sandbox;
2777 eap->did_sandbox = TRUE;
2778 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002779#endif
2780 continue;
2781 }
2782 if (!checkforcmd(&eap->cmd, "silent", 3))
2783 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002784 if (!skip_only)
2785 {
2786 if (eap->save_msg_silent == -1)
2787 eap->save_msg_silent = msg_silent;
2788 ++msg_silent;
2789 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2791 {
2792 /* ":silent!", but not "silent !cmd" */
2793 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002794 if (!skip_only)
2795 {
2796 ++emsg_silent;
2797 ++eap->did_esilent;
2798 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 }
2800 continue;
2801
2802 case 't': if (checkforcmd(&p, "tab", 3))
2803 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002804 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002806 long tabnr = get_address(eap, &eap->cmd,
2807 ADDR_TABS, eap->skip,
2808 skip_only, FALSE, 1);
2809 if (tabnr == MAXLNUM)
2810 cmdmod.tab = tabpage_index(curtab) + 1;
2811 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002812 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002813 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2814 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002815 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002816 return FAIL;
2817 }
2818 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 }
2821 eap->cmd = p;
2822 continue;
2823 }
2824 if (!checkforcmd(&eap->cmd, "topleft", 2))
2825 break;
2826 cmdmod.split |= WSP_TOP;
2827 continue;
2828
2829 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2830 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (!skip_only)
2832 {
2833 if (eap->save_msg_silent == -1)
2834 eap->save_msg_silent = msg_silent;
2835 msg_silent = 0;
2836 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 continue;
2838
2839 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2840 {
2841 cmdmod.split |= WSP_VERT;
2842 continue;
2843 }
2844 if (!checkforcmd(&p, "verbose", 4))
2845 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002846 if (!skip_only)
2847 {
2848 if (eap->verbose_save < 0)
2849 eap->verbose_save = p_verbose;
2850 if (vim_isdigit(*eap->cmd))
2851 p_verbose = atoi((char *)eap->cmd);
2852 else
2853 p_verbose = 1;
2854 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 eap->cmd = p;
2856 continue;
2857 }
2858 break;
2859 }
2860
2861 return OK;
2862}
2863
2864/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002865 * Free contents of "cmdmod".
2866 */
2867 static void
2868free_cmdmod(void)
2869{
2870 if (cmdmod.save_ei != NULL)
2871 {
2872 /* Restore 'eventignore' to the value before ":noautocmd". */
2873 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2874 OPT_FREE, SID_NONE);
2875 free_string_option(cmdmod.save_ei);
2876 }
2877
2878 if (cmdmod.filter_regmatch.regprog != NULL)
2879 vim_regfree(cmdmod.filter_regmatch.regprog);
2880}
2881
2882/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002883 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002884 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002885 * Return FAIL and set "errormsg" or return OK.
2886 */
2887 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002888parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002889{
2890 int address_count = 1;
2891 linenr_T lnum;
2892
2893 // Repeat for all ',' or ';' separated addresses.
2894 for (;;)
2895 {
2896 eap->line1 = eap->line2;
2897 switch (eap->addr_type)
2898 {
2899 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002900 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002901 // default is current line number
2902 eap->line2 = curwin->w_cursor.lnum;
2903 break;
2904 case ADDR_WINDOWS:
2905 eap->line2 = CURRENT_WIN_NR;
2906 break;
2907 case ADDR_ARGUMENTS:
2908 eap->line2 = curwin->w_arg_idx + 1;
2909 if (eap->line2 > ARGCOUNT)
2910 eap->line2 = ARGCOUNT;
2911 break;
2912 case ADDR_LOADED_BUFFERS:
2913 case ADDR_BUFFERS:
2914 eap->line2 = curbuf->b_fnum;
2915 break;
2916 case ADDR_TABS:
2917 eap->line2 = CURRENT_TAB_NR;
2918 break;
2919 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002920 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002921 eap->line2 = 1;
2922 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002923 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002924#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002925 eap->line2 = qf_get_cur_idx(eap);
2926#endif
2927 break;
2928 case ADDR_QUICKFIX_VALID:
2929#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002930 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002931#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002932 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002933 case ADDR_NONE:
2934 // Will give an error later if a range is found.
2935 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002936 }
2937 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002938 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002939 eap->addr_count == 0, address_count++);
2940 if (eap->cmd == NULL) // error detected
2941 return FAIL;
2942 if (lnum == MAXLNUM)
2943 {
2944 if (*eap->cmd == '%') // '%' - all lines
2945 {
2946 ++eap->cmd;
2947 switch (eap->addr_type)
2948 {
2949 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002950 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002951 eap->line1 = 1;
2952 eap->line2 = curbuf->b_ml.ml_line_count;
2953 break;
2954 case ADDR_LOADED_BUFFERS:
2955 {
2956 buf_T *buf = firstbuf;
2957
2958 while (buf->b_next != NULL
2959 && buf->b_ml.ml_mfp == NULL)
2960 buf = buf->b_next;
2961 eap->line1 = buf->b_fnum;
2962 buf = lastbuf;
2963 while (buf->b_prev != NULL
2964 && buf->b_ml.ml_mfp == NULL)
2965 buf = buf->b_prev;
2966 eap->line2 = buf->b_fnum;
2967 break;
2968 }
2969 case ADDR_BUFFERS:
2970 eap->line1 = firstbuf->b_fnum;
2971 eap->line2 = lastbuf->b_fnum;
2972 break;
2973 case ADDR_WINDOWS:
2974 case ADDR_TABS:
2975 if (IS_USER_CMDIDX(eap->cmdidx))
2976 {
2977 eap->line1 = 1;
2978 eap->line2 = eap->addr_type == ADDR_WINDOWS
2979 ? LAST_WIN_NR : LAST_TAB_NR;
2980 }
2981 else
2982 {
2983 // there is no Vim command which uses '%' and
2984 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002985 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002986 return FAIL;
2987 }
2988 break;
2989 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002990 case ADDR_UNSIGNED:
2991 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002992 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002993 return FAIL;
2994 case ADDR_ARGUMENTS:
2995 if (ARGCOUNT == 0)
2996 eap->line1 = eap->line2 = 0;
2997 else
2998 {
2999 eap->line1 = 1;
3000 eap->line2 = ARGCOUNT;
3001 }
3002 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003003 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003004#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003006 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 if (eap->line2 == 0)
3008 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003010 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003011 case ADDR_NONE:
3012 // Will give an error later if a range is found.
3013 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003014 }
3015 ++eap->addr_count;
3016 }
3017 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3018 {
3019 pos_T *fp;
3020
3021 // '*' - visual area
3022 if (eap->addr_type != ADDR_LINES)
3023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003024 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003025 return FAIL;
3026 }
3027
3028 ++eap->cmd;
3029 if (!eap->skip)
3030 {
3031 fp = getmark('<', FALSE);
3032 if (check_mark(fp) == FAIL)
3033 return FAIL;
3034 eap->line1 = fp->lnum;
3035 fp = getmark('>', FALSE);
3036 if (check_mark(fp) == FAIL)
3037 return FAIL;
3038 eap->line2 = fp->lnum;
3039 ++eap->addr_count;
3040 }
3041 }
3042 }
3043 else
3044 eap->line2 = lnum;
3045 eap->addr_count++;
3046
3047 if (*eap->cmd == ';')
3048 {
3049 if (!eap->skip)
3050 {
3051 curwin->w_cursor.lnum = eap->line2;
3052 // don't leave the cursor on an illegal line or column
3053 check_cursor();
3054 }
3055 }
3056 else if (*eap->cmd != ',')
3057 break;
3058 ++eap->cmd;
3059 }
3060
3061 // One address given: set start and end lines.
3062 if (eap->addr_count == 1)
3063 {
3064 eap->line1 = eap->line2;
3065 // ... but only implicit: really no address given
3066 if (lnum == MAXLNUM)
3067 eap->addr_count = 0;
3068 }
3069 return OK;
3070}
3071
3072/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003073 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 * If there is a match advance "pp" to the argument and return TRUE.
3075 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003076 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003077checkforcmd(
3078 char_u **pp, /* start of command */
3079 char *cmd, /* name of command */
3080 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 int i;
3083
3084 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003085 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 break;
3087 if (i >= len && !isalpha((*pp)[i]))
3088 {
3089 *pp = skipwhite(*pp + i);
3090 return TRUE;
3091 }
3092 return FALSE;
3093}
3094
3095/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003096 * Append "cmd" to the error message in IObuff.
3097 * Takes care of limiting the length and handling 0xa0, which would be
3098 * invisible otherwise.
3099 */
3100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003101append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003102{
3103 char_u *s = cmd;
3104 char_u *d;
3105
3106 STRCAT(IObuff, ": ");
3107 d = IObuff + STRLEN(IObuff);
3108 while (*s != NUL && d - IObuff < IOSIZE - 7)
3109 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003110 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003111 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003112 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003113 STRCPY(d, "<a0>");
3114 d += 4;
3115 }
3116 else
3117 MB_COPY_CHAR(s, d);
3118 }
3119 *d = NUL;
3120}
3121
3122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003124 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003125 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * Returns NULL for an ambiguous user command.
3128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003130find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 int len;
3133 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003134 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135
3136 /*
3137 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003138 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 * - the 'k' command can directly be followed by any character.
3140 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003141 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003143 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 */
3145 p = eap->cmd;
3146 if (*p == 'k')
3147 {
3148 eap->cmdidx = CMD_k;
3149 ++p;
3150 }
3151 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003152 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3153 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 || p[1] == 'g'
3155 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3156 || p[1] == 'I'
3157 || (p[1] == 'r' && p[2] != 'e')))
3158 {
3159 eap->cmdidx = CMD_substitute;
3160 ++p;
3161 }
3162 else
3163 {
3164 while (ASCII_ISALPHA(*p))
3165 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003166 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003167 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003168 while (ASCII_ISALNUM(*p))
3169 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003170
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 /* check for non-alpha command */
3172 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3173 ++p;
3174 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003175 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3176 {
3177 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3178 * :delete with the 'l' flag. Same for 'p'. */
3179 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003180 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003181 break;
3182 if (i == len - 1)
3183 {
3184 --len;
3185 if (p[-1] == 'l')
3186 eap->flags |= EXFLAG_LIST;
3187 else
3188 eap->flags |= EXFLAG_PRINT;
3189 }
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003192 if (ASCII_ISLOWER(eap->cmd[0]))
3193 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003194 int c1 = eap->cmd[0];
3195 int c2 = eap->cmd[1];
3196
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003197 if (command_count != (int)CMD_SIZE)
3198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003199 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003200 getout(1);
3201 }
3202
3203 /* Use a precomputed index for fast look-up in cmdnames[]
3204 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003205 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3206 if (ASCII_ISLOWER(c2))
3207 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003210 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
3212 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3213 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3214 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3215 (size_t)len) == 0)
3216 {
3217#ifdef FEAT_EVAL
3218 if (full != NULL
3219 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3220 *full = TRUE;
3221#endif
3222 break;
3223 }
3224
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003225 // Look for a user defined command as a last resort. Let ":Print" be
3226 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003227 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3228 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003230 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 while (ASCII_ISALNUM(*p))
3232 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 eap->cmdidx = CMD_SIZE;
3237 }
3238
3239 return p;
3240}
3241
3242#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003243static struct cmdmod
3244{
3245 char *name;
3246 int minlen;
3247 int has_count; /* :123verbose :3tab */
3248} cmdmods[] = {
3249 {"aboveleft", 3, FALSE},
3250 {"belowright", 3, FALSE},
3251 {"botright", 2, FALSE},
3252 {"browse", 3, FALSE},
3253 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003254 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003255 {"hide", 3, FALSE},
3256 {"keepalt", 5, FALSE},
3257 {"keepjumps", 5, FALSE},
3258 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003259 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003260 {"leftabove", 5, FALSE},
3261 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003262 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003263 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003264 {"rightbelow", 6, FALSE},
3265 {"sandbox", 3, FALSE},
3266 {"silent", 3, FALSE},
3267 {"tab", 3, TRUE},
3268 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003269 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003270 {"verbose", 4, TRUE},
3271 {"vertical", 4, FALSE},
3272};
3273
3274/*
3275 * Return length of a command modifier (including optional count).
3276 * Return zero when it's not a modifier.
3277 */
3278 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003279modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003280{
3281 int i, j;
3282 char_u *p = cmd;
3283
3284 if (VIM_ISDIGIT(*cmd))
3285 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003286 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003287 {
3288 for (j = 0; p[j] != NUL; ++j)
3289 if (p[j] != cmdmods[i].name[j])
3290 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003291 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003292 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003293 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003294 }
3295 return 0;
3296}
3297
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298/*
3299 * Return > 0 if an Ex command "name" exists.
3300 * Return 2 if there is an exact match.
3301 * Return 3 if there is an ambiguous match.
3302 */
3303 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003304cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305{
3306 exarg_T ea;
3307 int full = FALSE;
3308 int i;
3309 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003310 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
3312 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003313 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
3315 for (j = 0; name[j] != NUL; ++j)
3316 if (name[j] != cmdmods[i].name[j])
3317 break;
3318 if (name[j] == NUL && j >= cmdmods[i].minlen)
3319 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3320 }
3321
Bram Moolenaara9587612006-05-04 21:47:50 +00003322 /* Check built-in commands and user defined commands.
3323 * For ":2match" and ":3match" we need to skip the number. */
3324 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003326 p = find_command(&ea, &full);
3327 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003329 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3330 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003331 if (*skipwhite(p) != NUL)
3332 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3334}
3335#endif
3336
3337/*
3338 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3339 * we don't need/want deleted. Maybe this could be done better if we didn't
3340 * repeat all this stuff. The only problem is that they may not stay
3341 * perfectly compatible with each other, but then the command line syntax
3342 * probably won't change that much -- webb.
3343 */
3344 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003345set_one_cmd_context(
3346 expand_T *xp,
3347 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348{
3349 char_u *p;
3350 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003351 int len = 0;
3352 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003354 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 int delim;
3356#endif
3357 int forceit = FALSE;
3358 int usefilter = FALSE; /* filter instead of file name */
3359
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003360 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 xp->xp_pattern = buff;
3362 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003363 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003366 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 */
3368 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3369 ;
3370 xp->xp_pattern = cmd;
3371
3372 if (*cmd == NUL)
3373 return NULL;
3374 if (*cmd == '"') /* ignore comment lines */
3375 {
3376 xp->xp_context = EXPAND_NOTHING;
3377 return NULL;
3378 }
3379
3380/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003381 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 */
3383 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 xp->xp_pattern = cmd;
3385 if (*cmd == NUL)
3386 return NULL;
3387 if (*cmd == '"')
3388 {
3389 xp->xp_context = EXPAND_NOTHING;
3390 return NULL;
3391 }
3392
3393 if (*cmd == '|' || *cmd == '\n')
3394 return cmd + 1; /* There's another command */
3395
3396 /*
3397 * Isolate the command and search for it in the command table.
3398 * Exceptions:
3399 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003400 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3402 */
3403 if (*cmd == 'k' && cmd[1] != 'e')
3404 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003405 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 p = cmd + 1;
3407 }
3408 else
3409 {
3410 p = cmd;
3411 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3412 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003413 /* a user command may contain digits */
3414 if (ASCII_ISUPPER(cmd[0]))
3415 while (ASCII_ISALNUM(*p) || *p == '*')
3416 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003417 /* for python 3.x: ":py3*" commands completion */
3418 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003419 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003420 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003421 while (ASCII_ISALPHA(*p) || *p == '*')
3422 ++p;
3423 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003424 /* check for non-alpha command */
3425 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3426 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
3431 xp->xp_context = EXPAND_UNSUCCESSFUL;
3432 return NULL;
3433 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003434 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003435 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3436 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3437 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 break;
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003441 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444
3445 /*
3446 * If the cursor is touching the command, and it ends in an alpha-numeric
3447 * character, complete the command name.
3448 */
3449 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3450 return NULL;
3451
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003452 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3455 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 p = cmd + 1;
3458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3460 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003461 ea.cmd = cmd;
3462 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003463#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003464 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003465#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003466 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003467#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003469 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003470 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003473 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
3475 /* Not still touching the command and it was an illegal one */
3476 xp->xp_context = EXPAND_UNSUCCESSFUL;
3477 return NULL;
3478 }
3479
3480 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3481
3482 if (*p == '!') /* forced commands */
3483 {
3484 forceit = TRUE;
3485 ++p;
3486 }
3487
3488/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003489 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003491 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003492 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493
3494 arg = skipwhite(p);
3495
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003496 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 {
3498 if (*arg == '>') /* append */
3499 {
3500 if (*++arg == '>')
3501 ++arg;
3502 arg = skipwhite(arg);
3503 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 ++arg;
3507 usefilter = TRUE;
3508 }
3509 }
3510
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 usefilter = forceit; /* :r! filter if forced */
3514 if (*arg == '!') /* :r !filter */
3515 {
3516 ++arg;
3517 usefilter = TRUE;
3518 }
3519 }
3520
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003521 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
3523 while (*arg == *cmd) /* allow any number of '>' or '<' */
3524 ++arg;
3525 arg = skipwhite(arg);
3526 }
3527
3528 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003529 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 /* Check if we're in the +command */
3532 p = arg + 1;
3533 arg = skip_cmd_arg(arg, FALSE);
3534
3535 /* Still touching the command after '+'? */
3536 if (*arg == NUL)
3537 return p;
3538
3539 /* Skip space(s) after +command to get to the real argument */
3540 arg = skipwhite(arg);
3541 }
3542
3543 /*
3544 * Check for '|' to separate commands and '"' to start comments.
3545 * Don't do this for ":read !cmd" and ":write !cmd".
3546 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003547 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 p = arg;
3550 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003551 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 p += 2;
3553 while (*p)
3554 {
3555 if (*p == Ctrl_V)
3556 {
3557 if (p[1] != NUL)
3558 ++p;
3559 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003560 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 || *p == '|' || *p == '\n')
3562 {
3563 if (*(p - 1) != '\\')
3564 {
3565 if (*p == '|' || *p == '\n')
3566 return p + 1;
3567 return NULL; /* It's a comment */
3568 }
3569 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003570 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 }
3572 }
3573
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003574 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3575 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3576 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 return NULL;
3578
3579 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003580 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003582 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003583 while (*p && p < buff + len)
3584 {
3585 if (*p == ' ' || *p == TAB)
3586 {
3587 /* argument starts after a space */
3588 xp->xp_pattern = ++p;
3589 }
3590 else
3591 {
3592 if (*p == '\\' && *(p + 1) != NUL)
3593 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003594 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003595 }
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003598 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003600 int c;
3601 int in_quote = FALSE;
3602 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604 /*
3605 * Allow spaces within back-quotes to count as part of the argument
3606 * being expanded.
3607 */
3608 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003609 p = xp->xp_pattern;
3610 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003612 if (has_mbyte)
3613 c = mb_ptr2char(p);
3614 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003615 c = *p;
3616 if (c == '\\' && p[1] != NUL)
3617 ++p;
3618 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 {
3620 if (!in_quote)
3621 {
3622 xp->xp_pattern = p;
3623 bow = p + 1;
3624 }
3625 in_quote = !in_quote;
3626 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003627 /* An argument can contain just about everything, except
3628 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003629 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003630#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003631 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003632#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003633 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003634 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003635 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003636 while (*p != NUL)
3637 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003638 if (has_mbyte)
3639 c = mb_ptr2char(p);
3640 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003641 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003642 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003643 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003644 if (has_mbyte)
3645 len = (*mb_ptr2len)(p);
3646 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003647 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003648 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003649 }
3650 if (in_quote)
3651 bow = p;
3652 else
3653 xp->xp_pattern = p;
3654 p -= len;
3655 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003656 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
3658
3659 /*
3660 * If we are still inside the quotes, and we passed a space, just
3661 * expand from there.
3662 */
3663 if (bow != NULL && in_quote)
3664 xp->xp_pattern = bow;
3665 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003666
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003667 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003668 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003669 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003670#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003671 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003672#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003673 /* When still after the command name expand executables. */
3674 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003675 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 /* Check for environment variable */
3679 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003680#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 || *xp->xp_pattern == '%'
3682#endif
3683 )
3684 {
3685 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3686 if (!vim_isIDc(*p))
3687 break;
3688 if (*p == NUL)
3689 {
3690 xp->xp_context = EXPAND_ENV_VARS;
3691 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003692#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003693 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003694 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003695 compl = EXPAND_ENV_VARS;
3696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 }
3698 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003699#if defined(FEAT_CMDL_COMPL)
3700 /* Check for user names */
3701 if (*xp->xp_pattern == '~')
3702 {
3703 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3704 ;
3705 /* Complete ~user only if it partially matches a user name.
3706 * A full match ~user<Tab> will be replaced by user's home
3707 * directory i.e. something like ~user<Tab> -> /home/user/ */
3708 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003709 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003710 {
3711 xp->xp_context = EXPAND_USER;
3712 ++xp->xp_pattern;
3713 }
3714 }
3715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 }
3717
3718/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003719 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003721 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003723 case CMD_find:
3724 case CMD_sfind:
3725 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003726 if (xp->xp_context == EXPAND_FILES)
3727 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 case CMD_cd:
3730 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003731 case CMD_tcd:
3732 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 case CMD_lcd:
3734 case CMD_lchdir:
3735 if (xp->xp_context == EXPAND_FILES)
3736 xp->xp_context = EXPAND_DIRECTORIES;
3737 break;
3738 case CMD_help:
3739 xp->xp_context = EXPAND_HELP;
3740 xp->xp_pattern = arg;
3741 break;
3742
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003743 /* Command modifiers: return the argument.
3744 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003746 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 case CMD_belowright:
3748 case CMD_botright:
3749 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003750 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003751 case CMD_cdo:
3752 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003754 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 case CMD_folddoclosed:
3756 case CMD_folddoopen:
3757 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003758 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 case CMD_keepjumps:
3760 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003761 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003762 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003764 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003766 case CMD_noautocmd:
3767 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003769 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003771 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003772 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 case CMD_topleft:
3774 case CMD_verbose:
3775 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003776 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 return arg;
3778
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003779 case CMD_filter:
3780 if (*arg != NUL)
3781 arg = skip_vimgrep_pat(arg, NULL, NULL);
3782 if (arg == NULL || *arg == NUL)
3783 {
3784 xp->xp_context = EXPAND_NOTHING;
3785 return NULL;
3786 }
3787 return skipwhite(arg);
3788
Bram Moolenaar4f688582007-07-24 12:34:30 +00003789#ifdef FEAT_CMDL_COMPL
3790# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 case CMD_match:
3792 if (*arg == NUL || !ends_excmd(*arg))
3793 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003794 /* also complete "None" */
3795 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 arg = skipwhite(skiptowhite(arg));
3797 if (*arg != NUL)
3798 {
3799 xp->xp_context = EXPAND_NOTHING;
3800 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3801 }
3802 }
3803 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806/*
3807 * All completion for the +cmdline_compl feature goes here.
3808 */
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003811 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812
3813 case CMD_delcommand:
3814 xp->xp_context = EXPAND_USER_COMMANDS;
3815 xp->xp_pattern = arg;
3816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
3818 case CMD_global:
3819 case CMD_vglobal:
3820 delim = *arg; /* get the delimiter */
3821 if (delim)
3822 ++arg; /* skip delimiter if there is one */
3823
3824 while (arg[0] != NUL && arg[0] != delim)
3825 {
3826 if (arg[0] == '\\' && arg[1] != NUL)
3827 ++arg;
3828 ++arg;
3829 }
3830 if (arg[0] != NUL)
3831 return arg + 1;
3832 break;
3833 case CMD_and:
3834 case CMD_substitute:
3835 delim = *arg;
3836 if (delim)
3837 {
3838 /* skip "from" part */
3839 ++arg;
3840 arg = skip_regexp(arg, delim, p_magic, NULL);
3841 }
3842 /* skip "to" part */
3843 while (arg[0] != NUL && arg[0] != delim)
3844 {
3845 if (arg[0] == '\\' && arg[1] != NUL)
3846 ++arg;
3847 ++arg;
3848 }
3849 if (arg[0] != NUL) /* skip delimiter */
3850 ++arg;
3851 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3852 ++arg;
3853 if (arg[0] != NUL)
3854 return arg;
3855 break;
3856 case CMD_isearch:
3857 case CMD_dsearch:
3858 case CMD_ilist:
3859 case CMD_dlist:
3860 case CMD_ijump:
3861 case CMD_psearch:
3862 case CMD_djump:
3863 case CMD_isplit:
3864 case CMD_dsplit:
3865 arg = skipwhite(skipdigits(arg)); /* skip count */
3866 if (*arg == '/') /* Match regexp, not just whole words */
3867 {
3868 for (++arg; *arg && *arg != '/'; arg++)
3869 if (*arg == '\\' && arg[1] != NUL)
3870 arg++;
3871 if (*arg)
3872 {
3873 arg = skipwhite(arg + 1);
3874
3875 /* Check for trailing illegal characters */
3876 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3877 xp->xp_context = EXPAND_NOTHING;
3878 else
3879 return arg;
3880 }
3881 }
3882 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 case CMD_autocmd:
3885 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003887 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_set:
3890 set_context_in_set_cmd(xp, arg, 0);
3891 break;
3892 case CMD_setglobal:
3893 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3894 break;
3895 case CMD_setlocal:
3896 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3897 break;
3898 case CMD_tag:
3899 case CMD_stag:
3900 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003901 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 case CMD_tselect:
3903 case CMD_stselect:
3904 case CMD_ptselect:
3905 case CMD_tjump:
3906 case CMD_stjump:
3907 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003908 if (*p_wop != NUL)
3909 xp->xp_context = EXPAND_TAGS_LISTFILES;
3910 else
3911 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 xp->xp_pattern = arg;
3913 break;
3914 case CMD_augroup:
3915 xp->xp_context = EXPAND_AUGROUP;
3916 xp->xp_pattern = arg;
3917 break;
3918#ifdef FEAT_SYN_HL
3919 case CMD_syntax:
3920 set_context_in_syntax_cmd(xp, arg);
3921 break;
3922#endif
3923#ifdef FEAT_EVAL
3924 case CMD_let:
3925 case CMD_if:
3926 case CMD_elseif:
3927 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003928 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 case CMD_echo:
3930 case CMD_echon:
3931 case CMD_execute:
3932 case CMD_echomsg:
3933 case CMD_echoerr:
3934 case CMD_call:
3935 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003936 case CMD_cexpr:
3937 case CMD_caddexpr:
3938 case CMD_cgetexpr:
3939 case CMD_lexpr:
3940 case CMD_laddexpr:
3941 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003942 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 break;
3944
3945 case CMD_unlet:
3946 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3947 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 xp->xp_context = EXPAND_USER_VARS;
3950 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003951
3952 if (*xp->xp_pattern == '$')
3953 {
3954 xp->xp_context = EXPAND_ENV_VARS;
3955 ++xp->xp_pattern;
3956 }
3957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 break;
3959
3960 case CMD_function:
3961 case CMD_delfunction:
3962 xp->xp_context = EXPAND_USER_FUNC;
3963 xp->xp_pattern = arg;
3964 break;
3965
3966 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003967 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 break;
3969#endif
3970 case CMD_highlight:
3971 set_context_in_highlight_cmd(xp, arg);
3972 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003973#ifdef FEAT_CSCOPE
3974 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003975 case CMD_lcscope:
3976 case CMD_scscope:
3977 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003978 break;
3979#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003980#ifdef FEAT_SIGNS
3981 case CMD_sign:
3982 set_context_in_sign_cmd(xp, arg);
3983 break;
3984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 case CMD_bdelete:
3986 case CMD_bwipeout:
3987 case CMD_bunload:
3988 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3989 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003990 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 case CMD_buffer:
3992 case CMD_sbuffer:
3993 case CMD_checktime:
3994 xp->xp_context = EXPAND_BUFFERS;
3995 xp->xp_pattern = arg;
3996 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 case CMD_USER:
3999 case CMD_USER_BUF:
4000 if (compl != EXPAND_NOTHING)
4001 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004002 // EX_XFILE: file names are handled above
4003 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004005#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (compl == EXPAND_MENUS)
4007 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 if (compl == EXPAND_COMMANDS)
4010 return arg;
4011 if (compl == EXPAND_MAPPINGS)
4012 return set_context_in_map_cmd(xp, (char_u *)"map",
4013 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004014 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004015 p = arg;
4016 while (*p)
4017 {
4018 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004019 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004020 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004021 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004022 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004023 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 xp->xp_pattern = arg;
4026 }
4027 xp->xp_context = compl;
4028 }
4029 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 case CMD_map: case CMD_noremap:
4032 case CMD_nmap: case CMD_nnoremap:
4033 case CMD_vmap: case CMD_vnoremap:
4034 case CMD_omap: case CMD_onoremap:
4035 case CMD_imap: case CMD_inoremap:
4036 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004037 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004038 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004039 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004040 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004042 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 case CMD_unmap:
4044 case CMD_nunmap:
4045 case CMD_vunmap:
4046 case CMD_ounmap:
4047 case CMD_iunmap:
4048 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004049 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004050 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004051 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004052 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004054 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004055 case CMD_mapclear:
4056 case CMD_nmapclear:
4057 case CMD_vmapclear:
4058 case CMD_omapclear:
4059 case CMD_imapclear:
4060 case CMD_cmapclear:
4061 case CMD_lmapclear:
4062 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004063 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004064 case CMD_xmapclear:
4065 xp->xp_context = EXPAND_MAPCLEAR;
4066 xp->xp_pattern = arg;
4067 break;
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 case CMD_abbreviate: case CMD_noreabbrev:
4070 case CMD_cabbrev: case CMD_cnoreabbrev:
4071 case CMD_iabbrev: case CMD_inoreabbrev:
4072 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004073 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 case CMD_unabbreviate:
4075 case CMD_cunabbrev:
4076 case CMD_iunabbrev:
4077 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004078 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079#ifdef FEAT_MENU
4080 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4081 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4082 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4083 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4084 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4085 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4086 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004087 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 case CMD_tmenu: case CMD_tunmenu:
4089 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4090 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4091#endif
4092
4093 case CMD_colorscheme:
4094 xp->xp_context = EXPAND_COLORS;
4095 xp->xp_pattern = arg;
4096 break;
4097
4098 case CMD_compiler:
4099 xp->xp_context = EXPAND_COMPILER;
4100 xp->xp_pattern = arg;
4101 break;
4102
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004103 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004104 xp->xp_context = EXPAND_OWNSYNTAX;
4105 xp->xp_pattern = arg;
4106 break;
4107
4108 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004109 xp->xp_context = EXPAND_FILETYPE;
4110 xp->xp_pattern = arg;
4111 break;
4112
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004113 case CMD_packadd:
4114 xp->xp_context = EXPAND_PACKADD;
4115 xp->xp_pattern = arg;
4116 break;
4117
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004118#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004120 p = skiptowhite(arg);
4121 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
4123 xp->xp_context = EXPAND_LANGUAGE;
4124 xp->xp_pattern = arg;
4125 }
4126 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004127 {
4128 if ( STRNCMP(arg, "messages", p - arg) == 0
4129 || STRNCMP(arg, "ctype", p - arg) == 0
4130 || STRNCMP(arg, "time", p - arg) == 0)
4131 {
4132 xp->xp_context = EXPAND_LOCALES;
4133 xp->xp_pattern = skipwhite(p);
4134 }
4135 else
4136 xp->xp_context = EXPAND_NOTHING;
4137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 break;
4139#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004140#if defined(FEAT_PROFILE)
4141 case CMD_profile:
4142 set_context_in_profile_cmd(xp, arg);
4143 break;
4144#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004145 case CMD_behave:
4146 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004147 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004148 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004150 case CMD_messages:
4151 xp->xp_context = EXPAND_MESSAGES;
4152 xp->xp_pattern = arg;
4153 break;
4154
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004155#if defined(FEAT_CMDHIST)
4156 case CMD_history:
4157 xp->xp_context = EXPAND_HISTORY;
4158 xp->xp_pattern = arg;
4159 break;
4160#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004161#if defined(FEAT_PROFILE)
4162 case CMD_syntime:
4163 xp->xp_context = EXPAND_SYNTIME;
4164 xp->xp_pattern = arg;
4165 break;
4166#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004167
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004168 case CMD_argdelete:
4169 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4170 arg = xp->xp_pattern + 1;
4171 xp->xp_context = EXPAND_ARGLIST;
4172 xp->xp_pattern = arg;
4173 break;
4174
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175#endif /* FEAT_CMDL_COMPL */
4176
4177 default:
4178 break;
4179 }
4180 return NULL;
4181}
4182
4183/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004184 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 *
4186 * Backslashed delimiters after / or ? will be skipped, and commands will
4187 * not be expanded between /'s and ?'s or after "'".
4188 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004189 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * Returns the "cmd" pointer advanced to beyond the range.
4191 */
4192 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004193skip_range(
4194 char_u *cmd,
4195 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004197 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004199 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004201 if (*cmd == '\\')
4202 {
4203 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4204 ++cmd;
4205 else
4206 break;
4207 }
4208 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 if (*++cmd == NUL && ctx != NULL)
4211 *ctx = EXPAND_NOTHING;
4212 }
4213 else if (*cmd == '/' || *cmd == '?')
4214 {
4215 delim = *cmd++;
4216 while (*cmd != NUL && *cmd != delim)
4217 if (*cmd++ == '\\' && *cmd != NUL)
4218 ++cmd;
4219 if (*cmd == NUL && ctx != NULL)
4220 *ctx = EXPAND_NOTHING;
4221 }
4222 if (*cmd != NUL)
4223 ++cmd;
4224 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004225
4226 /* Skip ":" and white space. */
4227 while (*cmd == ':')
4228 cmd = skipwhite(cmd + 1);
4229
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return cmd;
4231}
4232
4233/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004234 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 *
4236 * Set ptr to the next character after the part that was interpreted.
4237 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004238 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 *
4240 * Return MAXLNUM when no Ex address was found.
4241 */
4242 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004243get_address(
4244 exarg_T *eap UNUSED,
4245 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004246 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004247 int skip, // only skip the address, don't use it
4248 int silent, // no errors or side effects
4249 int to_other_file, // flag: may jump to other file
4250 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004252 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 int c;
4254 int i;
4255 long n;
4256 char_u *cmd;
4257 pos_T pos;
4258 pos_T *fp;
4259 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004260 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 cmd = skipwhite(*ptr);
4263 lnum = MAXLNUM;
4264 do
4265 {
4266 switch (*cmd)
4267 {
4268 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004269 ++cmd;
4270 switch (addr_type)
4271 {
4272 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004273 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 lnum = curwin->w_cursor.lnum;
4275 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004276 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004277 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004278 break;
4279 case ADDR_ARGUMENTS:
4280 lnum = curwin->w_arg_idx + 1;
4281 break;
4282 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004283 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004284 lnum = curbuf->b_fnum;
4285 break;
4286 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004287 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004288 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004289 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004290 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004291 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004292 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004293 cmd = NULL;
4294 goto error;
4295 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004296 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004297#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004298 lnum = qf_get_cur_idx(eap);
4299#endif
4300 break;
4301 case ADDR_QUICKFIX_VALID:
4302#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004303 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004304#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004305 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004306 }
4307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004310 ++cmd;
4311 switch (addr_type)
4312 {
4313 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004314 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 lnum = curbuf->b_ml.ml_line_count;
4316 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004317 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004318 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004319 break;
4320 case ADDR_ARGUMENTS:
4321 lnum = ARGCOUNT;
4322 break;
4323 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004324 buf = lastbuf;
4325 while (buf->b_ml.ml_mfp == NULL)
4326 {
4327 if (buf->b_prev == NULL)
4328 break;
4329 buf = buf->b_prev;
4330 }
4331 lnum = buf->b_fnum;
4332 break;
4333 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004334 lnum = lastbuf->b_fnum;
4335 break;
4336 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004337 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004339 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004340 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004341 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004342 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004343 cmd = NULL;
4344 goto error;
4345 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004346 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004347#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004348 lnum = qf_get_size(eap);
4349 if (lnum == 0)
4350 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004351#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004352 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004353 case ADDR_QUICKFIX_VALID:
4354#ifdef FEAT_QUICKFIX
4355 lnum = qf_get_valid_size(eap);
4356 if (lnum == 0)
4357 lnum = 1;
4358#endif
4359 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004360 }
4361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 if (*++cmd == NUL)
4365 {
4366 cmd = NULL;
4367 goto error;
4368 }
4369 if (addr_type != ADDR_LINES)
4370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004371 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004372 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 goto error;
4374 }
4375 if (skip)
4376 ++cmd;
4377 else
4378 {
4379 /* Only accept a mark in another file when it is
4380 * used by itself: ":'M". */
4381 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4382 ++cmd;
4383 if (fp == (pos_T *)-1)
4384 /* Jumped to another file. */
4385 lnum = curwin->w_cursor.lnum;
4386 else
4387 {
4388 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 {
4390 cmd = NULL;
4391 goto error;
4392 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 lnum = fp->lnum;
4394 }
4395 }
4396 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398 case '/':
4399 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 c = *cmd++;
4401 if (addr_type != ADDR_LINES)
4402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004403 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004404 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004405 goto error;
4406 }
4407 if (skip) /* skip "/pat/" */
4408 {
4409 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4410 if (*cmd == c)
4411 ++cmd;
4412 }
4413 else
4414 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004415 int flags;
4416
4417 pos = curwin->w_cursor; // save curwin->w_cursor
4418
4419 // When '/' or '?' follows another address, start from
4420 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 if (lnum != MAXLNUM)
4422 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004423
4424 // Start a forward search at the end of the line (unless
4425 // before the first line).
4426 // Start a backward search at the start of the line.
4427 // This makes sure we never match in the current
4428 // line, and can match anywhere in the
4429 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004430 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004431 curwin->w_cursor.col = MAXCOL;
4432 else
4433 curwin->w_cursor.col = 0;
4434 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004435 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4436 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004437 {
4438 curwin->w_cursor = pos;
4439 cmd = NULL;
4440 goto error;
4441 }
4442 lnum = curwin->w_cursor.lnum;
4443 curwin->w_cursor = pos;
4444 /* adjust command string pointer */
4445 cmd += searchcmdlen;
4446 }
4447 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 ++cmd;
4451 if (addr_type != ADDR_LINES)
4452 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004453 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004454 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 goto error;
4456 }
4457 if (*cmd == '&')
4458 i = RE_SUBST;
4459 else if (*cmd == '?' || *cmd == '/')
4460 i = RE_SEARCH;
4461 else
4462 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004463 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004464 cmd = NULL;
4465 goto error;
4466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 if (!skip)
4469 {
4470 /*
4471 * When search follows another address, start from
4472 * there.
4473 */
4474 if (lnum != MAXLNUM)
4475 pos.lnum = lnum;
4476 else
4477 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 /*
4480 * Start the search just like for the above
4481 * do_search().
4482 */
4483 if (*cmd != '?')
4484 pos.col = MAXCOL;
4485 else
4486 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004487 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004488 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 *cmd == '?' ? BACKWARD : FORWARD,
4490 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004491 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 lnum = pos.lnum;
4493 else
4494 {
4495 cmd = NULL;
4496 goto error;
4497 }
4498 }
4499 ++cmd;
4500 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004503 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4504 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506
4507 for (;;)
4508 {
4509 cmd = skipwhite(cmd);
4510 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4511 break;
4512
4513 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 {
4515 switch (addr_type)
4516 {
4517 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004518 case ADDR_OTHER:
4519 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004520 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004521 break;
4522 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004523 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004524 break;
4525 case ADDR_ARGUMENTS:
4526 lnum = curwin->w_arg_idx + 1;
4527 break;
4528 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004529 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 lnum = curbuf->b_fnum;
4531 break;
4532 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004533 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004534 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004535 case ADDR_TABS_RELATIVE:
4536 lnum = 1;
4537 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004538 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004539#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004540 lnum = qf_get_cur_idx(eap);
4541#endif
4542 break;
4543 case ADDR_QUICKFIX_VALID:
4544#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004545 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004546#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004547 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004548 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004549 case ADDR_UNSIGNED:
4550 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004551 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004552 }
4553 }
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 if (VIM_ISDIGIT(*cmd))
4556 i = '+'; /* "number" is same as "+number" */
4557 else
4558 i = *cmd++;
4559 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4560 n = 1;
4561 else
4562 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004563
4564 if (addr_type == ADDR_TABS_RELATIVE)
4565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004566 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004567 cmd = NULL;
4568 goto error;
4569 }
4570 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004571 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004572 lnum = compute_buffer_local_count(
4573 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004575 {
4576#ifdef FEAT_FOLDING
4577 /* Relative line addressing, need to adjust for folded lines
4578 * now, but only do it after the first address. */
4579 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4580 && address_count >= 2)
4581 (void)hasFolding(lnum, NULL, &lnum);
4582#endif
4583 if (i == '-')
4584 lnum -= n;
4585 else
4586 lnum += n;
4587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
4589 } while (*cmd == '/' || *cmd == '?');
4590
4591error:
4592 *ptr = cmd;
4593 return lnum;
4594}
4595
4596/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004597 * Get flags from an Ex command argument.
4598 */
4599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004600get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004601{
4602 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4603 {
4604 if (*eap->arg == 'l')
4605 eap->flags |= EXFLAG_LIST;
4606 else if (*eap->arg == 'p')
4607 eap->flags |= EXFLAG_PRINT;
4608 else
4609 eap->flags |= EXFLAG_NR;
4610 eap->arg = skipwhite(eap->arg + 1);
4611 }
4612}
4613
4614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 * Function called for command which is Not Implemented. NI!
4616 */
4617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004618ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619{
4620 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004621 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622}
4623
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004624#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625/*
4626 * Function called for script command which is Not Implemented. NI!
4627 * Skips over ":perl <<EOF" constructs.
4628 */
4629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
4632 if (!eap->skip)
4633 ex_ni(eap);
4634 else
4635 vim_free(script_get(eap, eap->arg));
4636}
4637#endif
4638
4639/*
4640 * Check range in Ex command for validity.
4641 * Return NULL when valid, error message when invalid.
4642 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004643 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004644invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004646 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004647
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if ( eap->line1 < 0
4649 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004650 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004651 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004652
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004653 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004655 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004656 {
4657 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004658 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659#ifdef FEAT_DIFF
4660 + (eap->cmdidx == CMD_diffget)
4661#endif
4662 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004663 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004664 break;
4665 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004666 /* add 1 if ARGCOUNT is 0 */
4667 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004668 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004669 break;
4670 case ADDR_BUFFERS:
4671 if (eap->line1 < firstbuf->b_fnum
4672 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004673 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004674 break;
4675 case ADDR_LOADED_BUFFERS:
4676 buf = firstbuf;
4677 while (buf->b_ml.ml_mfp == NULL)
4678 {
4679 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004680 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004681 buf = buf->b_next;
4682 }
4683 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004684 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004685 buf = lastbuf;
4686 while (buf->b_ml.ml_mfp == NULL)
4687 {
4688 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004689 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004690 buf = buf->b_prev;
4691 }
4692 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004693 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004694 break;
4695 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004696 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004697 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004698 break;
4699 case ADDR_TABS:
4700 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004701 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004702 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004703 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004704 case ADDR_OTHER:
4705 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004706 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004707 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004708#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004709 // No error for value that is too big, will use the last entry.
4710 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004711 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004712#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004713 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004714 case ADDR_QUICKFIX_VALID:
4715#ifdef FEAT_QUICKFIX
4716 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4717 || eap->line2 < 0)
4718 return _(e_invrange);
4719#endif
4720 break;
4721 case ADDR_UNSIGNED:
4722 if (eap->line2 < 0)
4723 return _(e_invrange);
4724 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004725 case ADDR_NONE:
4726 // Will give an error elsewhere.
4727 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004728 }
4729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 return NULL;
4731}
4732
4733/*
4734 * Correct the range for zero line number, if required.
4735 */
4736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004737correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004739 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 {
4741 if (eap->line1 == 0)
4742 eap->line1 = 1;
4743 if (eap->line2 == 0)
4744 eap->line2 = 1;
4745 }
4746}
4747
Bram Moolenaar748bf032005-02-02 23:04:36 +00004748#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004749/*
4750 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4751 * pattern. Otherwise return eap->arg.
4752 */
4753 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004754skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004755{
4756 char_u *p = eap->arg;
4757
Bram Moolenaara37420f2006-02-04 22:37:47 +00004758 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4759 || eap->cmdidx == CMD_vimgrepadd
4760 || eap->cmdidx == CMD_lvimgrepadd
4761 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004762 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004763 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004764 if (p == NULL)
4765 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004766 }
4767 return p;
4768}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004769
4770/*
4771 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4772 * in the command line, so that things like % get expanded.
4773 */
4774 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004775replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004776{
4777 char_u *new_cmdline;
4778 char_u *program;
4779 char_u *pos;
4780 char_u *ptr;
4781 int len;
4782 int i;
4783
4784 /*
4785 * Don't do it when ":vimgrep" is used for ":grep".
4786 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004787 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4788 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4789 || eap->cmdidx == CMD_grepadd
4790 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004791 && !grep_internal(eap->cmdidx))
4792 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004793 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4794 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004795 {
4796 if (*curbuf->b_p_gp == NUL)
4797 program = p_gp;
4798 else
4799 program = curbuf->b_p_gp;
4800 }
4801 else
4802 {
4803 if (*curbuf->b_p_mp == NUL)
4804 program = p_mp;
4805 else
4806 program = curbuf->b_p_mp;
4807 }
4808
4809 p = skipwhite(p);
4810
4811 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4812 {
4813 /* replace $* by given arguments */
4814 i = 1;
4815 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4816 ++i;
4817 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004818 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004819 if (new_cmdline == NULL)
4820 return NULL; /* out of memory */
4821 ptr = new_cmdline;
4822 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4823 {
4824 i = (int)(pos - program);
4825 STRNCPY(ptr, program, i);
4826 STRCPY(ptr += i, p);
4827 ptr += len;
4828 program = pos + 2;
4829 }
4830 STRCPY(ptr, program);
4831 }
4832 else
4833 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004834 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004835 if (new_cmdline == NULL)
4836 return NULL; /* out of memory */
4837 STRCPY(new_cmdline, program);
4838 STRCAT(new_cmdline, " ");
4839 STRCAT(new_cmdline, p);
4840 }
4841 msg_make(p);
4842
4843 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4844 vim_free(*cmdlinep);
4845 *cmdlinep = new_cmdline;
4846 p = new_cmdline;
4847 }
4848 return p;
4849}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004850#endif
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852/*
4853 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004854 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 * Return FAIL for failure, OK otherwise.
4856 */
4857 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004858expand_filename(
4859 exarg_T *eap,
4860 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004861 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 int has_wildcards; /* need to expand wildcards */
4864 char_u *repl;
4865 int srclen;
4866 char_u *p;
4867 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004868 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
Bram Moolenaar748bf032005-02-02 23:04:36 +00004870#ifdef FEAT_QUICKFIX
4871 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4872 p = skip_grep_pat(eap);
4873#else
4874 p = eap->arg;
4875#endif
4876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 /*
4878 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4879 * the file name contains a wildcard it should not cause expanding.
4880 * (it will be expanded anyway if there is a wildcard before replacing).
4881 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004882 has_wildcards = mch_has_wildcard(p);
4883 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004885#ifdef FEAT_EVAL
4886 /* Skip over `=expr`, wildcards in it are not expanded. */
4887 if (p[0] == '`' && p[1] == '=')
4888 {
4889 p += 2;
4890 (void)skip_expr(&p);
4891 if (*p == '`')
4892 ++p;
4893 continue;
4894 }
4895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 /*
4897 * Quick check if this cannot be the start of a special string.
4898 * Also removes backslash before '%', '#' and '<'.
4899 */
4900 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4901 {
4902 ++p;
4903 continue;
4904 }
4905
4906 /*
4907 * Try to find a match at this position.
4908 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004909 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4910 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (*errormsgp != NULL) /* error detected */
4912 return FAIL;
4913 if (repl == NULL) /* no match found */
4914 {
4915 p += srclen;
4916 continue;
4917 }
4918
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004919 /* Wildcards won't be expanded below, the replacement is taken
4920 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4921 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4922 {
4923 char_u *l = repl;
4924
4925 repl = expand_env_save(repl);
4926 vim_free(l);
4927 }
4928
Bram Moolenaar63b92542007-03-27 14:57:09 +00004929 /* Need to escape white space et al. with a backslash.
4930 * Don't do this for:
4931 * - replacement that already has been escaped: "##"
4932 * - shell commands (may have to use quotes instead).
4933 * - non-unix systems when there is a single argument (spaces don't
4934 * separate arguments then).
4935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004937 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 && eap->cmdidx != CMD_grep
4940 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004941 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004942 && eap->cmdidx != CMD_lgrep
4943 && eap->cmdidx != CMD_lgrepadd
4944 && eap->cmdidx != CMD_lmake
4945 && eap->cmdidx != CMD_make
4946 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004948 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949#endif
4950 )
4951 {
4952 char_u *l;
4953#ifdef BACKSLASH_IN_FILENAME
4954 /* Don't escape a backslash here, because rem_backslash() doesn't
4955 * remove it later. */
4956 static char_u *nobslash = (char_u *)" \t\"|";
4957# define ESCAPE_CHARS nobslash
4958#else
4959# define ESCAPE_CHARS escape_chars
4960#endif
4961
4962 for (l = repl; *l; ++l)
4963 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4964 {
4965 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4966 if (l != NULL)
4967 {
4968 vim_free(repl);
4969 repl = l;
4970 }
4971 break;
4972 }
4973 }
4974
4975 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004976 if ((eap->usefilter || eap->cmdidx == CMD_bang
4977 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004978 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 char_u *l;
4981
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004982 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (l != NULL)
4984 {
4985 vim_free(repl);
4986 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 }
4989
4990 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4991 vim_free(repl);
4992 if (p == NULL)
4993 return FAIL;
4994 }
4995
4996 /*
4997 * One file argument: Expand wildcards.
4998 * Don't do this with ":r !command" or ":w !command".
4999 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005000 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
5002 /*
5003 * May do this twice:
5004 * 1. Replace environment variables.
5005 * 2. Replace any other wildcards, remove backslashes.
5006 */
5007 for (n = 1; n <= 2; ++n)
5008 {
5009 if (n == 2)
5010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 /*
5012 * Halve the number of backslashes (this is Vi compatible).
5013 * For Unix and OS/2, when wildcards are expanded, this is
5014 * done by ExpandOne() below.
5015 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005016#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (!has_wildcards)
5018#endif
5019 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021
5022 if (has_wildcards)
5023 {
5024 if (n == 1)
5025 {
5026 /*
5027 * First loop: May expand environment variables. This
5028 * can be done much faster with expand_env() than with
5029 * something else (e.g., calling a shell).
5030 * After expanding environment variables, check again
5031 * if there are still wildcards present.
5032 */
5033 if (vim_strchr(eap->arg, '$') != NULL
5034 || vim_strchr(eap->arg, '~') != NULL)
5035 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005036 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005037 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 has_wildcards = mch_has_wildcard(NameBuff);
5039 p = NameBuff;
5040 }
5041 else
5042 p = NULL;
5043 }
5044 else /* n == 2 */
5045 {
5046 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005047 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 ExpandInit(&xpc);
5050 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005051 if (p_wic)
5052 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005054 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (p == NULL)
5056 return FAIL;
5057 }
5058 if (p != NULL)
5059 {
5060 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5061 p, cmdlinep);
5062 if (n == 2) /* p came from ExpandOne() */
5063 vim_free(p);
5064 }
5065 }
5066 }
5067 }
5068 return OK;
5069}
5070
5071/*
5072 * Replace part of the command line, keeping eap->cmd, eap->arg and
5073 * eap->nextcmd correct.
5074 * "src" points to the part that is to be replaced, of length "srclen".
5075 * "repl" is the replacement string.
5076 * Returns a pointer to the character after the replaced string.
5077 * Returns NULL for failure.
5078 */
5079 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005080repl_cmdline(
5081 exarg_T *eap,
5082 char_u *src,
5083 int srclen,
5084 char_u *repl,
5085 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 int len;
5088 int i;
5089 char_u *new_cmdline;
5090
5091 /*
5092 * The new command line is build in new_cmdline[].
5093 * First allocate it.
5094 * Careful: a "+cmd" argument may have been NUL terminated.
5095 */
5096 len = (int)STRLEN(repl);
5097 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005098 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005100 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 return NULL; /* out of memory! */
5102
5103 /*
5104 * Copy the stuff before the expanded part.
5105 * Copy the expanded stuff.
5106 * Copy what came after the expanded part.
5107 * Copy the next commands, if there are any.
5108 */
5109 i = (int)(src - *cmdlinep); /* length of part before match */
5110 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 mch_memmove(new_cmdline + i, repl, (size_t)len);
5113 i += len; /* remember the end of the string */
5114 STRCPY(new_cmdline + i, src + srclen);
5115 src = new_cmdline + i; /* remember where to continue */
5116
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005117 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 i = (int)STRLEN(new_cmdline) + 1;
5120 STRCPY(new_cmdline + i, eap->nextcmd);
5121 eap->nextcmd = new_cmdline + i;
5122 }
5123 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5124 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5125 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5126 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5127 vim_free(*cmdlinep);
5128 *cmdlinep = new_cmdline;
5129
5130 return src;
5131}
5132
5133/*
5134 * Check for '|' to separate commands and '"' to start comments.
5135 */
5136 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005137separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
5139 char_u *p;
5140
Bram Moolenaar86b68352004-12-27 21:59:20 +00005141#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005142 p = skip_grep_pat(eap);
5143#else
5144 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005145#endif
5146
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005147 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 if (*p == Ctrl_V)
5150 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005151 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 ++p; /* skip CTRL-V and next char */
5153 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005154 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005155 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (*p == NUL) /* stop at NUL after CTRL-V */
5157 break;
5158 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005159
5160#ifdef FEAT_EVAL
5161 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005162 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005163 {
5164 p += 2;
5165 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005166 }
5167#endif
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 /* Check for '"': start of comment or '|': next command */
5170 /* :@" and :*" do not start a comment!
5171 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005172 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5174 || p != eap->arg)
5175 && (eap->cmdidx != CMD_redir
5176 || p != eap->arg + 1 || p[-1] != '@'))
5177 || *p == '|' || *p == '\n')
5178 {
5179 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005180 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 * AND 'b' is present in 'cpoptions'.
5182 */
5183 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005184 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005186 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 --p;
5188 }
5189 else
5190 {
5191 eap->nextcmd = check_nextcmd(p);
5192 *p = NUL;
5193 break;
5194 }
5195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005197
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005198 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 del_trailing_spaces(eap->arg);
5200}
5201
5202/*
5203 * get + command from ex argument
5204 */
5205 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005206getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 char_u *arg = *argp;
5209 char_u *command = NULL;
5210
5211 if (*arg == '+') /* +[command] */
5212 {
5213 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005214 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 command = dollar_command;
5216 else
5217 {
5218 command = arg;
5219 arg = skip_cmd_arg(command, TRUE);
5220 if (*arg != NUL)
5221 *arg++ = NUL; /* terminate command with NUL */
5222 }
5223
5224 arg = skipwhite(arg); /* skip over spaces */
5225 *argp = arg;
5226 }
5227 return command;
5228}
5229
5230/*
5231 * Find end of "+command" argument. Skip over "\ " and "\\".
5232 */
5233 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005234skip_cmd_arg(
5235 char_u *p,
5236 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 while (*p && !vim_isspace(*p))
5239 {
5240 if (*p == '\\' && p[1] != NUL)
5241 {
5242 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005243 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 else
5245 ++p;
5246 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005247 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249 return p;
5250}
5251
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005252 int
5253get_bad_opt(char_u *p, exarg_T *eap)
5254{
5255 if (STRICMP(p, "keep") == 0)
5256 eap->bad_char = BAD_KEEP;
5257 else if (STRICMP(p, "drop") == 0)
5258 eap->bad_char = BAD_DROP;
5259 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5260 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005261 else
5262 return FAIL;
5263 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005264}
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266/*
5267 * Get "++opt=arg" argument.
5268 * Return FAIL or OK.
5269 */
5270 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005271getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 char_u *arg = eap->arg + 2;
5274 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005275 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /* ":edit ++[no]bin[ary] file" */
5279 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5280 {
5281 if (*arg == 'n')
5282 {
5283 arg += 2;
5284 eap->force_bin = FORCE_NOBIN;
5285 }
5286 else
5287 eap->force_bin = FORCE_BIN;
5288 if (!checkforcmd(&arg, "binary", 3))
5289 return FAIL;
5290 eap->arg = skipwhite(arg);
5291 return OK;
5292 }
5293
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005294 /* ":read ++edit file" */
5295 if (STRNCMP(arg, "edit", 4) == 0)
5296 {
5297 eap->read_edit = TRUE;
5298 eap->arg = skipwhite(arg + 4);
5299 return OK;
5300 }
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (STRNCMP(arg, "ff", 2) == 0)
5303 {
5304 arg += 2;
5305 pp = &eap->force_ff;
5306 }
5307 else if (STRNCMP(arg, "fileformat", 10) == 0)
5308 {
5309 arg += 10;
5310 pp = &eap->force_ff;
5311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 else if (STRNCMP(arg, "enc", 3) == 0)
5313 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005314 if (STRNCMP(arg, "encoding", 8) == 0)
5315 arg += 8;
5316 else
5317 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 pp = &eap->force_enc;
5319 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005320 else if (STRNCMP(arg, "bad", 3) == 0)
5321 {
5322 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005323 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
5326 if (pp == NULL || *arg != '=')
5327 return FAIL;
5328
5329 ++arg;
5330 *pp = (int)(arg - eap->cmd);
5331 arg = skip_cmd_arg(arg, FALSE);
5332 eap->arg = skipwhite(arg);
5333 *arg = NUL;
5334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 if (pp == &eap->force_ff)
5336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5338 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005339 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005341 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 /* Make 'fileencoding' lower case. */
5344 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5345 *p = TOLOWER_ASC(*p);
5346 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005347 else
5348 {
5349 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5350 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005351 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005352 return FAIL;
5353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355 return OK;
5356}
5357
5358/*
5359 * ":abbreviate" and friends.
5360 */
5361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363{
5364 do_exmap(eap, TRUE); /* almost the same as mapping */
5365}
5366
5367/*
5368 * ":map" and friends.
5369 */
5370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005371ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
5373 /*
5374 * If we are sourcing .exrc or .vimrc in current directory we
5375 * print the mappings for security reasons.
5376 */
5377 if (secure)
5378 {
5379 secure = 2;
5380 msg_outtrans(eap->cmd);
5381 msg_putchar('\n');
5382 }
5383 do_exmap(eap, FALSE);
5384}
5385
5386/*
5387 * ":unmap" and friends.
5388 */
5389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005390ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 do_exmap(eap, FALSE);
5393}
5394
5395/*
5396 * ":mapclear" and friends.
5397 */
5398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005399ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5402}
5403
5404/*
5405 * ":abclear" and friends.
5406 */
5407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5411}
5412
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005417 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 * directory for security reasons.
5419 */
5420 if (secure)
5421 {
5422 secure = 2;
5423 eap->errmsg = e_curdir;
5424 }
5425 else if (eap->cmdidx == CMD_autocmd)
5426 do_autocmd(eap->arg, eap->forceit);
5427 else
5428 do_augroup(eap->arg, eap->forceit);
5429}
5430
5431/*
5432 * ":doautocmd": Apply the automatic commands to the current buffer.
5433 */
5434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005435ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005437 char_u *arg = eap->arg;
5438 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005439 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005440
Bram Moolenaar1610d052016-06-09 22:53:01 +02005441 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5442 /* Only when there is no <nomodeline>. */
5443 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005444 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447/*
5448 * :[N]bunload[!] [N] [bufname] unload buffer
5449 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5450 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5451 */
5452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005453ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005455 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005456 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 eap->errmsg = do_bufdel(
5458 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5459 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5460 : DOBUF_UNLOAD, eap->arg,
5461 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5462}
5463
5464/*
5465 * :[N]buffer [N] to buffer N
5466 * :[N]sbuffer [N] to buffer N
5467 */
5468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005469ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005471 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005472 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 if (*eap->arg)
5474 eap->errmsg = e_trailing;
5475 else
5476 {
5477 if (eap->addr_count == 0) /* default is current buffer */
5478 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5479 else
5480 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005481 if (eap->do_ecmd_cmd != NULL)
5482 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484}
5485
5486/*
5487 * :[N]bmodified [N] to next mod. buffer
5488 * :[N]sbmodified [N] to next mod. buffer
5489 */
5490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005494 if (eap->do_ecmd_cmd != NULL)
5495 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496}
5497
5498/*
5499 * :[N]bnext [N] to next buffer
5500 * :[N]sbnext [N] split and to next buffer
5501 */
5502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005503ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005506 if (eap->do_ecmd_cmd != NULL)
5507 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508}
5509
5510/*
5511 * :[N]bNext [N] to previous buffer
5512 * :[N]bprevious [N] to previous buffer
5513 * :[N]sbNext [N] split and to previous buffer
5514 * :[N]sbprevious [N] split and to previous buffer
5515 */
5516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005520 if (eap->do_ecmd_cmd != NULL)
5521 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522}
5523
5524/*
5525 * :brewind to first buffer
5526 * :bfirst to first buffer
5527 * :sbrewind split and to first buffer
5528 * :sbfirst split and to first buffer
5529 */
5530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005534 if (eap->do_ecmd_cmd != NULL)
5535 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536}
5537
5538/*
5539 * :blast to last buffer
5540 * :sblast split and to last buffer
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005546 if (eap->do_ecmd_cmd != NULL)
5547 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005551ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 return (c == NUL || c == '|' || c == '"' || c == '\n');
5554}
5555
5556#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5557 || defined(PROTO)
5558/*
5559 * Return the next command, after the first '|' or '\n'.
5560 * Return NULL if not found.
5561 */
5562 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005563find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 while (*p != '|' && *p != '\n')
5566 {
5567 if (*p == NUL)
5568 return NULL;
5569 ++p;
5570 }
5571 return (p + 1);
5572}
5573#endif
5574
5575/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005576 * Check if *p is a separator between Ex commands, skipping over white space.
5577 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 */
5579 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005582 char_u *s = skipwhite(p);
5583
5584 if (*s == '|' || *s == '\n')
5585 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 else
5587 return NULL;
5588}
5589
5590/*
5591 * - if there are more files to edit
5592 * - and this is the last window
5593 * - and forceit not used
5594 * - and not repeated twice on a row
5595 * return FAIL and give error message if 'message' TRUE
5596 * return OK otherwise
5597 */
5598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599check_more(
5600 int message, /* when FALSE check only, no messages */
5601 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5604
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005605 if (!forceit && only_one_window()
5606 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
5608 if (message)
5609 {
5610#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5611 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5612 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005613 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005615 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5616 NGETTEXT("%d more file to edit. Quit anyway?",
5617 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5619 return OK;
5620 return FAIL;
5621 }
5622#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005623 semsg(NGETTEXT("E173: %d more file to edit",
5624 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 quitmore = 2; /* next try to quit is allowed */
5626 }
5627 return FAIL;
5628 }
5629 return OK;
5630}
5631
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005632#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633/*
5634 * Function given to ExpandGeneric() to obtain the list of command names.
5635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 return cmdnames[idx].cmd_name;
5642}
5643#endif
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
Bram Moolenaare6850792010-05-14 15:28:44 +02005648 if (*eap->arg == NUL)
5649 {
5650#ifdef FEAT_EVAL
5651 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5652 char_u *p = NULL;
5653
5654 if (expr != NULL)
5655 {
5656 ++emsg_off;
5657 p = eval_to_string(expr, NULL, FALSE);
5658 --emsg_off;
5659 vim_free(expr);
5660 }
5661 if (p != NULL)
5662 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005663 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005664 vim_free(p);
5665 }
5666 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005667 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005668#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005669 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005670#endif
5671 }
5672 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005673 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005674
5675#ifdef FEAT_VTP
5676 else if (has_vtp_working())
5677 {
5678 // background color change requires clear + redraw
5679 update_screen(CLEAR);
5680 redrawcmd();
5681 }
5682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683}
5684
5685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005686ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005689 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 do_highlight(eap->arg, eap->forceit, FALSE);
5691}
5692
5693
5694/*
5695 * Call this function if we thought we were going to exit, but we won't
5696 * (because of an error). May need to restore the terminal mode.
5697 */
5698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 exiting = FALSE;
5702 settmode(TMODE_RAW);
5703}
5704
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005705 static int
5706before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5707{
5708 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5709
5710 /* Bail out when autocommands closed the window.
5711 * Refuse to quit when the buffer in the last window is being closed (can
5712 * only happen in autocommands). */
5713 if (!win_valid(wp)
5714 || curbuf_locked()
5715 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5716 return TRUE;
5717
5718 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5719 {
5720 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5721 /* Refuse to quit when locked or when the buffer in the last window is
5722 * being closed (can only happen in autocommands). */
5723 if (curbuf_locked()
5724 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5725 return TRUE;
5726 }
5727
5728 return FALSE;
5729}
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005732 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005733 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 */
5735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005738 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740#ifdef FEAT_CMDWIN
5741 if (cmdwin_type != 0)
5742 {
5743 cmdwin_result = Ctrl_C;
5744 return;
5745 }
5746#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005747 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005748 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005749 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005750 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005751 return;
5752 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005753 if (eap->addr_count > 0)
5754 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005755 int wnr = eap->line2;
5756
5757 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5758 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005759 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005760 }
5761 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005762 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005763
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005764 /* Refuse to quit when locked. */
5765 if (curbuf_locked())
5766 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005767
5768 /* Trigger QuitPre and maybe ExitPre */
5769 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005770 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772#ifdef FEAT_NETBEANS_INTG
5773 netbeansForcedQuit = eap->forceit;
5774#endif
5775
5776 /*
5777 * If there are more files or windows we won't exit.
5778 */
5779 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5780 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005781 if ((!buf_hide(wp->w_buffer)
5782 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005783 | (eap->forceit ? CCGD_FORCEIT : 0)
5784 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005786 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 not_exiting();
5789 }
5790 else
5791 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005792 /* quit last window
5793 * Note: only_one_window() returns true, even so a help window is
5794 * still open. In that case only quit, if no address has been
5795 * specified. Example:
5796 * :h|wincmd w|1q - don't quit
5797 * :h|wincmd w|q - quit
5798 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005799 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005801 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005802#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005806 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808}
5809
5810/*
5811 * ":cquit".
5812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005814ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 getout(1); /* this does not always pass on the exit code to the Manx
5817 compiler. why? */
5818}
5819
5820/*
5821 * ":qall": try to quit all windows
5822 */
5823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005824ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826# ifdef FEAT_CMDWIN
5827 if (cmdwin_type != 0)
5828 {
5829 if (eap->forceit)
5830 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5831 else
5832 cmdwin_result = K_XF2;
5833 return;
5834 }
5835# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005836
5837 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005838 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005839 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005840 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005841 return;
5842 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005843
5844 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005845 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005848 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 getout(0);
5850 not_exiting();
5851}
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853/*
5854 * ":close": close current window, unless it is the last one
5855 */
5856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005857ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005859 win_T *win;
5860 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005861#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005863 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005865#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005866 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005867 {
5868 if (eap->addr_count == 0)
5869 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005870 else
5871 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005872 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005873 {
5874 winnr++;
5875 if (winnr == eap->line2)
5876 break;
5877 }
5878 if (win == NULL)
5879 win = lastwin;
5880 ex_win_close(eap->forceit, win, NULL);
5881 }
5882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883}
5884
Bram Moolenaar4033c552017-09-16 20:54:51 +02005885#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005886/*
5887 * ":pclose": Close any preview window.
5888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005890ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005891{
5892 win_T *win;
5893
Bram Moolenaar79648732019-07-18 21:43:07 +02005894 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005895 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005896 if (win->w_p_pvw)
5897 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005898 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005899 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005900 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005901# ifdef FEAT_TEXT_PROP
5902 // Also when 'previewpopup' is empty, it might have been cleared.
5903 popup_close_preview();
5904# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005905}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005906#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005907
Bram Moolenaarf740b292006-02-16 22:11:02 +00005908/*
5909 * Close window "win" and take care of handling closing the last window for a
5910 * modified buffer.
5911 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005913ex_win_close(
5914 int forceit,
5915 win_T *win,
5916 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917{
5918 int need_hide;
5919 buf_T *buf = win->w_buffer;
5920
5921 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005922 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005924#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if ((p_confirm || cmdmod.confirm) && p_write)
5926 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005927 bufref_T bufref;
5928
5929 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005931 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 return;
5933 need_hide = FALSE;
5934 }
5935 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005938 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 return;
5940 }
5941 }
5942
Bram Moolenaar4033c552017-09-16 20:54:51 +02005943#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005945#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005946
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005948 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005949 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005950 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005951 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952}
5953
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005955 * Handle the argument for a tabpage related ex command.
5956 * Returns a tabpage number.
5957 * When an error is encountered then eap->errmsg is set.
5958 */
5959 static int
5960get_tabpage_arg(exarg_T *eap)
5961{
5962 int tab_number;
5963 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5964
5965 if (eap->arg && *eap->arg != NUL)
5966 {
5967 char_u *p = eap->arg;
5968 char_u *p_save;
5969 int relative = 0; /* argument +N/-N means: go to N places to the
5970 * right/left relative to the current position. */
5971
5972 if (*p == '-')
5973 {
5974 relative = -1;
5975 p++;
5976 }
5977 else if (*p == '+')
5978 {
5979 relative = 1;
5980 p++;
5981 }
5982
5983 p_save = p;
5984 tab_number = getdigits(&p);
5985
5986 if (relative == 0)
5987 {
5988 if (STRCMP(p, "$") == 0)
5989 tab_number = LAST_TAB_NR;
5990 else if (p == p_save || *p_save == '-' || *p != NUL
5991 || tab_number > LAST_TAB_NR)
5992 {
5993 /* No numbers as argument. */
5994 eap->errmsg = e_invarg;
5995 goto theend;
5996 }
5997 }
5998 else
5999 {
6000 if (*p_save == NUL)
6001 tab_number = 1;
6002 else if (p == p_save || *p_save == '-' || *p != NUL
6003 || tab_number == 0)
6004 {
6005 /* No numbers as argument. */
6006 eap->errmsg = e_invarg;
6007 goto theend;
6008 }
6009 tab_number = tab_number * relative + tabpage_index(curtab);
6010 if (!unaccept_arg0 && relative == -1)
6011 --tab_number;
6012 }
6013 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6014 eap->errmsg = e_invarg;
6015 }
6016 else if (eap->addr_count > 0)
6017 {
6018 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006019 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006020 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006021 tab_number = 0;
6022 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006023 else
6024 {
6025 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006026 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006027 {
6028 --tab_number;
6029 if (tab_number < unaccept_arg0)
6030 eap->errmsg = e_invarg;
6031 }
6032 }
6033 }
6034 else
6035 {
6036 switch (eap->cmdidx)
6037 {
6038 case CMD_tabnext:
6039 tab_number = tabpage_index(curtab) + 1;
6040 if (tab_number > LAST_TAB_NR)
6041 tab_number = 1;
6042 break;
6043 case CMD_tabmove:
6044 tab_number = LAST_TAB_NR;
6045 break;
6046 default:
6047 tab_number = tabpage_index(curtab);
6048 }
6049 }
6050
6051theend:
6052 return tab_number;
6053}
6054
6055/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006056 * ":tabclose": close current tab page, unless it is the last one.
6057 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 */
6059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006060ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006062 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006063 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006064
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006065# ifdef FEAT_CMDWIN
6066 if (cmdwin_type != 0)
6067 cmdwin_result = K_IGNORE;
6068 else
6069# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006070 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006071 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006072 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006074 tab_number = get_tabpage_arg(eap);
6075 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006076 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006077 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006078 if (tp == NULL)
6079 {
6080 beep_flush();
6081 return;
6082 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006083 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006084 {
6085 tabpage_close_other(tp, eap->forceit);
6086 return;
6087 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006088 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006089 tabpage_close(eap->forceit);
6090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006092}
6093
6094/*
6095 * ":tabonly": close all tab pages except the current one
6096 */
6097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006098ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006099{
6100 tabpage_T *tp;
6101 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006102 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006103
6104# ifdef FEAT_CMDWIN
6105 if (cmdwin_type != 0)
6106 cmdwin_result = K_IGNORE;
6107 else
6108# endif
6109 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006110 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006111 else
6112 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006113 tab_number = get_tabpage_arg(eap);
6114 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006115 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006116 goto_tabpage(tab_number);
6117 /* Repeat this up to a 1000 times, because autocommands may
6118 * mess up the lists. */
6119 for (done = 0; done < 1000; ++done)
6120 {
6121 FOR_ALL_TABPAGES(tp)
6122 if (tp->tp_topframe != topframe)
6123 {
6124 tabpage_close_other(tp, eap->forceit);
6125 /* if we failed to close it quit */
6126 if (valid_tabpage(tp))
6127 done = 1000;
6128 /* start over, "tp" is now invalid */
6129 break;
6130 }
6131 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006132 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006133 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006134 }
6135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137
6138/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006139 * Close the current tab page.
6140 */
6141 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006142tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006143{
6144 /* First close all the windows but the current one. If that worked then
6145 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006146 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006147 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006148 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006149 ex_win_close(forceit, curwin, NULL);
6150# ifdef FEAT_GUI
6151 need_mouse_correct = TRUE;
6152# endif
6153}
6154
6155/*
6156 * Close tab page "tp", which is not the current tab page.
6157 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006158 * Also takes care of the tab pages line disappearing when closing the
6159 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006160 */
6161 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006162tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006163{
6164 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006165 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006166 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006167
6168 /* Limit to 1000 windows, autocommands may add a window while we close
6169 * one. OK, so I'm paranoid... */
6170 while (++done < 1000)
6171 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006172 wp = tp->tp_firstwin;
6173 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006174
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006175 /* Autocommands may delete the tab page under our fingers and we may
6176 * fail to close a window with a modified buffer. */
6177 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006178 break;
6179 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006180
Bram Moolenaar29323592016-07-24 22:04:11 +02006181 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006182
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006183 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006184 if (h != tabline_height())
6185 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006186}
6187
6188/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 * ":only".
6190 */
6191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006192ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006194 win_T *wp;
6195 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196# ifdef FEAT_GUI
6197 need_mouse_correct = TRUE;
6198# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006199 if (eap->addr_count > 0)
6200 {
6201 wnr = eap->line2;
6202 for (wp = firstwin; --wnr > 0; )
6203 {
6204 if (wp->w_next == NULL)
6205 break;
6206 else
6207 wp = wp->w_next;
6208 }
6209 win_goto(wp);
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 close_others(TRUE, eap->forceit);
6212}
6213
6214/*
6215 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006216 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006218 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006219ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 if (eap->addr_count == 0)
6222 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006223 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
6226 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006227ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006229 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006230 if (!eap->skip)
6231 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006232#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006233 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006234#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006235 if (eap->addr_count == 0)
6236 win_close(curwin, FALSE); /* don't free buffer */
6237 else
6238 {
6239 int winnr = 0;
6240 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006241
Bram Moolenaar2256c992016-11-15 21:17:07 +01006242 FOR_ALL_WINDOWS(win)
6243 {
6244 winnr++;
6245 if (winnr == eap->line2)
6246 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006247 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006248 if (win == NULL)
6249 win = lastwin;
6250 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 }
6253}
6254
6255/*
6256 * ":stop" and ":suspend": Suspend Vim.
6257 */
6258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 /*
6262 * Disallow suspending for "rvim".
6263 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006264 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 {
6266 if (!eap->forceit)
6267 autowrite_all();
6268 windgoto((int)Rows - 1, 0);
6269 out_char('\n');
6270 out_flush();
6271 stoptermcap();
6272 out_flush(); /* needed for SUN to restore xterm buffer */
6273#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006274 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275#endif
6276 ui_suspend(); /* call machine specific function */
6277#ifdef FEAT_TITLE
6278 maketitle();
6279 resettitle(); /* force updating the title */
6280#endif
6281 starttermcap();
6282 scroll_start(); /* scroll screen before redrawing */
6283 redraw_later_clear();
6284 shell_resized(); /* may have resized window */
6285 }
6286}
6287
6288/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006289 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 */
6291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006292ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293{
6294#ifdef FEAT_CMDWIN
6295 if (cmdwin_type != 0)
6296 {
6297 cmdwin_result = Ctrl_C;
6298 return;
6299 }
6300#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006301 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006302 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006303 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006304 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006305 return;
6306 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006307
6308 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006309 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310
6311 /*
6312 * if more files or windows we won't exit
6313 */
6314 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6315 exiting = TRUE;
6316 if ( ((eap->cmdidx == CMD_wq
6317 || curbufIsChanged())
6318 && do_write(eap) == FAIL)
6319 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006320 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 {
6322 not_exiting();
6323 }
6324 else
6325 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006328 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329# ifdef FEAT_GUI
6330 need_mouse_correct = TRUE;
6331# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006332 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006333 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 }
6335}
6336
6337/*
6338 * ":print", ":list", ":number".
6339 */
6340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006341ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006343 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006344 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006347 for ( ;!got_int; ui_breakcheck())
6348 {
6349 print_line(eap->line1,
6350 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6351 || (eap->flags & EXFLAG_NR)),
6352 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6353 if (++eap->line1 > eap->line2)
6354 break;
6355 out_flush(); /* show one line at a time */
6356 }
6357 setpcmark();
6358 /* put cursor at last line */
6359 curwin->w_cursor.lnum = eap->line2;
6360 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 }
6362
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364}
6365
6366#ifdef FEAT_BYTEOFF
6367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006368ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369{
6370 goto_byte(eap->line2);
6371}
6372#endif
6373
6374/*
6375 * ":shell".
6376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006378ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 do_shell(NULL, 0);
6381}
6382
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006383#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006385static int drop_busy = FALSE;
6386static int drop_filec;
6387static char_u **drop_filev = NULL;
6388static int drop_split;
6389static void (*drop_callback)(void *);
6390static void *drop_cookie;
6391
6392 static void
6393handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 exarg_T ea;
6396 int save_msg_scroll = msg_scroll;
6397
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006398 // Setting the argument list may cause screen updates and being called
6399 // recursively. Avoid that by setting drop_busy.
6400 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402 /* Check whether the current buffer is changed. If so, we will need
6403 * to split the current window or data could be lost.
6404 * We don't need to check if the 'hidden' option is set, as in this
6405 * case the buffer won't be lost.
6406 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006407 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 {
6409 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006410 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 --emsg_off;
6412 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006413 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if (win_split(0, 0) == FAIL)
6416 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006417 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418
6419 /* When splitting the window, create a new alist. Otherwise the
6420 * existing one is overwritten. */
6421 alist_unlink(curwin->w_alist);
6422 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 }
6424
6425 /*
6426 * Set up the new argument list.
6427 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006428 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
6430 /*
6431 * Move to the first file.
6432 */
6433 /* Fake up a minimal "next" command for do_argfile() */
6434 vim_memset(&ea, 0, sizeof(ea));
6435 ea.cmd = (char_u *)"next";
6436 do_argfile(&ea, 0);
6437
6438 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6439 * mode that is not needed here. */
6440 need_start_insertmode = FALSE;
6441
6442 /* Restore msg_scroll, otherwise a following command may cause scrolling
6443 * unexpectedly. The screen will be redrawn by the caller, thus
6444 * msg_scroll being set by displaying a message is irrelevant. */
6445 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006446
6447 if (drop_callback != NULL)
6448 drop_callback(drop_cookie);
6449
6450 drop_filev = NULL;
6451 drop_busy = FALSE;
6452}
6453
6454/*
6455 * Handle a file drop. The code is here because a drop is *nearly* like an
6456 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006457 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006458 * code is very similar to :args and hence needs access to a lot of the static
6459 * functions in this file.
6460 *
6461 * The "filev" list must have been allocated using alloc(), as should each item
6462 * in the list. This function takes over responsibility for freeing the "filev"
6463 * list.
6464 */
6465 void
6466handle_drop(
6467 int filec, // the number of files dropped
6468 char_u **filev, // the list of files dropped
6469 int split, // force splitting the window
6470 void (*callback)(void *), // to be called after setting the argument
6471 // list
6472 void *cookie) // argument for "callback" (allocated)
6473{
6474 // Cannot handle recursive drops, finish the pending one.
6475 if (drop_busy)
6476 {
6477 FreeWild(filec, filev);
6478 vim_free(cookie);
6479 return;
6480 }
6481
6482 // When calling handle_drop() more than once in a row we only use the last
6483 // one.
6484 if (drop_filev != NULL)
6485 {
6486 FreeWild(drop_filec, drop_filev);
6487 vim_free(drop_cookie);
6488 }
6489
6490 drop_filec = filec;
6491 drop_filev = filev;
6492 drop_split = split;
6493 drop_callback = callback;
6494 drop_cookie = cookie;
6495
6496 // Postpone this when:
6497 // - editing the command line
6498 // - not possible to change the current buffer
6499 // - updating the screen
6500 // As it may change buffers and window structures that are in use and cause
6501 // freed memory to be used.
6502 if (text_locked() || curbuf_locked() || updating_screen)
6503 return;
6504
6505 handle_drop_internal();
6506}
6507
6508/*
6509 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6510 * reset: Handle a postponed drop.
6511 */
6512 void
6513handle_any_postponed_drop(void)
6514{
6515 if (!drop_busy && drop_filev != NULL
6516 && !text_locked() && !curbuf_locked() && !updating_screen)
6517 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518}
6519#endif
6520
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521/*
6522 * Clear an argument list: free all file names and reset it to zero entries.
6523 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006524 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006525alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526{
6527 while (--al->al_ga.ga_len >= 0)
6528 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6529 ga_clear(&al->al_ga);
6530}
6531
6532/*
6533 * Init an argument list.
6534 */
6535 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006536alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6539}
6540
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541/*
6542 * Remove a reference from an argument list.
6543 * Ignored when the argument list is the global one.
6544 * If the argument list is no longer used by any window, free it.
6545 */
6546 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006547alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 if (al != &global_alist && --al->al_refcount <= 0)
6550 {
6551 alist_clear(al);
6552 vim_free(al);
6553 }
6554}
6555
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556/*
6557 * Create a new argument list and use it for the current window.
6558 */
6559 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006560alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006562 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 if (curwin->w_alist == NULL)
6564 {
6565 curwin->w_alist = &global_alist;
6566 ++global_alist.al_refcount;
6567 }
6568 else
6569 {
6570 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006571 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 alist_init(curwin->w_alist);
6573 }
6574}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006576#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577/*
6578 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006579 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6580 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 */
6582 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006583alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006586 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 char_u **new_arg_files;
6588 int new_arg_file_count;
6589 char_u *save_p_su = p_su;
6590 int i;
6591
6592 /* Don't use 'suffixes' here. This should work like the shell did the
6593 * expansion. Also, the vimrc file isn't read yet, thus the user
6594 * can't set the options. */
6595 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006596 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (old_arg_files != NULL)
6598 {
6599 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006600 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6601 old_arg_count = GARGCOUNT;
6602 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006604 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 && new_arg_file_count > 0)
6606 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006607 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6608 TRUE, fnum_list, fnum_len);
6609 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 }
6612 p_su = save_p_su;
6613}
6614#endif
6615
6616/*
6617 * Set the argument list for the current window.
6618 * Takes over the allocated files[] and the allocated fnames in it.
6619 */
6620 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006621alist_set(
6622 alist_T *al,
6623 int count,
6624 char_u **files,
6625 int use_curbuf,
6626 int *fnum_list,
6627 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006630 static int recursive = 0;
6631
6632 if (recursive)
6633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006634 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006635 return;
6636 }
6637 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
6639 alist_clear(al);
6640 if (ga_grow(&al->al_ga, count) == OK)
6641 {
6642 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006643 {
6644 if (got_int)
6645 {
6646 /* When adding many buffers this can take a long time. Allow
6647 * interrupting here. */
6648 while (i < count)
6649 vim_free(files[i++]);
6650 break;
6651 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006652
6653 /* May set buffer name of a buffer previously used for the
6654 * argument list, so that it's re-used by alist_add. */
6655 if (fnum_list != NULL && i < fnum_len)
6656 buf_set_name(fnum_list[i], files[i]);
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006659 ui_breakcheck();
6660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 vim_free(files);
6662 }
6663 else
6664 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006667
6668 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669}
6670
6671/*
6672 * Add file "fname" to argument list "al".
6673 * "fname" must have been allocated and "al" must have been checked for room.
6674 */
6675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006676alist_add(
6677 alist_T *al,
6678 char_u *fname,
6679 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 if (fname == NULL) /* don't add NULL file names */
6682 return;
6683#ifdef BACKSLASH_IN_FILENAME
6684 slash_adjust(fname);
6685#endif
6686 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6687 if (set_fnum > 0)
6688 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6689 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6690 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691}
6692
6693#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6694/*
6695 * Adjust slashes in file names. Called after 'shellslash' was set.
6696 */
6697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006698alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
6700 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006702 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703
6704 for (i = 0; i < GARGCOUNT; ++i)
6705 if (GARGLIST[i].ae_fname != NULL)
6706 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006707 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 if (wp->w_alist != &global_alist)
6709 for (i = 0; i < WARGCOUNT(wp); ++i)
6710 if (WARGLIST(wp)[i].ae_fname != NULL)
6711 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712}
6713#endif
6714
6715/*
6716 * ":preserve".
6717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006719ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006721 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 ml_preserve(curbuf, TRUE);
6723}
6724
6725/*
6726 * ":recover".
6727 */
6728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006729ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6732 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006733 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6734 | CCGD_MULTWIN
6735 | (eap->forceit ? CCGD_FORCEIT : 0)
6736 | CCGD_EXCMD)
6737
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 && (*eap->arg == NUL
6739 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006740 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 recoverymode = FALSE;
6742}
6743
6744/*
6745 * Command modifier used in a wrong way.
6746 */
6747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006748ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 eap->errmsg = e_invcmd;
6751}
6752
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753/*
6754 * :sview [+command] file split window with new file, read-only
6755 * :split [[+command] file] split window with current or new file
6756 * :vsplit [[+command] file] split window vertically with current or new file
6757 * :new [[+command] file] split window with no or new file
6758 * :vnew [[+command] file] split vertically window with no or new file
6759 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006760 *
6761 * :tabedit open new Tab page with empty window
6762 * :tabedit [+command] file open new Tab page and edit "file"
6763 * :tabnew [[+command] file] just like :tabedit
6764 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 */
6766 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006769 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006770#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006772#endif
6773#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006775#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006776 int use_tab = eap->cmdidx == CMD_tabedit
6777 || eap->cmdidx == CMD_tabfind
6778 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006780 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006781 return;
6782
Bram Moolenaar4033c552017-09-16 20:54:51 +02006783#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
Bram Moolenaar4033c552017-09-16 20:54:51 +02006787#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006789 * quickfix windows. But it's OK when doing ":tab split". */
6790 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 if (eap->cmdidx == CMD_split)
6793 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 if (eap->cmdidx == CMD_vsplit)
6795 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798
Bram Moolenaar4033c552017-09-16 20:54:51 +02006799#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006800 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {
6802 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6803 FNAME_MESS, TRUE, curbuf->b_ffname);
6804 if (fname == NULL)
6805 goto theend;
6806 eap->arg = fname;
6807 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006808# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006809 else
6810# endif
6811#endif
6812#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 && eap->cmdidx != CMD_new)
6816 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006817 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006818# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006819 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006820# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006821 au_has_group((char_u *)"FileExplorer"))
6822 {
6823 /* No browsing supported but we do have the file explorer:
6824 * Edit the directory. */
6825 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6826 eap->arg = (char_u *)".";
6827 }
6828 else
6829 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006830 fname = do_browse(0, (char_u *)(use_tab
6831 ? _("Edit File in new tab page")
6832 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006834 if (fname == NULL)
6835 goto theend;
6836 eap->arg = fname;
6837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 }
6839 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006842 /*
6843 * Either open new tab page or split the window.
6844 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006845 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006846 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006847 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6848 : eap->addr_count == 0 ? 0
6849 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006850 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006851 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006852
6853 /* set the alternate buffer for the window we came from */
6854 if (curwin != old_curwin
6855 && win_valid(old_curwin)
6856 && old_curwin->w_buffer != curbuf
6857 && !cmdmod.keepalt)
6858 old_curwin->w_alt_fnum = curbuf->b_fnum;
6859 }
6860 }
6861 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6863 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 /* Reset 'scrollbind' when editing another file, but keep it when
6865 * doing ":split" without arguments. */
6866 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006867# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006871 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 else
6873 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 do_exedit(eap, old_curwin);
6875 }
6876
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006877# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006881# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882theend:
6883 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006884# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006886
6887/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006888 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006889 */
6890 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006891tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006892{
6893 exarg_T ea;
6894
6895 vim_memset(&ea, 0, sizeof(ea));
6896 ea.cmdidx = CMD_tabnew;
6897 ea.cmd = (char_u *)"tabn";
6898 ea.arg = (char_u *)"";
6899 ex_splitview(&ea);
6900}
6901
6902/*
6903 * :tabnext command
6904 */
6905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006906ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006907{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006908 int tab_number;
6909
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006910 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006911 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006912 switch (eap->cmdidx)
6913 {
6914 case CMD_tabfirst:
6915 case CMD_tabrewind:
6916 goto_tabpage(1);
6917 break;
6918 case CMD_tablast:
6919 goto_tabpage(9999);
6920 break;
6921 case CMD_tabprevious:
6922 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006923 if (eap->arg && *eap->arg != NUL)
6924 {
6925 char_u *p = eap->arg;
6926 char_u *p_save = p;
6927
6928 tab_number = getdigits(&p);
6929 if (p == p_save || *p_save == '-' || *p != NUL
6930 || tab_number == 0)
6931 {
6932 /* No numbers as argument. */
6933 eap->errmsg = e_invarg;
6934 return;
6935 }
6936 }
6937 else
6938 {
6939 if (eap->addr_count == 0)
6940 tab_number = 1;
6941 else
6942 {
6943 tab_number = eap->line2;
6944 if (tab_number < 1)
6945 {
6946 eap->errmsg = e_invrange;
6947 return;
6948 }
6949 }
6950 }
6951 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006952 break;
6953 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006954 tab_number = get_tabpage_arg(eap);
6955 if (eap->errmsg == NULL)
6956 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006957 break;
6958 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006959}
6960
6961/*
6962 * :tabmove command
6963 */
6964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006965ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006966{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006967 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006968
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006969 tab_number = get_tabpage_arg(eap);
6970 if (eap->errmsg == NULL)
6971 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006972}
6973
6974/*
6975 * :tabs command: List tabs and their contents.
6976 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006978ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006979{
6980 tabpage_T *tp;
6981 win_T *wp;
6982 int tabcount = 1;
6983
6984 msg_start();
6985 msg_scroll = TRUE;
6986 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6987 {
6988 msg_putchar('\n');
6989 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006990 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006991 out_flush(); /* output one line at a time */
6992 ui_breakcheck();
6993
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006994 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006995 wp = firstwin;
6996 else
6997 wp = tp->tp_firstwin;
6998 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6999 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007000 msg_putchar('\n');
7001 msg_putchar(wp == curwin ? '>' : ' ');
7002 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007003 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7004 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007005 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007006 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007007 else
7008 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7009 IObuff, IOSIZE, TRUE);
7010 msg_outtrans(IObuff);
7011 out_flush(); /* output one line at a time */
7012 ui_breakcheck();
7013 }
7014 }
7015}
7016
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017/*
7018 * ":mode": Set screen mode.
7019 * If no argument given, just get the screen size and redraw.
7020 */
7021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007022ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 if (*eap->arg == NUL)
7025 shell_resized();
7026 else
7027 mch_screenmode(eap->arg);
7028}
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030/*
7031 * ":resize".
7032 * set, increment or decrement current window height
7033 */
7034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007035ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 int n;
7038 win_T *wp = curwin;
7039
7040 if (eap->addr_count > 0)
7041 {
7042 n = eap->line2;
7043 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7044 ;
7045 }
7046
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007047# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 if (cmdmod.split & WSP_VERT)
7052 {
7053 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007054 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7056 n = 9999;
7057 win_setwidth_win((int)n, wp);
7058 }
7059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 {
7061 if (*eap->arg == '-' || *eap->arg == '+')
7062 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007063 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 n = 9999;
7065 win_setheight_win((int)n, wp);
7066 }
7067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
7069/*
7070 * ":find [+command] <file>" command.
7071 */
7072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007073ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
7075#ifdef FEAT_SEARCHPATH
7076 char_u *fname;
7077 int count;
7078
7079 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7080 TRUE, curbuf->b_ffname);
7081 if (eap->addr_count > 0)
7082 {
7083 /* Repeat finding the file "count" times. This matters when it
7084 * appears several times in the path. */
7085 count = eap->line2;
7086 while (fname != NULL && --count > 0)
7087 {
7088 vim_free(fname);
7089 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7090 FALSE, curbuf->b_ffname);
7091 }
7092 }
7093
7094 if (fname != NULL)
7095 {
7096 eap->arg = fname;
7097#endif
7098 do_exedit(eap, NULL);
7099#ifdef FEAT_SEARCHPATH
7100 vim_free(fname);
7101 }
7102#endif
7103}
7104
7105/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007106 * ":open" simulation: for now just work like ":visual".
7107 */
7108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007109ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007110{
7111 regmatch_T regmatch;
7112 char_u *p;
7113
7114 curwin->w_cursor.lnum = eap->line2;
7115 beginline(BL_SOL | BL_FIX);
7116 if (*eap->arg == '/')
7117 {
7118 /* ":open /pattern/": put cursor in column found with pattern */
7119 ++eap->arg;
7120 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7121 *p = NUL;
7122 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7123 if (regmatch.regprog != NULL)
7124 {
7125 regmatch.rm_ic = p_ic;
7126 p = ml_get_curline();
7127 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007128 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007129 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007130 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007131 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007132 }
7133 /* Move to the NUL, ignore any other arguments. */
7134 eap->arg += STRLEN(eap->arg);
7135 }
7136 check_cursor();
7137
7138 eap->cmdidx = CMD_visual;
7139 do_exedit(eap, NULL);
7140}
7141
7142/*
7143 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 */
7145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007146ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148 do_exedit(eap, NULL);
7149}
7150
7151/*
7152 * ":edit <file>" command and alikes.
7153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007155do_exedit(
7156 exarg_T *eap,
7157 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007161 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007163 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007164 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 /*
7166 * ":vi" command ends Ex mode.
7167 */
7168 if (exmode_active && (eap->cmdidx == CMD_visual
7169 || eap->cmdidx == CMD_view))
7170 {
7171 exmode_active = FALSE;
7172 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007173 {
7174 /* Special case: ":global/pat/visual\NLvi-commands" */
7175 if (global_busy)
7176 {
7177 int rd = RedrawingDisabled;
7178 int nwr = no_wait_return;
7179 int ms = msg_scroll;
7180#ifdef FEAT_GUI
7181 int he = hold_gui_events;
7182#endif
7183
7184 if (eap->nextcmd != NULL)
7185 {
7186 stuffReadbuff(eap->nextcmd);
7187 eap->nextcmd = NULL;
7188 }
7189
7190 if (exmode_was != EXMODE_VIM)
7191 settmode(TMODE_RAW);
7192 RedrawingDisabled = 0;
7193 no_wait_return = 0;
7194 need_wait_return = FALSE;
7195 msg_scroll = 0;
7196#ifdef FEAT_GUI
7197 hold_gui_events = 0;
7198#endif
7199 must_redraw = CLEAR;
7200
7201 main_loop(FALSE, TRUE);
7202
7203 RedrawingDisabled = rd;
7204 no_wait_return = nwr;
7205 msg_scroll = ms;
7206#ifdef FEAT_GUI
7207 hold_gui_events = he;
7208#endif
7209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213
7214 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007215 || eap->cmdidx == CMD_tabnew
7216 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007217 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007219 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 setpcmark();
7221 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007222 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7223 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007225 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 || *eap->arg != NUL
7227#ifdef FEAT_BROWSE
7228 || cmdmod.browse
7229#endif
7230 )
7231 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007232 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7233 * can bring us here, others are stopped earlier. */
7234 if (*eap->arg != NUL && curbuf_locked())
7235 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 n = readonlymode;
7238 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7239 readonlymode = TRUE;
7240 else if (eap->cmdidx == CMD_enew)
7241 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7242 empty buffer */
7243 setpcmark();
7244 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7245 NULL, eap,
7246 /* ":edit" goes to first line if Vi compatible */
7247 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7248 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7249 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007250 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007252 /* after a split we can use an existing buffer */
7253 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007255 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 {
7257 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 if (old_curwin != NULL)
7259 {
7260 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007261 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007263#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007264 cleanup_T cs;
7265
7266 /* Reset the error/interrupt/exception state here so that
7267 * aborting() returns FALSE when closing a window. */
7268 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007269#endif
7270#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007272#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007273 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007274
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007275#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007276 /* Restore the error/interrupt/exception state if not
7277 * discarded by a new aborting error, interrupt, or
7278 * uncaught exception. */
7279 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 }
7282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 }
7284 else if (readonlymode && curbuf->b_nwindows == 1)
7285 {
7286 /* When editing an already visited buffer, 'readonly' won't be set
7287 * but the previous value is kept. With ":view" and ":sview" we
7288 * want the file to be readonly, except when another window is
7289 * editing the same buffer. */
7290 curbuf->b_p_ro = TRUE;
7291 }
7292 readonlymode = n;
7293 }
7294 else
7295 {
7296 if (eap->do_ecmd_cmd != NULL)
7297 do_cmdline_cmd(eap->do_ecmd_cmd);
7298#ifdef FEAT_TITLE
7299 n = curwin->w_arg_idx_invalid;
7300#endif
7301 check_arg_idx(curwin);
7302#ifdef FEAT_TITLE
7303 if (n != curwin->w_arg_idx_invalid)
7304 maketitle();
7305#endif
7306 }
7307
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 /*
7309 * if ":split file" worked, set alternate file name in old window to new
7310 * file
7311 */
7312 if (old_curwin != NULL
7313 && *eap->arg != NUL
7314 && curwin != old_curwin
7315 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007316 && old_curwin->w_buffer != curbuf
7317 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 ex_no_reprint = TRUE;
7321}
7322
7323#ifndef FEAT_GUI
7324/*
7325 * ":gui" and ":gvim" when there is no GUI.
7326 */
7327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007328ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329{
7330 eap->errmsg = e_nogvim;
7331}
7332#endif
7333
Bram Moolenaar4f974752019-02-17 17:44:42 +01007334#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007336ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
7338 gui_make_tearoff(eap->arg);
7339}
7340#endif
7341
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007342#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7343 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007345ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007347# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7348 if (gui.in_use)
7349 gui_make_popup(eap->arg, eap->forceit);
7350# ifdef FEAT_TERM_POPUP_MENU
7351 else
7352# endif
7353# endif
7354# ifdef FEAT_TERM_POPUP_MENU
7355 pum_make_popup(eap->arg, eap->forceit);
7356# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357}
7358#endif
7359
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007361ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362{
7363 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007364 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007366 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367}
7368
7369/*
7370 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7371 * offset.
7372 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007375ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007378 win_T *save_curwin = curwin;
7379 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 long topline;
7381 long y;
7382 linenr_T old_linenr = curwin->w_cursor.lnum;
7383
7384 setpcmark();
7385
7386 /*
7387 * determine max topline
7388 */
7389 if (curwin->w_p_scb)
7390 {
7391 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007392 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 {
7394 if (wp->w_p_scb && wp->w_buffer)
7395 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007396 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (topline > y)
7398 topline = y;
7399 }
7400 }
7401 if (topline < 1)
7402 topline = 1;
7403 }
7404 else
7405 {
7406 topline = 1;
7407 }
7408
7409
7410 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007411 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007413 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
7415 if (curwin->w_p_scb)
7416 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007417 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 y = topline - curwin->w_topline;
7419 if (y > 0)
7420 scrollup(y, TRUE);
7421 else
7422 scrolldown(-y, TRUE);
7423 curwin->w_scbind_pos = topline;
7424 redraw_later(VALID);
7425 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007429 curwin = save_curwin;
7430 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 if (curwin->w_p_scb)
7432 {
7433 did_syncbind = TRUE;
7434 checkpcmark();
7435 if (old_linenr != curwin->w_cursor.lnum)
7436 {
7437 char_u ctrl_o[2];
7438
7439 ctrl_o[0] = Ctrl_O;
7440 ctrl_o[1] = 0;
7441 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7442 }
7443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444}
7445
7446
7447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007448ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007450 int i;
7451 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7452 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
7454 if (eap->usefilter) /* :r!cmd */
7455 do_bang(1, eap, FALSE, FALSE, TRUE);
7456 else
7457 {
7458 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7459 return;
7460
7461#ifdef FEAT_BROWSE
7462 if (cmdmod.browse)
7463 {
7464 char_u *browseFile;
7465
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007466 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 NULL, NULL, NULL, curbuf);
7468 if (browseFile != NULL)
7469 {
7470 i = readfile(browseFile, NULL,
7471 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7472 vim_free(browseFile);
7473 }
7474 else
7475 i = OK;
7476 }
7477 else
7478#endif
7479 if (*eap->arg == NUL)
7480 {
7481 if (check_fname() == FAIL) /* check for no file name */
7482 return;
7483 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7484 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7485 }
7486 else
7487 {
7488 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7489 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7490 i = readfile(eap->arg, NULL,
7491 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7492
7493 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007494 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007496#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 if (!aborting())
7498#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007499 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500 }
7501 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 {
7503 if (empty && exmode_active)
7504 {
7505 /* Delete the empty line that remains. Historically ex does
7506 * this but vi doesn't. */
7507 if (eap->line2 == 0)
7508 lnum = curbuf->b_ml.ml_line_count;
7509 else
7510 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007511 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007512 {
7513 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007514 if (curwin->w_cursor.lnum > 1
7515 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007516 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007517 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007518 }
7519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 }
7523}
7524
Bram Moolenaarea408852005-06-25 22:49:46 +00007525static char_u *prev_dir = NULL;
7526
7527#if defined(EXITFREE) || defined(PROTO)
7528 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007529free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007530{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007531 VIM_CLEAR(prev_dir);
7532 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007533}
7534#endif
7535
Bram Moolenaarf4258302013-06-02 18:20:17 +02007536/*
7537 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007538 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7539 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007540 */
7541 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007542post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007543{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007544 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007545 // Clear tab local directory for both :cd and :tcd
7546 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007547 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007548 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007549 {
7550 /* If still in global directory, need to remember current
7551 * directory as global directory. */
7552 if (globaldir == NULL && prev_dir != NULL)
7553 globaldir = vim_strsave(prev_dir);
7554 /* Remember this local directory for the window. */
7555 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007556 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007557 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007558 curtab->tp_localdir = vim_strsave(NameBuff);
7559 else
7560 curwin->w_localdir = vim_strsave(NameBuff);
7561 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007562 }
7563 else
7564 {
7565 /* We are now in the global directory, no need to remember its
7566 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007567 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007568 }
7569
7570 shorten_fnames(TRUE);
7571}
7572
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007573/*
7574 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7575 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7576 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7577 * Otherwise changes the global directory.
7578 * Returns TRUE if the directory is successfully changed.
7579 */
7580 int
7581changedir_func(
7582 char_u *new_dir,
7583 int forceit,
7584 cdscope_T scope)
7585{
7586 char_u *tofree;
7587 int dir_differs;
7588 int retval = FALSE;
7589
7590 if (allbuf_locked())
7591 return FALSE;
7592
7593 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7594 {
7595 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7596 return FALSE;
7597 }
7598
7599 // ":cd -": Change to previous directory
7600 if (STRCMP(new_dir, "-") == 0)
7601 {
7602 if (prev_dir == NULL)
7603 {
7604 emsg(_("E186: No previous directory"));
7605 return FALSE;
7606 }
7607 new_dir = prev_dir;
7608 }
7609
7610 // Save current directory for next ":cd -"
7611 tofree = prev_dir;
7612 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7613 prev_dir = vim_strsave(NameBuff);
7614 else
7615 prev_dir = NULL;
7616
7617#if defined(UNIX) || defined(VMS)
7618 // for UNIX ":cd" means: go to home directory
7619 if (*new_dir == NUL)
7620 {
7621 // use NameBuff for home directory name
7622# ifdef VMS
7623 char_u *p;
7624
7625 p = mch_getenv((char_u *)"SYS$LOGIN");
7626 if (p == NULL || *p == NUL) // empty is the same as not set
7627 NameBuff[0] = NUL;
7628 else
7629 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7630# else
7631 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7632# endif
7633 new_dir = NameBuff;
7634 }
7635#endif
7636 dir_differs = new_dir == NULL || prev_dir == NULL
7637 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7638 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7639 emsg(_(e_failed));
7640 else
7641 {
7642 char_u *acmd_fname;
7643
7644 post_chdir(scope);
7645
7646 if (dir_differs)
7647 {
7648 if (scope == CDSCOPE_WINDOW)
7649 acmd_fname = (char_u *)"window";
7650 else if (scope == CDSCOPE_TABPAGE)
7651 acmd_fname = (char_u *)"tabpage";
7652 else
7653 acmd_fname = (char_u *)"global";
7654 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7655 curbuf);
7656 }
7657 retval = TRUE;
7658 }
7659 vim_free(tofree);
7660
7661 return retval;
7662}
Bram Moolenaarea408852005-06-25 22:49:46 +00007663
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007665 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007667 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007668ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007670 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671
7672 new_dir = eap->arg;
7673#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007674 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (*new_dir == NUL)
7676 ex_pwd(NULL);
7677 else
7678#endif
7679 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007680 cdscope_T scope = CDSCOPE_GLOBAL;
7681
7682 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7683 scope = CDSCOPE_WINDOW;
7684 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7685 scope = CDSCOPE_TABPAGE;
7686
7687 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007688 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007689 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007690 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 ex_pwd(eap);
7692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 }
7694}
7695
7696/*
7697 * ":pwd".
7698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007700ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7703 {
7704#ifdef BACKSLASH_IN_FILENAME
7705 slash_adjust(NameBuff);
7706#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007707 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
7709 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007710 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711}
7712
7713/*
7714 * ":=".
7715 */
7716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007717ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007719 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007720 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721}
7722
7723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007724ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007726 int n;
7727 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728
7729 if (cursor_valid())
7730 {
7731 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7732 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007733 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007735
7736 len = eap->line2;
7737 switch (*eap->arg)
7738 {
7739 case 'm': break;
7740 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007741 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007742 }
7743 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744}
7745
7746/*
7747 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7748 */
7749 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007750do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007753 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754
7755 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007756 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007757 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007759 wait_now = msec - done > 1000L ? 1000L : msec - done;
7760#ifdef FEAT_TIMERS
7761 {
7762 long due_time = check_due_timer();
7763
7764 if (due_time > 0 && due_time < wait_now)
7765 wait_now = due_time;
7766 }
7767#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007768#ifdef FEAT_JOB_CHANNEL
7769 if (has_any_channel() && wait_now > 100L)
7770 wait_now = 100L;
7771#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007772 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007773#ifdef FEAT_JOB_CHANNEL
7774 if (has_any_channel())
7775 ui_breakcheck_force(TRUE);
7776 else
7777#endif
7778 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007779#ifdef MESSAGE_QUEUE
7780 /* Process the netbeans and clientserver messages that may have been
7781 * received in the call to ui_breakcheck() when the GUI is in use. This
7782 * may occur when running a test case. */
7783 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007786
7787 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7788 // input buffer, otherwise a following call to input() fails.
7789 if (got_int)
7790 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791}
7792
7793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007794do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 int mode;
7797 char_u *cmdp;
7798
7799 cmdp = eap->cmd;
7800 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7801
7802 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7803 eap->arg, mode, isabbrev))
7804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007805 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007807 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 break;
7809 }
7810}
7811
7812/*
7813 * ":winsize" command (obsolete).
7814 */
7815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007816ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817{
7818 int w, h;
7819 char_u *arg = eap->arg;
7820 char_u *p;
7821
7822 w = getdigits(&arg);
7823 arg = skipwhite(arg);
7824 p = arg;
7825 h = getdigits(&arg);
7826 if (*p != NUL && *arg == NUL)
7827 set_shellsize(w, h, TRUE);
7828 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007829 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830}
7831
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007833ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
7835 int xchar = NUL;
7836 char_u *p;
7837
7838 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7839 {
7840 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7841 if (eap->arg[1] == NUL)
7842 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007843 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 return;
7845 }
7846 xchar = eap->arg[1];
7847 p = eap->arg + 2;
7848 }
7849 else
7850 p = eap->arg + 1;
7851
7852 eap->nextcmd = check_nextcmd(p);
7853 p = skipwhite(p);
7854 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007855 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007856 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 {
7858 /* Pass flags on for ":vertical wincmd ]". */
7859 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007860 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7862 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007863 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866
Bram Moolenaar843ee412004-06-30 16:16:41 +00007867#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868/*
7869 * ":winpos".
7870 */
7871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007872ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873{
7874 int x, y;
7875 char_u *arg = eap->arg;
7876 char_u *p;
7877
7878 if (*arg == NUL)
7879 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007880# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007881# ifdef VIMDLL
7882 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7883 mch_get_winpos(&x, &y) != FAIL)
7884# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007886# else
7887 if (mch_get_winpos(&x, &y) != FAIL)
7888# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 {
7890 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007891 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 }
7893 else
7894# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007895 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897 else
7898 {
7899 x = getdigits(&arg);
7900 arg = skipwhite(arg);
7901 p = arg;
7902 y = getdigits(&arg);
7903 if (*p == NUL || *arg != NUL)
7904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007905 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 return;
7907 }
7908# ifdef FEAT_GUI
7909 if (gui.in_use)
7910 gui_mch_set_winpos(x, y);
7911 else if (gui.starting)
7912 {
7913 /* Remember the coordinates for when the window is opened. */
7914 gui_win_x = x;
7915 gui_win_y = y;
7916 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007917# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 else
7919# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007920# endif
7921# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007922 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923# endif
7924# ifdef HAVE_TGETENT
7925 if (*T_CWP)
7926 term_set_winpos(x, y);
7927# endif
7928 }
7929}
7930#endif
7931
7932/*
7933 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7934 */
7935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007936ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937{
7938 oparg_T oa;
7939
7940 clear_oparg(&oa);
7941 oa.regname = eap->regname;
7942 oa.start.lnum = eap->line1;
7943 oa.end.lnum = eap->line2;
7944 oa.line_count = eap->line2 - eap->line1 + 1;
7945 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7948 {
7949 setpcmark();
7950 curwin->w_cursor.lnum = eap->line1;
7951 beginline(BL_SOL | BL_FIX);
7952 }
7953
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007954 if (VIsual_active)
7955 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007956
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 switch (eap->cmdidx)
7958 {
7959 case CMD_delete:
7960 oa.op_type = OP_DELETE;
7961 op_delete(&oa);
7962 break;
7963
7964 case CMD_yank:
7965 oa.op_type = OP_YANK;
7966 (void)op_yank(&oa, FALSE, TRUE);
7967 break;
7968
7969 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007970 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007972 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7973#else
7974 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007976 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 oa.op_type = OP_RSHIFT;
7978 else
7979 oa.op_type = OP_LSHIFT;
7980 op_shift(&oa, FALSE, eap->amount);
7981 break;
7982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007984 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985}
7986
7987/*
7988 * ":put".
7989 */
7990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007991ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992{
7993 /* ":0put" works like ":1put!". */
7994 if (eap->line2 == 0)
7995 {
7996 eap->line2 = 1;
7997 eap->forceit = TRUE;
7998 }
7999 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008000 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8001 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002}
8003
8004/*
8005 * Handle ":copy" and ":move".
8006 */
8007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008008ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 long n;
8011
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008012 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 if (eap->arg == NULL) /* error detected */
8014 {
8015 eap->nextcmd = NULL;
8016 return;
8017 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008018 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020 /*
8021 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8022 */
8023 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008025 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 return;
8027 }
8028
8029 if (eap->cmdidx == CMD_move)
8030 {
8031 if (do_move(eap->line1, eap->line2, n) == FAIL)
8032 return;
8033 }
8034 else
8035 ex_copy(eap->line1, eap->line2, n);
8036 u_clearline();
8037 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008038 ex_may_print(eap);
8039}
8040
8041/*
8042 * Print the current line if flags were given to the Ex command.
8043 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008044 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008045ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008046{
8047 if (eap->flags != 0)
8048 {
8049 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8050 (eap->flags & EXFLAG_LIST));
8051 ex_no_reprint = TRUE;
8052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053}
8054
8055/*
8056 * ":smagic" and ":snomagic".
8057 */
8058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008059ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060{
8061 int magic_save = p_magic;
8062
8063 p_magic = (eap->cmdidx == CMD_smagic);
8064 do_sub(eap);
8065 p_magic = magic_save;
8066}
8067
8068/*
8069 * ":join".
8070 */
8071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008072ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 curwin->w_cursor.lnum = eap->line1;
8075 if (eap->line1 == eap->line2)
8076 {
8077 if (eap->addr_count >= 2) /* :2,2join does nothing */
8078 return;
8079 if (eap->line2 == curbuf->b_ml.ml_line_count)
8080 {
8081 beep_flush();
8082 return;
8083 }
8084 ++eap->line2;
8085 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008086 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008088 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089}
8090
8091/*
8092 * ":[addr]@r" or ":[addr]*r": execute register
8093 */
8094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008095ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008098 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099
8100 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008101 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102
8103#ifdef USE_ON_FLY_SCROLL
8104 dont_scroll = TRUE; /* disallow scrolling here */
8105#endif
8106
8107 /* get the register name. No name means to use the previous one */
8108 c = *eap->arg;
8109 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8110 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008111 /* Put the register in the typeahead buffer with the "silent" flag. */
8112 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8113 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008114 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 else
8118 {
8119 int save_efr = exec_from_reg;
8120
8121 exec_from_reg = TRUE;
8122
8123 /*
8124 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008125 * Continue until the stuff buffer is empty and all added characters
8126 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008128 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8130
8131 exec_from_reg = save_efr;
8132 }
8133}
8134
8135/*
8136 * ":!".
8137 */
8138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008139ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140{
8141 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8142}
8143
8144/*
8145 * ":undo".
8146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008148ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008150 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008151 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008152 else
8153 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154}
8155
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008156#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008159{
8160 char_u hash[UNDO_HASH_SIZE];
8161
8162 u_compute_hash(hash);
8163 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8164}
8165
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008167ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008168{
8169 char_u hash[UNDO_HASH_SIZE];
8170
8171 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008172 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008173}
8174#endif
8175
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176/*
8177 * ":redo".
8178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008180ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181{
8182 u_redo(1);
8183}
8184
8185/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008186 * ":earlier" and ":later".
8187 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008189ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008190{
8191 long count = 0;
8192 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008193 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008194 char_u *p = eap->arg;
8195
8196 if (*p == NUL)
8197 count = 1;
8198 else if (isdigit(*p))
8199 {
8200 count = getdigits(&p);
8201 switch (*p)
8202 {
8203 case 's': ++p; sec = TRUE; break;
8204 case 'm': ++p; sec = TRUE; count *= 60; break;
8205 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008206 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8207 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008208 }
8209 }
8210
8211 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008212 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008213 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008214 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8215 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008216}
8217
8218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 * ":redir": start/stop redirection.
8220 */
8221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008222ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223{
8224 char *mode;
8225 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008226 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227
Bram Moolenaarba768492016-07-08 15:32:54 +02008228#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008229 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008231 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008232 return;
8233 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008234#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008235
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (STRICMP(eap->arg, "END") == 0)
8237 close_redir();
8238 else
8239 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008240 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008242 ++arg;
8243 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008245 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 mode = "a";
8247 }
8248 else
8249 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008250 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252 close_redir();
8253
8254 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008255 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 if (fname == NULL)
8257 return;
8258#ifdef FEAT_BROWSE
8259 if (cmdmod.browse)
8260 {
8261 char_u *browseFile;
8262
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008263 browseFile = do_browse(BROWSE_SAVE,
8264 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008265 fname, NULL, NULL,
8266 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 if (browseFile == NULL)
8268 return; /* operation cancelled */
8269 vim_free(fname);
8270 fname = browseFile;
8271 eap->forceit = TRUE; /* since dialog already asked */
8272 }
8273#endif
8274
8275 redir_fd = open_exfile(fname, eap->forceit, mode);
8276 vim_free(fname);
8277 }
8278#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008279 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {
8281 /* redirect to a register a-z (resp. A-Z for appending) */
8282 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008283 ++arg;
8284 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008286 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008287 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008289 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008291 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008292 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008293 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008296 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008297 if (*arg == '>')
8298 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008299 /* Make register empty when not using @A-@Z and the
8300 * command is valid. */
8301 if (*arg == NUL && !isupper(redir_reg))
8302 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008304 }
8305 if (*arg != NUL)
8306 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008307 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008308 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008309 }
8310 }
8311 else if (*arg == '=' && arg[1] == '>')
8312 {
8313 int append;
8314
8315 /* redirect to a variable */
8316 close_redir();
8317 arg += 2;
8318
8319 if (*arg == '>')
8320 {
8321 ++arg;
8322 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 }
8324 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008325 append = FALSE;
8326
8327 if (var_redir_start(skipwhite(arg), append) == OK)
8328 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 }
8330#endif
8331
8332 /* TODO: redirect to a buffer */
8333
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008335 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008337
8338 /* Make sure redirection is not off. Can happen for cmdline completion
8339 * that indirectly invokes a command to catch its output. */
8340 if (redir_fd != NULL
8341#ifdef FEAT_EVAL
8342 || redir_reg || redir_vname
8343#endif
8344 )
8345 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346}
8347
8348/*
8349 * ":redraw": force redraw
8350 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008351 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008352ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353{
8354 int r = RedrawingDisabled;
8355 int p = p_lz;
8356
8357 RedrawingDisabled = 0;
8358 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008359 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008361 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362#ifdef FEAT_TITLE
8363 if (need_maketitle)
8364 maketitle();
8365#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008366#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8367# ifdef VIMDLL
8368 if (!gui.in_use)
8369# endif
8370 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 RedrawingDisabled = r;
8373 p_lz = p;
8374
8375 /* Reset msg_didout, so that a message that's there is overwritten. */
8376 msg_didout = FALSE;
8377 msg_col = 0;
8378
Bram Moolenaar56667a52013-07-17 11:54:28 +02008379 /* No need to wait after an intentional redraw. */
8380 need_wait_return = FALSE;
8381
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 out_flush();
8383}
8384
8385/*
8386 * ":redrawstatus": force redraw of status line(s)
8387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008389ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 int r = RedrawingDisabled;
8392 int p = p_lz;
8393
8394 RedrawingDisabled = 0;
8395 p_lz = FALSE;
8396 if (eap->forceit)
8397 status_redraw_all();
8398 else
8399 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008400 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 RedrawingDisabled = r;
8402 p_lz = p;
8403 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404}
8405
Bram Moolenaare12bab32019-01-08 22:02:56 +01008406/*
8407 * ":redrawtabline": force redraw of the tabline
8408 */
8409 static void
8410ex_redrawtabline(exarg_T *eap UNUSED)
8411{
8412 int r = RedrawingDisabled;
8413 int p = p_lz;
8414
8415 RedrawingDisabled = 0;
8416 p_lz = FALSE;
8417
8418 draw_tabline();
8419
8420 RedrawingDisabled = r;
8421 p_lz = p;
8422 out_flush();
8423}
8424
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008426close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427{
8428 if (redir_fd != NULL)
8429 {
8430 fclose(redir_fd);
8431 redir_fd = NULL;
8432 }
8433#ifdef FEAT_EVAL
8434 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008435 if (redir_vname)
8436 {
8437 var_redir_stop();
8438 redir_vname = 0;
8439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440#endif
8441}
8442
8443#if defined(FEAT_SESSION) && defined(USE_CRNL)
8444# define MKSESSION_NL
8445static int mksession_nl = FALSE; /* use NL only in put_eol() */
8446#endif
8447
8448/*
8449 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8450 */
8451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008452ex_mkrc(
8453 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 FILE *fd;
8456 int failed = FALSE;
8457 char_u *fname;
8458#ifdef FEAT_BROWSE
8459 char_u *browseFile = NULL;
8460#endif
8461#ifdef FEAT_SESSION
8462 int view_session = FALSE;
8463 int using_vdir = FALSE; /* using 'viewdir'? */
8464 char_u *viewFile = NULL;
8465 unsigned *flagp;
8466#endif
8467
8468 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8469 {
8470#ifdef FEAT_SESSION
8471 view_session = TRUE;
8472#else
8473 ex_ni(eap);
8474 return;
8475#endif
8476 }
8477
8478#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008479 /* Use the short file name until ":lcd" is used. We also don't use the
8480 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008481 did_lcd = FALSE;
8482
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8484 if (eap->cmdidx == CMD_mkview
8485 && (*eap->arg == NUL
8486 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8487 {
8488 eap->forceit = TRUE;
8489 fname = get_view_file(*eap->arg);
8490 if (fname == NULL)
8491 return;
8492 viewFile = fname;
8493 using_vdir = TRUE;
8494 }
8495 else
8496#endif
8497 if (*eap->arg != NUL)
8498 fname = eap->arg;
8499 else if (eap->cmdidx == CMD_mkvimrc)
8500 fname = (char_u *)VIMRC_FILE;
8501#ifdef FEAT_SESSION
8502 else if (eap->cmdidx == CMD_mksession)
8503 fname = (char_u *)SESSION_FILE;
8504#endif
8505 else
8506 fname = (char_u *)EXRC_FILE;
8507
8508#ifdef FEAT_BROWSE
8509 if (cmdmod.browse)
8510 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008511 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512# ifdef FEAT_SESSION
8513 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8514 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8515# endif
8516 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008517 fname, (char_u *)"vim", NULL,
8518 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 if (browseFile == NULL)
8520 goto theend;
8521 fname = browseFile;
8522 eap->forceit = TRUE; /* since dialog already asked */
8523 }
8524#endif
8525
8526#if defined(FEAT_SESSION) && defined(vim_mkdir)
8527 /* When using 'viewdir' may have to create the directory. */
8528 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008529 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530#endif
8531
8532 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8533 if (fd != NULL)
8534 {
8535#ifdef FEAT_SESSION
8536 if (eap->cmdidx == CMD_mkview)
8537 flagp = &vop_flags;
8538 else
8539 flagp = &ssop_flags;
8540#endif
8541
8542#ifdef MKSESSION_NL
8543 /* "unix" in 'sessionoptions': use NL line separator */
8544 if (view_session && (*flagp & SSOP_UNIX))
8545 mksession_nl = TRUE;
8546#endif
8547
8548 /* Write the version command for :mkvimrc */
8549 if (eap->cmdidx == CMD_mkvimrc)
8550 (void)put_line(fd, "version 6.0");
8551
8552#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008553 if (eap->cmdidx == CMD_mksession)
8554 {
8555 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8556 failed = TRUE;
8557 }
8558
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 if (eap->cmdidx != CMD_mkview)
8560#endif
8561 {
8562 /* Write setting 'compatible' first, because it has side effects.
8563 * For that same reason only do it when needed. */
8564 if (p_cp)
8565 (void)put_line(fd, "if !&cp | set cp | endif");
8566 else
8567 (void)put_line(fd, "if &cp | set nocp | endif");
8568 }
8569
8570#ifdef FEAT_SESSION
8571 if (!view_session
8572 || (eap->cmdidx == CMD_mksession
8573 && (*flagp & SSOP_OPTIONS)))
8574#endif
8575 failed |= (makemap(fd, NULL) == FAIL
8576 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8577
8578#ifdef FEAT_SESSION
8579 if (!failed && view_session)
8580 {
8581 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8582 failed = TRUE;
8583 if (eap->cmdidx == CMD_mksession)
8584 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008585 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586
Bram Moolenaard9462e32011-04-11 21:35:11 +02008587 dirnow = alloc(MAXPATHL);
8588 if (dirnow == NULL)
8589 failed = TRUE;
8590 else
8591 {
8592 /*
8593 * Change to session file's dir.
8594 */
8595 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008597 *dirnow = NUL;
8598 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8599 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008600 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008601 shorten_fnames(TRUE);
8602 }
8603 else if (*dirnow != NUL
8604 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8605 {
8606 if (mch_chdir((char *)globaldir) == 0)
8607 shorten_fnames(TRUE);
8608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609
Bram Moolenaard9462e32011-04-11 21:35:11 +02008610 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611
Bram Moolenaard9462e32011-04-11 21:35:11 +02008612 /* restore original dir */
8613 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008615 {
8616 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008617 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008618 shorten_fnames(TRUE);
8619 }
8620 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 }
8622 }
8623 else
8624 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008625 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8626 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 }
8628 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8629 == FAIL)
8630 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008631# ifdef FEAT_SEARCH_EXTRA
8632 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8633 failed = TRUE;
8634# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008635 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8636 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008637 if (eap->cmdidx == CMD_mksession)
8638 {
8639 if (put_line(fd, "unlet SessionLoad") == FAIL)
8640 failed = TRUE;
8641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 }
8643#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008644 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8645 failed = TRUE;
8646
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 failed |= fclose(fd);
8648
8649 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008650 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8652 else if (eap->cmdidx == CMD_mksession)
8653 {
8654 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008655 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
Bram Moolenaard9462e32011-04-11 21:35:11 +02008657 tbuf = alloc(MAXPATHL);
8658 if (tbuf != NULL)
8659 {
8660 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8661 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8662 vim_free(tbuf);
8663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 }
8665#endif
8666#ifdef MKSESSION_NL
8667 mksession_nl = FALSE;
8668#endif
8669 }
8670
8671#ifdef FEAT_BROWSE
8672theend:
8673 vim_free(browseFile);
8674#endif
8675#ifdef FEAT_SESSION
8676 vim_free(viewFile);
8677#endif
8678}
8679
Bram Moolenaardf177f62005-02-22 08:39:57 +00008680#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8681 || defined(PROTO)
8682 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008683vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008684{
8685 if (vim_mkdir(name, prot) != 0)
8686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008687 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008688 return FAIL;
8689 }
8690 return OK;
8691}
8692#endif
8693
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694/*
8695 * Open a file for writing for an Ex command, with some checks.
8696 * Return file descriptor, or NULL on failure.
8697 */
8698 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008699open_exfile(
8700 char_u *fname,
8701 int forceit,
8702 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
8704 FILE *fd;
8705
8706#ifdef UNIX
8707 /* with Unix it is possible to open a directory */
8708 if (mch_isdir(fname))
8709 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008710 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 return NULL;
8712 }
8713#endif
8714 if (!forceit && *mode != 'a' && vim_fexists(fname))
8715 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008716 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 return NULL;
8718 }
8719
8720 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008721 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722
8723 return fd;
8724}
8725
8726/*
8727 * ":mark" and ":k".
8728 */
8729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008730ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
8732 pos_T pos;
8733
8734 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008735 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008737 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 else
8739 {
8740 pos = curwin->w_cursor; /* save curwin->w_cursor */
8741 curwin->w_cursor.lnum = eap->line2;
8742 beginline(BL_WHITE | BL_FIX);
8743 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008744 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8746 }
8747}
8748
8749/*
8750 * Update w_topline, w_leftcol and the cursor position.
8751 */
8752 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008753update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754{
8755 check_cursor(); /* put cursor on valid line */
8756 update_topline();
8757 if (!curwin->w_p_wrap)
8758 validate_cursor();
8759 update_curswant();
8760}
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008763 * Save the current State and go to Normal mode.
8764 * Return TRUE if the typeahead could be saved.
8765 */
8766 int
8767save_current_state(save_state_T *sst)
8768{
8769 sst->save_msg_scroll = msg_scroll;
8770 sst->save_restart_edit = restart_edit;
8771 sst->save_msg_didout = msg_didout;
8772 sst->save_State = State;
8773 sst->save_insertmode = p_im;
8774 sst->save_finish_op = finish_op;
8775 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008776 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008777
8778 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8779 restart_edit = 0; /* don't go to Insert mode */
8780 p_im = FALSE; /* don't use 'insertmode' */
8781
8782 /*
8783 * Save the current typeahead. This is required to allow using ":normal"
8784 * from an event handler and makes sure we don't hang when the argument
8785 * ends with half a command.
8786 */
8787 save_typeahead(&sst->tabuf);
8788 return sst->tabuf.typebuf_valid;
8789}
8790
8791 void
8792restore_current_state(save_state_T *sst)
8793{
8794 /* Restore the previous typeahead. */
8795 restore_typeahead(&sst->tabuf);
8796
8797 msg_scroll = sst->save_msg_scroll;
8798 restart_edit = sst->save_restart_edit;
8799 p_im = sst->save_insertmode;
8800 finish_op = sst->save_finish_op;
8801 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008802 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008803 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8804
8805 /* Restore the state (needed when called from a function executed for
8806 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8807 State = sst->save_State;
8808#ifdef CURSOR_SHAPE
8809 ui_cursor_shape(); /* may show different cursor shape */
8810#endif
8811}
8812
8813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 * ":normal[!] {commands}": Execute normal mode commands.
8815 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008816 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008817ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008819 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 char_u *arg = NULL;
8821 int l;
8822 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008824 if (ex_normal_lock > 0)
8825 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008826 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008827 return;
8828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 if (ex_normal_busy >= p_mmd)
8830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008831 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 return;
8833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 /*
8836 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8837 * this for the K_SPECIAL leading byte, otherwise special keys will not
8838 * work.
8839 */
8840 if (has_mbyte)
8841 {
8842 int len = 0;
8843
8844 /* Count the number of characters to be escaped. */
8845 for (p = eap->arg; *p != NUL; ++p)
8846 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008847#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 if (*p == CSI) /* leadbyte CSI */
8849 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008850#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008851 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008853#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 )
8857 len += 2;
8858 }
8859 if (len > 0)
8860 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008861 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (arg != NULL)
8863 {
8864 len = 0;
8865 for (p = eap->arg; *p != NUL; ++p)
8866 {
8867 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008868#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 if (*p == CSI)
8870 {
8871 arg[len++] = KS_EXTRA;
8872 arg[len++] = (int)KE_CSI;
8873 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008874#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008875 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 {
8877 arg[len++] = *++p;
8878 if (*p == K_SPECIAL)
8879 {
8880 arg[len++] = KS_SPECIAL;
8881 arg[len++] = KE_FILLER;
8882 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008883#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 else if (*p == CSI)
8885 {
8886 arg[len++] = KS_EXTRA;
8887 arg[len++] = (int)KE_CSI;
8888 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 }
8891 arg[len] = NUL;
8892 }
8893 }
8894 }
8895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008897 ++ex_normal_busy;
8898 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
8900 /*
8901 * Repeat the :normal command for each line in the range. When no
8902 * range given, execute it just once, without positioning the cursor
8903 * first.
8904 */
8905 do
8906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 if (eap->addr_count != 0)
8908 {
8909 curwin->w_cursor.lnum = eap->line1++;
8910 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008911 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
8913
Bram Moolenaar13505972019-01-24 15:04:48 +01008914 exec_normal_cmd(arg != NULL
8915 ? arg
8916 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 }
8918 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8919 }
8920
8921 /* Might not return to the main loop when in an event handler. */
8922 update_topline_cursor();
8923
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008924 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008926#ifdef FEAT_MOUSE
8927 setmouse();
8928#endif
8929#ifdef CURSOR_SHAPE
8930 ui_cursor_shape(); /* may show different cursor shape */
8931#endif
8932
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934}
8935
8936/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008937 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 */
8939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008940ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008942 if (eap->forceit)
8943 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008944 /* cursor line can be zero on startup */
8945 if (!curwin->w_cursor.lnum)
8946 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008947 coladvance((colnr_T)MAXCOL);
8948 curwin->w_curswant = MAXCOL;
8949 curwin->w_set_curswant = FALSE;
8950 }
8951
Bram Moolenaara40c5002005-01-09 21:16:21 +00008952 /* Ignore the command when already in Insert mode. Inserting an
8953 * expression register that invokes a function can do this. */
8954 if (State & INSERT)
8955 return;
8956
Bram Moolenaar64969662005-12-14 21:59:55 +00008957 if (eap->cmdidx == CMD_startinsert)
8958 restart_edit = 'a';
8959 else if (eap->cmdidx == CMD_startreplace)
8960 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008962 restart_edit = 'V';
8963
8964 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008966 if (eap->cmdidx == CMD_startinsert)
8967 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 curwin->w_curswant = 0; /* avoid MAXCOL */
8969 }
8970}
8971
8972/*
8973 * ":stopinsert"
8974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008976ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977{
8978 restart_edit = 0;
8979 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008980 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008983/*
8984 * Execute normal mode command "cmd".
8985 * "remap" can be REMAP_NONE or REMAP_YES.
8986 */
8987 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008988exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008989{
Bram Moolenaar25281632016-01-21 23:32:32 +01008990 /* Stuff the argument into the typeahead buffer. */
8991 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008992 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008993}
Bram Moolenaar25281632016-01-21 23:32:32 +01008994
Bram Moolenaar25281632016-01-21 23:32:32 +01008995/*
8996 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008997 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008998 */
8999 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009000exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009001{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009002 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009003 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009004
Bram Moolenaar905dd902019-04-07 14:21:47 +02009005 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9006 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009007 clear_oparg(&oa);
9008 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009009 while ((!stuff_empty()
9010 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009011 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009012 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009013 {
9014 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009015#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009016 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009017 && oa.op_type == OP_NOP && oa.regname == NUL
9018 && !VIsual_active)
9019 {
9020 /* If terminal_loop() returns OK we got a key that is handled
9021 * in Normal model. With FAIL we first need to position the
9022 * cursor and the screen needs to be redrawn. */
9023 if (terminal_loop(TRUE) == OK)
9024 normal_cmd(&oa, TRUE);
9025 }
9026 else
9027#endif
9028 /* execute a Normal mode cmd */
9029 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009030 }
9031}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009032
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033#ifdef FEAT_FIND_ID
9034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009035ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036{
9037 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9038 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9039 (linenr_T)1, (linenr_T)MAXLNUM);
9040}
9041
Bram Moolenaar4033c552017-09-16 20:54:51 +02009042#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043/*
9044 * ":psearch"
9045 */
9046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009047ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
9049 g_do_tagpreview = p_pvh;
9050 ex_findpat(eap);
9051 g_do_tagpreview = 0;
9052}
9053#endif
9054
9055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009056ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057{
9058 int whole = TRUE;
9059 long n;
9060 char_u *p;
9061 int action;
9062
9063 switch (cmdnames[eap->cmdidx].cmd_name[2])
9064 {
9065 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9066 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9067 action = ACTION_GOTO;
9068 else
9069 action = ACTION_SHOW;
9070 break;
9071 case 'i': /* ":ilist" and ":dlist" */
9072 action = ACTION_SHOW_ALL;
9073 break;
9074 case 'u': /* ":ijump" and ":djump" */
9075 action = ACTION_GOTO;
9076 break;
9077 default: /* ":isplit" and ":dsplit" */
9078 action = ACTION_SPLIT;
9079 break;
9080 }
9081
9082 n = 1;
9083 if (vim_isdigit(*eap->arg)) /* get count */
9084 {
9085 n = getdigits(&eap->arg);
9086 eap->arg = skipwhite(eap->arg);
9087 }
9088 if (*eap->arg == '/') /* Match regexp, not just whole words */
9089 {
9090 whole = FALSE;
9091 ++eap->arg;
9092 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9093 if (*p)
9094 {
9095 *p++ = NUL;
9096 p = skipwhite(p);
9097
9098 /* Check for trailing illegal characters */
9099 if (!ends_excmd(*p))
9100 eap->errmsg = e_trailing;
9101 else
9102 eap->nextcmd = check_nextcmd(p);
9103 }
9104 }
9105 if (!eap->skip)
9106 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9107 whole, !eap->forceit,
9108 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9109 n, action, eap->line1, eap->line2);
9110}
9111#endif
9112
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
Bram Moolenaar4033c552017-09-16 20:54:51 +02009114#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115/*
9116 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9117 */
9118 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009119ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009121 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9123}
9124
9125/*
9126 * ":pedit"
9127 */
9128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009129ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130{
9131 win_T *curwin_save = curwin;
9132
9133 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009134 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009135 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 do_exedit(eap, NULL);
9137 keep_help_flag = FALSE;
9138 if (curwin != curwin_save && win_valid(curwin_save))
9139 {
9140 /* Return cursor to where we were */
9141 validate_cursor();
9142 redraw_later(VALID);
9143 win_enter(curwin_save, TRUE);
9144 }
9145 g_do_tagpreview = 0;
9146}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148
9149/*
9150 * ":stag", ":stselect" and ":stjump".
9151 */
9152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009153ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155 postponed_split = -1;
9156 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009157 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9159 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009160 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162
9163/*
9164 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9165 */
9166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009167ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9170}
9171
9172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009173ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174{
9175 int cmd;
9176
9177 switch (name[1])
9178 {
9179 case 'j': cmd = DT_JUMP; /* ":tjump" */
9180 break;
9181 case 's': cmd = DT_SELECT; /* ":tselect" */
9182 break;
9183 case 'p': cmd = DT_PREV; /* ":tprevious" */
9184 break;
9185 case 'N': cmd = DT_PREV; /* ":tNext" */
9186 break;
9187 case 'n': cmd = DT_NEXT; /* ":tnext" */
9188 break;
9189 case 'o': cmd = DT_POP; /* ":pop" */
9190 break;
9191 case 'f': /* ":tfirst" */
9192 case 'r': cmd = DT_FIRST; /* ":trewind" */
9193 break;
9194 case 'l': cmd = DT_LAST; /* ":tlast" */
9195 break;
9196 default: /* ":tag" */
9197#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009198 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009200 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 return;
9202 }
9203#endif
9204 cmd = DT_TAG;
9205 break;
9206 }
9207
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009208 if (name[0] == 'l')
9209 {
9210#ifndef FEAT_QUICKFIX
9211 ex_ni(eap);
9212 return;
9213#else
9214 cmd = DT_LTAG;
9215#endif
9216 }
9217
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9219 eap->forceit, TRUE);
9220}
9221
9222/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009223 * Check "str" for starting with a special cmdline variable.
9224 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9225 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9226 */
9227 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009228find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009229{
9230 int len;
9231 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009232 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009233 "%",
9234#define SPEC_PERC 0
9235 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009236#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009237 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009238#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009239 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009240#define SPEC_CCWORD (SPEC_CWORD + 1)
9241 "<cexpr>", /* expr under cursor */
9242#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009243 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009244#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009245 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009246#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009247 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009248#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009249 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009250#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009251 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009252#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009253 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009254#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009255 "<sflnum>", /* script file line number */
9256#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009257#ifdef FEAT_CLIENTSERVER
9258 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009259# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009260#endif
9261 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009262
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009263 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009264 {
9265 len = (int)STRLEN(spec_str[i]);
9266 if (STRNCMP(src, spec_str[i], len) == 0)
9267 {
9268 *usedlen = len;
9269 return i;
9270 }
9271 }
9272 return -1;
9273}
9274
9275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 * Evaluate cmdline variables.
9277 *
9278 * change '%' to curbuf->b_ffname
9279 * '#' to curwin->w_altfile
9280 * '<cword>' to word under the cursor
9281 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02009282 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 * '<cfile>' to path name under the cursor
9284 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009285 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 * '<afile>' to file name for autocommand
9287 * '<abuf>' to buffer number for autocommand
9288 * '<amatch>' to matching name for autocommand
9289 *
9290 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9291 * "" for error without a message) and NULL is returned.
9292 * Returns an allocated string if a valid match was found.
9293 * Returns NULL if no match was found. "usedlen" then still contains the
9294 * number of characters to skip.
9295 */
9296 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009297eval_vars(
9298 char_u *src, /* pointer into commandline */
9299 char_u *srcstart, /* beginning of valid memory for src */
9300 int *usedlen, /* characters after src that are used */
9301 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009302 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009303 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009304 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
9306 int i;
9307 char_u *s;
9308 char_u *result;
9309 char_u *resultbuf = NULL;
9310 int resultlen;
9311 buf_T *buf;
9312 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9313 int spec_idx;
9314#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009315 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 int skip_mod = FALSE;
9317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319
9320 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009321 if (escaped != NULL)
9322 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323
9324 /*
9325 * Check if there is something to do.
9326 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009327 spec_idx = find_cmdline_var(src, usedlen);
9328 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 {
9330 *usedlen = 1;
9331 return NULL;
9332 }
9333
9334 /*
9335 * Skip when preceded with a backslash "\%" and "\#".
9336 * Note: In "\\%" the % is also not recognized!
9337 */
9338 if (src > srcstart && src[-1] == '\\')
9339 {
9340 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009341 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 return NULL;
9343 }
9344
9345 /*
9346 * word or WORD under cursor
9347 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009348 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9349 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009351 resultlen = find_ident_under_cursor(&result,
9352 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9353 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9354 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 if (resultlen == 0)
9356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009357 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 return NULL;
9359 }
9360 }
9361
9362 /*
9363 * '#': Alternate file name
9364 * '%': Current file name
9365 * File name under the cursor
9366 * File name for autocommand
9367 * and following modifiers
9368 */
9369 else
9370 {
9371 switch (spec_idx)
9372 {
9373 case SPEC_PERC: /* '%': current file */
9374 if (curbuf->b_fname == NULL)
9375 {
9376 result = (char_u *)"";
9377 valid = 0; /* Must have ":p:h" to be valid */
9378 }
9379 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009380 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009382#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009383 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009384#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386 break;
9387
9388 case SPEC_HASH: /* '#' or "#99": alternate file */
9389 if (src[1] == '#') /* "##": the argument list */
9390 {
9391 result = arg_all();
9392 resultbuf = result;
9393 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009394 if (escaped != NULL)
9395 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396#ifdef FEAT_MODIFY_FNAME
9397 skip_mod = TRUE;
9398#endif
9399 break;
9400 }
9401 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009402 if (*s == '<') /* "#<99" uses v:oldfiles */
9403 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009405 if (s == src + 2 && src[1] == '-')
9406 /* just a minus sign, don't skip over it */
9407 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 *usedlen = (int)(s - src); /* length of what we expand */
9409
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009410 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009412 if (*usedlen < 2)
9413 {
9414 /* Should we give an error message for #<text? */
9415 *usedlen = 1;
9416 return NULL;
9417 }
9418#ifdef FEAT_EVAL
9419 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9420 (long)i);
9421 if (result == NULL)
9422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009423 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009424 return NULL;
9425 }
9426#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009427 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009429#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 }
9431 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009432 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009433 if (i == 0 && src[1] == '<' && *usedlen > 1)
9434 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009435 buf = buflist_findnr(i);
9436 if (buf == NULL)
9437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009438 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009439 return NULL;
9440 }
9441 if (lnump != NULL)
9442 *lnump = ECMD_LAST;
9443 if (buf->b_fname == NULL)
9444 {
9445 result = (char_u *)"";
9446 valid = 0; /* Must have ":p:h" to be valid */
9447 }
9448 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009449 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009450 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009451#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009452 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009453#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009454 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 break;
9457
9458#ifdef FEAT_SEARCHPATH
9459 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009460 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 if (result == NULL)
9462 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009463 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 return NULL;
9465 }
9466 resultbuf = result; /* remember allocated string */
9467 break;
9468#endif
9469
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 case SPEC_AFILE: /* file name for autocommand */
9471 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009472 if (result != NULL && !autocmd_fname_full)
9473 {
9474 /* Still need to turn the fname into a full path. It is
9475 * postponed to avoid a delay when <afile> is not used. */
9476 autocmd_fname_full = TRUE;
9477 result = FullName_save(autocmd_fname, FALSE);
9478 vim_free(autocmd_fname);
9479 autocmd_fname = result;
9480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 if (result == NULL)
9482 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009483 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 return NULL;
9485 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009486 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 break;
9488
9489 case SPEC_ABUF: /* buffer number for autocommand */
9490 if (autocmd_bufnr <= 0)
9491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009492 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 return NULL;
9494 }
9495 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9496 result = strbuf;
9497 break;
9498
9499 case SPEC_AMATCH: /* match name for autocommand */
9500 result = autocmd_match;
9501 if (result == NULL)
9502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009503 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 return NULL;
9505 }
9506 break;
9507
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 case SPEC_SFILE: /* file name for ":so" command */
9509 result = sourcing_name;
9510 if (result == NULL)
9511 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009512 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 return NULL;
9514 }
9515 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009516
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009517 case SPEC_SLNUM: /* line in file for ":so" command */
9518 if (sourcing_name == NULL || sourcing_lnum == 0)
9519 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009520 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009521 return NULL;
9522 }
9523 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9524 result = strbuf;
9525 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009526
9527#ifdef FEAT_EVAL
9528 case SPEC_SFLNUM: /* line in script file */
9529 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009531 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009532 return NULL;
9533 }
9534 sprintf((char *)strbuf, "%ld",
9535 (long)(current_sctx.sc_lnum + sourcing_lnum));
9536 result = strbuf;
9537 break;
9538#endif
9539
9540#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009542 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9543 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 result = strbuf;
9545 break;
9546#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009547
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009548 default:
9549 result = (char_u *)""; /* avoid gcc warning */
9550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 }
9552
9553 resultlen = (int)STRLEN(result); /* length of new string */
9554 if (src[*usedlen] == '<') /* remove the file name extension */
9555 {
9556 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 resultlen = (int)(s - result);
9559 }
9560#ifdef FEAT_MODIFY_FNAME
9561 else if (!skip_mod)
9562 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009563 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 &resultlen);
9565 if (result == NULL)
9566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009567 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 return NULL;
9569 }
9570 }
9571#endif
9572 }
9573
9574 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9575 {
9576 if (valid != VALID_HEAD + VALID_PATH)
9577 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009578 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009580 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 result = NULL;
9582 }
9583 else
9584 result = vim_strnsave(result, resultlen);
9585 vim_free(resultbuf);
9586 return result;
9587}
9588
9589/*
9590 * Concatenate all files in the argument list, separated by spaces, and return
9591 * it in one allocated string.
9592 * Spaces and backslashes in the file names are escaped with a backslash.
9593 * Returns NULL when out of memory.
9594 */
9595 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009596arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 int len;
9599 int idx;
9600 char_u *retval = NULL;
9601 char_u *p;
9602
9603 /*
9604 * Do this loop two times:
9605 * first time: compute the total length
9606 * second time: concatenate the names
9607 */
9608 for (;;)
9609 {
9610 len = 0;
9611 for (idx = 0; idx < ARGCOUNT; ++idx)
9612 {
9613 p = alist_name(&ARGLIST[idx]);
9614 if (p != NULL)
9615 {
9616 if (len > 0)
9617 {
9618 /* insert a space in between names */
9619 if (retval != NULL)
9620 retval[len] = ' ';
9621 ++len;
9622 }
9623 for ( ; *p != NUL; ++p)
9624 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009625 if (*p == ' '
9626#ifndef BACKSLASH_IN_FILENAME
9627 || *p == '\\'
9628#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009629 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630 {
9631 /* insert a backslash */
9632 if (retval != NULL)
9633 retval[len] = '\\';
9634 ++len;
9635 }
9636 if (retval != NULL)
9637 retval[len] = *p;
9638 ++len;
9639 }
9640 }
9641 }
9642
9643 /* second time: break here */
9644 if (retval != NULL)
9645 {
9646 retval[len] = NUL;
9647 break;
9648 }
9649
9650 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009651 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 if (retval == NULL)
9653 break;
9654 }
9655
9656 return retval;
9657}
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659/*
9660 * Expand the <sfile> string in "arg".
9661 *
9662 * Returns an allocated string, or NULL for any error.
9663 */
9664 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009665expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009667 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668 int len;
9669 char_u *result;
9670 char_u *newres;
9671 char_u *repl;
9672 int srclen;
9673 char_u *p;
9674
9675 result = vim_strsave(arg);
9676 if (result == NULL)
9677 return NULL;
9678
9679 for (p = result; *p; )
9680 {
9681 if (STRNCMP(p, "<sfile>", 7) != 0)
9682 ++p;
9683 else
9684 {
9685 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009686 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 if (errormsg != NULL)
9688 {
9689 if (*errormsg)
9690 emsg(errormsg);
9691 vim_free(result);
9692 return NULL;
9693 }
9694 if (repl == NULL) /* no match (cannot happen) */
9695 {
9696 p += srclen;
9697 continue;
9698 }
9699 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9700 newres = alloc(len);
9701 if (newres == NULL)
9702 {
9703 vim_free(repl);
9704 vim_free(result);
9705 return NULL;
9706 }
9707 mch_memmove(newres, result, (size_t)(p - result));
9708 STRCPY(newres + (p - result), repl);
9709 len = (int)STRLEN(newres);
9710 STRCAT(newres, p + srclen);
9711 vim_free(repl);
9712 vim_free(result);
9713 result = newres;
9714 p = newres + len; /* continue after the match */
9715 }
9716 }
9717
9718 return result;
9719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720
9721#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009722static int ses_winsizes(FILE *fd, int restore_size,
9723 win_T *tab_firstwin);
9724static int ses_win_rec(FILE *fd, frame_T *fr);
9725static frame_T *ses_skipframe(frame_T *fr);
9726static int ses_do_frame(frame_T *fr);
9727static int ses_do_win(win_T *wp);
9728static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9729static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009730static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731
9732/*
9733 * Write openfile commands for the current buffers to an .exrc file.
9734 * Return FAIL on error, OK otherwise.
9735 */
9736 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009737makeopens(
9738 FILE *fd,
9739 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741 buf_T *buf;
9742 int only_save_windows = TRUE;
9743 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 int restore_size = TRUE;
9745 win_T *wp;
9746 char_u *sname;
9747 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009748 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009749 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009750 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009751 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009752 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009753 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754
9755 if (ssop_flags & SSOP_BUFFERS)
9756 only_save_windows = FALSE; /* Save ALL buffers */
9757
9758 /*
9759 * Begin by setting the this_session variable, and then other
9760 * sessionable variables.
9761 */
9762#ifdef FEAT_EVAL
9763 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9764 return FAIL;
9765 if (ssop_flags & SSOP_GLOBALS)
9766 if (store_session_globals(fd) == FAIL)
9767 return FAIL;
9768#endif
9769
9770 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009771 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 */
9773 if (put_line(fd, "silent only") == FAIL)
9774 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009775 if ((ssop_flags & SSOP_TABPAGES)
9776 && put_line(fd, "silent tabonly") == FAIL)
9777 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778
9779 /*
9780 * Now a :cd command to the session directory or the current directory
9781 */
9782 if (ssop_flags & SSOP_SESDIR)
9783 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009784 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9785 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 return FAIL;
9787 }
9788 else if (ssop_flags & SSOP_CURDIR)
9789 {
9790 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9791 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009792 || fputs("cd ", fd) < 0
9793 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9794 || put_eol(fd) == FAIL)
9795 {
9796 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 vim_free(sname);
9800 }
9801
9802 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009803 * If there is an empty, unnamed buffer we will wipe it out later.
9804 * Remember the buffer number.
9805 */
9806 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9807 return FAIL;
9808 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9809 return FAIL;
9810 if (put_line(fd, "endif") == FAIL)
9811 return FAIL;
9812
9813 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 * Now save the current files, current buffer first.
9815 */
9816 if (put_line(fd, "set shortmess=aoO") == FAIL)
9817 return FAIL;
9818
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009820 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9822 return FAIL;
9823
9824 if (ssop_flags & SSOP_RESIZE)
9825 {
9826 /* Note: after the restore we still check it worked!*/
9827 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9828 || put_eol(fd) == FAIL)
9829 return FAIL;
9830 }
9831
9832#ifdef FEAT_GUI
9833 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9834 {
9835 int x, y;
9836
9837 if (gui_mch_get_winpos(&x, &y) == OK)
9838 {
9839 /* Note: after the restore we still check it worked!*/
9840 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9841 return FAIL;
9842 }
9843 }
9844#endif
9845
9846 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009847 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9848 * will be displayed when creating the next tab. That resizes the windows
9849 * in the first tab, which may cause problems. Set 'showtabline' to 2
9850 * temporarily to avoid that.
9851 */
9852 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9853 {
9854 if (put_line(fd, "set stal=2") == FAIL)
9855 return FAIL;
9856 restore_stal = TRUE;
9857 }
9858
9859 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009860 * May repeat putting Windows for each tab, when "tabpages" is in
9861 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009862 * Don't use goto_tabpage(), it may change directory and trigger
9863 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009865 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009866 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009867 if ((ssop_flags & SSOP_TABPAGES))
9868 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009869 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009870
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009871 // Similar to ses_win_rec() below, populate the tab pages first so
9872 // later local options won't be copied to the new tabs.
9873 FOR_ALL_TABPAGES(tp)
9874 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009875 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009876 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009877 return FAIL;
9878 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009879 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009881 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009882 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009883 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009884
Bram Moolenaar18144c82006-04-12 21:52:12 +00009885 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009887 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009888
9889 if (tp == NULL)
9890 break; /* done all tab pages */
9891 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009892 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009893 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009894 tab_topframe = topframe;
9895 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009896 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009897 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009898 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009899 tab_topframe = tp->tp_topframe;
9900 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009901 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009902 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
Bram Moolenaar18144c82006-04-12 21:52:12 +00009905 /*
9906 * Before creating the window layout, try loading one file. If this
9907 * is aborted we don't end up with a number of useless windows.
9908 * This may have side effects! (e.g., compressed or network file).
9909 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009910 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009911 {
9912 if (ses_do_win(wp)
9913 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009914 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009915#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02009916 && !bt_nofilename(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009917#endif
9918 )
9919 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009920 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9921 return FAIL;
9922 need_tabnext = FALSE;
9923
9924 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009925 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9926 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009927 return FAIL;
9928 if (!wp->w_arg_idx_invalid)
9929 edited_win = wp;
9930 break;
9931 }
9932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009934 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009935 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009936 return FAIL;
9937
Bram Moolenaar18144c82006-04-12 21:52:12 +00009938 /*
9939 * Save current window layout.
9940 */
9941 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009943 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009945 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9946 return FAIL;
9947 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9948 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949
Bram Moolenaar18144c82006-04-12 21:52:12 +00009950 /*
9951 * Check if window sizes can be restored (no windows omitted).
9952 * Remember the window number of the current window after restoring.
9953 */
9954 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009955 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009956 {
9957 if (ses_do_win(wp))
9958 ++nr;
9959 else
9960 restore_size = FALSE;
9961 if (curwin == wp)
9962 cnr = nr;
9963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964
Bram Moolenaar18144c82006-04-12 21:52:12 +00009965 /* Go to the first window. */
9966 if (put_line(fd, "wincmd t") == FAIL)
9967 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009968
Bram Moolenaar18144c82006-04-12 21:52:12 +00009969 /*
9970 * If more than one window, see if sizes can be restored.
9971 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9972 * resized when moving between windows.
9973 * Do this before restoring the view, so that the topline and the
9974 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009975 * winminheight and winminwidth need to be set to avoid an error if the
9976 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009977 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009978 if (put_line(fd, "set winminheight=0") == FAIL
9979 || put_line(fd, "set winheight=1") == FAIL
9980 || put_line(fd, "set winminwidth=0") == FAIL
9981 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009982 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009983 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009984 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009986 // Restore the tab-local working directory if specified
9987 // Do this before the windows, so that the window-local directory can
9988 // override the tab-local directory.
9989 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9990 {
9991 if (fputs("tcd ", fd) < 0
9992 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9993 || put_eol(fd) == FAIL)
9994 return FAIL;
9995 did_lcd = TRUE;
9996 }
9997
Bram Moolenaar18144c82006-04-12 21:52:12 +00009998 /*
9999 * Restore the view of the window (options, file, cursor, etc.).
10000 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010001 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010002 {
10003 if (!ses_do_win(wp))
10004 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010005 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10006 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010007 return FAIL;
10008 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10009 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010010 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010011 }
10012
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010013 /* The argument index in the first tab page is zero, need to set it in
10014 * each window. For further tab pages it's the window where we do
10015 * "tabedit". */
10016 cur_arg_idx = next_arg_idx;
10017
Bram Moolenaar18144c82006-04-12 21:52:12 +000010018 /*
10019 * Restore cursor to the current window if it's not the first one.
10020 */
10021 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10022 || put_eol(fd) == FAIL))
10023 return FAIL;
10024
10025 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010026 * Restore window sizes again after jumping around in windows, because
10027 * the current window has a minimum size while others may not.
10028 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010029 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010030 return FAIL;
10031
Bram Moolenaar18144c82006-04-12 21:52:12 +000010032 /* Don't continue in another tab page when doing only the current one
10033 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010034 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010035 break;
10036 }
10037
10038 if (ssop_flags & SSOP_TABPAGES)
10039 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010040 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10041 || put_eol(fd) == FAIL)
10042 return FAIL;
10043 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010044 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10045 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010046
Bram Moolenaard39e2752019-01-26 20:07:38 +010010047 // Now put the remaining buffers into the buffer list.
10048 // This is near the end, so that when 'hidden' is set we don't create extra
10049 // buffers. If the buffer was already created with another command the
10050 // ":badd" will have no effect.
10051 FOR_ALL_BUFFERS(buf)
10052 {
10053 if (!(only_save_windows && buf->b_nwindows == 0)
10054 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10055#ifdef FEAT_TERMINAL
10056 // Skip terminal buffers: finished ones are not useful, others
10057 // will be resurrected and result in a new buffer.
10058 && !bt_terminal(buf)
10059#endif
10060 && buf->b_fname != NULL
10061 && buf->b_p_bl)
10062 {
10063 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10064 : buf->b_wininfo->wi_fpos.lnum) < 0
10065 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
10066 return FAIL;
10067 }
10068 }
10069
Bram Moolenaar9c102382006-05-03 21:26:49 +000010070 /*
10071 * Wipe out an empty unnamed buffer we started in.
10072 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020010073 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010074 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010075 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010076 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010077 return FAIL;
10078 if (put_line(fd, "endif") == FAIL)
10079 return FAIL;
10080 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10081 return FAIL;
10082
10083 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10084 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10085 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10086 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010010087 /* Re-apply 'winminheight' and 'winminwidth'. */
10088 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
10089 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
10090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091
10092 /*
10093 * Lastly, execute the x.vim file if it exists.
10094 */
10095 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10096 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010097 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 || put_line(fd, "endif") == FAIL)
10099 return FAIL;
10100
10101 return OK;
10102}
10103
10104 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010105ses_winsizes(
10106 FILE *fd,
10107 int restore_size,
10108 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109{
10110 int n = 0;
10111 win_T *wp;
10112
10113 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10114 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010115 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 {
10117 if (!ses_do_win(wp))
10118 continue;
10119 ++n;
10120
10121 /* restore height when not full height */
10122 if (wp->w_height + wp->w_status_height < topframe->fr_height
10123 && (fprintf(fd,
10124 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10125 n, (long)wp->w_height, Rows / 2, Rows) < 0
10126 || put_eol(fd) == FAIL))
10127 return FAIL;
10128
10129 /* restore width when not full width */
10130 if (wp->w_width < Columns && (fprintf(fd,
10131 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10132 n, (long)wp->w_width, Columns / 2, Columns) < 0
10133 || put_eol(fd) == FAIL))
10134 return FAIL;
10135 }
10136 }
10137 else
10138 {
10139 /* Just equalise window sizes */
10140 if (put_line(fd, "wincmd =") == FAIL)
10141 return FAIL;
10142 }
10143 return OK;
10144}
10145
10146/*
10147 * Write commands to "fd" to recursively create windows for frame "fr",
10148 * horizontally and vertically split.
10149 * After the commands the last window in the frame is the current window.
10150 * Returns FAIL when writing the commands to "fd" fails.
10151 */
10152 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010153ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154{
10155 frame_T *frc;
10156 int count = 0;
10157
10158 if (fr->fr_layout != FR_LEAF)
10159 {
10160 /* Find first frame that's not skipped and then create a window for
10161 * each following one (first frame is already there). */
10162 frc = ses_skipframe(fr->fr_child);
10163 if (frc != NULL)
10164 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10165 {
10166 /* Make window as big as possible so that we have lots of room
10167 * to split. */
10168 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10169 || put_line(fd, fr->fr_layout == FR_COL
10170 ? "split" : "vsplit") == FAIL)
10171 return FAIL;
10172 ++count;
10173 }
10174
10175 /* Go back to the first window. */
10176 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10177 ? "%dwincmd k" : "%dwincmd h", count) < 0
10178 || put_eol(fd) == FAIL))
10179 return FAIL;
10180
10181 /* Recursively create frames/windows in each window of this column or
10182 * row. */
10183 frc = ses_skipframe(fr->fr_child);
10184 while (frc != NULL)
10185 {
10186 ses_win_rec(fd, frc);
10187 frc = ses_skipframe(frc->fr_next);
10188 /* Go to next window. */
10189 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10190 return FAIL;
10191 }
10192 }
10193 return OK;
10194}
10195
10196/*
10197 * Skip frames that don't contain windows we want to save in the Session.
10198 * Returns NULL when there none.
10199 */
10200 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010201ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202{
10203 frame_T *frc;
10204
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010205 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 if (ses_do_frame(frc))
10207 break;
10208 return frc;
10209}
10210
10211/*
10212 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10213 * the Session.
10214 */
10215 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010216ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217{
10218 frame_T *frc;
10219
10220 if (fr->fr_layout == FR_LEAF)
10221 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010222 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 if (ses_do_frame(frc))
10224 return TRUE;
10225 return FALSE;
10226}
10227
10228/*
10229 * Return non-zero if window "wp" is to be stored in the Session.
10230 */
10231 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010232ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010234#ifdef FEAT_TERMINAL
10235 if (bt_terminal(wp->w_buffer))
10236 return !term_is_finished(wp->w_buffer)
10237 && (ssop_flags & SSOP_TERMINAL)
10238 && term_should_restore(wp->w_buffer);
10239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240 if (wp->w_buffer->b_fname == NULL
10241#ifdef FEAT_QUICKFIX
10242 /* When 'buftype' is "nofile" can't restore the window contents. */
Bram Moolenaar26910de2019-06-15 19:37:15 +020010243 || bt_nofilename(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244#endif
10245 )
10246 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010247 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 return (ssop_flags & SSOP_HELP);
10249 return TRUE;
10250}
10251
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010252 static int
10253put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10254{
10255 int r;
10256
10257 if (wp->w_curswant == MAXCOL)
10258 r = fprintf(fd, "%snormal! $", spaces);
10259 else
10260 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10261 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10262}
10263
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264/*
10265 * Write commands to "fd" to restore the view of a window.
10266 * Caller must make sure 'scrolloff' is zero.
10267 */
10268 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010269put_view(
10270 FILE *fd,
10271 win_T *wp,
10272 int add_edit, /* add ":edit" command to view */
10273 unsigned *flagp, /* vop_flags or ssop_flags */
10274 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010275 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276{
10277 win_T *save_curwin;
10278 int f;
10279 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010280 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281
10282 /* Always restore cursor position for ":mksession". For ":mkview" only
10283 * when 'viewoptions' contains "cursor". */
10284 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10285
10286 /*
10287 * Local argument list.
10288 */
10289 if (wp->w_alist == &global_alist)
10290 {
10291 if (put_line(fd, "argglobal") == FAIL)
10292 return FAIL;
10293 }
10294 else
10295 {
10296 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10297 flagp == &vop_flags
10298 || !(*flagp & SSOP_CURDIR)
10299 || wp->w_localdir != NULL, flagp) == FAIL)
10300 return FAIL;
10301 }
10302
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010303 /* Only when part of a session: restore the argument index. Some
10304 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010305 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010306 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010308 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 || put_eol(fd) == FAIL)
10310 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010311 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 }
10313
10314 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010315 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010317# ifdef FEAT_TERMINAL
10318 if (bt_terminal(wp->w_buffer))
10319 {
10320 if (term_write_session(fd, wp) == FAIL)
10321 return FAIL;
10322 }
10323 else
10324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 /*
10326 * Load the file.
10327 */
10328 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010329# ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +020010330 && !bt_nofilename(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332 )
10333 {
10334 /*
10335 * Editing a file in this buffer: use ":edit file".
10336 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010337 *
10338 * Note, if a buffer for that file already exists, use :badd to
10339 * edit that buffer, to not lose folding information (:edit resets
10340 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010342 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010343 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010344 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010345 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10346 || fputs(" | else | edit ", fd) < 0
10347 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10348 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010349 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 return FAIL;
10351 }
10352 else
10353 {
10354 /* No file in this buffer, just make it empty. */
10355 if (put_line(fd, "enew") == FAIL)
10356 return FAIL;
10357#ifdef FEAT_QUICKFIX
10358 if (wp->w_buffer->b_ffname != NULL)
10359 {
10360 /* The buffer does have a name, but it's not a file name. */
10361 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010362 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 return FAIL;
10364 }
10365#endif
10366 do_cursor = FALSE;
10367 }
10368 }
10369
10370 /*
10371 * Local mappings and abbreviations.
10372 */
10373 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10374 && makemap(fd, wp->w_buffer) == FAIL)
10375 return FAIL;
10376
10377 /*
10378 * Local options. Need to go to the window temporarily.
10379 * Store only local values when using ":mkview" and when ":mksession" is
10380 * used and 'sessionoptions' doesn't include "options".
10381 * Some folding options are always stored when "folds" is included,
10382 * otherwise the folds would not be restored correctly.
10383 */
10384 save_curwin = curwin;
10385 curwin = wp;
10386 curbuf = curwin->w_buffer;
10387 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10388 f = makeset(fd, OPT_LOCAL,
10389 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10390#ifdef FEAT_FOLDING
10391 else if (*flagp & SSOP_FOLDS)
10392 f = makefoldset(fd);
10393#endif
10394 else
10395 f = OK;
10396 curwin = save_curwin;
10397 curbuf = curwin->w_buffer;
10398 if (f == FAIL)
10399 return FAIL;
10400
10401#ifdef FEAT_FOLDING
10402 /*
10403 * Save Folds when 'buftype' is empty and for help files.
10404 */
10405 if ((*flagp & SSOP_FOLDS)
10406 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010407 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 {
10409 if (put_folds(fd, wp) == FAIL)
10410 return FAIL;
10411 }
10412#endif
10413
10414 /*
10415 * Set the cursor after creating folds, since that moves the cursor.
10416 */
10417 if (do_cursor)
10418 {
10419
10420 /* Restore the cursor line in the file and relatively in the
10421 * window. Don't use "G", it changes the jumplist. */
10422 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10423 (long)wp->w_cursor.lnum,
10424 (long)(wp->w_cursor.lnum - wp->w_topline),
10425 (long)wp->w_height / 2, (long)wp->w_height) < 0
10426 || put_eol(fd) == FAIL
10427 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10428 || put_line(fd, "exe s:l") == FAIL
10429 || put_line(fd, "normal! zt") == FAIL
10430 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10431 || put_eol(fd) == FAIL)
10432 return FAIL;
10433 /* Restore the cursor column and left offset when not wrapping. */
10434 if (wp->w_cursor.col == 0)
10435 {
10436 if (put_line(fd, "normal! 0") == FAIL)
10437 return FAIL;
10438 }
10439 else
10440 {
10441 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10442 {
10443 if (fprintf(fd,
10444 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010445 (long)wp->w_virtcol + 1,
10446 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 (long)wp->w_width / 2, (long)wp->w_width) < 0
10448 || put_eol(fd) == FAIL
10449 || put_line(fd, "if s:c > 0") == FAIL
10450 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010451 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10452 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 || put_eol(fd) == FAIL
10454 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010455 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 || put_line(fd, "endif") == FAIL)
10457 return FAIL;
10458 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010459 else if (put_view_curpos(fd, wp, "") == FAIL)
10460 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462 }
10463
10464 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010465 * Local directory, if the current flag is not view options or the "curdir"
10466 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010468 if (wp->w_localdir != NULL
10469 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 {
10471 if (fputs("lcd ", fd) < 0
10472 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10473 || put_eol(fd) == FAIL)
10474 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010475 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 }
10477
10478 return OK;
10479}
10480
10481/*
10482 * Write an argument list to the session file.
10483 * Returns FAIL if writing fails.
10484 */
10485 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010486ses_arglist(
10487 FILE *fd,
10488 char *cmd,
10489 garray_T *gap,
10490 int fullname, /* TRUE: use full path name */
10491 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010494 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 char_u *s;
10496
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010497 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10498 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010499 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 return FAIL;
10501 for (i = 0; i < gap->ga_len; ++i)
10502 {
10503 /* NULL file names are skipped (only happens when out of memory). */
10504 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10505 if (s != NULL)
10506 {
10507 if (fullname)
10508 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010509 buf = alloc(MAXPATHL);
10510 if (buf != NULL)
10511 {
10512 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10513 s = buf;
10514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010516 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010517 || ses_put_fname(fd, s, flagp) == FAIL
10518 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010519 {
10520 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010522 }
10523 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524 }
10525 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010526 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527}
10528
10529/*
10530 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010531 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 * Returns FAIL if writing fails.
10533 */
10534 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010535ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536{
10537 char_u *name;
10538
10539 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010540 * the session file will be sourced.
10541 * Don't do this for ":mkview", we don't know the current directory.
10542 * Don't do this after ":lcd", we don't keep track of what the current
10543 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 if (buf->b_sfname != NULL
10545 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010546 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010547#ifdef FEAT_AUTOCHDIR
10548 && !p_acd
10549#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010550 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551 name = buf->b_sfname;
10552 else
10553 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010554 if (ses_put_fname(fd, name, flagp) == FAIL
10555 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 return FAIL;
10557 return OK;
10558}
10559
10560/*
10561 * Write a file name to the session file.
10562 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10563 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010564 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 */
10566 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010567ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568{
10569 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010570 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572
10573 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010574 if (sname == NULL)
10575 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010577 if (*flagp & SSOP_SLASH)
10578 {
10579 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010580 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010581 if (*p == '\\')
10582 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010584
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010585 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010586 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010588 if (p == NULL)
10589 return FAIL;
10590
10591 /* write the result */
10592 if (fputs((char *)p, fd) < 0)
10593 retval = FAIL;
10594
10595 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 return retval;
10597}
10598
10599/*
10600 * ":loadview [nr]"
10601 */
10602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010603ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604{
10605 char_u *fname;
10606
10607 fname = get_view_file(*eap->arg);
10608 if (fname != NULL)
10609 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010610 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611 vim_free(fname);
10612 }
10613}
10614
10615/*
10616 * Get the name of the view file for the current buffer.
10617 */
10618 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010619get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620{
10621 int len = 0;
10622 char_u *p, *s;
10623 char_u *retval;
10624 char_u *sname;
10625
10626 if (curbuf->b_ffname == NULL)
10627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010628 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 return NULL;
10630 }
10631 sname = home_replace_save(NULL, curbuf->b_ffname);
10632 if (sname == NULL)
10633 return NULL;
10634
10635 /*
10636 * We want a file name without separators, because we're not going to make
10637 * a directory.
10638 * "normal" path separator -> "=+"
10639 * "=" -> "=="
10640 * ":" path separator -> "=-"
10641 */
10642 for (p = sname; *p; ++p)
10643 if (*p == '=' || vim_ispathsep(*p))
10644 ++len;
Bram Moolenaar964b3742019-05-24 18:54:09 +020010645 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646 if (retval != NULL)
10647 {
10648 STRCPY(retval, p_vdir);
10649 add_pathsep(retval);
10650 s = retval + STRLEN(retval);
10651 for (p = sname; *p; ++p)
10652 {
10653 if (*p == '=')
10654 {
10655 *s++ = '=';
10656 *s++ = '=';
10657 }
10658 else if (vim_ispathsep(*p))
10659 {
10660 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010661#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 if (*p == ':')
10663 *s++ = '-';
10664 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010666 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 }
10668 else
10669 *s++ = *p;
10670 }
10671 *s++ = '=';
10672 *s++ = c;
10673 STRCPY(s, ".vim");
10674 }
10675
10676 vim_free(sname);
10677 return retval;
10678}
10679
10680#endif /* FEAT_SESSION */
10681
10682/*
10683 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10684 * Return FAIL for a write error.
10685 */
10686 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010687put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688{
10689 if (
10690#ifdef USE_CRNL
10691 (
10692# ifdef MKSESSION_NL
10693 !mksession_nl &&
10694# endif
10695 (putc('\r', fd) < 0)) ||
10696#endif
10697 (putc('\n', fd) < 0))
10698 return FAIL;
10699 return OK;
10700}
10701
10702/*
10703 * Write a line to "fd".
10704 * Return FAIL for a write error.
10705 */
10706 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010707put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708{
10709 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10710 return FAIL;
10711 return OK;
10712}
10713
Bram Moolenaar071d4272004-06-13 20:20:40 +000010714#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010715/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010716 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010717 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010720dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 if (fname == NULL)
10723 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010724 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725}
10726#endif
10727
10728/*
10729 * ":behave {mswin,xterm}"
10730 */
10731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010732ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733{
10734 if (STRCMP(eap->arg, "mswin") == 0)
10735 {
10736 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10737 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10738 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10739 set_option_value((char_u *)"keymodel", 0L,
10740 (char_u *)"startsel,stopsel", 0);
10741 }
10742 else if (STRCMP(eap->arg, "xterm") == 0)
10743 {
10744 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10745 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10746 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10747 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10748 }
10749 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010750 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751}
10752
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010753#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10754/*
10755 * Function given to ExpandGeneric() to obtain the possible arguments of the
10756 * ":behave {mswin,xterm}" command.
10757 */
10758 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010759get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010760{
10761 if (idx == 0)
10762 return (char_u *)"mswin";
10763 if (idx == 1)
10764 return (char_u *)"xterm";
10765 return NULL;
10766}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010767
10768/*
10769 * Function given to ExpandGeneric() to obtain the possible arguments of the
10770 * ":messages {clear}" command.
10771 */
10772 char_u *
10773get_messages_arg(expand_T *xp UNUSED, int idx)
10774{
10775 if (idx == 0)
10776 return (char_u *)"clear";
10777 return NULL;
10778}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010779#endif
10780
Bram Moolenaar113e1072019-01-20 15:30:40 +010010781#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010782 char_u *
10783get_mapclear_arg(expand_T *xp UNUSED, int idx)
10784{
10785 if (idx == 0)
10786 return (char_u *)"<buffer>";
10787 return NULL;
10788}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010789#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010790
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791static int filetype_detect = FALSE;
10792static int filetype_plugin = FALSE;
10793static int filetype_indent = FALSE;
10794
10795/*
10796 * ":filetype [plugin] [indent] {on,off,detect}"
10797 * on: Load the filetype.vim file to install autocommands for file types.
10798 * off: Load the ftoff.vim file to remove all autocommands for file types.
10799 * plugin on: load filetype.vim and ftplugin.vim
10800 * plugin off: load ftplugof.vim
10801 * indent on: load filetype.vim and indent.vim
10802 * indent off: load indoff.vim
10803 */
10804 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010805ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806{
10807 char_u *arg = eap->arg;
10808 int plugin = FALSE;
10809 int indent = FALSE;
10810
10811 if (*eap->arg == NUL)
10812 {
10813 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010814 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 filetype_detect ? "ON" : "OFF",
10816 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10817 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10818 return;
10819 }
10820
10821 /* Accept "plugin" and "indent" in any order. */
10822 for (;;)
10823 {
10824 if (STRNCMP(arg, "plugin", 6) == 0)
10825 {
10826 plugin = TRUE;
10827 arg = skipwhite(arg + 6);
10828 continue;
10829 }
10830 if (STRNCMP(arg, "indent", 6) == 0)
10831 {
10832 indent = TRUE;
10833 arg = skipwhite(arg + 6);
10834 continue;
10835 }
10836 break;
10837 }
10838 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10839 {
10840 if (*arg == 'o' || !filetype_detect)
10841 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010842 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 filetype_detect = TRUE;
10844 if (plugin)
10845 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010846 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 filetype_plugin = TRUE;
10848 }
10849 if (indent)
10850 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010851 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 filetype_indent = TRUE;
10853 }
10854 }
10855 if (*arg == 'd')
10856 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010857 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010858 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859 }
10860 }
10861 else if (STRCMP(arg, "off") == 0)
10862 {
10863 if (plugin || indent)
10864 {
10865 if (plugin)
10866 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010867 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 filetype_plugin = FALSE;
10869 }
10870 if (indent)
10871 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010872 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 filetype_indent = FALSE;
10874 }
10875 }
10876 else
10877 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010878 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 filetype_detect = FALSE;
10880 }
10881 }
10882 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010883 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884}
10885
10886/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010887 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 */
10889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010890ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891{
10892 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010893 {
10894 char_u *arg = eap->arg;
10895
10896 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10897 arg += 9;
10898
10899 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10900 if (arg != eap->arg)
10901 did_filetype = FALSE;
10902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010906ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907{
10908#ifdef FEAT_DIGRAPHS
10909 if (*eap->arg != NUL)
10910 putdigraph(eap->arg);
10911 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010912 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010914 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915#endif
10916}
10917
10918 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010919ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920{
10921 int flags = 0;
10922
10923 if (eap->cmdidx == CMD_setlocal)
10924 flags = OPT_LOCAL;
10925 else if (eap->cmdidx == CMD_setglobal)
10926 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010927#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 if (cmdmod.browse && flags == 0)
10929 ex_options(eap);
10930 else
10931#endif
10932 (void)do_set(eap->arg, flags);
10933}
10934
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010935#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10936 void
10937set_no_hlsearch(int flag)
10938{
10939 no_hlsearch = flag;
10940# ifdef FEAT_EVAL
10941 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10942# endif
10943}
10944
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945/*
10946 * ":nohlsearch"
10947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010949ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010951 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010952 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953}
10954
10955/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010956 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 * Sets nextcmd to the start of the next command, if any. Also called when
10958 * skipping commands to find the next command.
10959 */
10960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010961ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962{
10963 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010964 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 char_u *end;
10966 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010967 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010968
10969 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010970 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010971 else
10972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010973 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010974 return;
10975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976
10977 /* First clear any old pattern. */
10978 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010979 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980
10981 if (ends_excmd(*eap->arg))
10982 end = eap->arg;
10983 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010010984 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 end = eap->arg + 4;
10986 else
10987 {
10988 p = skiptowhite(eap->arg);
10989 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010990 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 p = skipwhite(p);
10992 if (*p == NUL)
10993 {
10994 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010995 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010996 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 return;
10998 }
10999 end = skip_regexp(p + 1, *p, TRUE, NULL);
11000 if (!eap->skip)
11001 {
11002 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11003 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011004 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 eap->errmsg = e_trailing;
11006 return;
11007 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011008 if (*end != *p)
11009 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011010 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011011 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011012 return;
11013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014
11015 c = *end;
11016 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011017 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011018 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011019 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 }
11021 }
11022 eap->nextcmd = find_nextcmd(end);
11023}
11024#endif
11025
11026#ifdef FEAT_CRYPT
11027/*
11028 * ":X": Get crypt key
11029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011031ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011033 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011034 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035}
11036#endif
11037
11038#ifdef FEAT_FOLDING
11039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011040ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041{
11042 if (foldManualAllowed(TRUE))
11043 foldCreate(eap->line1, eap->line2);
11044}
11045
11046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011047ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048{
11049 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11050 eap->forceit, FALSE);
11051}
11052
11053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011054ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055{
11056 linenr_T lnum;
11057
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011058#ifdef FEAT_CLIPBOARD
11059 start_global_changes();
11060#endif
11061
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 /* First set the marks for all lines closed/open. */
11063 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11064 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11065 ml_setmarked(lnum);
11066
11067 /* Execute the command on the marked lines. */
11068 global_exe(eap->arg);
11069 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011070#ifdef FEAT_CLIPBOARD
11071 end_global_changes();
11072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073}
11074#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011075
Bram Moolenaar39665952018-08-15 20:59:48 +020011076#ifdef FEAT_QUICKFIX
11077/*
11078 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11079 * instead of a quickfix command.
11080 */
11081 int
11082is_loclist_cmd(int cmdidx)
11083{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011084 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011085 return FALSE;
11086 return cmdnames[cmdidx].cmd_name[0] == 'l';
11087}
11088#endif
11089
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011090# if defined(FEAT_TIMERS) || defined(PROTO)
11091 int
11092get_pressedreturn(void)
11093{
11094 return ex_pressedreturn;
11095}
11096
11097 void
11098set_pressedreturn(int val)
11099{
11100 ex_pressedreturn = val;
11101}
11102#endif