blob: f3a4eb7c1b66afa5ebd266c11e00ceadedb07259 [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);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#else
343# define ex_nohlsearch ex_ni
344# define ex_match ex_ni
345#endif
346#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100347static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#else
349# define ex_X ex_ni
350#endif
351#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100352static void ex_fold(exarg_T *eap);
353static void ex_foldopen(exarg_T *eap);
354static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#else
356# define ex_fold ex_ni
357# define ex_foldopen ex_ni
358# define ex_folddo ex_ni
359#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100360#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_language ex_ni
362#endif
363#ifndef FEAT_SIGNS
364# define ex_sign ex_ni
365#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000366#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200367# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000368# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200369# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371
372#ifndef FEAT_EVAL
373# define ex_debug ex_ni
374# define ex_breakadd ex_ni
375# define ex_debuggreedy ex_ni
376# define ex_breakdel ex_ni
377# define ex_breaklist ex_ni
378#endif
379
380#ifndef FEAT_CMDHIST
381# define ex_history ex_ni
382#endif
383#ifndef FEAT_JUMPLIST
384# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200385# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386# define ex_changes ex_ni
387#endif
388
Bram Moolenaar05159a02005-02-26 23:04:13 +0000389#ifndef FEAT_PROFILE
390# define ex_profile ex_ni
391#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200392#ifndef FEAT_TERMINAL
393# define ex_terminal ex_ni
394#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200395#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
396# define ex_xrestore ex_ni
397#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200398#if !defined(FEAT_TEXT_PROP)
399# define ex_popupclear ex_ni
400#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000401
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402/*
403 * Declare cmdnames[].
404 */
405#define DO_DECLARE_EXCMD
406#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200407#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100408
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409static char_u dollar_command[2] = {'$', 0};
410
411
412#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000413/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414typedef struct
415{
416 char_u *line; /* command line */
417 linenr_T lnum; /* sourcing_lnum of the line */
418} wcmd_T;
419
420/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000421 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000423 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000425struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426{
427 garray_T *lines_gap; /* growarray with line info */
428 int current_line; /* last read line from growarray */
429 int repeating; /* TRUE when looping a second time */
430 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200431 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 void *cookie;
433};
434
Bram Moolenaare96a2492019-06-25 04:12:16 +0200435static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100436static int store_loop_line(garray_T *gap, char_u *line);
437static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000438
439/* Struct to save a few things while debugging. Used in do_cmdline() only. */
440struct dbg_stuff
441{
442 int trylevel;
443 int force_abort;
444 except_T *caught_stack;
445 char_u *vv_exception;
446 char_u *vv_throwpoint;
447 int did_emsg;
448 int got_int;
449 int did_throw;
450 int need_rethrow;
451 int check_cstack;
452 except_T *current_exception;
453};
454
Bram Moolenaared203462004-06-16 11:19:22 +0000455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100456save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000457{
458 dsp->trylevel = trylevel; trylevel = 0;
459 dsp->force_abort = force_abort; force_abort = FALSE;
460 dsp->caught_stack = caught_stack; caught_stack = NULL;
461 dsp->vv_exception = v_exception(NULL);
462 dsp->vv_throwpoint = v_throwpoint(NULL);
463
464 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
465 dsp->did_emsg = did_emsg; did_emsg = FALSE;
466 dsp->got_int = got_int; got_int = FALSE;
467 dsp->did_throw = did_throw; did_throw = FALSE;
468 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
469 dsp->check_cstack = check_cstack; check_cstack = FALSE;
470 dsp->current_exception = current_exception; current_exception = NULL;
471}
472
473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100474restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000475{
476 suppress_errthrow = FALSE;
477 trylevel = dsp->trylevel;
478 force_abort = dsp->force_abort;
479 caught_stack = dsp->caught_stack;
480 (void)v_exception(dsp->vv_exception);
481 (void)v_throwpoint(dsp->vv_throwpoint);
482 did_emsg = dsp->did_emsg;
483 got_int = dsp->got_int;
484 did_throw = dsp->did_throw;
485 need_rethrow = dsp->need_rethrow;
486 check_cstack = dsp->check_cstack;
487 current_exception = dsp->current_exception;
488}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489#endif
490
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491/*
492 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
493 * command is given.
494 */
495 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100496do_exmode(
497 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498{
499 int save_msg_scroll;
500 int prev_msg_row;
501 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100502 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000503
504 if (improved)
505 exmode_active = EXMODE_VIM;
506 else
507 exmode_active = EXMODE_NORMAL;
508 State = NORMAL;
509
510 /* When using ":global /pat/ visual" and then "Q" we return to continue
511 * the :global command. */
512 if (global_busy)
513 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514
515 save_msg_scroll = msg_scroll;
516 ++RedrawingDisabled; /* don't redisplay the window */
517 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#ifdef FEAT_GUI
519 /* Ignore scrollbar and mouse events in Ex mode */
520 ++hold_gui_events;
521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522
Bram Moolenaar32526b32019-01-19 17:43:09 +0100523 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 while (exmode_active)
525 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000526 /* Check for a ":normal" command and no more characters left. */
527 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
528 {
529 exmode_active = FALSE;
530 break;
531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 msg_scroll = TRUE;
533 need_wait_return = FALSE;
534 ex_pressedreturn = FALSE;
535 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100536 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 prev_msg_row = msg_row;
538 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 if (improved)
540 {
541 cmdline_row = msg_row;
542 do_cmdline(NULL, getexline, NULL, 0);
543 }
544 else
545 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
546 lines_left = Rows - 1;
547
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100549 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100552 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000555 if (ex_pressedreturn)
556 {
557 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000558 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000559 msg_row = prev_msg_row;
560 if (prev_msg_row == Rows - 1)
561 msg_row--;
562 }
563 msg_col = 0;
564 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
565 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000568 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
569 {
570 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100571 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000572 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100573 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 }
576
577#ifdef FEAT_GUI
578 --hold_gui_events;
579#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 --RedrawingDisabled;
581 --no_wait_return;
582 update_screen(CLEAR);
583 need_wait_return = FALSE;
584 msg_scroll = save_msg_scroll;
585}
586
587/*
588 * Execute a simple command line. Used for translated commands like "*".
589 */
590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100591do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 return do_cmdline(cmd, NULL, NULL,
594 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
595}
596
597/*
598 * do_cmdline(): execute one Ex command line
599 *
600 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100601 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 * 2. Split up in parts separated with '|'.
603 *
604 * This function can be called recursively!
605 *
606 * flags:
607 * DOCMD_VERBOSE - The command will be included in the error message.
608 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100609 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 * DOCMD_KEYTYPED - Don't reset KeyTyped.
611 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
612 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
613 *
614 * return FAIL if cmdline could not be executed, OK otherwise
615 */
616 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617do_cmdline(
618 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200619 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100620 void *cookie, /* argument for fgetline() */
621 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622{
623 char_u *next_cmdline; /* next cmd to execute */
624 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100625 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 static int recursive = 0; /* recursive depth */
627 int msg_didout_before_start = 0;
628 int count = 0; /* line number count */
629 int did_inc = FALSE; /* incremented RedrawingDisabled */
630 int retval = OK;
631#ifdef FEAT_EVAL
632 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000633 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 int current_line = 0; /* active line in lines_ga */
635 char_u *fname = NULL; /* function or script name */
636 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
637 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000638 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 int initial_trylevel;
640 struct msglist **saved_msg_list = NULL;
641 struct msglist *private_msg_list;
642
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100643 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200644 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000646 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000648 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100650# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651# define cmd_cookie cookie
652#endif
653 static int call_depth = 0; /* recursiveness */
654
655#ifdef FEAT_EVAL
656 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
657 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000658 * This ensures that the do_errthrow() call in do_one_cmd() does not
659 * combine the messages stored by an earlier invocation of do_one_cmd()
660 * with the command name of the later one. This would happen when
661 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 saved_msg_list = msg_list;
663 msg_list = &private_msg_list;
664 private_msg_list = NULL;
665#endif
666
667 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100668 * here. The value of 200 allows nested function calls, ":source", etc.
669 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100670 if (call_depth >= 200
671#ifdef FEAT_EVAL
672 && call_depth >= p_mfd
673#endif
674 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100676 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#ifdef FEAT_EVAL
678 /* When converting to an exception, we do not include the command name
679 * since this is not an error of the specific command. */
680 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
681 msg_list = saved_msg_list;
682#endif
683 return FAIL;
684 }
685 ++call_depth;
686
687#ifdef FEAT_EVAL
688 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000689 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 cstack.cs_trylevel = 0;
691 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000692 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
694
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
697 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++ex_nesting_level;
701
702 /* Get the function or script name and the address where the next breakpoint
703 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = func_name(real_cookie);
707 breakpoint = func_breakpoint(real_cookie);
708 dbg_tick = func_dbg_tick(real_cookie);
709 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100710 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
712 fname = sourcing_name;
713 breakpoint = source_breakpoint(real_cookie);
714 dbg_tick = source_dbg_tick(real_cookie);
715 }
716
717 /*
718 * Initialize "force_abort" and "suppress_errthrow" at the top level.
719 */
720 if (!recursive)
721 {
722 force_abort = FALSE;
723 suppress_errthrow = FALSE;
724 }
725
726 /*
727 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 * exception handling (used when debugging). Otherwise clear it to avoid
729 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000731 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000732 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000733 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200734 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 initial_trylevel = trylevel;
737
738 /*
739 * "did_throw" will be set to TRUE when an exception is being thrown.
740 */
741 did_throw = FALSE;
742#endif
743 /*
744 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000745 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * If force_abort is set, we cancel everything.
747 */
748 did_emsg = FALSE;
749
750 /*
751 * KeyTyped is only set when calling vgetc(). Reset it here when not
752 * calling vgetc() (sourced command lines).
753 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 if (!(flags & DOCMD_KEYTYPED)
755 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 KeyTyped = FALSE;
757
758 /*
759 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * - for multiple commands on one line, separated with '|'
762 * - when repeating until there are no more lines (for ":source")
763 */
764 next_cmdline = cmdline;
765 do
766 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100768 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769#endif
770
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000771 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (next_cmdline == NULL
773#ifdef FEAT_EVAL
774 && !force_abort
775 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000776 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778 )
779 did_emsg = FALSE;
780
781 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100783 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * 3. If a line is given: Make a copy, so we can mess with it.
785 */
786
787#ifdef FEAT_EVAL
788 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
791 /* Each '|' separated command is stored separately in lines_ga, to
792 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100793 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
795 /* Check if a function has returned or, unless it has an unclosed
796 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000800 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801 func_line_end(real_cookie);
802# endif
803 if (func_has_ended(real_cookie))
804 {
805 retval = FAIL;
806 break;
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000810 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 script_line_end();
813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
815 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100816 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
818 retval = FAIL;
819 break;
820 }
821
822 /* If breakpoints have been added/deleted need to check for it. */
823 if (breakpoint != NULL && dbg_tick != NULL
824 && *dbg_tick != debug_tick)
825 {
826 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 fname, sourcing_lnum);
829 *dbg_tick = debug_tick;
830 }
831
832 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
833 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
834
835 /* Did we encounter a breakpoint? */
836 if (breakpoint != NULL && *breakpoint != 0
837 && *breakpoint <= sourcing_lnum)
838 {
839 dbg_breakpoint(fname, sourcing_lnum);
840 /* Find next breakpoint. */
841 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100842 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 fname, sourcing_lnum);
844 *dbg_tick = debug_tick;
845 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000847 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848 {
849 if (getline_is_func)
850 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100851 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000852 script_line_start();
853 }
854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000859 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 * below, so that it stores lines in or reads them from
862 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000863 * while/for loop. */
864 cmd_getline = get_loop_line;
865 cmd_cookie = (void *)&cmd_loop_cookie;
866 cmd_loop_cookie.lines_gap = &lines_ga;
867 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000869 cmd_loop_cookie.cookie = cookie;
870 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872 else
873 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100874 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 cmd_cookie = cookie;
876 }
877#endif
878
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100879 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 if (next_cmdline == NULL)
881 {
882 /*
883 * Need to set msg_didout for the first line after an ":if",
884 * otherwise the ":if" will be overwritten.
885 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100888 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889#ifdef FEAT_EVAL
890 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
891#else
892 0
893#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200894 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 {
896 /* Don't call wait_return for aborted command line. The NULL
897 * returned for the end of a sourced file or executed function
898 * doesn't do this. */
899 if (KeyTyped && !(flags & DOCMD_REPEAT))
900 need_wait_return = FALSE;
901 retval = FAIL;
902 break;
903 }
904 used_getline = TRUE;
905
906 /*
907 * Keep the first typed line. Clear it when more lines are typed.
908 */
909 if (flags & DOCMD_KEEPLINE)
910 {
911 vim_free(repeat_cmdline);
912 if (count == 0)
913 repeat_cmdline = vim_strsave(next_cmdline);
914 else
915 repeat_cmdline = NULL;
916 }
917 }
918
919 /* 3. Make a copy of the command so we can mess with it. */
920 else if (cmdline_copy == NULL)
921 {
922 next_cmdline = vim_strsave(next_cmdline);
923 if (next_cmdline == NULL)
924 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100925 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 retval = FAIL;
927 break;
928 }
929 }
930 cmdline_copy = next_cmdline;
931
932#ifdef FEAT_EVAL
933 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000934 * Save the current line when inside a ":while" or ":for", and when
935 * the command looks like a ":while" or ":for", because we may need it
936 * later. When there is a '|' and another command, it is stored
937 * separately, because we need to be able to jump back to it from an
938 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000940 if (current_line == lines_ga.ga_len
941 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000943 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
945 retval = FAIL;
946 break;
947 }
948 }
949 did_endif = FALSE;
950#endif
951
952 if (count++ == 0)
953 {
954 /*
955 * All output from the commands is put below each other, without
956 * waiting for a return. Don't do this when executing commands
957 * from a script or when being called recursive (e.g. for ":e
958 * +command file").
959 */
960 if (!(flags & DOCMD_NOWAIT) && !recursive)
961 {
962 msg_didout_before_start = msg_didout;
963 msg_didany = FALSE; /* no output yet */
964 msg_start();
965 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200966 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 ++RedrawingDisabled;
968 did_inc = TRUE;
969 }
970 }
971
972 if (p_verbose >= 15 && sourcing_name != NULL)
973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000975 verbose_enter_scroll();
976
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100977 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000979 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100980 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000981
982 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 --no_wait_return;
984 }
985
986 /*
987 * 2. Execute one '|' separated command.
988 * do_one_cmd() will return NULL if there is no trailing '|'.
989 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
990 */
991 ++recursive;
992 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
993#ifdef FEAT_EVAL
994 &cstack,
995#endif
996 cmd_getline, cmd_cookie);
997 --recursive;
998
999#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 if (cmd_cookie == (void *)&cmd_loop_cookie)
1001 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#endif
1005
1006 if (next_cmdline == NULL)
1007 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001008 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009#ifdef FEAT_CMDHIST
1010 /*
1011 * If the command was typed, remember it for the ':' register.
1012 * Do this AFTER executing the command to make :@: work.
1013 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 && new_last_cmdline != NULL)
1016 {
1017 vim_free(last_cmdline);
1018 last_cmdline = new_last_cmdline;
1019 new_last_cmdline = NULL;
1020 }
1021#endif
1022 }
1023 else
1024 {
1025 /* need to copy the command after the '|' to cmdline_copy, for the
1026 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001027 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 next_cmdline = cmdline_copy;
1029 }
1030
1031
1032#ifdef FEAT_EVAL
1033 /* reset did_emsg for a function that is not aborted by an error */
1034 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001035 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 && !func_has_abort(real_cookie))
1037 did_emsg = FALSE;
1038
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 ++current_line;
1042
1043 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 * An ":endwhile", ":endfor" and ":continue" is handled here.
1045 * If we were executing commands, jump back to the ":while" or
1046 * ":for".
1047 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 /* Jump back to the matching ":while" or ":for". Be careful
1054 * not to use a cs_line[] from an entry that isn't a ":while"
1055 * or ":for": It would make "current_line" invalid and can
1056 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (!did_emsg && !got_int && !did_throw
1058 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 && (cstack.cs_flags[cstack.cs_idx]
1060 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 && cstack.cs_line[cstack.cs_idx] >= 0
1062 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1063 {
1064 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 /* remember we jumped there */
1066 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 line_breakcheck(); /* check if CTRL-C typed */
1068
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 /* Check for the next breakpoint at or after the ":while"
1070 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 if (breakpoint != NULL)
1072 {
1073 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001074 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 fname,
1076 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1077 *dbg_tick = debug_tick;
1078 }
1079 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001084 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1085 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088
1089 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001090 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1096 }
1097 }
1098
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001099 /* Check for the next breakpoint after a watchexpression */
1100 if (breakpoint != NULL && has_watchexpr())
1101 {
1102 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1103 *dbg_tick = debug_tick;
1104 }
1105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 /*
1107 * When not inside any ":while" loop, clear remembered lines.
1108 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 if (lines_ga.ga_len > 0)
1112 {
1113 sourcing_lnum =
1114 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1115 free_cmdlines(&lines_ga);
1116 }
1117 current_line = 0;
1118 }
1119
1120 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1122 * being restored at the ":endtry". Reset them here and set the
1123 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1124 * This includes the case where a missing ":endif", ":endwhile" or
1125 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001127 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 cstack.cs_lflags &= ~CSL_HAD_FINA;
1130 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1131 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 did_throw ? (void *)current_exception : NULL);
1133 did_emsg = got_int = did_throw = FALSE;
1134 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1135 }
1136
1137 /* Update global "trylevel" for recursive calls to do_cmdline() from
1138 * within this loop. */
1139 trylevel = initial_trylevel + cstack.cs_trylevel;
1140
1141 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001142 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 * files) is aborted because of an error, an interrupt, or an uncaught
1144 * exception, cancel everything. If it is left normally, reset
1145 * force_abort to get the non-EH compatible abortion behavior for
1146 * the rest of the script.
1147 */
1148 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1149 force_abort = FALSE;
1150
1151 /* Convert an interrupt to an exception if appropriate. */
1152 (void)do_intthrow(&cstack);
1153#endif /* FEAT_EVAL */
1154
1155 }
1156 /*
1157 * Continue executing command lines when:
1158 * - no CTRL-C typed, no aborting error, no exception thrown or try
1159 * conditionals need to be checked for executing finally clauses or
1160 * catching an interrupt exception
1161 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001162 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 * looping for ":source" command or function call.
1164 */
1165 while (!((got_int
1166#ifdef FEAT_EVAL
1167 || (did_emsg && force_abort) || did_throw
1168#endif
1169 )
1170#ifdef FEAT_EVAL
1171 && cstack.cs_trylevel == 0
1172#endif
1173 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001174 && !(did_emsg
1175#ifdef FEAT_EVAL
1176 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001177 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001178 * the :endtry to be missed. */
1179 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1180#endif
1181 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001182 && (getline_equal(fgetline, cookie, getexmodeline)
1183 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 && (next_cmdline != NULL
1185#ifdef FEAT_EVAL
1186 || cstack.cs_idx >= 0
1187#endif
1188 || (flags & DOCMD_REPEAT)));
1189
1190 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001191 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192#ifdef FEAT_EVAL
1193 free_cmdlines(&lines_ga);
1194 ga_clear(&lines_ga);
1195
1196 if (cstack.cs_idx >= 0)
1197 {
1198 /*
1199 * If a sourced file or executed function ran to its end, report the
1200 * unclosed conditional.
1201 */
1202 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001203 && ((getline_equal(fgetline, cookie, getsourceline)
1204 && !source_finished(fgetline, cookie))
1205 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 && !func_has_ended(real_cookie))))
1207 {
1208 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001209 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001215 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 }
1217
1218 /*
1219 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1220 * ":endtry" in a sourced file or executed function. If the try
1221 * conditional is in its finally clause, ignore anything pending.
1222 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001223 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 */
1225 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001226 {
1227 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1228
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001229 if (idx >= 0)
1230 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001231 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1232 &cstack.cs_looplevel);
1233 }
1234 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 trylevel = initial_trylevel;
1236 }
1237
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001238 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1239 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001241 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 ? (char_u *)"endfunction" : (char_u *)NULL);
1243
1244 if (trylevel == 0)
1245 {
1246 /*
1247 * When an exception is being thrown out of the outermost try
1248 * conditional, discard the uncaught exception, disable the conversion
1249 * of interrupts or errors to exceptions, and ensure that no more
1250 * commands are executed.
1251 */
1252 if (did_throw)
1253 {
1254 void *p = NULL;
1255 char_u *saved_sourcing_name;
1256 int saved_sourcing_lnum;
1257 struct msglist *messages = NULL, *next;
1258
1259 /*
1260 * If the uncaught exception is a user exception, report it as an
1261 * error. If it is an error exception, display the saved error
1262 * message now. For an interrupt exception, do nothing; the
1263 * interrupt message is given elsewhere.
1264 */
1265 switch (current_exception->type)
1266 {
1267 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001268 vim_snprintf((char *)IObuff, IOSIZE,
1269 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 current_exception->value);
1271 p = vim_strsave(IObuff);
1272 break;
1273 case ET_ERROR:
1274 messages = current_exception->messages;
1275 current_exception->messages = NULL;
1276 break;
1277 case ET_INTERRUPT:
1278 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280
1281 saved_sourcing_name = sourcing_name;
1282 saved_sourcing_lnum = sourcing_lnum;
1283 sourcing_name = current_exception->throw_name;
1284 sourcing_lnum = current_exception->throw_lnum;
1285 current_exception->throw_name = NULL;
1286
1287 discard_current_exception(); /* uses IObuff if 'verbose' */
1288 suppress_errthrow = TRUE;
1289 force_abort = TRUE;
1290
1291 if (messages != NULL)
1292 {
1293 do
1294 {
1295 next = messages->next;
1296 emsg(messages->msg);
1297 vim_free(messages->msg);
1298 vim_free(messages);
1299 messages = next;
1300 }
1301 while (messages != NULL);
1302 }
1303 else if (p != NULL)
1304 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001305 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 vim_free(p);
1307 }
1308 vim_free(sourcing_name);
1309 sourcing_name = saved_sourcing_name;
1310 sourcing_lnum = saved_sourcing_lnum;
1311 }
1312
1313 /*
1314 * On an interrupt or an aborting error not converted to an exception,
1315 * disable the conversion of errors to exceptions. (Interrupts are not
1316 * converted any more, here.) This enables also the interrupt message
1317 * when force_abort is set and did_emsg unset in case of an interrupt
1318 * from a finally clause after an error.
1319 */
1320 else if (got_int || (did_emsg && force_abort))
1321 suppress_errthrow = TRUE;
1322 }
1323
1324 /*
1325 * The current cstack will be freed when do_cmdline() returns. An uncaught
1326 * exception will have to be rethrown in the previous cstack. If a function
1327 * has just returned or a script file was just finished and the previous
1328 * cstack belongs to the same function or, respectively, script file, it
1329 * will have to be checked for finally clauses to be executed due to the
1330 * ":return" or ":finish". This is done in do_one_cmd().
1331 */
1332 if (did_throw)
1333 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001334 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001336 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 && ex_nesting_level > func_level(real_cookie) + 1))
1338 {
1339 if (!did_throw)
1340 check_cstack = TRUE;
1341 }
1342 else
1343 {
1344 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001345 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 --ex_nesting_level;
1347 /*
1348 * Go to debug mode when returning from a function in which we are
1349 * single-stepping.
1350 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001351 if ((getline_equal(fgetline, cookie, getsourceline)
1352 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001354 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 ? (char_u *)_("End of sourced file")
1356 : (char_u *)_("End of function"));
1357 }
1358
1359 /*
1360 * Restore the exception environment (done after returning from the
1361 * debugger).
1362 */
1363 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001364 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
1366 msg_list = saved_msg_list;
1367#endif /* FEAT_EVAL */
1368
1369 /*
1370 * If there was too much output to fit on the command line, ask the user to
1371 * hit return before redrawing the screen. With the ":global" command we do
1372 * this only once after the command is finished.
1373 */
1374 if (did_inc)
1375 {
1376 --RedrawingDisabled;
1377 --no_wait_return;
1378 msg_scroll = FALSE;
1379
1380 /*
1381 * When just finished an ":if"-":else" which was typed, no need to
1382 * wait for hit-return. Also for an error situation.
1383 */
1384 if (retval == FAIL
1385#ifdef FEAT_EVAL
1386 || (did_endif && KeyTyped && !did_emsg)
1387#endif
1388 )
1389 {
1390 need_wait_return = FALSE;
1391 msg_didany = FALSE; /* don't wait when restarting edit */
1392 }
1393 else if (need_wait_return)
1394 {
1395 /*
1396 * The msg_start() above clears msg_didout. The wait_return we do
1397 * here should not overwrite the command that may be shown before
1398 * doing that.
1399 */
1400 msg_didout |= msg_didout_before_start;
1401 wait_return(FALSE);
1402 }
1403 }
1404
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#ifdef FEAT_EVAL
1406 did_endif = FALSE; /* in case do_cmdline used recursively */
1407#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /*
1409 * Reset if_level, in case a sourced script file contains more ":if" than
1410 * ":endif" (could be ":if x | foo | endif").
1411 */
1412 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001413#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 --call_depth;
1416 return retval;
1417}
1418
1419#ifdef FEAT_EVAL
1420/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001421 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 */
1423 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001424get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001426 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 wcmd_T *wp;
1428 char_u *line;
1429
1430 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1431 {
1432 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001433 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001435 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001437 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001439 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001440 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 ++cp->current_line;
1442
1443 return line;
1444 }
1445
1446 KeyTyped = FALSE;
1447 ++cp->current_line;
1448 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1449 sourcing_lnum = wp->lnum;
1450 return vim_strsave(wp->line);
1451}
1452
1453/*
1454 * Store a line in "gap" so that a ":while" loop can execute it again.
1455 */
1456 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001457store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 if (ga_grow(gap, 1) == FAIL)
1460 return FAIL;
1461 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1462 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1463 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return OK;
1465}
1466
1467/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001468 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 */
1470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001471free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473 while (gap->ga_len > 0)
1474 {
1475 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1476 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478}
1479#endif
1480
1481/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001482 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1483 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001486getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001487 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001488 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001489 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001492 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001493 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
Bram Moolenaar89d40322006-08-29 15:30:07 +00001495 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001496 * function that's originally used to obtain the lines. This may be
1497 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001498 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 cp = (struct loop_cookie *)cookie;
1500 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 gp = cp->getline;
1503 cp = cp->cookie;
1504 }
1505 return gp == func;
1506#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#endif
1509}
1510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001512 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 * getline function. Otherwise return "cookie".
1514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001516getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001517 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001518 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar13505972019-01-24 15:04:48 +01001520#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001521 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001522 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar89d40322006-08-29 15:30:07 +00001524 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001525 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001527 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001528 cp = (struct loop_cookie *)cookie;
1529 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 gp = cp->getline;
1532 cp = cp->cookie;
1533 }
1534 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001540
1541/*
1542 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1543 * ":bwipeout", etc.
1544 * Returns the buffer number.
1545 */
1546 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001547compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001548{
1549 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001550 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001551 int count = offset;
1552
1553 buf = firstbuf;
1554 while (buf->b_next != NULL && buf->b_fnum < lnum)
1555 buf = buf->b_next;
1556 while (count != 0)
1557 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001558 count += (offset < 0) ? 1 : -1;
1559 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1560 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001561 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001562 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001563 if (addr_type == ADDR_LOADED_BUFFERS)
1564 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001565 while (buf->b_ml.ml_mfp == NULL)
1566 {
1567 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1568 if (nextbuf == NULL)
1569 break;
1570 buf = nextbuf;
1571 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001572 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001573 /* we might have gone too far, last buffer is not loadedd */
1574 if (addr_type == ADDR_LOADED_BUFFERS)
1575 while (buf->b_ml.ml_mfp == NULL)
1576 {
1577 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1578 if (nextbuf == NULL)
1579 break;
1580 buf = nextbuf;
1581 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001582 return buf->b_fnum;
1583}
1584
Bram Moolenaarb7316892019-05-01 18:08:42 +02001585/*
1586 * Return the window number of "win".
1587 * When "win" is NULL return the number of windows.
1588 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001589 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001590current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001591{
1592 win_T *wp;
1593 int nr = 0;
1594
Bram Moolenaar29323592016-07-24 22:04:11 +02001595 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001596 {
1597 ++nr;
1598 if (wp == win)
1599 break;
1600 }
1601 return nr;
1602}
1603
1604 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001605current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001606{
1607 tabpage_T *tp;
1608 int nr = 0;
1609
Bram Moolenaar29323592016-07-24 22:04:11 +02001610 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001611 {
1612 ++nr;
1613 if (tp == tab)
1614 break;
1615 }
1616 return nr;
1617}
1618
1619# define CURRENT_WIN_NR current_win_nr(curwin)
1620# define LAST_WIN_NR current_win_nr(NULL)
1621# define CURRENT_TAB_NR current_tab_nr(curtab)
1622# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624/*
1625 * Execute one Ex command.
1626 *
1627 * If 'sourcing' is TRUE, the command will be included in the error message.
1628 *
1629 * 1. skip comment lines and leading space
1630 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001631 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001632 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001633 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 * 6. parse arguments
1635 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 *
1639 * This function may be called recursively!
1640 */
1641#if (_MSC_VER == 1200)
1642/*
Bram Moolenaared203462004-06-16 11:19:22 +00001643 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001645 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646#endif
1647 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001648do_one_cmd(
1649 char_u **cmdlinep,
1650 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001652 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001654 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001655 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
1657 char_u *p;
1658 linenr_T lnum;
1659 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001660 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001661 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001663 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001665 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001667 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 vim_memset(&ea, 0, sizeof(ea));
1670 ea.line1 = 1;
1671 ea.line2 = 1;
1672#ifdef FEAT_EVAL
1673 ++ex_nesting_level;
1674#endif
1675
Bram Moolenaar21691f82012-12-05 19:13:18 +01001676 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 if (quitmore
1678#ifdef FEAT_EVAL
1679 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001680 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001681#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001682 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001683 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 --quitmore;
1685
1686 /*
1687 * Reset browse, confirm, etc.. They are restored when returning, for
1688 * recursive calls.
1689 */
1690 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001692 /* "#!anything" is handled like a comment. */
1693 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1694 goto doend;
1695
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001696/*
1697 * 1. Skip comment lines and leading white space and colons.
1698 * 2. Handle command modifiers.
1699 */
1700 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001702 ea.cmdlinep = cmdlinep;
1703 ea.getline = fgetline;
1704 ea.cookie = cookie;
1705#ifdef FEAT_EVAL
1706 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001708 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001709 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001711 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
1713#ifdef FEAT_EVAL
1714 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1715 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1716#else
1717 ea.skip = (if_level > 0);
1718#endif
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001721 * 3. Skip over the range to find the command. Let "p" point to after it.
1722 *
1723 * We need the command to know what kind of range it uses.
1724 */
1725 cmd = ea.cmd;
1726 ea.cmd = skip_range(ea.cmd, NULL);
1727 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1728 ea.cmd = skipwhite(ea.cmd + 1);
1729 p = find_command(&ea, NULL);
1730
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001731#ifdef FEAT_EVAL
1732# ifdef FEAT_PROFILE
1733 // Count this line for profiling if skip is TRUE.
1734 if (do_profiling == PROF_YES
1735 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1736 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1737 {
1738 int skip = did_emsg || got_int || did_throw;
1739
1740 if (ea.cmdidx == CMD_catch)
1741 skip = !skip && !(cstack->cs_idx >= 0
1742 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1743 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1744 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1745 skip = skip || !(cstack->cs_idx >= 0
1746 && !(cstack->cs_flags[cstack->cs_idx]
1747 & (CSF_ACTIVE | CSF_TRUE)));
1748 else if (ea.cmdidx == CMD_finally)
1749 skip = FALSE;
1750 else if (ea.cmdidx != CMD_endif
1751 && ea.cmdidx != CMD_endfor
1752 && ea.cmdidx != CMD_endtry
1753 && ea.cmdidx != CMD_endwhile)
1754 skip = ea.skip;
1755
1756 if (!skip)
1757 {
1758 if (getline_equal(fgetline, cookie, get_func_line))
1759 func_line_exec(getline_cookie(fgetline, cookie));
1760 else if (getline_equal(fgetline, cookie, getsourceline))
1761 script_line_exec();
1762 }
1763 }
1764# endif
1765
1766 /* May go to debug mode. If this happens and the ">quit" debug command is
1767 * used, throw an interrupt exception and skip the next command. */
1768 dbg_check_breakpoint(&ea);
1769 if (!ea.skip && got_int)
1770 {
1771 ea.skip = TRUE;
1772 (void)do_intthrow(cstack);
1773 }
1774#endif
1775
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001776/*
1777 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 *
1779 * where 'addr' is:
1780 *
1781 * % (entire file)
1782 * $ [+-NUM]
1783 * 'x [+-NUM] (where x denotes a currently defined mark)
1784 * . [+-NUM]
1785 * [+-NUM]..
1786 * NUM
1787 *
1788 * The ea.cmd pointer is updated to point to the first character following the
1789 * range spec. If an initial address is found, but no second, the upper bound
1790 * is equal to the lower.
1791 */
1792
Bram Moolenaar25190db2019-05-04 15:05:28 +02001793 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001794 if (!IS_USER_CMDIDX(ea.cmdidx))
1795 {
1796 if (ea.cmdidx != CMD_SIZE)
1797 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1798 else
1799 ea.addr_type = ADDR_LINES;
1800
Bram Moolenaar25190db2019-05-04 15:05:28 +02001801 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001802 if (ea.cmdidx == CMD_wincmd && p != NULL)
1803 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001804#ifdef FEAT_QUICKFIX
1805 // :.cc in quickfix window uses line number
1806 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1807 ea.addr_type = ADDR_OTHER;
1808#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001809 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001810
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001811 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001812 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001813 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001816 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 */
1818
1819 /*
1820 * Skip ':' and any white space
1821 */
1822 ea.cmd = skipwhite(ea.cmd);
1823 while (*ea.cmd == ':')
1824 ea.cmd = skipwhite(ea.cmd + 1);
1825
1826 /*
1827 * If we got a line, but no command, then go to the line.
1828 * If we find a '|' or '\n' we set ea.nextcmd.
1829 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001830 if (*ea.cmd == NUL || *ea.cmd == '"'
1831 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833 /*
1834 * strange vi behaviour:
1835 * ":3" jumps to line 3
1836 * ":3|..." prints line 3
1837 * ":|" prints current line
1838 */
1839 if (ea.skip) /* skip this if inside :if */
1840 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001841 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
1843 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001844 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 if ((errormsg = invalid_range(&ea)) == NULL)
1846 {
1847 correct_range(&ea);
1848 ex_print(&ea);
1849 }
1850 }
1851 else if (ea.addr_count != 0)
1852 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001853 if (ea.line2 > curbuf->b_ml.ml_line_count)
1854 {
1855 /* With '-' in 'cpoptions' a line number past the file is an
1856 * error, otherwise put it at the end of the file. */
1857 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1858 ea.line2 = -1;
1859 else
1860 ea.line2 = curbuf->b_ml.ml_line_count;
1861 }
1862
1863 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001864 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 else
1866 {
1867 if (ea.line2 == 0)
1868 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 else
1870 curwin->w_cursor.lnum = ea.line2;
1871 beginline(BL_SOL | BL_FIX);
1872 }
1873 }
1874 goto doend;
1875 }
1876
Bram Moolenaard5005162014-08-22 23:05:54 +02001877 /* If this looks like an undefined user command and there are CmdUndefined
1878 * autocommands defined, trigger the matching autocommands. */
1879 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1880 && ASCII_ISUPPER(*ea.cmd)
1881 && has_cmdundefined())
1882 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001883 int ret;
1884
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001885 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001886 while (ASCII_ISALNUM(*p))
1887 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001888 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001889 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1890 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001891 /* If the autocommands did something and didn't cause an error, try
1892 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001893 p = (ret
1894#ifdef FEAT_EVAL
1895 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001896#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001897 ) ? find_command(&ea, NULL) : ea.cmd;
1898 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (p == NULL)
1901 {
1902 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001903 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 goto doend;
1905 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001906 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001907 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1908 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 errormsg = uc_fun_cmd();
1911 goto doend;
1912 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (ea.cmdidx == CMD_SIZE)
1915 {
1916 if (!ea.skip)
1917 {
1918 STRCPY(IObuff, _("E492: Not an editor command"));
1919 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001920 {
1921 /* If the modifier was parsed OK the error must be in the
1922 * following command */
1923 if (after_modifier != NULL)
1924 append_command(after_modifier);
1925 else
1926 append_command(*cmdlinep);
1927 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001928 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001929 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 goto doend;
1932 }
1933
Bram Moolenaar958636c2014-10-21 20:01:58 +02001934 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1935 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001936#ifdef HAVE_EX_SCRIPT_NI
1937 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1938#endif
1939 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
1941#ifndef FEAT_EVAL
1942 /*
1943 * When the expression evaluation is disabled, recognize the ":if" and
1944 * ":endif" commands and ignore everything in between it.
1945 */
1946 if (ea.cmdidx == CMD_if)
1947 ++if_level;
1948 if (if_level)
1949 {
1950 if (ea.cmdidx == CMD_endif)
1951 --if_level;
1952 goto doend;
1953 }
1954
1955#endif
1956
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001957 /* forced commands */
1958 if (*p == '!' && ea.cmdidx != CMD_substitute
1959 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
1961 ++p;
1962 ea.forceit = TRUE;
1963 }
1964 else
1965 ea.forceit = FALSE;
1966
1967/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001968 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001970 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001971 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 if (!ea.skip)
1974 {
1975#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001976 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001978 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001979 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 goto doend;
1981 }
1982#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001983 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001984 {
1985 errormsg = _("E981: Command not allowed in rvim");
1986 goto doend;
1987 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001988 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
1990 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001991 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 goto doend;
1993 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001994
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001995 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001996 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001998 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001999 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 goto doend;
2001 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002002
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002003 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002004 * Do allow ":checktime" (it is postponed).
2005 * Do allow ":edit" (check for an argument later).
2006 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002007 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002008 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002009 && ea.cmdidx != CMD_edit
2010 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002011 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002012 && curbuf_locked())
2013 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002015 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
2017 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002018 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 goto doend;
2020 }
2021 }
2022
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002023 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002025 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 goto doend;
2027 }
2028
2029 /*
2030 * Don't complain about the range if it is not used
2031 * (could happen if line_count is accidentally set to 0).
2032 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002033 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
2035 /*
2036 * If the range is backwards, ask for confirmation and, if given, swap
2037 * ea.line1 & ea.line2 so it's forwards again.
2038 * When global command is busy, don't ask, will fail below.
2039 */
2040 if (!global_busy && ea.line1 > ea.line2)
2041 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002042 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002044 if (sourcing || exmode_active)
2045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002046 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002047 goto doend;
2048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 if (ask_yesno((char_u *)
2050 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002051 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
2053 lnum = ea.line1;
2054 ea.line1 = ea.line2;
2055 ea.line2 = lnum;
2056 }
2057 if ((errormsg = invalid_range(&ea)) != NULL)
2058 goto doend;
2059 }
2060
Bram Moolenaarb7316892019-05-01 18:08:42 +02002061 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2062 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 ea.line2 = 1;
2064
2065 correct_range(&ea);
2066
2067#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002068 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002069 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
2071 /* Put the first line at the start of a closed fold, put the last line
2072 * at the end of a closed fold. */
2073 (void)hasFolding(ea.line1, &ea.line1, NULL);
2074 (void)hasFolding(ea.line2, NULL, &ea.line2);
2075 }
2076#endif
2077
2078#ifdef FEAT_QUICKFIX
2079 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002080 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 * option here, so things like % get expanded.
2082 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002083 p = replace_makeprg(&ea, p, cmdlinep);
2084 if (p == NULL)
2085 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086#endif
2087
2088 /*
2089 * Skip to start of argument.
2090 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2091 */
2092 if (ea.cmdidx == CMD_bang)
2093 ea.arg = p;
2094 else
2095 ea.arg = skipwhite(p);
2096
Bram Moolenaar379fb762018-08-30 15:58:28 +02002097 // ":file" cannot be run with an argument when "curbuf_lock" is set
2098 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2099 goto doend;
2100
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 /*
2102 * Check for "++opt=val" argument.
2103 * Must be first, allow ":w ++enc=utf8 !cmd"
2104 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002105 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2107 if (getargopt(&ea) == FAIL && !ni)
2108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002109 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 goto doend;
2111 }
2112
2113 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2114 {
2115 if (*ea.arg == '>') /* append */
2116 {
2117 if (*++ea.arg != '>') /* typed wrong */
2118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002119 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 goto doend;
2121 }
2122 ea.arg = skipwhite(ea.arg + 1);
2123 ea.append = TRUE;
2124 }
2125 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2126 {
2127 ++ea.arg;
2128 ea.usefilter = TRUE;
2129 }
2130 }
2131
2132 if (ea.cmdidx == CMD_read)
2133 {
2134 if (ea.forceit)
2135 {
2136 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2137 ea.forceit = FALSE;
2138 }
2139 else if (*ea.arg == '!') /* :r !filter */
2140 {
2141 ++ea.arg;
2142 ea.usefilter = TRUE;
2143 }
2144 }
2145
2146 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2147 {
2148 ea.amount = 1;
2149 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2150 {
2151 ++ea.arg;
2152 ++ea.amount;
2153 }
2154 ea.arg = skipwhite(ea.arg);
2155 }
2156
2157 /*
2158 * Check for "+command" argument, before checking for next command.
2159 * Don't do this for ":read !cmd" and ":write !cmd".
2160 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002161 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2163
2164 /*
2165 * Check for '|' to separate commands and '"' to start comments.
2166 * Don't do this for ":read !cmd" and ":write !cmd".
2167 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002168 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 separate_nextcmd(&ea);
2170
2171 /*
2172 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002173 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2174 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002176 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002177 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002178 || ea.cmdidx == CMD_global
2179 || ea.cmdidx == CMD_vglobal
2180 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182 for (p = ea.arg; *p; ++p)
2183 {
2184 /* Remove one backslash before a newline, so that it's possible to
2185 * pass a newline to the shell and also a newline that is preceded
2186 * with a backslash. This makes it impossible to end a shell
2187 * command in a backslash, but that doesn't appear useful.
2188 * Halving the number of backslashes is incompatible with previous
2189 * versions. */
2190 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002191 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 else if (*p == '\n')
2193 {
2194 ea.nextcmd = p + 1;
2195 *p = NUL;
2196 break;
2197 }
2198 }
2199 }
2200
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002201 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002203 buf_T *buf;
2204
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002206 switch (ea.addr_type)
2207 {
2208 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002209 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002210 ea.line2 = curbuf->b_ml.ml_line_count;
2211 break;
2212 case ADDR_LOADED_BUFFERS:
2213 buf = firstbuf;
2214 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2215 buf = buf->b_next;
2216 ea.line1 = buf->b_fnum;
2217 buf = lastbuf;
2218 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2219 buf = buf->b_prev;
2220 ea.line2 = buf->b_fnum;
2221 break;
2222 case ADDR_BUFFERS:
2223 ea.line1 = firstbuf->b_fnum;
2224 ea.line2 = lastbuf->b_fnum;
2225 break;
2226 case ADDR_WINDOWS:
2227 ea.line2 = LAST_WIN_NR;
2228 break;
2229 case ADDR_TABS:
2230 ea.line2 = LAST_TAB_NR;
2231 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002232 case ADDR_TABS_RELATIVE:
2233 ea.line2 = 1;
2234 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002235 case ADDR_ARGUMENTS:
2236 if (ARGCOUNT == 0)
2237 ea.line1 = ea.line2 = 0;
2238 else
2239 ea.line2 = ARGCOUNT;
2240 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002241 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002242#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002243 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002244 if (ea.line2 == 0)
2245 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002246#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002247 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002248 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002249 case ADDR_UNSIGNED:
2250 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002251 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002252 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255
2256 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002257 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002259 /* Do not allow register = for user commands */
2260 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002261 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002263#ifndef FEAT_CLIPBOARD
2264 /* check these explicitly for a more specific error message */
2265 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002267 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002268 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 }
2270#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002271 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2272 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002273 {
2274 ea.regname = *ea.arg++;
2275#ifdef FEAT_EVAL
2276 /* for '=' register: accept the rest of the line as an expression */
2277 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2278 {
2279 set_expr_line(vim_strsave(ea.arg));
2280 ea.arg += STRLEN(ea.arg);
2281 }
2282#endif
2283 ea.arg = skipwhite(ea.arg);
2284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 }
2286
2287 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002288 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 * count, it's a buffer name.
2290 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002291 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2292 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002293 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 n = getdigits(&ea.arg);
2296 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002297 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002299 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 goto doend;
2301 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002302 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 ea.line2 = n;
2305 if (ea.addr_count == 0)
2306 ea.addr_count = 1;
2307 }
2308 else
2309 {
2310 ea.line1 = ea.line2;
2311 ea.line2 += n - 1;
2312 ++ea.addr_count;
2313 /*
2314 * Be vi compatible: no error message for out of range.
2315 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002316 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 ea.line2 = curbuf->b_ml.ml_line_count;
2318 }
2319 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002320
2321 /*
2322 * Check for flags: 'l', 'p' and '#'.
2323 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002325 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002326 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2327 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002329 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002330 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 goto doend;
2332 }
2333
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002334 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002336 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 goto doend;
2338 }
2339
2340#ifdef FEAT_EVAL
2341 /*
2342 * Skip the command when it's not going to be executed.
2343 * The commands like :if, :endif, etc. always need to be executed.
2344 * Also make an exception for commands that handle a trailing command
2345 * themselves.
2346 */
2347 if (ea.skip)
2348 {
2349 switch (ea.cmdidx)
2350 {
2351 /* commands that need evaluation */
2352 case CMD_while:
2353 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002354 case CMD_for:
2355 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 case CMD_if:
2357 case CMD_elseif:
2358 case CMD_else:
2359 case CMD_endif:
2360 case CMD_try:
2361 case CMD_catch:
2362 case CMD_finally:
2363 case CMD_endtry:
2364 case CMD_function:
2365 break;
2366
2367 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002368 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 * the list at ":help :bar". */
2370 case CMD_aboveleft:
2371 case CMD_and:
2372 case CMD_belowright:
2373 case CMD_botright:
2374 case CMD_browse:
2375 case CMD_call:
2376 case CMD_confirm:
2377 case CMD_delfunction:
2378 case CMD_djump:
2379 case CMD_dlist:
2380 case CMD_dsearch:
2381 case CMD_dsplit:
2382 case CMD_echo:
2383 case CMD_echoerr:
2384 case CMD_echomsg:
2385 case CMD_echon:
2386 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002387 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 case CMD_help:
2389 case CMD_hide:
2390 case CMD_ijump:
2391 case CMD_ilist:
2392 case CMD_isearch:
2393 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002394 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 case CMD_keepjumps:
2396 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002397 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 case CMD_leftabove:
2399 case CMD_let:
2400 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002401 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002403 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002404 case CMD_noautocmd:
2405 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_perl:
2407 case CMD_psearch:
2408 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002409 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002410 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 case CMD_return:
2412 case CMD_rightbelow:
2413 case CMD_ruby:
2414 case CMD_silent:
2415 case CMD_smagic:
2416 case CMD_snomagic:
2417 case CMD_substitute:
2418 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002419 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CMD_tcl:
2421 case CMD_throw:
2422 case CMD_tilde:
2423 case CMD_topleft:
2424 case CMD_unlet:
2425 case CMD_verbose:
2426 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002427 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 break;
2429
2430 default: goto doend;
2431 }
2432 }
2433#endif
2434
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 {
2437 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2438 goto doend;
2439 }
2440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 /*
2442 * Accept buffer name. Cannot be used at the same time with a buffer
2443 * number. Don't do this for a user command.
2444 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002445 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002446 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 {
2448 /*
2449 * :bdelete, :bwipeout and :bunload take several arguments, separated
2450 * by spaces: find next space (skipping over escaped characters).
2451 * The others take one argument: ignore trailing spaces.
2452 */
2453 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2454 || ea.cmdidx == CMD_bunload)
2455 p = skiptowhite_esc(ea.arg);
2456 else
2457 {
2458 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002459 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 --p;
2461 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002462 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002463 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 if (ea.line2 < 0) /* failed */
2465 goto doend;
2466 ea.addr_count = 1;
2467 ea.arg = skipwhite(p);
2468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar2be57332018-02-13 18:05:18 +01002470 /* The :try command saves the emsg_silent flag, reset it here when
2471 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002472 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002473 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002474 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002475 if (emsg_silent < 0)
2476 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002477 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002478 }
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002481 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
Bram Moolenaar958636c2014-10-21 20:01:58 +02002484 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 /*
2487 * Execute a user-defined command.
2488 */
2489 do_ucmd(&ea);
2490 }
2491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
2493 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002494 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
2496 ea.errmsg = NULL;
2497 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2498 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002499 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 }
2501
2502#ifdef FEAT_EVAL
2503 /*
2504 * If the command just executed called do_cmdline(), any throw or ":return"
2505 * or ":finish" encountered there must also check the cstack of the still
2506 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2507 * exception, or reanimate a returned function or finished script file and
2508 * return or finish it again.
2509 */
2510 if (need_rethrow)
2511 do_throw(cstack);
2512 else if (check_cstack)
2513 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002514 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 && current_func_returned())
2518 do_return(&ea, TRUE, FALSE, NULL);
2519 }
2520 need_rethrow = check_cstack = FALSE;
2521#endif
2522
2523doend:
2524 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002527 curwin->w_cursor.col = 0;
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2531 {
2532 if (sourcing)
2533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002534 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002537 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002539 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541 emsg(errormsg);
2542 }
2543#ifdef FEAT_EVAL
2544 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002545 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2546 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547#endif
2548
Bram Moolenaareffed932018-08-14 13:38:17 +02002549 if (ea.verbose_save >= 0)
2550 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002551
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002552 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002554 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaareffed932018-08-14 13:38:17 +02002556 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 /* messages could be enabled for a serious error, need to check if the
2559 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002560 if (!did_emsg || msg_silent > ea.save_msg_silent)
2561 msg_silent = ea.save_msg_silent;
2562 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (emsg_silent < 0)
2564 emsg_silent = 0;
2565 /* Restore msg_scroll, it's set by file I/O commands, even when no
2566 * message is actually displayed. */
2567 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002568
2569 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2570 * somewhere in the line. Put it back in the first column. */
2571 if (redirecting())
2572 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002575#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002576 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002577 --sandbox;
2578#endif
2579
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2581 ea.nextcmd = NULL;
2582
2583#ifdef FEAT_EVAL
2584 --ex_nesting_level;
2585#endif
2586
2587 return ea.nextcmd;
2588}
2589#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002590 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591#endif
2592
2593/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002594 * Parse and skip over command modifiers:
2595 * - update eap->cmd
2596 * - store flags in "cmdmod".
2597 * - Set ex_pressedreturn for an empty command line.
2598 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002599 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002600 * - set p_verbose for ":verbose"
2601 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002602 * When "skip_only" is TRUE the global variables are not changed, except for
2603 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 * Return FAIL when the command is not to be executed.
2605 * May set "errormsg" to an error message.
2606 */
2607 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002608parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002609{
2610 char_u *p;
2611
2612 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2613 eap->verbose_save = -1;
2614 eap->save_msg_silent = -1;
2615
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002616 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002617 for (;;)
2618 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002619 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2620 ++eap->cmd;
2621
2622 /* in ex mode, an empty line works like :+ */
2623 if (*eap->cmd == NUL && exmode_active
2624 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2625 || getline_equal(eap->getline, eap->cookie, getexline))
2626 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2627 {
2628 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002629 if (!skip_only)
2630 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002631 }
2632
2633 /* ignore comment and empty lines */
2634 if (*eap->cmd == '"')
2635 return FAIL;
2636 if (*eap->cmd == NUL)
2637 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002638 if (!skip_only)
2639 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002640 return FAIL;
2641 }
2642
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 p = skip_range(eap->cmd, NULL);
2644 switch (*p)
2645 {
2646 /* When adding an entry, also modify cmd_exists(). */
2647 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2648 break;
2649 cmdmod.split |= WSP_ABOVE;
2650 continue;
2651
2652 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2653 {
2654 cmdmod.split |= WSP_BELOW;
2655 continue;
2656 }
2657 if (checkforcmd(&eap->cmd, "browse", 3))
2658 {
2659#ifdef FEAT_BROWSE_CMD
2660 cmdmod.browse = TRUE;
2661#endif
2662 continue;
2663 }
2664 if (!checkforcmd(&eap->cmd, "botright", 2))
2665 break;
2666 cmdmod.split |= WSP_BOT;
2667 continue;
2668
2669 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2670 break;
2671#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2672 cmdmod.confirm = TRUE;
2673#endif
2674 continue;
2675
2676 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2677 {
2678 cmdmod.keepmarks = TRUE;
2679 continue;
2680 }
2681 if (checkforcmd(&eap->cmd, "keepalt", 5))
2682 {
2683 cmdmod.keepalt = TRUE;
2684 continue;
2685 }
2686 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2687 {
2688 cmdmod.keeppatterns = TRUE;
2689 continue;
2690 }
2691 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2692 break;
2693 cmdmod.keepjumps = TRUE;
2694 continue;
2695
2696 case 'f': /* only accept ":filter {pat} cmd" */
2697 {
2698 char_u *reg_pat;
2699
2700 if (!checkforcmd(&p, "filter", 4)
2701 || *p == NUL || ends_excmd(*p))
2702 break;
2703 if (*p == '!')
2704 {
2705 cmdmod.filter_force = TRUE;
2706 p = skipwhite(p + 1);
2707 if (*p == NUL || ends_excmd(*p))
2708 break;
2709 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002710 if (skip_only)
2711 p = skip_vimgrep_pat(p, NULL, NULL);
2712 else
2713 // NOTE: This puts a NUL after the pattern.
2714 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002715 if (p == NULL || *p == NUL)
2716 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002717 if (!skip_only)
2718 {
2719 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002721 if (cmdmod.filter_regmatch.regprog == NULL)
2722 break;
2723 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002724 eap->cmd = p;
2725 continue;
2726 }
2727
2728 /* ":hide" and ":hide | cmd" are not modifiers */
2729 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2730 || *p == NUL || ends_excmd(*p))
2731 break;
2732 eap->cmd = p;
2733 cmdmod.hide = TRUE;
2734 continue;
2735
2736 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2737 {
2738 cmdmod.lockmarks = TRUE;
2739 continue;
2740 }
2741
2742 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2743 break;
2744 cmdmod.split |= WSP_ABOVE;
2745 continue;
2746
2747 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2748 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002749 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002750 {
2751 /* Set 'eventignore' to "all". Restore the
2752 * existing option value later. */
2753 cmdmod.save_ei = vim_strsave(p_ei);
2754 set_string_option_direct((char_u *)"ei", -1,
2755 (char_u *)"all", OPT_FREE, SID_NONE);
2756 }
2757 continue;
2758 }
2759 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2760 break;
2761 cmdmod.noswapfile = TRUE;
2762 continue;
2763
2764 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2765 break;
2766 cmdmod.split |= WSP_BELOW;
2767 continue;
2768
2769 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2770 {
2771#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002772 if (!skip_only)
2773 {
2774 if (!eap->did_sandbox)
2775 ++sandbox;
2776 eap->did_sandbox = TRUE;
2777 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002778#endif
2779 continue;
2780 }
2781 if (!checkforcmd(&eap->cmd, "silent", 3))
2782 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002783 if (!skip_only)
2784 {
2785 if (eap->save_msg_silent == -1)
2786 eap->save_msg_silent = msg_silent;
2787 ++msg_silent;
2788 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002789 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2790 {
2791 /* ":silent!", but not "silent !cmd" */
2792 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002793 if (!skip_only)
2794 {
2795 ++emsg_silent;
2796 ++eap->did_esilent;
2797 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 }
2799 continue;
2800
2801 case 't': if (checkforcmd(&p, "tab", 3))
2802 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002803 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002805 long tabnr = get_address(eap, &eap->cmd,
2806 ADDR_TABS, eap->skip,
2807 skip_only, FALSE, 1);
2808 if (tabnr == MAXLNUM)
2809 cmdmod.tab = tabpage_index(curtab) + 1;
2810 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002812 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002814 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002815 return FAIL;
2816 }
2817 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 }
2820 eap->cmd = p;
2821 continue;
2822 }
2823 if (!checkforcmd(&eap->cmd, "topleft", 2))
2824 break;
2825 cmdmod.split |= WSP_TOP;
2826 continue;
2827
2828 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2829 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002830 if (!skip_only)
2831 {
2832 if (eap->save_msg_silent == -1)
2833 eap->save_msg_silent = msg_silent;
2834 msg_silent = 0;
2835 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 continue;
2837
2838 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2839 {
2840 cmdmod.split |= WSP_VERT;
2841 continue;
2842 }
2843 if (!checkforcmd(&p, "verbose", 4))
2844 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002845 if (!skip_only)
2846 {
2847 if (eap->verbose_save < 0)
2848 eap->verbose_save = p_verbose;
2849 if (vim_isdigit(*eap->cmd))
2850 p_verbose = atoi((char *)eap->cmd);
2851 else
2852 p_verbose = 1;
2853 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 eap->cmd = p;
2855 continue;
2856 }
2857 break;
2858 }
2859
2860 return OK;
2861}
2862
2863/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002864 * Free contents of "cmdmod".
2865 */
2866 static void
2867free_cmdmod(void)
2868{
2869 if (cmdmod.save_ei != NULL)
2870 {
2871 /* Restore 'eventignore' to the value before ":noautocmd". */
2872 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2873 OPT_FREE, SID_NONE);
2874 free_string_option(cmdmod.save_ei);
2875 }
2876
2877 if (cmdmod.filter_regmatch.regprog != NULL)
2878 vim_regfree(cmdmod.filter_regmatch.regprog);
2879}
2880
2881/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002882 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002883 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002884 * Return FAIL and set "errormsg" or return OK.
2885 */
2886 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002887parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002888{
2889 int address_count = 1;
2890 linenr_T lnum;
2891
2892 // Repeat for all ',' or ';' separated addresses.
2893 for (;;)
2894 {
2895 eap->line1 = eap->line2;
2896 switch (eap->addr_type)
2897 {
2898 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002899 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002900 // default is current line number
2901 eap->line2 = curwin->w_cursor.lnum;
2902 break;
2903 case ADDR_WINDOWS:
2904 eap->line2 = CURRENT_WIN_NR;
2905 break;
2906 case ADDR_ARGUMENTS:
2907 eap->line2 = curwin->w_arg_idx + 1;
2908 if (eap->line2 > ARGCOUNT)
2909 eap->line2 = ARGCOUNT;
2910 break;
2911 case ADDR_LOADED_BUFFERS:
2912 case ADDR_BUFFERS:
2913 eap->line2 = curbuf->b_fnum;
2914 break;
2915 case ADDR_TABS:
2916 eap->line2 = CURRENT_TAB_NR;
2917 break;
2918 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002919 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002920 eap->line2 = 1;
2921 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002922 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002923#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002924 eap->line2 = qf_get_cur_idx(eap);
2925#endif
2926 break;
2927 case ADDR_QUICKFIX_VALID:
2928#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002929 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002930#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002931 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002932 case ADDR_NONE:
2933 // Will give an error later if a range is found.
2934 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002935 }
2936 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002937 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002938 eap->addr_count == 0, address_count++);
2939 if (eap->cmd == NULL) // error detected
2940 return FAIL;
2941 if (lnum == MAXLNUM)
2942 {
2943 if (*eap->cmd == '%') // '%' - all lines
2944 {
2945 ++eap->cmd;
2946 switch (eap->addr_type)
2947 {
2948 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002949 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 eap->line1 = 1;
2951 eap->line2 = curbuf->b_ml.ml_line_count;
2952 break;
2953 case ADDR_LOADED_BUFFERS:
2954 {
2955 buf_T *buf = firstbuf;
2956
2957 while (buf->b_next != NULL
2958 && buf->b_ml.ml_mfp == NULL)
2959 buf = buf->b_next;
2960 eap->line1 = buf->b_fnum;
2961 buf = lastbuf;
2962 while (buf->b_prev != NULL
2963 && buf->b_ml.ml_mfp == NULL)
2964 buf = buf->b_prev;
2965 eap->line2 = buf->b_fnum;
2966 break;
2967 }
2968 case ADDR_BUFFERS:
2969 eap->line1 = firstbuf->b_fnum;
2970 eap->line2 = lastbuf->b_fnum;
2971 break;
2972 case ADDR_WINDOWS:
2973 case ADDR_TABS:
2974 if (IS_USER_CMDIDX(eap->cmdidx))
2975 {
2976 eap->line1 = 1;
2977 eap->line2 = eap->addr_type == ADDR_WINDOWS
2978 ? LAST_WIN_NR : LAST_TAB_NR;
2979 }
2980 else
2981 {
2982 // there is no Vim command which uses '%' and
2983 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002984 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002985 return FAIL;
2986 }
2987 break;
2988 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002989 case ADDR_UNSIGNED:
2990 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002991 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002992 return FAIL;
2993 case ADDR_ARGUMENTS:
2994 if (ARGCOUNT == 0)
2995 eap->line1 = eap->line2 = 0;
2996 else
2997 {
2998 eap->line1 = 1;
2999 eap->line2 = ARGCOUNT;
3000 }
3001 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003002 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003003#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003004 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003005 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003006 if (eap->line2 == 0)
3007 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003008#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003009 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003010 case ADDR_NONE:
3011 // Will give an error later if a range is found.
3012 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003013 }
3014 ++eap->addr_count;
3015 }
3016 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3017 {
3018 pos_T *fp;
3019
3020 // '*' - visual area
3021 if (eap->addr_type != ADDR_LINES)
3022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003024 return FAIL;
3025 }
3026
3027 ++eap->cmd;
3028 if (!eap->skip)
3029 {
3030 fp = getmark('<', FALSE);
3031 if (check_mark(fp) == FAIL)
3032 return FAIL;
3033 eap->line1 = fp->lnum;
3034 fp = getmark('>', FALSE);
3035 if (check_mark(fp) == FAIL)
3036 return FAIL;
3037 eap->line2 = fp->lnum;
3038 ++eap->addr_count;
3039 }
3040 }
3041 }
3042 else
3043 eap->line2 = lnum;
3044 eap->addr_count++;
3045
3046 if (*eap->cmd == ';')
3047 {
3048 if (!eap->skip)
3049 {
3050 curwin->w_cursor.lnum = eap->line2;
3051 // don't leave the cursor on an illegal line or column
3052 check_cursor();
3053 }
3054 }
3055 else if (*eap->cmd != ',')
3056 break;
3057 ++eap->cmd;
3058 }
3059
3060 // One address given: set start and end lines.
3061 if (eap->addr_count == 1)
3062 {
3063 eap->line1 = eap->line2;
3064 // ... but only implicit: really no address given
3065 if (lnum == MAXLNUM)
3066 eap->addr_count = 0;
3067 }
3068 return OK;
3069}
3070
3071/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003072 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 * If there is a match advance "pp" to the argument and return TRUE.
3074 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003075 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003076checkforcmd(
3077 char_u **pp, /* start of command */
3078 char *cmd, /* name of command */
3079 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 int i;
3082
3083 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003084 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 break;
3086 if (i >= len && !isalpha((*pp)[i]))
3087 {
3088 *pp = skipwhite(*pp + i);
3089 return TRUE;
3090 }
3091 return FALSE;
3092}
3093
3094/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003095 * Append "cmd" to the error message in IObuff.
3096 * Takes care of limiting the length and handling 0xa0, which would be
3097 * invisible otherwise.
3098 */
3099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003100append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003101{
3102 char_u *s = cmd;
3103 char_u *d;
3104
3105 STRCAT(IObuff, ": ");
3106 d = IObuff + STRLEN(IObuff);
3107 while (*s != NUL && d - IObuff < IOSIZE - 7)
3108 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003109 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003110 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003111 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003112 STRCPY(d, "<a0>");
3113 d += 4;
3114 }
3115 else
3116 MB_COPY_CHAR(s, d);
3117 }
3118 *d = NUL;
3119}
3120
3121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003123 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003124 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003125 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 * Returns NULL for an ambiguous user command.
3127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003129find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130{
3131 int len;
3132 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003133 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134
3135 /*
3136 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003137 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * - the 'k' command can directly be followed by any character.
3139 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003140 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003142 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 */
3144 p = eap->cmd;
3145 if (*p == 'k')
3146 {
3147 eap->cmdidx = CMD_k;
3148 ++p;
3149 }
3150 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003151 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3152 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 || p[1] == 'g'
3154 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3155 || p[1] == 'I'
3156 || (p[1] == 'r' && p[2] != 'e')))
3157 {
3158 eap->cmdidx = CMD_substitute;
3159 ++p;
3160 }
3161 else
3162 {
3163 while (ASCII_ISALPHA(*p))
3164 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003165 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003166 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003167 while (ASCII_ISALNUM(*p))
3168 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003169
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 /* check for non-alpha command */
3171 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3172 ++p;
3173 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003174 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3175 {
3176 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3177 * :delete with the 'l' flag. Same for 'p'. */
3178 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003179 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003180 break;
3181 if (i == len - 1)
3182 {
3183 --len;
3184 if (p[-1] == 'l')
3185 eap->flags |= EXFLAG_LIST;
3186 else
3187 eap->flags |= EXFLAG_PRINT;
3188 }
3189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003191 if (ASCII_ISLOWER(eap->cmd[0]))
3192 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003193 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003194 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003195
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003196 if (command_count != (int)CMD_SIZE)
3197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003198 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003199 getout(1);
3200 }
3201
3202 /* Use a precomputed index for fast look-up in cmdnames[]
3203 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003204 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3205 if (ASCII_ISLOWER(c2))
3206 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003209 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210
3211 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3212 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3213 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3214 (size_t)len) == 0)
3215 {
3216#ifdef FEAT_EVAL
3217 if (full != NULL
3218 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3219 *full = TRUE;
3220#endif
3221 break;
3222 }
3223
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003224 // Look for a user defined command as a last resort. Let ":Print" be
3225 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003226 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3227 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003229 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 while (ASCII_ISALNUM(*p))
3231 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003232 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 eap->cmdidx = CMD_SIZE;
3236 }
3237
3238 return p;
3239}
3240
3241#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003242static struct cmdmod
3243{
3244 char *name;
3245 int minlen;
3246 int has_count; /* :123verbose :3tab */
3247} cmdmods[] = {
3248 {"aboveleft", 3, FALSE},
3249 {"belowright", 3, FALSE},
3250 {"botright", 2, FALSE},
3251 {"browse", 3, FALSE},
3252 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003253 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003254 {"hide", 3, FALSE},
3255 {"keepalt", 5, FALSE},
3256 {"keepjumps", 5, FALSE},
3257 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003258 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003259 {"leftabove", 5, FALSE},
3260 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003261 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003262 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003263 {"rightbelow", 6, FALSE},
3264 {"sandbox", 3, FALSE},
3265 {"silent", 3, FALSE},
3266 {"tab", 3, TRUE},
3267 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003268 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003269 {"verbose", 4, TRUE},
3270 {"vertical", 4, FALSE},
3271};
3272
3273/*
3274 * Return length of a command modifier (including optional count).
3275 * Return zero when it's not a modifier.
3276 */
3277 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003278modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003279{
3280 int i, j;
3281 char_u *p = cmd;
3282
3283 if (VIM_ISDIGIT(*cmd))
3284 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003285 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003286 {
3287 for (j = 0; p[j] != NUL; ++j)
3288 if (p[j] != cmdmods[i].name[j])
3289 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003290 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003291 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003292 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003293 }
3294 return 0;
3295}
3296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297/*
3298 * Return > 0 if an Ex command "name" exists.
3299 * Return 2 if there is an exact match.
3300 * Return 3 if there is an ambiguous match.
3301 */
3302 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003303cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 exarg_T ea;
3306 int full = FALSE;
3307 int i;
3308 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003309 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
3311 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003312 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 for (j = 0; name[j] != NUL; ++j)
3315 if (name[j] != cmdmods[i].name[j])
3316 break;
3317 if (name[j] == NUL && j >= cmdmods[i].minlen)
3318 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3319 }
3320
Bram Moolenaara9587612006-05-04 21:47:50 +00003321 /* Check built-in commands and user defined commands.
3322 * For ":2match" and ":3match" we need to skip the number. */
3323 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003325 p = find_command(&ea, &full);
3326 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003328 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3329 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003330 if (*skipwhite(p) != NUL)
3331 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3333}
3334#endif
3335
3336/*
3337 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3338 * we don't need/want deleted. Maybe this could be done better if we didn't
3339 * repeat all this stuff. The only problem is that they may not stay
3340 * perfectly compatible with each other, but then the command line syntax
3341 * probably won't change that much -- webb.
3342 */
3343 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003344set_one_cmd_context(
3345 expand_T *xp,
3346 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 char_u *p;
3349 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003350 int len = 0;
3351 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003353 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 int delim;
3355#endif
3356 int forceit = FALSE;
3357 int usefilter = FALSE; /* filter instead of file name */
3358
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003359 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 xp->xp_pattern = buff;
3361 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003362 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003365 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 */
3367 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3368 ;
3369 xp->xp_pattern = cmd;
3370
3371 if (*cmd == NUL)
3372 return NULL;
3373 if (*cmd == '"') /* ignore comment lines */
3374 {
3375 xp->xp_context = EXPAND_NOTHING;
3376 return NULL;
3377 }
3378
3379/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003380 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 */
3382 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 xp->xp_pattern = cmd;
3384 if (*cmd == NUL)
3385 return NULL;
3386 if (*cmd == '"')
3387 {
3388 xp->xp_context = EXPAND_NOTHING;
3389 return NULL;
3390 }
3391
3392 if (*cmd == '|' || *cmd == '\n')
3393 return cmd + 1; /* There's another command */
3394
3395 /*
3396 * Isolate the command and search for it in the command table.
3397 * Exceptions:
3398 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003399 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3401 */
3402 if (*cmd == 'k' && cmd[1] != 'e')
3403 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 p = cmd + 1;
3406 }
3407 else
3408 {
3409 p = cmd;
3410 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3411 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003412 /* a user command may contain digits */
3413 if (ASCII_ISUPPER(cmd[0]))
3414 while (ASCII_ISALNUM(*p) || *p == '*')
3415 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003416 /* for python 3.x: ":py3*" commands completion */
3417 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003418 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003419 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003420 while (ASCII_ISALPHA(*p) || *p == '*')
3421 ++p;
3422 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003423 /* check for non-alpha command */
3424 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3425 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003426 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003428 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 {
3430 xp->xp_context = EXPAND_UNSUCCESSFUL;
3431 return NULL;
3432 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003433 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003434 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3435 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3436 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 break;
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003440 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 }
3443
3444 /*
3445 * If the cursor is touching the command, and it ends in an alpha-numeric
3446 * character, complete the command name.
3447 */
3448 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3449 return NULL;
3450
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003451 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3454 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003455 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 p = cmd + 1;
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3459 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003460 ea.cmd = cmd;
3461 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003462#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003463 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003464#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003465 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003466#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003467 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003468 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003469 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003472 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 {
3474 /* Not still touching the command and it was an illegal one */
3475 xp->xp_context = EXPAND_UNSUCCESSFUL;
3476 return NULL;
3477 }
3478
3479 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3480
3481 if (*p == '!') /* forced commands */
3482 {
3483 forceit = TRUE;
3484 ++p;
3485 }
3486
3487/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003488 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003490 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003491 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
3493 arg = skipwhite(p);
3494
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003495 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 if (*arg == '>') /* append */
3498 {
3499 if (*++arg == '>')
3500 ++arg;
3501 arg = skipwhite(arg);
3502 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003503 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 {
3505 ++arg;
3506 usefilter = TRUE;
3507 }
3508 }
3509
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003510 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 {
3512 usefilter = forceit; /* :r! filter if forced */
3513 if (*arg == '!') /* :r !filter */
3514 {
3515 ++arg;
3516 usefilter = TRUE;
3517 }
3518 }
3519
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003520 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 while (*arg == *cmd) /* allow any number of '>' or '<' */
3523 ++arg;
3524 arg = skipwhite(arg);
3525 }
3526
3527 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003528 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 {
3530 /* Check if we're in the +command */
3531 p = arg + 1;
3532 arg = skip_cmd_arg(arg, FALSE);
3533
3534 /* Still touching the command after '+'? */
3535 if (*arg == NUL)
3536 return p;
3537
3538 /* Skip space(s) after +command to get to the real argument */
3539 arg = skipwhite(arg);
3540 }
3541
3542 /*
3543 * Check for '|' to separate commands and '"' to start comments.
3544 * Don't do this for ":read !cmd" and ":write !cmd".
3545 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003546 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
3548 p = arg;
3549 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003550 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 p += 2;
3552 while (*p)
3553 {
3554 if (*p == Ctrl_V)
3555 {
3556 if (p[1] != NUL)
3557 ++p;
3558 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003559 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 || *p == '|' || *p == '\n')
3561 {
3562 if (*(p - 1) != '\\')
3563 {
3564 if (*p == '|' || *p == '\n')
3565 return p + 1;
3566 return NULL; /* It's a comment */
3567 }
3568 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003569 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 }
3571 }
3572
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003573 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3574 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3575 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 return NULL;
3577
3578 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003579 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003581 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003582 while (*p && p < buff + len)
3583 {
3584 if (*p == ' ' || *p == TAB)
3585 {
3586 /* argument starts after a space */
3587 xp->xp_pattern = ++p;
3588 }
3589 else
3590 {
3591 if (*p == '\\' && *(p + 1) != NUL)
3592 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003593 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003594 }
3595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003597 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003599 int c;
3600 int in_quote = FALSE;
3601 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 /*
3604 * Allow spaces within back-quotes to count as part of the argument
3605 * being expanded.
3606 */
3607 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003608 p = xp->xp_pattern;
3609 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003611 if (has_mbyte)
3612 c = mb_ptr2char(p);
3613 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003614 c = *p;
3615 if (c == '\\' && p[1] != NUL)
3616 ++p;
3617 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 {
3619 if (!in_quote)
3620 {
3621 xp->xp_pattern = p;
3622 bow = p + 1;
3623 }
3624 in_quote = !in_quote;
3625 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003626 /* An argument can contain just about everything, except
3627 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003628 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003629#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003630 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003631#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003632 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003633 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003634 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003635 while (*p != NUL)
3636 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003637 if (has_mbyte)
3638 c = mb_ptr2char(p);
3639 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003640 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003641 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003642 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003643 if (has_mbyte)
3644 len = (*mb_ptr2len)(p);
3645 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003646 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003647 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003648 }
3649 if (in_quote)
3650 bow = p;
3651 else
3652 xp->xp_pattern = p;
3653 p -= len;
3654 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003655 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657
3658 /*
3659 * If we are still inside the quotes, and we passed a space, just
3660 * expand from there.
3661 */
3662 if (bow != NULL && in_quote)
3663 xp->xp_pattern = bow;
3664 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003665
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003666 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003667 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003668 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003669#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003670 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003671#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003672 /* When still after the command name expand executables. */
3673 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003674 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
3677 /* Check for environment variable */
3678 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003679#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 || *xp->xp_pattern == '%'
3681#endif
3682 )
3683 {
3684 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3685 if (!vim_isIDc(*p))
3686 break;
3687 if (*p == NUL)
3688 {
3689 xp->xp_context = EXPAND_ENV_VARS;
3690 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003691#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003692 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003693 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003694 compl = EXPAND_ENV_VARS;
3695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 }
3697 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003698#if defined(FEAT_CMDL_COMPL)
3699 /* Check for user names */
3700 if (*xp->xp_pattern == '~')
3701 {
3702 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3703 ;
3704 /* Complete ~user only if it partially matches a user name.
3705 * A full match ~user<Tab> will be replaced by user's home
3706 * directory i.e. something like ~user<Tab> -> /home/user/ */
3707 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003708 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003709 {
3710 xp->xp_context = EXPAND_USER;
3711 ++xp->xp_pattern;
3712 }
3713 }
3714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716
3717/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003718 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003720 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003722 case CMD_find:
3723 case CMD_sfind:
3724 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003725 if (xp->xp_context == EXPAND_FILES)
3726 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003727 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 case CMD_cd:
3729 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003730 case CMD_tcd:
3731 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 case CMD_lcd:
3733 case CMD_lchdir:
3734 if (xp->xp_context == EXPAND_FILES)
3735 xp->xp_context = EXPAND_DIRECTORIES;
3736 break;
3737 case CMD_help:
3738 xp->xp_context = EXPAND_HELP;
3739 xp->xp_pattern = arg;
3740 break;
3741
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003742 /* Command modifiers: return the argument.
3743 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003745 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 case CMD_belowright:
3747 case CMD_botright:
3748 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003749 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003750 case CMD_cdo:
3751 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003753 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 case CMD_folddoclosed:
3755 case CMD_folddoopen:
3756 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003757 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 case CMD_keepjumps:
3759 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003760 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003761 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003763 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003765 case CMD_noautocmd:
3766 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003768 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003770 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003771 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 case CMD_topleft:
3773 case CMD_verbose:
3774 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003775 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 return arg;
3777
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003778 case CMD_filter:
3779 if (*arg != NUL)
3780 arg = skip_vimgrep_pat(arg, NULL, NULL);
3781 if (arg == NULL || *arg == NUL)
3782 {
3783 xp->xp_context = EXPAND_NOTHING;
3784 return NULL;
3785 }
3786 return skipwhite(arg);
3787
Bram Moolenaar4f688582007-07-24 12:34:30 +00003788#ifdef FEAT_CMDL_COMPL
3789# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 case CMD_match:
3791 if (*arg == NUL || !ends_excmd(*arg))
3792 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003793 /* also complete "None" */
3794 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 arg = skipwhite(skiptowhite(arg));
3796 if (*arg != NUL)
3797 {
3798 xp->xp_context = EXPAND_NOTHING;
3799 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3800 }
3801 }
3802 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805/*
3806 * All completion for the +cmdline_compl feature goes here.
3807 */
3808
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003810 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
3812 case CMD_delcommand:
3813 xp->xp_context = EXPAND_USER_COMMANDS;
3814 xp->xp_pattern = arg;
3815 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
3817 case CMD_global:
3818 case CMD_vglobal:
3819 delim = *arg; /* get the delimiter */
3820 if (delim)
3821 ++arg; /* skip delimiter if there is one */
3822
3823 while (arg[0] != NUL && arg[0] != delim)
3824 {
3825 if (arg[0] == '\\' && arg[1] != NUL)
3826 ++arg;
3827 ++arg;
3828 }
3829 if (arg[0] != NUL)
3830 return arg + 1;
3831 break;
3832 case CMD_and:
3833 case CMD_substitute:
3834 delim = *arg;
3835 if (delim)
3836 {
3837 /* skip "from" part */
3838 ++arg;
3839 arg = skip_regexp(arg, delim, p_magic, NULL);
3840 }
3841 /* skip "to" part */
3842 while (arg[0] != NUL && arg[0] != delim)
3843 {
3844 if (arg[0] == '\\' && arg[1] != NUL)
3845 ++arg;
3846 ++arg;
3847 }
3848 if (arg[0] != NUL) /* skip delimiter */
3849 ++arg;
3850 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3851 ++arg;
3852 if (arg[0] != NUL)
3853 return arg;
3854 break;
3855 case CMD_isearch:
3856 case CMD_dsearch:
3857 case CMD_ilist:
3858 case CMD_dlist:
3859 case CMD_ijump:
3860 case CMD_psearch:
3861 case CMD_djump:
3862 case CMD_isplit:
3863 case CMD_dsplit:
3864 arg = skipwhite(skipdigits(arg)); /* skip count */
3865 if (*arg == '/') /* Match regexp, not just whole words */
3866 {
3867 for (++arg; *arg && *arg != '/'; arg++)
3868 if (*arg == '\\' && arg[1] != NUL)
3869 arg++;
3870 if (*arg)
3871 {
3872 arg = skipwhite(arg + 1);
3873
3874 /* Check for trailing illegal characters */
3875 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3876 xp->xp_context = EXPAND_NOTHING;
3877 else
3878 return arg;
3879 }
3880 }
3881 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 case CMD_autocmd:
3884 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003886 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_set:
3889 set_context_in_set_cmd(xp, arg, 0);
3890 break;
3891 case CMD_setglobal:
3892 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3893 break;
3894 case CMD_setlocal:
3895 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3896 break;
3897 case CMD_tag:
3898 case CMD_stag:
3899 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003900 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 case CMD_tselect:
3902 case CMD_stselect:
3903 case CMD_ptselect:
3904 case CMD_tjump:
3905 case CMD_stjump:
3906 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003907 if (*p_wop != NUL)
3908 xp->xp_context = EXPAND_TAGS_LISTFILES;
3909 else
3910 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 xp->xp_pattern = arg;
3912 break;
3913 case CMD_augroup:
3914 xp->xp_context = EXPAND_AUGROUP;
3915 xp->xp_pattern = arg;
3916 break;
3917#ifdef FEAT_SYN_HL
3918 case CMD_syntax:
3919 set_context_in_syntax_cmd(xp, arg);
3920 break;
3921#endif
3922#ifdef FEAT_EVAL
3923 case CMD_let:
3924 case CMD_if:
3925 case CMD_elseif:
3926 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003927 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 case CMD_echo:
3929 case CMD_echon:
3930 case CMD_execute:
3931 case CMD_echomsg:
3932 case CMD_echoerr:
3933 case CMD_call:
3934 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003935 case CMD_cexpr:
3936 case CMD_caddexpr:
3937 case CMD_cgetexpr:
3938 case CMD_lexpr:
3939 case CMD_laddexpr:
3940 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003941 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 break;
3943
3944 case CMD_unlet:
3945 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3946 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003947
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 xp->xp_context = EXPAND_USER_VARS;
3949 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003950
3951 if (*xp->xp_pattern == '$')
3952 {
3953 xp->xp_context = EXPAND_ENV_VARS;
3954 ++xp->xp_pattern;
3955 }
3956
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 break;
3958
3959 case CMD_function:
3960 case CMD_delfunction:
3961 xp->xp_context = EXPAND_USER_FUNC;
3962 xp->xp_pattern = arg;
3963 break;
3964
3965 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003966 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 break;
3968#endif
3969 case CMD_highlight:
3970 set_context_in_highlight_cmd(xp, arg);
3971 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003972#ifdef FEAT_CSCOPE
3973 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003974 case CMD_lcscope:
3975 case CMD_scscope:
3976 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003977 break;
3978#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003979#ifdef FEAT_SIGNS
3980 case CMD_sign:
3981 set_context_in_sign_cmd(xp, arg);
3982 break;
3983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 case CMD_bdelete:
3985 case CMD_bwipeout:
3986 case CMD_bunload:
3987 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3988 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003989 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 case CMD_buffer:
3991 case CMD_sbuffer:
3992 case CMD_checktime:
3993 xp->xp_context = EXPAND_BUFFERS;
3994 xp->xp_pattern = arg;
3995 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 case CMD_USER:
3998 case CMD_USER_BUF:
3999 if (compl != EXPAND_NOTHING)
4000 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004001 // EX_XFILE: file names are handled above
4002 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004004#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 if (compl == EXPAND_MENUS)
4006 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 if (compl == EXPAND_COMMANDS)
4009 return arg;
4010 if (compl == EXPAND_MAPPINGS)
4011 return set_context_in_map_cmd(xp, (char_u *)"map",
4012 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004013 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004014 p = arg;
4015 while (*p)
4016 {
4017 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004018 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004019 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004020 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004021 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004022 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 xp->xp_pattern = arg;
4025 }
4026 xp->xp_context = compl;
4027 }
4028 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004029
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 case CMD_map: case CMD_noremap:
4031 case CMD_nmap: case CMD_nnoremap:
4032 case CMD_vmap: case CMD_vnoremap:
4033 case CMD_omap: case CMD_onoremap:
4034 case CMD_imap: case CMD_inoremap:
4035 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004036 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004037 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004038 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004039 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004041 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 case CMD_unmap:
4043 case CMD_nunmap:
4044 case CMD_vunmap:
4045 case CMD_ounmap:
4046 case CMD_iunmap:
4047 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004048 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004049 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004050 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004051 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004053 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004054 case CMD_mapclear:
4055 case CMD_nmapclear:
4056 case CMD_vmapclear:
4057 case CMD_omapclear:
4058 case CMD_imapclear:
4059 case CMD_cmapclear:
4060 case CMD_lmapclear:
4061 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004062 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004063 case CMD_xmapclear:
4064 xp->xp_context = EXPAND_MAPCLEAR;
4065 xp->xp_pattern = arg;
4066 break;
4067
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 case CMD_abbreviate: case CMD_noreabbrev:
4069 case CMD_cabbrev: case CMD_cnoreabbrev:
4070 case CMD_iabbrev: case CMD_inoreabbrev:
4071 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004072 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 case CMD_unabbreviate:
4074 case CMD_cunabbrev:
4075 case CMD_iunabbrev:
4076 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004077 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#ifdef FEAT_MENU
4079 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4080 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4081 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4082 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4083 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4084 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4085 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004086 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 case CMD_tmenu: case CMD_tunmenu:
4088 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4089 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4090#endif
4091
4092 case CMD_colorscheme:
4093 xp->xp_context = EXPAND_COLORS;
4094 xp->xp_pattern = arg;
4095 break;
4096
4097 case CMD_compiler:
4098 xp->xp_context = EXPAND_COMPILER;
4099 xp->xp_pattern = arg;
4100 break;
4101
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004102 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004103 xp->xp_context = EXPAND_OWNSYNTAX;
4104 xp->xp_pattern = arg;
4105 break;
4106
4107 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004108 xp->xp_context = EXPAND_FILETYPE;
4109 xp->xp_pattern = arg;
4110 break;
4111
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004112 case CMD_packadd:
4113 xp->xp_context = EXPAND_PACKADD;
4114 xp->xp_pattern = arg;
4115 break;
4116
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004117#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004119 p = skiptowhite(arg);
4120 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 {
4122 xp->xp_context = EXPAND_LANGUAGE;
4123 xp->xp_pattern = arg;
4124 }
4125 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004126 {
4127 if ( STRNCMP(arg, "messages", p - arg) == 0
4128 || STRNCMP(arg, "ctype", p - arg) == 0
4129 || STRNCMP(arg, "time", p - arg) == 0)
4130 {
4131 xp->xp_context = EXPAND_LOCALES;
4132 xp->xp_pattern = skipwhite(p);
4133 }
4134 else
4135 xp->xp_context = EXPAND_NOTHING;
4136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 break;
4138#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004139#if defined(FEAT_PROFILE)
4140 case CMD_profile:
4141 set_context_in_profile_cmd(xp, arg);
4142 break;
4143#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004144 case CMD_behave:
4145 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004146 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004147 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004149 case CMD_messages:
4150 xp->xp_context = EXPAND_MESSAGES;
4151 xp->xp_pattern = arg;
4152 break;
4153
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004154#if defined(FEAT_CMDHIST)
4155 case CMD_history:
4156 xp->xp_context = EXPAND_HISTORY;
4157 xp->xp_pattern = arg;
4158 break;
4159#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004160#if defined(FEAT_PROFILE)
4161 case CMD_syntime:
4162 xp->xp_context = EXPAND_SYNTIME;
4163 xp->xp_pattern = arg;
4164 break;
4165#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004166
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004167 case CMD_argdelete:
4168 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4169 arg = xp->xp_pattern + 1;
4170 xp->xp_context = EXPAND_ARGLIST;
4171 xp->xp_pattern = arg;
4172 break;
4173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174#endif /* FEAT_CMDL_COMPL */
4175
4176 default:
4177 break;
4178 }
4179 return NULL;
4180}
4181
4182/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004183 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 *
4185 * Backslashed delimiters after / or ? will be skipped, and commands will
4186 * not be expanded between /'s and ?'s or after "'".
4187 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004188 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 * Returns the "cmd" pointer advanced to beyond the range.
4190 */
4191 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004192skip_range(
4193 char_u *cmd,
4194 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004196 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004198 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004200 if (*cmd == '\\')
4201 {
4202 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4203 ++cmd;
4204 else
4205 break;
4206 }
4207 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
4209 if (*++cmd == NUL && ctx != NULL)
4210 *ctx = EXPAND_NOTHING;
4211 }
4212 else if (*cmd == '/' || *cmd == '?')
4213 {
4214 delim = *cmd++;
4215 while (*cmd != NUL && *cmd != delim)
4216 if (*cmd++ == '\\' && *cmd != NUL)
4217 ++cmd;
4218 if (*cmd == NUL && ctx != NULL)
4219 *ctx = EXPAND_NOTHING;
4220 }
4221 if (*cmd != NUL)
4222 ++cmd;
4223 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004224
4225 /* Skip ":" and white space. */
4226 while (*cmd == ':')
4227 cmd = skipwhite(cmd + 1);
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return cmd;
4230}
4231
4232/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004233 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 *
4235 * Set ptr to the next character after the part that was interpreted.
4236 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004237 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 *
4239 * Return MAXLNUM when no Ex address was found.
4240 */
4241 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004242get_address(
4243 exarg_T *eap UNUSED,
4244 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004245 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004246 int skip, // only skip the address, don't use it
4247 int silent, // no errors or side effects
4248 int to_other_file, // flag: may jump to other file
4249 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004251 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 int c;
4253 int i;
4254 long n;
4255 char_u *cmd;
4256 pos_T pos;
4257 pos_T *fp;
4258 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004259 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260
4261 cmd = skipwhite(*ptr);
4262 lnum = MAXLNUM;
4263 do
4264 {
4265 switch (*cmd)
4266 {
4267 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004268 ++cmd;
4269 switch (addr_type)
4270 {
4271 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004272 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 lnum = curwin->w_cursor.lnum;
4274 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004275 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004276 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004277 break;
4278 case ADDR_ARGUMENTS:
4279 lnum = curwin->w_arg_idx + 1;
4280 break;
4281 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004282 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004283 lnum = curbuf->b_fnum;
4284 break;
4285 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004286 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004287 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004288 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004289 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004290 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004291 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004292 cmd = NULL;
4293 goto error;
4294 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004295 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004296#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004297 lnum = qf_get_cur_idx(eap);
4298#endif
4299 break;
4300 case ADDR_QUICKFIX_VALID:
4301#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004302 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004303#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004304 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004305 }
4306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004309 ++cmd;
4310 switch (addr_type)
4311 {
4312 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004313 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 lnum = curbuf->b_ml.ml_line_count;
4315 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004316 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004317 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318 break;
4319 case ADDR_ARGUMENTS:
4320 lnum = ARGCOUNT;
4321 break;
4322 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004323 buf = lastbuf;
4324 while (buf->b_ml.ml_mfp == NULL)
4325 {
4326 if (buf->b_prev == NULL)
4327 break;
4328 buf = buf->b_prev;
4329 }
4330 lnum = buf->b_fnum;
4331 break;
4332 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004333 lnum = lastbuf->b_fnum;
4334 break;
4335 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004336 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004337 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004338 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004339 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004340 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004341 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004342 cmd = NULL;
4343 goto error;
4344 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004345 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004346#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004347 lnum = qf_get_size(eap);
4348 if (lnum == 0)
4349 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004350#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004351 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004352 case ADDR_QUICKFIX_VALID:
4353#ifdef FEAT_QUICKFIX
4354 lnum = qf_get_valid_size(eap);
4355 if (lnum == 0)
4356 lnum = 1;
4357#endif
4358 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 }
4360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361
4362 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004363 if (*++cmd == NUL)
4364 {
4365 cmd = NULL;
4366 goto error;
4367 }
4368 if (addr_type != ADDR_LINES)
4369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004370 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004371 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004372 goto error;
4373 }
4374 if (skip)
4375 ++cmd;
4376 else
4377 {
4378 /* Only accept a mark in another file when it is
4379 * used by itself: ":'M". */
4380 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4381 ++cmd;
4382 if (fp == (pos_T *)-1)
4383 /* Jumped to another file. */
4384 lnum = curwin->w_cursor.lnum;
4385 else
4386 {
4387 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
4389 cmd = NULL;
4390 goto error;
4391 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 lnum = fp->lnum;
4393 }
4394 }
4395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396
4397 case '/':
4398 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004399 c = *cmd++;
4400 if (addr_type != ADDR_LINES)
4401 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004402 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004403 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004404 goto error;
4405 }
4406 if (skip) /* skip "/pat/" */
4407 {
4408 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4409 if (*cmd == c)
4410 ++cmd;
4411 }
4412 else
4413 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004414 int flags;
4415
4416 pos = curwin->w_cursor; // save curwin->w_cursor
4417
4418 // When '/' or '?' follows another address, start from
4419 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 if (lnum != MAXLNUM)
4421 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004422
4423 // Start a forward search at the end of the line (unless
4424 // before the first line).
4425 // Start a backward search at the start of the line.
4426 // This makes sure we never match in the current
4427 // line, and can match anywhere in the
4428 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004429 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 curwin->w_cursor.col = MAXCOL;
4431 else
4432 curwin->w_cursor.col = 0;
4433 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004434 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4435 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004436 {
4437 curwin->w_cursor = pos;
4438 cmd = NULL;
4439 goto error;
4440 }
4441 lnum = curwin->w_cursor.lnum;
4442 curwin->w_cursor = pos;
4443 /* adjust command string pointer */
4444 cmd += searchcmdlen;
4445 }
4446 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004449 ++cmd;
4450 if (addr_type != ADDR_LINES)
4451 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004452 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004453 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 goto error;
4455 }
4456 if (*cmd == '&')
4457 i = RE_SUBST;
4458 else if (*cmd == '?' || *cmd == '/')
4459 i = RE_SEARCH;
4460 else
4461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004462 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 cmd = NULL;
4464 goto error;
4465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 if (!skip)
4468 {
4469 /*
4470 * When search follows another address, start from
4471 * there.
4472 */
4473 if (lnum != MAXLNUM)
4474 pos.lnum = lnum;
4475 else
4476 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 /*
4479 * Start the search just like for the above
4480 * do_search().
4481 */
4482 if (*cmd != '?')
4483 pos.col = MAXCOL;
4484 else
4485 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004486 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004487 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004488 *cmd == '?' ? BACKWARD : FORWARD,
4489 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004490 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004491 lnum = pos.lnum;
4492 else
4493 {
4494 cmd = NULL;
4495 goto error;
4496 }
4497 }
4498 ++cmd;
4499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004502 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4503 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505
4506 for (;;)
4507 {
4508 cmd = skipwhite(cmd);
4509 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4510 break;
4511
4512 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004513 {
4514 switch (addr_type)
4515 {
4516 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004517 case ADDR_OTHER:
4518 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004519 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 break;
4521 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004522 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004523 break;
4524 case ADDR_ARGUMENTS:
4525 lnum = curwin->w_arg_idx + 1;
4526 break;
4527 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004528 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004529 lnum = curbuf->b_fnum;
4530 break;
4531 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004532 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004533 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004534 case ADDR_TABS_RELATIVE:
4535 lnum = 1;
4536 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004537 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004538#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004539 lnum = qf_get_cur_idx(eap);
4540#endif
4541 break;
4542 case ADDR_QUICKFIX_VALID:
4543#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004544 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004545#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004546 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004547 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004548 case ADDR_UNSIGNED:
4549 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004550 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004551 }
4552 }
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (VIM_ISDIGIT(*cmd))
4555 i = '+'; /* "number" is same as "+number" */
4556 else
4557 i = *cmd++;
4558 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4559 n = 1;
4560 else
4561 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004562
4563 if (addr_type == ADDR_TABS_RELATIVE)
4564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004565 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004566 cmd = NULL;
4567 goto error;
4568 }
4569 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004570 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004571 lnum = compute_buffer_local_count(
4572 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004574 {
4575#ifdef FEAT_FOLDING
4576 /* Relative line addressing, need to adjust for folded lines
4577 * now, but only do it after the first address. */
4578 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4579 && address_count >= 2)
4580 (void)hasFolding(lnum, NULL, &lnum);
4581#endif
4582 if (i == '-')
4583 lnum -= n;
4584 else
4585 lnum += n;
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 } while (*cmd == '/' || *cmd == '?');
4589
4590error:
4591 *ptr = cmd;
4592 return lnum;
4593}
4594
4595/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004596 * Get flags from an Ex command argument.
4597 */
4598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004599get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004600{
4601 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4602 {
4603 if (*eap->arg == 'l')
4604 eap->flags |= EXFLAG_LIST;
4605 else if (*eap->arg == 'p')
4606 eap->flags |= EXFLAG_PRINT;
4607 else
4608 eap->flags |= EXFLAG_NR;
4609 eap->arg = skipwhite(eap->arg + 1);
4610 }
4611}
4612
4613/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 * Function called for command which is Not Implemented. NI!
4615 */
4616 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004617ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618{
4619 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004620 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621}
4622
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004623#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624/*
4625 * Function called for script command which is Not Implemented. NI!
4626 * Skips over ":perl <<EOF" constructs.
4627 */
4628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004629ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 if (!eap->skip)
4632 ex_ni(eap);
4633 else
4634 vim_free(script_get(eap, eap->arg));
4635}
4636#endif
4637
4638/*
4639 * Check range in Ex command for validity.
4640 * Return NULL when valid, error message when invalid.
4641 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004642 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004645 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if ( eap->line1 < 0
4648 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004649 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004650 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004651
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004652 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004653 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004654 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004655 {
4656 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004657 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004658#ifdef FEAT_DIFF
4659 + (eap->cmdidx == CMD_diffget)
4660#endif
4661 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004662 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004663 break;
4664 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004665 /* add 1 if ARGCOUNT is 0 */
4666 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004667 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004668 break;
4669 case ADDR_BUFFERS:
4670 if (eap->line1 < firstbuf->b_fnum
4671 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004672 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004673 break;
4674 case ADDR_LOADED_BUFFERS:
4675 buf = firstbuf;
4676 while (buf->b_ml.ml_mfp == NULL)
4677 {
4678 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004679 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004680 buf = buf->b_next;
4681 }
4682 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004683 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004684 buf = lastbuf;
4685 while (buf->b_ml.ml_mfp == NULL)
4686 {
4687 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004688 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004689 buf = buf->b_prev;
4690 }
4691 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004692 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004693 break;
4694 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004695 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004696 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004697 break;
4698 case ADDR_TABS:
4699 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004700 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004701 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004702 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004703 case ADDR_OTHER:
4704 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004705 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004706 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004707#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004708 // No error for value that is too big, will use the last entry.
4709 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004710 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004711#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004712 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004713 case ADDR_QUICKFIX_VALID:
4714#ifdef FEAT_QUICKFIX
4715 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4716 || eap->line2 < 0)
4717 return _(e_invrange);
4718#endif
4719 break;
4720 case ADDR_UNSIGNED:
4721 if (eap->line2 < 0)
4722 return _(e_invrange);
4723 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004724 case ADDR_NONE:
4725 // Will give an error elsewhere.
4726 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004727 }
4728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return NULL;
4730}
4731
4732/*
4733 * Correct the range for zero line number, if required.
4734 */
4735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004736correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004738 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740 if (eap->line1 == 0)
4741 eap->line1 = 1;
4742 if (eap->line2 == 0)
4743 eap->line2 = 1;
4744 }
4745}
4746
Bram Moolenaar748bf032005-02-02 23:04:36 +00004747#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004748/*
4749 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4750 * pattern. Otherwise return eap->arg.
4751 */
4752 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004753skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004754{
4755 char_u *p = eap->arg;
4756
Bram Moolenaara37420f2006-02-04 22:37:47 +00004757 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4758 || eap->cmdidx == CMD_vimgrepadd
4759 || eap->cmdidx == CMD_lvimgrepadd
4760 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004761 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004762 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004763 if (p == NULL)
4764 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004765 }
4766 return p;
4767}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004768
4769/*
4770 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4771 * in the command line, so that things like % get expanded.
4772 */
4773 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004775{
4776 char_u *new_cmdline;
4777 char_u *program;
4778 char_u *pos;
4779 char_u *ptr;
4780 int len;
4781 int i;
4782
4783 /*
4784 * Don't do it when ":vimgrep" is used for ":grep".
4785 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004786 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4787 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4788 || eap->cmdidx == CMD_grepadd
4789 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004790 && !grep_internal(eap->cmdidx))
4791 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004792 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4793 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004794 {
4795 if (*curbuf->b_p_gp == NUL)
4796 program = p_gp;
4797 else
4798 program = curbuf->b_p_gp;
4799 }
4800 else
4801 {
4802 if (*curbuf->b_p_mp == NUL)
4803 program = p_mp;
4804 else
4805 program = curbuf->b_p_mp;
4806 }
4807
4808 p = skipwhite(p);
4809
4810 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4811 {
4812 /* replace $* by given arguments */
4813 i = 1;
4814 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4815 ++i;
4816 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004817 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004818 if (new_cmdline == NULL)
4819 return NULL; /* out of memory */
4820 ptr = new_cmdline;
4821 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4822 {
4823 i = (int)(pos - program);
4824 STRNCPY(ptr, program, i);
4825 STRCPY(ptr += i, p);
4826 ptr += len;
4827 program = pos + 2;
4828 }
4829 STRCPY(ptr, program);
4830 }
4831 else
4832 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004833 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004834 if (new_cmdline == NULL)
4835 return NULL; /* out of memory */
4836 STRCPY(new_cmdline, program);
4837 STRCAT(new_cmdline, " ");
4838 STRCAT(new_cmdline, p);
4839 }
4840 msg_make(p);
4841
4842 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4843 vim_free(*cmdlinep);
4844 *cmdlinep = new_cmdline;
4845 p = new_cmdline;
4846 }
4847 return p;
4848}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004849#endif
4850
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851/*
4852 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004853 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 * Return FAIL for failure, OK otherwise.
4855 */
4856 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004857expand_filename(
4858 exarg_T *eap,
4859 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004860 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
4862 int has_wildcards; /* need to expand wildcards */
4863 char_u *repl;
4864 int srclen;
4865 char_u *p;
4866 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004867 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
Bram Moolenaar748bf032005-02-02 23:04:36 +00004869#ifdef FEAT_QUICKFIX
4870 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4871 p = skip_grep_pat(eap);
4872#else
4873 p = eap->arg;
4874#endif
4875
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 /*
4877 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4878 * the file name contains a wildcard it should not cause expanding.
4879 * (it will be expanded anyway if there is a wildcard before replacing).
4880 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004881 has_wildcards = mch_has_wildcard(p);
4882 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004884#ifdef FEAT_EVAL
4885 /* Skip over `=expr`, wildcards in it are not expanded. */
4886 if (p[0] == '`' && p[1] == '=')
4887 {
4888 p += 2;
4889 (void)skip_expr(&p);
4890 if (*p == '`')
4891 ++p;
4892 continue;
4893 }
4894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 /*
4896 * Quick check if this cannot be the start of a special string.
4897 * Also removes backslash before '%', '#' and '<'.
4898 */
4899 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4900 {
4901 ++p;
4902 continue;
4903 }
4904
4905 /*
4906 * Try to find a match at this position.
4907 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004908 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4909 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (*errormsgp != NULL) /* error detected */
4911 return FAIL;
4912 if (repl == NULL) /* no match found */
4913 {
4914 p += srclen;
4915 continue;
4916 }
4917
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004918 /* Wildcards won't be expanded below, the replacement is taken
4919 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4920 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4921 {
4922 char_u *l = repl;
4923
4924 repl = expand_env_save(repl);
4925 vim_free(l);
4926 }
4927
Bram Moolenaar63b92542007-03-27 14:57:09 +00004928 /* Need to escape white space et al. with a backslash.
4929 * Don't do this for:
4930 * - replacement that already has been escaped: "##"
4931 * - shell commands (may have to use quotes instead).
4932 * - non-unix systems when there is a single argument (spaces don't
4933 * separate arguments then).
4934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004936 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 && eap->cmdidx != CMD_grep
4939 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004940 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004941 && eap->cmdidx != CMD_lgrep
4942 && eap->cmdidx != CMD_lgrepadd
4943 && eap->cmdidx != CMD_lmake
4944 && eap->cmdidx != CMD_make
4945 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004947 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948#endif
4949 )
4950 {
4951 char_u *l;
4952#ifdef BACKSLASH_IN_FILENAME
4953 /* Don't escape a backslash here, because rem_backslash() doesn't
4954 * remove it later. */
4955 static char_u *nobslash = (char_u *)" \t\"|";
4956# define ESCAPE_CHARS nobslash
4957#else
4958# define ESCAPE_CHARS escape_chars
4959#endif
4960
4961 for (l = repl; *l; ++l)
4962 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4963 {
4964 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4965 if (l != NULL)
4966 {
4967 vim_free(repl);
4968 repl = l;
4969 }
4970 break;
4971 }
4972 }
4973
4974 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004975 if ((eap->usefilter || eap->cmdidx == CMD_bang
4976 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004977 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 char_u *l;
4980
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004981 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 if (l != NULL)
4983 {
4984 vim_free(repl);
4985 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 }
4988
4989 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4990 vim_free(repl);
4991 if (p == NULL)
4992 return FAIL;
4993 }
4994
4995 /*
4996 * One file argument: Expand wildcards.
4997 * Don't do this with ":r !command" or ":w !command".
4998 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004999 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001 /*
5002 * May do this twice:
5003 * 1. Replace environment variables.
5004 * 2. Replace any other wildcards, remove backslashes.
5005 */
5006 for (n = 1; n <= 2; ++n)
5007 {
5008 if (n == 2)
5009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 /*
5011 * Halve the number of backslashes (this is Vi compatible).
5012 * For Unix and OS/2, when wildcards are expanded, this is
5013 * done by ExpandOne() below.
5014 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005015#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (!has_wildcards)
5017#endif
5018 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020
5021 if (has_wildcards)
5022 {
5023 if (n == 1)
5024 {
5025 /*
5026 * First loop: May expand environment variables. This
5027 * can be done much faster with expand_env() than with
5028 * something else (e.g., calling a shell).
5029 * After expanding environment variables, check again
5030 * if there are still wildcards present.
5031 */
5032 if (vim_strchr(eap->arg, '$') != NULL
5033 || vim_strchr(eap->arg, '~') != NULL)
5034 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005035 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005036 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 has_wildcards = mch_has_wildcard(NameBuff);
5038 p = NameBuff;
5039 }
5040 else
5041 p = NULL;
5042 }
5043 else /* n == 2 */
5044 {
5045 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005046 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 ExpandInit(&xpc);
5049 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005050 if (p_wic)
5051 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005053 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (p == NULL)
5055 return FAIL;
5056 }
5057 if (p != NULL)
5058 {
5059 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5060 p, cmdlinep);
5061 if (n == 2) /* p came from ExpandOne() */
5062 vim_free(p);
5063 }
5064 }
5065 }
5066 }
5067 return OK;
5068}
5069
5070/*
5071 * Replace part of the command line, keeping eap->cmd, eap->arg and
5072 * eap->nextcmd correct.
5073 * "src" points to the part that is to be replaced, of length "srclen".
5074 * "repl" is the replacement string.
5075 * Returns a pointer to the character after the replaced string.
5076 * Returns NULL for failure.
5077 */
5078 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005079repl_cmdline(
5080 exarg_T *eap,
5081 char_u *src,
5082 int srclen,
5083 char_u *repl,
5084 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
5086 int len;
5087 int i;
5088 char_u *new_cmdline;
5089
5090 /*
5091 * The new command line is build in new_cmdline[].
5092 * First allocate it.
5093 * Careful: a "+cmd" argument may have been NUL terminated.
5094 */
5095 len = (int)STRLEN(repl);
5096 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005097 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005099 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 return NULL; /* out of memory! */
5101
5102 /*
5103 * Copy the stuff before the expanded part.
5104 * Copy the expanded stuff.
5105 * Copy what came after the expanded part.
5106 * Copy the next commands, if there are any.
5107 */
5108 i = (int)(src - *cmdlinep); /* length of part before match */
5109 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 mch_memmove(new_cmdline + i, repl, (size_t)len);
5112 i += len; /* remember the end of the string */
5113 STRCPY(new_cmdline + i, src + srclen);
5114 src = new_cmdline + i; /* remember where to continue */
5115
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005116 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 i = (int)STRLEN(new_cmdline) + 1;
5119 STRCPY(new_cmdline + i, eap->nextcmd);
5120 eap->nextcmd = new_cmdline + i;
5121 }
5122 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5123 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5124 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5125 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5126 vim_free(*cmdlinep);
5127 *cmdlinep = new_cmdline;
5128
5129 return src;
5130}
5131
5132/*
5133 * Check for '|' to separate commands and '"' to start comments.
5134 */
5135 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005136separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137{
5138 char_u *p;
5139
Bram Moolenaar86b68352004-12-27 21:59:20 +00005140#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005141 p = skip_grep_pat(eap);
5142#else
5143 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005144#endif
5145
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005146 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 if (*p == Ctrl_V)
5149 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005150 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 ++p; /* skip CTRL-V and next char */
5152 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005153 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005154 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (*p == NUL) /* stop at NUL after CTRL-V */
5156 break;
5157 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005158
5159#ifdef FEAT_EVAL
5160 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005161 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005162 {
5163 p += 2;
5164 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005165 }
5166#endif
5167
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 /* Check for '"': start of comment or '|': next command */
5169 /* :@" and :*" do not start a comment!
5170 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005171 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5173 || p != eap->arg)
5174 && (eap->cmdidx != CMD_redir
5175 || p != eap->arg + 1 || p[-1] != '@'))
5176 || *p == '|' || *p == '\n')
5177 {
5178 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005179 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 * AND 'b' is present in 'cpoptions'.
5181 */
5182 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005183 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005185 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 --p;
5187 }
5188 else
5189 {
5190 eap->nextcmd = check_nextcmd(p);
5191 *p = NUL;
5192 break;
5193 }
5194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005196
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005197 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 del_trailing_spaces(eap->arg);
5199}
5200
5201/*
5202 * get + command from ex argument
5203 */
5204 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005205getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
5207 char_u *arg = *argp;
5208 char_u *command = NULL;
5209
5210 if (*arg == '+') /* +[command] */
5211 {
5212 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005213 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 command = dollar_command;
5215 else
5216 {
5217 command = arg;
5218 arg = skip_cmd_arg(command, TRUE);
5219 if (*arg != NUL)
5220 *arg++ = NUL; /* terminate command with NUL */
5221 }
5222
5223 arg = skipwhite(arg); /* skip over spaces */
5224 *argp = arg;
5225 }
5226 return command;
5227}
5228
5229/*
5230 * Find end of "+command" argument. Skip over "\ " and "\\".
5231 */
5232 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005233skip_cmd_arg(
5234 char_u *p,
5235 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 while (*p && !vim_isspace(*p))
5238 {
5239 if (*p == '\\' && p[1] != NUL)
5240 {
5241 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005242 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else
5244 ++p;
5245 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005246 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248 return p;
5249}
5250
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005251 int
5252get_bad_opt(char_u *p, exarg_T *eap)
5253{
5254 if (STRICMP(p, "keep") == 0)
5255 eap->bad_char = BAD_KEEP;
5256 else if (STRICMP(p, "drop") == 0)
5257 eap->bad_char = BAD_DROP;
5258 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5259 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005260 else
5261 return FAIL;
5262 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005263}
5264
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265/*
5266 * Get "++opt=arg" argument.
5267 * Return FAIL or OK.
5268 */
5269 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005270getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271{
5272 char_u *arg = eap->arg + 2;
5273 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005274 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /* ":edit ++[no]bin[ary] file" */
5278 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5279 {
5280 if (*arg == 'n')
5281 {
5282 arg += 2;
5283 eap->force_bin = FORCE_NOBIN;
5284 }
5285 else
5286 eap->force_bin = FORCE_BIN;
5287 if (!checkforcmd(&arg, "binary", 3))
5288 return FAIL;
5289 eap->arg = skipwhite(arg);
5290 return OK;
5291 }
5292
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005293 /* ":read ++edit file" */
5294 if (STRNCMP(arg, "edit", 4) == 0)
5295 {
5296 eap->read_edit = TRUE;
5297 eap->arg = skipwhite(arg + 4);
5298 return OK;
5299 }
5300
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (STRNCMP(arg, "ff", 2) == 0)
5302 {
5303 arg += 2;
5304 pp = &eap->force_ff;
5305 }
5306 else if (STRNCMP(arg, "fileformat", 10) == 0)
5307 {
5308 arg += 10;
5309 pp = &eap->force_ff;
5310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 else if (STRNCMP(arg, "enc", 3) == 0)
5312 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005313 if (STRNCMP(arg, "encoding", 8) == 0)
5314 arg += 8;
5315 else
5316 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 pp = &eap->force_enc;
5318 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005319 else if (STRNCMP(arg, "bad", 3) == 0)
5320 {
5321 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005322 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
5325 if (pp == NULL || *arg != '=')
5326 return FAIL;
5327
5328 ++arg;
5329 *pp = (int)(arg - eap->cmd);
5330 arg = skip_cmd_arg(arg, FALSE);
5331 eap->arg = skipwhite(arg);
5332 *arg = NUL;
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (pp == &eap->force_ff)
5335 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5337 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005338 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005340 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 /* Make 'fileencoding' lower case. */
5343 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5344 *p = TOLOWER_ASC(*p);
5345 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005346 else
5347 {
5348 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5349 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005350 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005351 return FAIL;
5352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353
5354 return OK;
5355}
5356
5357/*
5358 * ":abbreviate" and friends.
5359 */
5360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005361ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
5363 do_exmap(eap, TRUE); /* almost the same as mapping */
5364}
5365
5366/*
5367 * ":map" and friends.
5368 */
5369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005370ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371{
5372 /*
5373 * If we are sourcing .exrc or .vimrc in current directory we
5374 * print the mappings for security reasons.
5375 */
5376 if (secure)
5377 {
5378 secure = 2;
5379 msg_outtrans(eap->cmd);
5380 msg_putchar('\n');
5381 }
5382 do_exmap(eap, FALSE);
5383}
5384
5385/*
5386 * ":unmap" and friends.
5387 */
5388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005389ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390{
5391 do_exmap(eap, FALSE);
5392}
5393
5394/*
5395 * ":mapclear" and friends.
5396 */
5397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005398ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399{
5400 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5401}
5402
5403/*
5404 * ":abclear" and friends.
5405 */
5406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005407ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
5409 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5410}
5411
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005416 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 * directory for security reasons.
5418 */
5419 if (secure)
5420 {
5421 secure = 2;
5422 eap->errmsg = e_curdir;
5423 }
5424 else if (eap->cmdidx == CMD_autocmd)
5425 do_autocmd(eap->arg, eap->forceit);
5426 else
5427 do_augroup(eap->arg, eap->forceit);
5428}
5429
5430/*
5431 * ":doautocmd": Apply the automatic commands to the current buffer.
5432 */
5433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005434ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005436 char_u *arg = eap->arg;
5437 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005438 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005439
Bram Moolenaar1610d052016-06-09 22:53:01 +02005440 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5441 /* Only when there is no <nomodeline>. */
5442 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005443 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446/*
5447 * :[N]bunload[!] [N] [bufname] unload buffer
5448 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5449 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5450 */
5451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005452ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005454 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005455 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 eap->errmsg = do_bufdel(
5457 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5458 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5459 : DOBUF_UNLOAD, eap->arg,
5460 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5461}
5462
5463/*
5464 * :[N]buffer [N] to buffer N
5465 * :[N]sbuffer [N] to buffer N
5466 */
5467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005470 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005471 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 if (*eap->arg)
5473 eap->errmsg = e_trailing;
5474 else
5475 {
5476 if (eap->addr_count == 0) /* default is current buffer */
5477 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5478 else
5479 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005480 if (eap->do_ecmd_cmd != NULL)
5481 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 }
5483}
5484
5485/*
5486 * :[N]bmodified [N] to next mod. buffer
5487 * :[N]sbmodified [N] to next mod. buffer
5488 */
5489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005490ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
5492 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005493 if (eap->do_ecmd_cmd != NULL)
5494 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495}
5496
5497/*
5498 * :[N]bnext [N] to next buffer
5499 * :[N]sbnext [N] split and to next buffer
5500 */
5501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005502ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005505 if (eap->do_ecmd_cmd != NULL)
5506 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507}
5508
5509/*
5510 * :[N]bNext [N] to previous buffer
5511 * :[N]bprevious [N] to previous buffer
5512 * :[N]sbNext [N] split and to previous buffer
5513 * :[N]sbprevious [N] split and to previous buffer
5514 */
5515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005516ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
5518 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005519 if (eap->do_ecmd_cmd != NULL)
5520 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522
5523/*
5524 * :brewind to first buffer
5525 * :bfirst to first buffer
5526 * :sbrewind split and to first buffer
5527 * :sbfirst split and to first buffer
5528 */
5529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
5532 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005533 if (eap->do_ecmd_cmd != NULL)
5534 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535}
5536
5537/*
5538 * :blast to last buffer
5539 * :sblast split and to last buffer
5540 */
5541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005542ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543{
5544 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005545 if (eap->do_ecmd_cmd != NULL)
5546 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005550ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
5552 return (c == NUL || c == '|' || c == '"' || c == '\n');
5553}
5554
5555#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5556 || defined(PROTO)
5557/*
5558 * Return the next command, after the first '|' or '\n'.
5559 * Return NULL if not found.
5560 */
5561 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 while (*p != '|' && *p != '\n')
5565 {
5566 if (*p == NUL)
5567 return NULL;
5568 ++p;
5569 }
5570 return (p + 1);
5571}
5572#endif
5573
5574/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005575 * Check if *p is a separator between Ex commands, skipping over white space.
5576 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 */
5578 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005579check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005581 char_u *s = skipwhite(p);
5582
5583 if (*s == '|' || *s == '\n')
5584 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 else
5586 return NULL;
5587}
5588
5589/*
5590 * - if there are more files to edit
5591 * - and this is the last window
5592 * - and forceit not used
5593 * - and not repeated twice on a row
5594 * return FAIL and give error message if 'message' TRUE
5595 * return OK otherwise
5596 */
5597 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005598check_more(
5599 int message, /* when FALSE check only, no messages */
5600 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
5602 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5603
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005604 if (!forceit && only_one_window()
5605 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 {
5607 if (message)
5608 {
5609#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5610 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5611 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005612 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005614 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5615 NGETTEXT("%d more file to edit. Quit anyway?",
5616 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5618 return OK;
5619 return FAIL;
5620 }
5621#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005622 semsg(NGETTEXT("E173: %d more file to edit",
5623 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 quitmore = 2; /* next try to quit is allowed */
5625 }
5626 return FAIL;
5627 }
5628 return OK;
5629}
5630
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005631#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632/*
5633 * Function given to ExpandGeneric() to obtain the list of command names.
5634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 return cmdnames[idx].cmd_name;
5641}
5642#endif
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
Bram Moolenaare6850792010-05-14 15:28:44 +02005647 if (*eap->arg == NUL)
5648 {
5649#ifdef FEAT_EVAL
5650 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5651 char_u *p = NULL;
5652
5653 if (expr != NULL)
5654 {
5655 ++emsg_off;
5656 p = eval_to_string(expr, NULL, FALSE);
5657 --emsg_off;
5658 vim_free(expr);
5659 }
5660 if (p != NULL)
5661 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005662 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005663 vim_free(p);
5664 }
5665 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005666 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005667#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005668 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005669#endif
5670 }
5671 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005672 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005673
5674#ifdef FEAT_VTP
5675 else if (has_vtp_working())
5676 {
5677 // background color change requires clear + redraw
5678 update_screen(CLEAR);
5679 redrawcmd();
5680 }
5681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682}
5683
5684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005685ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686{
5687 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005688 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 do_highlight(eap->arg, eap->forceit, FALSE);
5690}
5691
5692
5693/*
5694 * Call this function if we thought we were going to exit, but we won't
5695 * (because of an error). May need to restore the terminal mode.
5696 */
5697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
5700 exiting = FALSE;
5701 settmode(TMODE_RAW);
5702}
5703
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005704 static int
5705before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5706{
5707 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5708
5709 /* Bail out when autocommands closed the window.
5710 * Refuse to quit when the buffer in the last window is being closed (can
5711 * only happen in autocommands). */
5712 if (!win_valid(wp)
5713 || curbuf_locked()
5714 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5715 return TRUE;
5716
5717 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5718 {
5719 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5720 /* Refuse to quit when locked or when the buffer in the last window is
5721 * being closed (can only happen in autocommands). */
5722 if (curbuf_locked()
5723 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5724 return TRUE;
5725 }
5726
5727 return FALSE;
5728}
5729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005731 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005732 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 */
5734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005735ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005737 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005738
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739#ifdef FEAT_CMDWIN
5740 if (cmdwin_type != 0)
5741 {
5742 cmdwin_result = Ctrl_C;
5743 return;
5744 }
5745#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005746 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005747 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005748 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005749 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005750 return;
5751 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005752 if (eap->addr_count > 0)
5753 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005754 int wnr = eap->line2;
5755
5756 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5757 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005758 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005759 }
5760 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005761 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005762
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005763 /* Refuse to quit when locked. */
5764 if (curbuf_locked())
5765 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005766
5767 /* Trigger QuitPre and maybe ExitPre */
5768 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005769 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770
5771#ifdef FEAT_NETBEANS_INTG
5772 netbeansForcedQuit = eap->forceit;
5773#endif
5774
5775 /*
5776 * If there are more files or windows we won't exit.
5777 */
5778 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5779 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005780 if ((!buf_hide(wp->w_buffer)
5781 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005782 | (eap->forceit ? CCGD_FORCEIT : 0)
5783 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005785 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 {
5787 not_exiting();
5788 }
5789 else
5790 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005791 /* quit last window
5792 * Note: only_one_window() returns true, even so a help window is
5793 * still open. In that case only quit, if no address has been
5794 * specified. Example:
5795 * :h|wincmd w|1q - don't quit
5796 * :h|wincmd w|q - quit
5797 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005798 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005800 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005801#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005805 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807}
5808
5809/*
5810 * ":cquit".
5811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005813ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
5815 getout(1); /* this does not always pass on the exit code to the Manx
5816 compiler. why? */
5817}
5818
5819/*
5820 * ":qall": try to quit all windows
5821 */
5822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005823ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825# ifdef FEAT_CMDWIN
5826 if (cmdwin_type != 0)
5827 {
5828 if (eap->forceit)
5829 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5830 else
5831 cmdwin_result = K_XF2;
5832 return;
5833 }
5834# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005835
5836 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005837 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005838 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005839 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005840 return;
5841 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005842
5843 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005844 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005847 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 getout(0);
5849 not_exiting();
5850}
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852/*
5853 * ":close": close current window, unless it is the last one
5854 */
5855 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005856ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005858 win_T *win;
5859 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005860#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005862 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005864#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005865 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005866 {
5867 if (eap->addr_count == 0)
5868 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005869 else
5870 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005871 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005872 {
5873 winnr++;
5874 if (winnr == eap->line2)
5875 break;
5876 }
5877 if (win == NULL)
5878 win = lastwin;
5879 ex_win_close(eap->forceit, win, NULL);
5880 }
5881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882}
5883
Bram Moolenaar4033c552017-09-16 20:54:51 +02005884#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005885/*
5886 * ":pclose": Close any preview window.
5887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005889ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005890{
5891 win_T *win;
5892
Bram Moolenaar79648732019-07-18 21:43:07 +02005893 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005894 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895 if (win->w_p_pvw)
5896 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005897 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005898 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005899 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005900# ifdef FEAT_TEXT_PROP
5901 // Also when 'previewpopup' is empty, it might have been cleared.
5902 popup_close_preview();
5903# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005904}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005905#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005906
Bram Moolenaarf740b292006-02-16 22:11:02 +00005907/*
5908 * Close window "win" and take care of handling closing the last window for a
5909 * modified buffer.
5910 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005912ex_win_close(
5913 int forceit,
5914 win_T *win,
5915 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
5917 int need_hide;
5918 buf_T *buf = win->w_buffer;
5919
5920 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005921 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005923#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 if ((p_confirm || cmdmod.confirm) && p_write)
5925 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005926 bufref_T bufref;
5927
5928 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005930 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 return;
5932 need_hide = FALSE;
5933 }
5934 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005937 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 return;
5939 }
5940 }
5941
Bram Moolenaar4033c552017-09-16 20:54:51 +02005942#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005944#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005947 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005948 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005949 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005950 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951}
5952
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005954 * Handle the argument for a tabpage related ex command.
5955 * Returns a tabpage number.
5956 * When an error is encountered then eap->errmsg is set.
5957 */
5958 static int
5959get_tabpage_arg(exarg_T *eap)
5960{
5961 int tab_number;
5962 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5963
5964 if (eap->arg && *eap->arg != NUL)
5965 {
5966 char_u *p = eap->arg;
5967 char_u *p_save;
5968 int relative = 0; /* argument +N/-N means: go to N places to the
5969 * right/left relative to the current position. */
5970
5971 if (*p == '-')
5972 {
5973 relative = -1;
5974 p++;
5975 }
5976 else if (*p == '+')
5977 {
5978 relative = 1;
5979 p++;
5980 }
5981
5982 p_save = p;
5983 tab_number = getdigits(&p);
5984
5985 if (relative == 0)
5986 {
5987 if (STRCMP(p, "$") == 0)
5988 tab_number = LAST_TAB_NR;
5989 else if (p == p_save || *p_save == '-' || *p != NUL
5990 || tab_number > LAST_TAB_NR)
5991 {
5992 /* No numbers as argument. */
5993 eap->errmsg = e_invarg;
5994 goto theend;
5995 }
5996 }
5997 else
5998 {
5999 if (*p_save == NUL)
6000 tab_number = 1;
6001 else if (p == p_save || *p_save == '-' || *p != NUL
6002 || tab_number == 0)
6003 {
6004 /* No numbers as argument. */
6005 eap->errmsg = e_invarg;
6006 goto theend;
6007 }
6008 tab_number = tab_number * relative + tabpage_index(curtab);
6009 if (!unaccept_arg0 && relative == -1)
6010 --tab_number;
6011 }
6012 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6013 eap->errmsg = e_invarg;
6014 }
6015 else if (eap->addr_count > 0)
6016 {
6017 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006018 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006019 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006020 tab_number = 0;
6021 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006022 else
6023 {
6024 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006025 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006026 {
6027 --tab_number;
6028 if (tab_number < unaccept_arg0)
6029 eap->errmsg = e_invarg;
6030 }
6031 }
6032 }
6033 else
6034 {
6035 switch (eap->cmdidx)
6036 {
6037 case CMD_tabnext:
6038 tab_number = tabpage_index(curtab) + 1;
6039 if (tab_number > LAST_TAB_NR)
6040 tab_number = 1;
6041 break;
6042 case CMD_tabmove:
6043 tab_number = LAST_TAB_NR;
6044 break;
6045 default:
6046 tab_number = tabpage_index(curtab);
6047 }
6048 }
6049
6050theend:
6051 return tab_number;
6052}
6053
6054/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006055 * ":tabclose": close current tab page, unless it is the last one.
6056 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 */
6058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006059ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006061 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006062 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006063
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006064# ifdef FEAT_CMDWIN
6065 if (cmdwin_type != 0)
6066 cmdwin_result = K_IGNORE;
6067 else
6068# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006069 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006070 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006073 tab_number = get_tabpage_arg(eap);
6074 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006075 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006076 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006077 if (tp == NULL)
6078 {
6079 beep_flush();
6080 return;
6081 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006082 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006083 {
6084 tabpage_close_other(tp, eap->forceit);
6085 return;
6086 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006087 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006088 tabpage_close(eap->forceit);
6089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006091}
6092
6093/*
6094 * ":tabonly": close all tab pages except the current one
6095 */
6096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006097ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006098{
6099 tabpage_T *tp;
6100 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006101 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006102
6103# ifdef FEAT_CMDWIN
6104 if (cmdwin_type != 0)
6105 cmdwin_result = K_IGNORE;
6106 else
6107# endif
6108 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006109 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006110 else
6111 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006112 tab_number = get_tabpage_arg(eap);
6113 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006114 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006115 goto_tabpage(tab_number);
6116 /* Repeat this up to a 1000 times, because autocommands may
6117 * mess up the lists. */
6118 for (done = 0; done < 1000; ++done)
6119 {
6120 FOR_ALL_TABPAGES(tp)
6121 if (tp->tp_topframe != topframe)
6122 {
6123 tabpage_close_other(tp, eap->forceit);
6124 /* if we failed to close it quit */
6125 if (valid_tabpage(tp))
6126 done = 1000;
6127 /* start over, "tp" is now invalid */
6128 break;
6129 }
6130 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006131 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006132 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006133 }
6134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136
6137/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006138 * Close the current tab page.
6139 */
6140 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006141tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006142{
6143 /* First close all the windows but the current one. If that worked then
6144 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006145 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006146 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006147 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006148 ex_win_close(forceit, curwin, NULL);
6149# ifdef FEAT_GUI
6150 need_mouse_correct = TRUE;
6151# endif
6152}
6153
6154/*
6155 * Close tab page "tp", which is not the current tab page.
6156 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006157 * Also takes care of the tab pages line disappearing when closing the
6158 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006159 */
6160 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006161tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006162{
6163 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006164 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006165 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006166
6167 /* Limit to 1000 windows, autocommands may add a window while we close
6168 * one. OK, so I'm paranoid... */
6169 while (++done < 1000)
6170 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006171 wp = tp->tp_firstwin;
6172 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006173
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006174 /* Autocommands may delete the tab page under our fingers and we may
6175 * fail to close a window with a modified buffer. */
6176 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006177 break;
6178 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006179
Bram Moolenaar29323592016-07-24 22:04:11 +02006180 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006181
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006182 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006183 if (h != tabline_height())
6184 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006185}
6186
6187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 * ":only".
6189 */
6190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006191ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006193 win_T *wp;
6194 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195# ifdef FEAT_GUI
6196 need_mouse_correct = TRUE;
6197# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006198 if (eap->addr_count > 0)
6199 {
6200 wnr = eap->line2;
6201 for (wp = firstwin; --wnr > 0; )
6202 {
6203 if (wp->w_next == NULL)
6204 break;
6205 else
6206 wp = wp->w_next;
6207 }
6208 win_goto(wp);
6209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 close_others(TRUE, eap->forceit);
6211}
6212
6213/*
6214 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006215 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006217 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006218ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
6220 if (eap->addr_count == 0)
6221 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006222 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224
6225 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006226ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006228 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006229 if (!eap->skip)
6230 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006231#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006232 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006233#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006234 if (eap->addr_count == 0)
6235 win_close(curwin, FALSE); /* don't free buffer */
6236 else
6237 {
6238 int winnr = 0;
6239 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006240
Bram Moolenaar2256c992016-11-15 21:17:07 +01006241 FOR_ALL_WINDOWS(win)
6242 {
6243 winnr++;
6244 if (winnr == eap->line2)
6245 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006246 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006247 if (win == NULL)
6248 win = lastwin;
6249 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 }
6252}
6253
6254/*
6255 * ":stop" and ":suspend": Suspend Vim.
6256 */
6257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006258ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 /*
6261 * Disallow suspending for "rvim".
6262 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006263 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 {
6265 if (!eap->forceit)
6266 autowrite_all();
6267 windgoto((int)Rows - 1, 0);
6268 out_char('\n');
6269 out_flush();
6270 stoptermcap();
6271 out_flush(); /* needed for SUN to restore xterm buffer */
6272#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006273 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274#endif
6275 ui_suspend(); /* call machine specific function */
6276#ifdef FEAT_TITLE
6277 maketitle();
6278 resettitle(); /* force updating the title */
6279#endif
6280 starttermcap();
6281 scroll_start(); /* scroll screen before redrawing */
6282 redraw_later_clear();
6283 shell_resized(); /* may have resized window */
6284 }
6285}
6286
6287/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006288 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 */
6290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293#ifdef FEAT_CMDWIN
6294 if (cmdwin_type != 0)
6295 {
6296 cmdwin_result = Ctrl_C;
6297 return;
6298 }
6299#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006300 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006301 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006302 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006303 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006304 return;
6305 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006306
6307 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006308 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310 /*
6311 * if more files or windows we won't exit
6312 */
6313 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6314 exiting = TRUE;
6315 if ( ((eap->cmdidx == CMD_wq
6316 || curbufIsChanged())
6317 && do_write(eap) == FAIL)
6318 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006319 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
6321 not_exiting();
6322 }
6323 else
6324 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006327 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328# ifdef FEAT_GUI
6329 need_mouse_correct = TRUE;
6330# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006331 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006332 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 }
6334}
6335
6336/*
6337 * ":print", ":list", ":number".
6338 */
6339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006340ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006342 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006343 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006346 for ( ;!got_int; ui_breakcheck())
6347 {
6348 print_line(eap->line1,
6349 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6350 || (eap->flags & EXFLAG_NR)),
6351 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6352 if (++eap->line1 > eap->line2)
6353 break;
6354 out_flush(); /* show one line at a time */
6355 }
6356 setpcmark();
6357 /* put cursor at last line */
6358 curwin->w_cursor.lnum = eap->line2;
6359 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 }
6361
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363}
6364
6365#ifdef FEAT_BYTEOFF
6366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006367ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369 goto_byte(eap->line2);
6370}
6371#endif
6372
6373/*
6374 * ":shell".
6375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006377ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 do_shell(NULL, 0);
6380}
6381
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006382#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006384static int drop_busy = FALSE;
6385static int drop_filec;
6386static char_u **drop_filev = NULL;
6387static int drop_split;
6388static void (*drop_callback)(void *);
6389static void *drop_cookie;
6390
6391 static void
6392handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 exarg_T ea;
6395 int save_msg_scroll = msg_scroll;
6396
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006397 // Setting the argument list may cause screen updates and being called
6398 // recursively. Avoid that by setting drop_busy.
6399 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401 /* Check whether the current buffer is changed. If so, we will need
6402 * to split the current window or data could be lost.
6403 * We don't need to check if the 'hidden' option is set, as in this
6404 * case the buffer won't be lost.
6405 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006406 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 {
6408 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006409 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 --emsg_off;
6411 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006412 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 if (win_split(0, 0) == FAIL)
6415 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006416 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417
6418 /* When splitting the window, create a new alist. Otherwise the
6419 * existing one is overwritten. */
6420 alist_unlink(curwin->w_alist);
6421 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423
6424 /*
6425 * Set up the new argument list.
6426 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006427 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428
6429 /*
6430 * Move to the first file.
6431 */
6432 /* Fake up a minimal "next" command for do_argfile() */
6433 vim_memset(&ea, 0, sizeof(ea));
6434 ea.cmd = (char_u *)"next";
6435 do_argfile(&ea, 0);
6436
6437 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6438 * mode that is not needed here. */
6439 need_start_insertmode = FALSE;
6440
6441 /* Restore msg_scroll, otherwise a following command may cause scrolling
6442 * unexpectedly. The screen will be redrawn by the caller, thus
6443 * msg_scroll being set by displaying a message is irrelevant. */
6444 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006445
6446 if (drop_callback != NULL)
6447 drop_callback(drop_cookie);
6448
6449 drop_filev = NULL;
6450 drop_busy = FALSE;
6451}
6452
6453/*
6454 * Handle a file drop. The code is here because a drop is *nearly* like an
6455 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006456 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006457 * code is very similar to :args and hence needs access to a lot of the static
6458 * functions in this file.
6459 *
6460 * The "filev" list must have been allocated using alloc(), as should each item
6461 * in the list. This function takes over responsibility for freeing the "filev"
6462 * list.
6463 */
6464 void
6465handle_drop(
6466 int filec, // the number of files dropped
6467 char_u **filev, // the list of files dropped
6468 int split, // force splitting the window
6469 void (*callback)(void *), // to be called after setting the argument
6470 // list
6471 void *cookie) // argument for "callback" (allocated)
6472{
6473 // Cannot handle recursive drops, finish the pending one.
6474 if (drop_busy)
6475 {
6476 FreeWild(filec, filev);
6477 vim_free(cookie);
6478 return;
6479 }
6480
6481 // When calling handle_drop() more than once in a row we only use the last
6482 // one.
6483 if (drop_filev != NULL)
6484 {
6485 FreeWild(drop_filec, drop_filev);
6486 vim_free(drop_cookie);
6487 }
6488
6489 drop_filec = filec;
6490 drop_filev = filev;
6491 drop_split = split;
6492 drop_callback = callback;
6493 drop_cookie = cookie;
6494
6495 // Postpone this when:
6496 // - editing the command line
6497 // - not possible to change the current buffer
6498 // - updating the screen
6499 // As it may change buffers and window structures that are in use and cause
6500 // freed memory to be used.
6501 if (text_locked() || curbuf_locked() || updating_screen)
6502 return;
6503
6504 handle_drop_internal();
6505}
6506
6507/*
6508 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6509 * reset: Handle a postponed drop.
6510 */
6511 void
6512handle_any_postponed_drop(void)
6513{
6514 if (!drop_busy && drop_filev != NULL
6515 && !text_locked() && !curbuf_locked() && !updating_screen)
6516 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517}
6518#endif
6519
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520/*
6521 * Clear an argument list: free all file names and reset it to zero entries.
6522 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006524alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 while (--al->al_ga.ga_len >= 0)
6527 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6528 ga_clear(&al->al_ga);
6529}
6530
6531/*
6532 * Init an argument list.
6533 */
6534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006535alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6538}
6539
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540/*
6541 * Remove a reference from an argument list.
6542 * Ignored when the argument list is the global one.
6543 * If the argument list is no longer used by any window, free it.
6544 */
6545 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006546alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547{
6548 if (al != &global_alist && --al->al_refcount <= 0)
6549 {
6550 alist_clear(al);
6551 vim_free(al);
6552 }
6553}
6554
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555/*
6556 * Create a new argument list and use it for the current window.
6557 */
6558 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006559alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006561 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 if (curwin->w_alist == NULL)
6563 {
6564 curwin->w_alist = &global_alist;
6565 ++global_alist.al_refcount;
6566 }
6567 else
6568 {
6569 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006570 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 alist_init(curwin->w_alist);
6572 }
6573}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006575#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576/*
6577 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006578 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6579 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 */
6581 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006582alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006585 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 char_u **new_arg_files;
6587 int new_arg_file_count;
6588 char_u *save_p_su = p_su;
6589 int i;
6590
6591 /* Don't use 'suffixes' here. This should work like the shell did the
6592 * expansion. Also, the vimrc file isn't read yet, thus the user
6593 * can't set the options. */
6594 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006595 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 if (old_arg_files != NULL)
6597 {
6598 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006599 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6600 old_arg_count = GARGCOUNT;
6601 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006603 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 && new_arg_file_count > 0)
6605 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006606 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6607 TRUE, fnum_list, fnum_len);
6608 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 }
6611 p_su = save_p_su;
6612}
6613#endif
6614
6615/*
6616 * Set the argument list for the current window.
6617 * Takes over the allocated files[] and the allocated fnames in it.
6618 */
6619 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006620alist_set(
6621 alist_T *al,
6622 int count,
6623 char_u **files,
6624 int use_curbuf,
6625 int *fnum_list,
6626 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006629 static int recursive = 0;
6630
6631 if (recursive)
6632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006633 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006634 return;
6635 }
6636 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637
6638 alist_clear(al);
6639 if (ga_grow(&al->al_ga, count) == OK)
6640 {
6641 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006642 {
6643 if (got_int)
6644 {
6645 /* When adding many buffers this can take a long time. Allow
6646 * interrupting here. */
6647 while (i < count)
6648 vim_free(files[i++]);
6649 break;
6650 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006651
6652 /* May set buffer name of a buffer previously used for the
6653 * argument list, so that it's re-used by alist_add. */
6654 if (fnum_list != NULL && i < fnum_len)
6655 buf_set_name(fnum_list[i], files[i]);
6656
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006658 ui_breakcheck();
6659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 vim_free(files);
6661 }
6662 else
6663 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006666
6667 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668}
6669
6670/*
6671 * Add file "fname" to argument list "al".
6672 * "fname" must have been allocated and "al" must have been checked for room.
6673 */
6674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006675alist_add(
6676 alist_T *al,
6677 char_u *fname,
6678 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 if (fname == NULL) /* don't add NULL file names */
6681 return;
6682#ifdef BACKSLASH_IN_FILENAME
6683 slash_adjust(fname);
6684#endif
6685 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6686 if (set_fnum > 0)
6687 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6688 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6689 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690}
6691
6692#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6693/*
6694 * Adjust slashes in file names. Called after 'shellslash' was set.
6695 */
6696 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006697alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698{
6699 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006701 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
6703 for (i = 0; i < GARGCOUNT; ++i)
6704 if (GARGLIST[i].ae_fname != NULL)
6705 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006706 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 if (wp->w_alist != &global_alist)
6708 for (i = 0; i < WARGCOUNT(wp); ++i)
6709 if (WARGLIST(wp)[i].ae_fname != NULL)
6710 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711}
6712#endif
6713
6714/*
6715 * ":preserve".
6716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006718ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006720 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 ml_preserve(curbuf, TRUE);
6722}
6723
6724/*
6725 * ":recover".
6726 */
6727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006728ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6731 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006732 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6733 | CCGD_MULTWIN
6734 | (eap->forceit ? CCGD_FORCEIT : 0)
6735 | CCGD_EXCMD)
6736
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 && (*eap->arg == NUL
6738 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006739 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 recoverymode = FALSE;
6741}
6742
6743/*
6744 * Command modifier used in a wrong way.
6745 */
6746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006747ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 eap->errmsg = e_invcmd;
6750}
6751
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752/*
6753 * :sview [+command] file split window with new file, read-only
6754 * :split [[+command] file] split window with current or new file
6755 * :vsplit [[+command] file] split window vertically with current or new file
6756 * :new [[+command] file] split window with no or new file
6757 * :vnew [[+command] file] split vertically window with no or new file
6758 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006759 *
6760 * :tabedit open new Tab page with empty window
6761 * :tabedit [+command] file open new Tab page and edit "file"
6762 * :tabnew [[+command] file] just like :tabedit
6763 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 */
6765 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006766ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006768 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006769#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006771#endif
6772#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006774#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006775 int use_tab = eap->cmdidx == CMD_tabedit
6776 || eap->cmdidx == CMD_tabfind
6777 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006779 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006780 return;
6781
Bram Moolenaar4033c552017-09-16 20:54:51 +02006782#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
Bram Moolenaar4033c552017-09-16 20:54:51 +02006786#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006788 * quickfix windows. But it's OK when doing ":tab split". */
6789 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 if (eap->cmdidx == CMD_split)
6792 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 if (eap->cmdidx == CMD_vsplit)
6794 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797
Bram Moolenaar4033c552017-09-16 20:54:51 +02006798#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006799 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
6801 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6802 FNAME_MESS, TRUE, curbuf->b_ffname);
6803 if (fname == NULL)
6804 goto theend;
6805 eap->arg = fname;
6806 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006807# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006808 else
6809# endif
6810#endif
6811#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 && eap->cmdidx != CMD_new)
6815 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006816 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006817# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006818 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006819# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006820 au_has_group((char_u *)"FileExplorer"))
6821 {
6822 /* No browsing supported but we do have the file explorer:
6823 * Edit the directory. */
6824 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6825 eap->arg = (char_u *)".";
6826 }
6827 else
6828 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006829 fname = do_browse(0, (char_u *)(use_tab
6830 ? _("Edit File in new tab page")
6831 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006833 if (fname == NULL)
6834 goto theend;
6835 eap->arg = fname;
6836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006841 /*
6842 * Either open new tab page or split the window.
6843 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006844 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006845 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006846 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6847 : eap->addr_count == 0 ? 0
6848 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006849 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006850 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006851
6852 /* set the alternate buffer for the window we came from */
6853 if (curwin != old_curwin
6854 && win_valid(old_curwin)
6855 && old_curwin->w_buffer != curbuf
6856 && !cmdmod.keepalt)
6857 old_curwin->w_alt_fnum = curbuf->b_fnum;
6858 }
6859 }
6860 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6862 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 /* Reset 'scrollbind' when editing another file, but keep it when
6864 * doing ":split" without arguments. */
6865 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006866# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006870 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 else
6872 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 do_exedit(eap, old_curwin);
6874 }
6875
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006876# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006880# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881theend:
6882 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006885
6886/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006887 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006888 */
6889 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006890tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006891{
6892 exarg_T ea;
6893
6894 vim_memset(&ea, 0, sizeof(ea));
6895 ea.cmdidx = CMD_tabnew;
6896 ea.cmd = (char_u *)"tabn";
6897 ea.arg = (char_u *)"";
6898 ex_splitview(&ea);
6899}
6900
6901/*
6902 * :tabnext command
6903 */
6904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006905ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006906{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006907 int tab_number;
6908
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006909 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006910 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006911 switch (eap->cmdidx)
6912 {
6913 case CMD_tabfirst:
6914 case CMD_tabrewind:
6915 goto_tabpage(1);
6916 break;
6917 case CMD_tablast:
6918 goto_tabpage(9999);
6919 break;
6920 case CMD_tabprevious:
6921 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006922 if (eap->arg && *eap->arg != NUL)
6923 {
6924 char_u *p = eap->arg;
6925 char_u *p_save = p;
6926
6927 tab_number = getdigits(&p);
6928 if (p == p_save || *p_save == '-' || *p != NUL
6929 || tab_number == 0)
6930 {
6931 /* No numbers as argument. */
6932 eap->errmsg = e_invarg;
6933 return;
6934 }
6935 }
6936 else
6937 {
6938 if (eap->addr_count == 0)
6939 tab_number = 1;
6940 else
6941 {
6942 tab_number = eap->line2;
6943 if (tab_number < 1)
6944 {
6945 eap->errmsg = e_invrange;
6946 return;
6947 }
6948 }
6949 }
6950 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006951 break;
6952 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006953 tab_number = get_tabpage_arg(eap);
6954 if (eap->errmsg == NULL)
6955 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006956 break;
6957 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006958}
6959
6960/*
6961 * :tabmove command
6962 */
6963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006964ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006965{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006966 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006967
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006968 tab_number = get_tabpage_arg(eap);
6969 if (eap->errmsg == NULL)
6970 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006971}
6972
6973/*
6974 * :tabs command: List tabs and their contents.
6975 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006977ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006978{
6979 tabpage_T *tp;
6980 win_T *wp;
6981 int tabcount = 1;
6982
6983 msg_start();
6984 msg_scroll = TRUE;
6985 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6986 {
6987 msg_putchar('\n');
6988 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006989 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006990 out_flush(); /* output one line at a time */
6991 ui_breakcheck();
6992
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006993 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006994 wp = firstwin;
6995 else
6996 wp = tp->tp_firstwin;
6997 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6998 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006999 msg_putchar('\n');
7000 msg_putchar(wp == curwin ? '>' : ' ');
7001 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007002 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7003 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007004 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007005 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007006 else
7007 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7008 IObuff, IOSIZE, TRUE);
7009 msg_outtrans(IObuff);
7010 out_flush(); /* output one line at a time */
7011 ui_breakcheck();
7012 }
7013 }
7014}
7015
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016/*
7017 * ":mode": Set screen mode.
7018 * If no argument given, just get the screen size and redraw.
7019 */
7020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007021ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 if (*eap->arg == NUL)
7024 shell_resized();
7025 else
7026 mch_screenmode(eap->arg);
7027}
7028
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029/*
7030 * ":resize".
7031 * set, increment or decrement current window height
7032 */
7033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007034ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
7036 int n;
7037 win_T *wp = curwin;
7038
7039 if (eap->addr_count > 0)
7040 {
7041 n = eap->line2;
7042 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7043 ;
7044 }
7045
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007046# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 if (cmdmod.split & WSP_VERT)
7051 {
7052 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007053 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7055 n = 9999;
7056 win_setwidth_win((int)n, wp);
7057 }
7058 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
7060 if (*eap->arg == '-' || *eap->arg == '+')
7061 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007062 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 n = 9999;
7064 win_setheight_win((int)n, wp);
7065 }
7066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
7068/*
7069 * ":find [+command] <file>" command.
7070 */
7071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074#ifdef FEAT_SEARCHPATH
7075 char_u *fname;
7076 int count;
7077
7078 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7079 TRUE, curbuf->b_ffname);
7080 if (eap->addr_count > 0)
7081 {
7082 /* Repeat finding the file "count" times. This matters when it
7083 * appears several times in the path. */
7084 count = eap->line2;
7085 while (fname != NULL && --count > 0)
7086 {
7087 vim_free(fname);
7088 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7089 FALSE, curbuf->b_ffname);
7090 }
7091 }
7092
7093 if (fname != NULL)
7094 {
7095 eap->arg = fname;
7096#endif
7097 do_exedit(eap, NULL);
7098#ifdef FEAT_SEARCHPATH
7099 vim_free(fname);
7100 }
7101#endif
7102}
7103
7104/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007105 * ":open" simulation: for now just work like ":visual".
7106 */
7107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007108ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007109{
7110 regmatch_T regmatch;
7111 char_u *p;
7112
7113 curwin->w_cursor.lnum = eap->line2;
7114 beginline(BL_SOL | BL_FIX);
7115 if (*eap->arg == '/')
7116 {
7117 /* ":open /pattern/": put cursor in column found with pattern */
7118 ++eap->arg;
7119 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7120 *p = NUL;
7121 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7122 if (regmatch.regprog != NULL)
7123 {
7124 regmatch.rm_ic = p_ic;
7125 p = ml_get_curline();
7126 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007127 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007128 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007129 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007130 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007131 }
7132 /* Move to the NUL, ignore any other arguments. */
7133 eap->arg += STRLEN(eap->arg);
7134 }
7135 check_cursor();
7136
7137 eap->cmdidx = CMD_visual;
7138 do_exedit(eap, NULL);
7139}
7140
7141/*
7142 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 */
7144 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007145ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
7147 do_exedit(eap, NULL);
7148}
7149
7150/*
7151 * ":edit <file>" command and alikes.
7152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007154do_exedit(
7155 exarg_T *eap,
7156 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157{
7158 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007160 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007162 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007163 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 /*
7165 * ":vi" command ends Ex mode.
7166 */
7167 if (exmode_active && (eap->cmdidx == CMD_visual
7168 || eap->cmdidx == CMD_view))
7169 {
7170 exmode_active = FALSE;
7171 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007172 {
7173 /* Special case: ":global/pat/visual\NLvi-commands" */
7174 if (global_busy)
7175 {
7176 int rd = RedrawingDisabled;
7177 int nwr = no_wait_return;
7178 int ms = msg_scroll;
7179#ifdef FEAT_GUI
7180 int he = hold_gui_events;
7181#endif
7182
7183 if (eap->nextcmd != NULL)
7184 {
7185 stuffReadbuff(eap->nextcmd);
7186 eap->nextcmd = NULL;
7187 }
7188
7189 if (exmode_was != EXMODE_VIM)
7190 settmode(TMODE_RAW);
7191 RedrawingDisabled = 0;
7192 no_wait_return = 0;
7193 need_wait_return = FALSE;
7194 msg_scroll = 0;
7195#ifdef FEAT_GUI
7196 hold_gui_events = 0;
7197#endif
7198 must_redraw = CLEAR;
7199
7200 main_loop(FALSE, TRUE);
7201
7202 RedrawingDisabled = rd;
7203 no_wait_return = nwr;
7204 msg_scroll = ms;
7205#ifdef FEAT_GUI
7206 hold_gui_events = he;
7207#endif
7208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212
7213 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007214 || eap->cmdidx == CMD_tabnew
7215 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007216 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007218 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 setpcmark();
7220 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007221 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7222 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007224 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 || *eap->arg != NUL
7226#ifdef FEAT_BROWSE
7227 || cmdmod.browse
7228#endif
7229 )
7230 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007231 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7232 * can bring us here, others are stopped earlier. */
7233 if (*eap->arg != NUL && curbuf_locked())
7234 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007235
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 n = readonlymode;
7237 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7238 readonlymode = TRUE;
7239 else if (eap->cmdidx == CMD_enew)
7240 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7241 empty buffer */
7242 setpcmark();
7243 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7244 NULL, eap,
7245 /* ":edit" goes to first line if Vi compatible */
7246 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7247 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7248 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007249 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007251 /* after a split we can use an existing buffer */
7252 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007254 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
7256 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 if (old_curwin != NULL)
7258 {
7259 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007260 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007262#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007263 cleanup_T cs;
7264
7265 /* Reset the error/interrupt/exception state here so that
7266 * aborting() returns FALSE when closing a window. */
7267 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007268#endif
7269#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007271#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007272 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007273
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007274#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007275 /* Restore the error/interrupt/exception state if not
7276 * discarded by a new aborting error, interrupt, or
7277 * uncaught exception. */
7278 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 }
7281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 }
7283 else if (readonlymode && curbuf->b_nwindows == 1)
7284 {
7285 /* When editing an already visited buffer, 'readonly' won't be set
7286 * but the previous value is kept. With ":view" and ":sview" we
7287 * want the file to be readonly, except when another window is
7288 * editing the same buffer. */
7289 curbuf->b_p_ro = TRUE;
7290 }
7291 readonlymode = n;
7292 }
7293 else
7294 {
7295 if (eap->do_ecmd_cmd != NULL)
7296 do_cmdline_cmd(eap->do_ecmd_cmd);
7297#ifdef FEAT_TITLE
7298 n = curwin->w_arg_idx_invalid;
7299#endif
7300 check_arg_idx(curwin);
7301#ifdef FEAT_TITLE
7302 if (n != curwin->w_arg_idx_invalid)
7303 maketitle();
7304#endif
7305 }
7306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 /*
7308 * if ":split file" worked, set alternate file name in old window to new
7309 * file
7310 */
7311 if (old_curwin != NULL
7312 && *eap->arg != NUL
7313 && curwin != old_curwin
7314 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007315 && old_curwin->w_buffer != curbuf
7316 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
7319 ex_no_reprint = TRUE;
7320}
7321
7322#ifndef FEAT_GUI
7323/*
7324 * ":gui" and ":gvim" when there is no GUI.
7325 */
7326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007327ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
7329 eap->errmsg = e_nogvim;
7330}
7331#endif
7332
Bram Moolenaar4f974752019-02-17 17:44:42 +01007333#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007335ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337 gui_make_tearoff(eap->arg);
7338}
7339#endif
7340
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007341#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7342 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007344ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007346# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7347 if (gui.in_use)
7348 gui_make_popup(eap->arg, eap->forceit);
7349# ifdef FEAT_TERM_POPUP_MENU
7350 else
7351# endif
7352# endif
7353# ifdef FEAT_TERM_POPUP_MENU
7354 pum_make_popup(eap->arg, eap->forceit);
7355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356}
7357#endif
7358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007360ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
7362 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007363 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007365 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366}
7367
7368/*
7369 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7370 * offset.
7371 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007374ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007377 win_T *save_curwin = curwin;
7378 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 long topline;
7380 long y;
7381 linenr_T old_linenr = curwin->w_cursor.lnum;
7382
7383 setpcmark();
7384
7385 /*
7386 * determine max topline
7387 */
7388 if (curwin->w_p_scb)
7389 {
7390 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007391 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 {
7393 if (wp->w_p_scb && wp->w_buffer)
7394 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007395 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 if (topline > y)
7397 topline = y;
7398 }
7399 }
7400 if (topline < 1)
7401 topline = 1;
7402 }
7403 else
7404 {
7405 topline = 1;
7406 }
7407
7408
7409 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007410 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007412 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
7414 if (curwin->w_p_scb)
7415 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007416 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 y = topline - curwin->w_topline;
7418 if (y > 0)
7419 scrollup(y, TRUE);
7420 else
7421 scrolldown(-y, TRUE);
7422 curwin->w_scbind_pos = topline;
7423 redraw_later(VALID);
7424 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 }
7427 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007428 curwin = save_curwin;
7429 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (curwin->w_p_scb)
7431 {
7432 did_syncbind = TRUE;
7433 checkpcmark();
7434 if (old_linenr != curwin->w_cursor.lnum)
7435 {
7436 char_u ctrl_o[2];
7437
7438 ctrl_o[0] = Ctrl_O;
7439 ctrl_o[1] = 0;
7440 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7441 }
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443}
7444
7445
7446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007447ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007449 int i;
7450 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7451 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
7453 if (eap->usefilter) /* :r!cmd */
7454 do_bang(1, eap, FALSE, FALSE, TRUE);
7455 else
7456 {
7457 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7458 return;
7459
7460#ifdef FEAT_BROWSE
7461 if (cmdmod.browse)
7462 {
7463 char_u *browseFile;
7464
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007465 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 NULL, NULL, NULL, curbuf);
7467 if (browseFile != NULL)
7468 {
7469 i = readfile(browseFile, NULL,
7470 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7471 vim_free(browseFile);
7472 }
7473 else
7474 i = OK;
7475 }
7476 else
7477#endif
7478 if (*eap->arg == NUL)
7479 {
7480 if (check_fname() == FAIL) /* check for no file name */
7481 return;
7482 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7483 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7484 }
7485 else
7486 {
7487 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7488 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7489 i = readfile(eap->arg, NULL,
7490 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7491
7492 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007493 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007495#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 if (!aborting())
7497#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007498 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
7500 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007501 {
7502 if (empty && exmode_active)
7503 {
7504 /* Delete the empty line that remains. Historically ex does
7505 * this but vi doesn't. */
7506 if (eap->line2 == 0)
7507 lnum = curbuf->b_ml.ml_line_count;
7508 else
7509 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007510 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007511 {
7512 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007513 if (curwin->w_cursor.lnum > 1
7514 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007515 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007516 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007517 }
7518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 }
7522}
7523
Bram Moolenaarea408852005-06-25 22:49:46 +00007524static char_u *prev_dir = NULL;
7525
7526#if defined(EXITFREE) || defined(PROTO)
7527 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007529{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007530 VIM_CLEAR(prev_dir);
7531 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007532}
7533#endif
7534
Bram Moolenaarf4258302013-06-02 18:20:17 +02007535/*
7536 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007537 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7538 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007539 */
7540 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007541post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007542{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007543 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007544 // Clear tab local directory for both :cd and :tcd
7545 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007546 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007547 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007548 {
7549 /* If still in global directory, need to remember current
7550 * directory as global directory. */
7551 if (globaldir == NULL && prev_dir != NULL)
7552 globaldir = vim_strsave(prev_dir);
7553 /* Remember this local directory for the window. */
7554 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007555 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007556 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007557 curtab->tp_localdir = vim_strsave(NameBuff);
7558 else
7559 curwin->w_localdir = vim_strsave(NameBuff);
7560 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007561 }
7562 else
7563 {
7564 /* We are now in the global directory, no need to remember its
7565 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007566 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007567 }
7568
7569 shorten_fnames(TRUE);
7570}
7571
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007572/*
7573 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7574 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7575 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7576 * Otherwise changes the global directory.
7577 * Returns TRUE if the directory is successfully changed.
7578 */
7579 int
7580changedir_func(
7581 char_u *new_dir,
7582 int forceit,
7583 cdscope_T scope)
7584{
7585 char_u *tofree;
7586 int dir_differs;
7587 int retval = FALSE;
7588
7589 if (allbuf_locked())
7590 return FALSE;
7591
7592 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7593 {
7594 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7595 return FALSE;
7596 }
7597
7598 // ":cd -": Change to previous directory
7599 if (STRCMP(new_dir, "-") == 0)
7600 {
7601 if (prev_dir == NULL)
7602 {
7603 emsg(_("E186: No previous directory"));
7604 return FALSE;
7605 }
7606 new_dir = prev_dir;
7607 }
7608
7609 // Save current directory for next ":cd -"
7610 tofree = prev_dir;
7611 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7612 prev_dir = vim_strsave(NameBuff);
7613 else
7614 prev_dir = NULL;
7615
7616#if defined(UNIX) || defined(VMS)
7617 // for UNIX ":cd" means: go to home directory
7618 if (*new_dir == NUL)
7619 {
7620 // use NameBuff for home directory name
7621# ifdef VMS
7622 char_u *p;
7623
7624 p = mch_getenv((char_u *)"SYS$LOGIN");
7625 if (p == NULL || *p == NUL) // empty is the same as not set
7626 NameBuff[0] = NUL;
7627 else
7628 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7629# else
7630 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7631# endif
7632 new_dir = NameBuff;
7633 }
7634#endif
7635 dir_differs = new_dir == NULL || prev_dir == NULL
7636 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7637 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7638 emsg(_(e_failed));
7639 else
7640 {
7641 char_u *acmd_fname;
7642
7643 post_chdir(scope);
7644
7645 if (dir_differs)
7646 {
7647 if (scope == CDSCOPE_WINDOW)
7648 acmd_fname = (char_u *)"window";
7649 else if (scope == CDSCOPE_TABPAGE)
7650 acmd_fname = (char_u *)"tabpage";
7651 else
7652 acmd_fname = (char_u *)"global";
7653 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7654 curbuf);
7655 }
7656 retval = TRUE;
7657 }
7658 vim_free(tofree);
7659
7660 return retval;
7661}
Bram Moolenaarea408852005-06-25 22:49:46 +00007662
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007664 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007666 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007667ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007669 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670
7671 new_dir = eap->arg;
7672#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007673 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 if (*new_dir == NUL)
7675 ex_pwd(NULL);
7676 else
7677#endif
7678 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007679 cdscope_T scope = CDSCOPE_GLOBAL;
7680
7681 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7682 scope = CDSCOPE_WINDOW;
7683 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7684 scope = CDSCOPE_TABPAGE;
7685
7686 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007687 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007688 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007689 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 ex_pwd(eap);
7691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693}
7694
7695/*
7696 * ":pwd".
7697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007699ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7702 {
7703#ifdef BACKSLASH_IN_FILENAME
7704 slash_adjust(NameBuff);
7705#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007706 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 }
7708 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007709 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710}
7711
7712/*
7713 * ":=".
7714 */
7715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007716ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007718 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007719 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720}
7721
7722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007723ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007725 int n;
7726 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727
7728 if (cursor_valid())
7729 {
7730 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7731 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007732 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007734
7735 len = eap->line2;
7736 switch (*eap->arg)
7737 {
7738 case 'm': break;
7739 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007740 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007741 }
7742 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745/*
7746 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7747 */
7748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007749do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750{
7751 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007752 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
7754 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007755 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007756 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007758 wait_now = msec - done > 1000L ? 1000L : msec - done;
7759#ifdef FEAT_TIMERS
7760 {
7761 long due_time = check_due_timer();
7762
7763 if (due_time > 0 && due_time < wait_now)
7764 wait_now = due_time;
7765 }
7766#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007767#ifdef FEAT_JOB_CHANNEL
7768 if (has_any_channel() && wait_now > 100L)
7769 wait_now = 100L;
7770#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007771 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007772#ifdef FEAT_JOB_CHANNEL
7773 if (has_any_channel())
7774 ui_breakcheck_force(TRUE);
7775 else
7776#endif
7777 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007778#ifdef MESSAGE_QUEUE
7779 /* Process the netbeans and clientserver messages that may have been
7780 * received in the call to ui_breakcheck() when the GUI is in use. This
7781 * may occur when running a test case. */
7782 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007785
7786 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7787 // input buffer, otherwise a following call to input() fails.
7788 if (got_int)
7789 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790}
7791
7792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007793do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 int mode;
7796 char_u *cmdp;
7797
7798 cmdp = eap->cmd;
7799 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7800
7801 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7802 eap->arg, mode, isabbrev))
7803 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007804 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007806 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 break;
7808 }
7809}
7810
7811/*
7812 * ":winsize" command (obsolete).
7813 */
7814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007815ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817 int w, h;
7818 char_u *arg = eap->arg;
7819 char_u *p;
7820
7821 w = getdigits(&arg);
7822 arg = skipwhite(arg);
7823 p = arg;
7824 h = getdigits(&arg);
7825 if (*p != NUL && *arg == NUL)
7826 set_shellsize(w, h, TRUE);
7827 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007828 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829}
7830
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007832ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833{
7834 int xchar = NUL;
7835 char_u *p;
7836
7837 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7838 {
7839 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7840 if (eap->arg[1] == NUL)
7841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007842 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 return;
7844 }
7845 xchar = eap->arg[1];
7846 p = eap->arg + 2;
7847 }
7848 else
7849 p = eap->arg + 1;
7850
7851 eap->nextcmd = check_nextcmd(p);
7852 p = skipwhite(p);
7853 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007854 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007855 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {
7857 /* Pass flags on for ":vertical wincmd ]". */
7858 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007859 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7861 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007862 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 }
7864}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
Bram Moolenaar843ee412004-06-30 16:16:41 +00007866#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867/*
7868 * ":winpos".
7869 */
7870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
7873 int x, y;
7874 char_u *arg = eap->arg;
7875 char_u *p;
7876
7877 if (*arg == NUL)
7878 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007879# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007880# ifdef VIMDLL
7881 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7882 mch_get_winpos(&x, &y) != FAIL)
7883# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007885# else
7886 if (mch_get_winpos(&x, &y) != FAIL)
7887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {
7889 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007890 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892 else
7893# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007894 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 }
7896 else
7897 {
7898 x = getdigits(&arg);
7899 arg = skipwhite(arg);
7900 p = arg;
7901 y = getdigits(&arg);
7902 if (*p == NUL || *arg != NUL)
7903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007904 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 return;
7906 }
7907# ifdef FEAT_GUI
7908 if (gui.in_use)
7909 gui_mch_set_winpos(x, y);
7910 else if (gui.starting)
7911 {
7912 /* Remember the coordinates for when the window is opened. */
7913 gui_win_x = x;
7914 gui_win_y = y;
7915 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007916# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 else
7918# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007919# endif
7920# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007921 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922# endif
7923# ifdef HAVE_TGETENT
7924 if (*T_CWP)
7925 term_set_winpos(x, y);
7926# endif
7927 }
7928}
7929#endif
7930
7931/*
7932 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7933 */
7934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
7937 oparg_T oa;
7938
7939 clear_oparg(&oa);
7940 oa.regname = eap->regname;
7941 oa.start.lnum = eap->line1;
7942 oa.end.lnum = eap->line2;
7943 oa.line_count = eap->line2 - eap->line1 + 1;
7944 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7947 {
7948 setpcmark();
7949 curwin->w_cursor.lnum = eap->line1;
7950 beginline(BL_SOL | BL_FIX);
7951 }
7952
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007953 if (VIsual_active)
7954 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007955
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 switch (eap->cmdidx)
7957 {
7958 case CMD_delete:
7959 oa.op_type = OP_DELETE;
7960 op_delete(&oa);
7961 break;
7962
7963 case CMD_yank:
7964 oa.op_type = OP_YANK;
7965 (void)op_yank(&oa, FALSE, TRUE);
7966 break;
7967
7968 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007969 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007971 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7972#else
7973 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007975 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 oa.op_type = OP_RSHIFT;
7977 else
7978 oa.op_type = OP_LSHIFT;
7979 op_shift(&oa, FALSE, eap->amount);
7980 break;
7981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007983 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984}
7985
7986/*
7987 * ":put".
7988 */
7989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007990ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 /* ":0put" works like ":1put!". */
7993 if (eap->line2 == 0)
7994 {
7995 eap->line2 = 1;
7996 eap->forceit = TRUE;
7997 }
7998 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007999 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8000 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001}
8002
8003/*
8004 * Handle ":copy" and ":move".
8005 */
8006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008007ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 long n;
8010
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008011 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 if (eap->arg == NULL) /* error detected */
8013 {
8014 eap->nextcmd = NULL;
8015 return;
8016 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008017 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
8019 /*
8020 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8021 */
8022 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8023 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008024 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 return;
8026 }
8027
8028 if (eap->cmdidx == CMD_move)
8029 {
8030 if (do_move(eap->line1, eap->line2, n) == FAIL)
8031 return;
8032 }
8033 else
8034 ex_copy(eap->line1, eap->line2, n);
8035 u_clearline();
8036 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008037 ex_may_print(eap);
8038}
8039
8040/*
8041 * Print the current line if flags were given to the Ex command.
8042 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008043 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008044ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008045{
8046 if (eap->flags != 0)
8047 {
8048 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8049 (eap->flags & EXFLAG_LIST));
8050 ex_no_reprint = TRUE;
8051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052}
8053
8054/*
8055 * ":smagic" and ":snomagic".
8056 */
8057 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008058ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059{
8060 int magic_save = p_magic;
8061
8062 p_magic = (eap->cmdidx == CMD_smagic);
8063 do_sub(eap);
8064 p_magic = magic_save;
8065}
8066
8067/*
8068 * ":join".
8069 */
8070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072{
8073 curwin->w_cursor.lnum = eap->line1;
8074 if (eap->line1 == eap->line2)
8075 {
8076 if (eap->addr_count >= 2) /* :2,2join does nothing */
8077 return;
8078 if (eap->line2 == curbuf->b_ml.ml_line_count)
8079 {
8080 beep_flush();
8081 return;
8082 }
8083 ++eap->line2;
8084 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008085 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008087 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088}
8089
8090/*
8091 * ":[addr]@r" or ":[addr]*r": execute register
8092 */
8093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008094ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095{
8096 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008097 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098
8099 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008100 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101
8102#ifdef USE_ON_FLY_SCROLL
8103 dont_scroll = TRUE; /* disallow scrolling here */
8104#endif
8105
8106 /* get the register name. No name means to use the previous one */
8107 c = *eap->arg;
8108 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8109 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008110 /* Put the register in the typeahead buffer with the "silent" flag. */
8111 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8112 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 else
8117 {
8118 int save_efr = exec_from_reg;
8119
8120 exec_from_reg = TRUE;
8121
8122 /*
8123 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008124 * Continue until the stuff buffer is empty and all added characters
8125 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008127 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8129
8130 exec_from_reg = save_efr;
8131 }
8132}
8133
8134/*
8135 * ":!".
8136 */
8137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008138ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8141}
8142
8143/*
8144 * ":undo".
8145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008147ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008149 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008150 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008151 else
8152 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153}
8154
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008155#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008157ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008158{
8159 char_u hash[UNDO_HASH_SIZE];
8160
8161 u_compute_hash(hash);
8162 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8163}
8164
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008166ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008167{
8168 char_u hash[UNDO_HASH_SIZE];
8169
8170 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008171 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008172}
8173#endif
8174
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175/*
8176 * ":redo".
8177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008179ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180{
8181 u_redo(1);
8182}
8183
8184/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008185 * ":earlier" and ":later".
8186 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008187 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008188ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008189{
8190 long count = 0;
8191 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008192 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008193 char_u *p = eap->arg;
8194
8195 if (*p == NUL)
8196 count = 1;
8197 else if (isdigit(*p))
8198 {
8199 count = getdigits(&p);
8200 switch (*p)
8201 {
8202 case 's': ++p; sec = TRUE; break;
8203 case 'm': ++p; sec = TRUE; count *= 60; break;
8204 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008205 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8206 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008207 }
8208 }
8209
8210 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008211 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008212 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008213 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8214 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008215}
8216
8217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 * ":redir": start/stop redirection.
8219 */
8220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008221ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222{
8223 char *mode;
8224 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008225 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226
Bram Moolenaarba768492016-07-08 15:32:54 +02008227#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008228 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008230 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008231 return;
8232 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008233#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008234
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 if (STRICMP(eap->arg, "END") == 0)
8236 close_redir();
8237 else
8238 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008239 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008241 ++arg;
8242 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008244 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 mode = "a";
8246 }
8247 else
8248 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008249 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250
8251 close_redir();
8252
8253 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008254 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 if (fname == NULL)
8256 return;
8257#ifdef FEAT_BROWSE
8258 if (cmdmod.browse)
8259 {
8260 char_u *browseFile;
8261
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008262 browseFile = do_browse(BROWSE_SAVE,
8263 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008264 fname, NULL, NULL,
8265 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 if (browseFile == NULL)
8267 return; /* operation cancelled */
8268 vim_free(fname);
8269 fname = browseFile;
8270 eap->forceit = TRUE; /* since dialog already asked */
8271 }
8272#endif
8273
8274 redir_fd = open_exfile(fname, eap->forceit, mode);
8275 vim_free(fname);
8276 }
8277#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008278 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {
8280 /* redirect to a register a-z (resp. A-Z for appending) */
8281 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008282 ++arg;
8283 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008285 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008286 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008288 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008290 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008291 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008292 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008293 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008295 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008296 if (*arg == '>')
8297 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008298 /* Make register empty when not using @A-@Z and the
8299 * command is valid. */
8300 if (*arg == NUL && !isupper(redir_reg))
8301 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008303 }
8304 if (*arg != NUL)
8305 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008306 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008307 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008308 }
8309 }
8310 else if (*arg == '=' && arg[1] == '>')
8311 {
8312 int append;
8313
8314 /* redirect to a variable */
8315 close_redir();
8316 arg += 2;
8317
8318 if (*arg == '>')
8319 {
8320 ++arg;
8321 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 }
8323 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008324 append = FALSE;
8325
8326 if (var_redir_start(skipwhite(arg), append) == OK)
8327 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 }
8329#endif
8330
8331 /* TODO: redirect to a buffer */
8332
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008334 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008336
8337 /* Make sure redirection is not off. Can happen for cmdline completion
8338 * that indirectly invokes a command to catch its output. */
8339 if (redir_fd != NULL
8340#ifdef FEAT_EVAL
8341 || redir_reg || redir_vname
8342#endif
8343 )
8344 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345}
8346
8347/*
8348 * ":redraw": force redraw
8349 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008350 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008351ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352{
8353 int r = RedrawingDisabled;
8354 int p = p_lz;
8355
8356 RedrawingDisabled = 0;
8357 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008358 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008360 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361#ifdef FEAT_TITLE
8362 if (need_maketitle)
8363 maketitle();
8364#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008365#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8366# ifdef VIMDLL
8367 if (!gui.in_use)
8368# endif
8369 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 RedrawingDisabled = r;
8372 p_lz = p;
8373
8374 /* Reset msg_didout, so that a message that's there is overwritten. */
8375 msg_didout = FALSE;
8376 msg_col = 0;
8377
Bram Moolenaar56667a52013-07-17 11:54:28 +02008378 /* No need to wait after an intentional redraw. */
8379 need_wait_return = FALSE;
8380
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 out_flush();
8382}
8383
8384/*
8385 * ":redrawstatus": force redraw of status line(s)
8386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008388ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 int r = RedrawingDisabled;
8391 int p = p_lz;
8392
8393 RedrawingDisabled = 0;
8394 p_lz = FALSE;
8395 if (eap->forceit)
8396 status_redraw_all();
8397 else
8398 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008399 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 RedrawingDisabled = r;
8401 p_lz = p;
8402 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403}
8404
Bram Moolenaare12bab32019-01-08 22:02:56 +01008405/*
8406 * ":redrawtabline": force redraw of the tabline
8407 */
8408 static void
8409ex_redrawtabline(exarg_T *eap UNUSED)
8410{
8411 int r = RedrawingDisabled;
8412 int p = p_lz;
8413
8414 RedrawingDisabled = 0;
8415 p_lz = FALSE;
8416
8417 draw_tabline();
8418
8419 RedrawingDisabled = r;
8420 p_lz = p;
8421 out_flush();
8422}
8423
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008425close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
8427 if (redir_fd != NULL)
8428 {
8429 fclose(redir_fd);
8430 redir_fd = NULL;
8431 }
8432#ifdef FEAT_EVAL
8433 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008434 if (redir_vname)
8435 {
8436 var_redir_stop();
8437 redir_vname = 0;
8438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439#endif
8440}
8441
8442#if defined(FEAT_SESSION) && defined(USE_CRNL)
8443# define MKSESSION_NL
8444static int mksession_nl = FALSE; /* use NL only in put_eol() */
8445#endif
8446
8447/*
8448 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8449 */
8450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008451ex_mkrc(
8452 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453{
8454 FILE *fd;
8455 int failed = FALSE;
8456 char_u *fname;
8457#ifdef FEAT_BROWSE
8458 char_u *browseFile = NULL;
8459#endif
8460#ifdef FEAT_SESSION
8461 int view_session = FALSE;
8462 int using_vdir = FALSE; /* using 'viewdir'? */
8463 char_u *viewFile = NULL;
8464 unsigned *flagp;
8465#endif
8466
8467 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8468 {
8469#ifdef FEAT_SESSION
8470 view_session = TRUE;
8471#else
8472 ex_ni(eap);
8473 return;
8474#endif
8475 }
8476
8477#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008478 /* Use the short file name until ":lcd" is used. We also don't use the
8479 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008480 did_lcd = FALSE;
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8483 if (eap->cmdidx == CMD_mkview
8484 && (*eap->arg == NUL
8485 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8486 {
8487 eap->forceit = TRUE;
8488 fname = get_view_file(*eap->arg);
8489 if (fname == NULL)
8490 return;
8491 viewFile = fname;
8492 using_vdir = TRUE;
8493 }
8494 else
8495#endif
8496 if (*eap->arg != NUL)
8497 fname = eap->arg;
8498 else if (eap->cmdidx == CMD_mkvimrc)
8499 fname = (char_u *)VIMRC_FILE;
8500#ifdef FEAT_SESSION
8501 else if (eap->cmdidx == CMD_mksession)
8502 fname = (char_u *)SESSION_FILE;
8503#endif
8504 else
8505 fname = (char_u *)EXRC_FILE;
8506
8507#ifdef FEAT_BROWSE
8508 if (cmdmod.browse)
8509 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008510 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511# ifdef FEAT_SESSION
8512 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8513 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8514# endif
8515 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008516 fname, (char_u *)"vim", NULL,
8517 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 if (browseFile == NULL)
8519 goto theend;
8520 fname = browseFile;
8521 eap->forceit = TRUE; /* since dialog already asked */
8522 }
8523#endif
8524
8525#if defined(FEAT_SESSION) && defined(vim_mkdir)
8526 /* When using 'viewdir' may have to create the directory. */
8527 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008528 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529#endif
8530
8531 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8532 if (fd != NULL)
8533 {
8534#ifdef FEAT_SESSION
8535 if (eap->cmdidx == CMD_mkview)
8536 flagp = &vop_flags;
8537 else
8538 flagp = &ssop_flags;
8539#endif
8540
8541#ifdef MKSESSION_NL
8542 /* "unix" in 'sessionoptions': use NL line separator */
8543 if (view_session && (*flagp & SSOP_UNIX))
8544 mksession_nl = TRUE;
8545#endif
8546
8547 /* Write the version command for :mkvimrc */
8548 if (eap->cmdidx == CMD_mkvimrc)
8549 (void)put_line(fd, "version 6.0");
8550
8551#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008552 if (eap->cmdidx == CMD_mksession)
8553 {
8554 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8555 failed = TRUE;
8556 }
8557
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 if (eap->cmdidx != CMD_mkview)
8559#endif
8560 {
8561 /* Write setting 'compatible' first, because it has side effects.
8562 * For that same reason only do it when needed. */
8563 if (p_cp)
8564 (void)put_line(fd, "if !&cp | set cp | endif");
8565 else
8566 (void)put_line(fd, "if &cp | set nocp | endif");
8567 }
8568
8569#ifdef FEAT_SESSION
8570 if (!view_session
8571 || (eap->cmdidx == CMD_mksession
8572 && (*flagp & SSOP_OPTIONS)))
8573#endif
8574 failed |= (makemap(fd, NULL) == FAIL
8575 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8576
8577#ifdef FEAT_SESSION
8578 if (!failed && view_session)
8579 {
8580 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8581 failed = TRUE;
8582 if (eap->cmdidx == CMD_mksession)
8583 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008584 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
Bram Moolenaard9462e32011-04-11 21:35:11 +02008586 dirnow = alloc(MAXPATHL);
8587 if (dirnow == NULL)
8588 failed = TRUE;
8589 else
8590 {
8591 /*
8592 * Change to session file's dir.
8593 */
8594 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008596 *dirnow = NUL;
8597 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8598 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008599 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008600 shorten_fnames(TRUE);
8601 }
8602 else if (*dirnow != NUL
8603 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8604 {
8605 if (mch_chdir((char *)globaldir) == 0)
8606 shorten_fnames(TRUE);
8607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608
Bram Moolenaard9462e32011-04-11 21:35:11 +02008609 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610
Bram Moolenaard9462e32011-04-11 21:35:11 +02008611 /* restore original dir */
8612 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008614 {
8615 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008616 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008617 shorten_fnames(TRUE);
8618 }
8619 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 }
8621 }
8622 else
8623 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008624 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8625 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 }
8627 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8628 == FAIL)
8629 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008630# ifdef FEAT_SEARCH_EXTRA
8631 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8632 failed = TRUE;
8633# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008634 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8635 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008636 if (eap->cmdidx == CMD_mksession)
8637 {
8638 if (put_line(fd, "unlet SessionLoad") == FAIL)
8639 failed = TRUE;
8640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
8642#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008643 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8644 failed = TRUE;
8645
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 failed |= fclose(fd);
8647
8648 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008649 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8651 else if (eap->cmdidx == CMD_mksession)
8652 {
8653 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008654 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655
Bram Moolenaard9462e32011-04-11 21:35:11 +02008656 tbuf = alloc(MAXPATHL);
8657 if (tbuf != NULL)
8658 {
8659 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8660 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8661 vim_free(tbuf);
8662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 }
8664#endif
8665#ifdef MKSESSION_NL
8666 mksession_nl = FALSE;
8667#endif
8668 }
8669
8670#ifdef FEAT_BROWSE
8671theend:
8672 vim_free(browseFile);
8673#endif
8674#ifdef FEAT_SESSION
8675 vim_free(viewFile);
8676#endif
8677}
8678
Bram Moolenaardf177f62005-02-22 08:39:57 +00008679#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8680 || defined(PROTO)
8681 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008682vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008683{
8684 if (vim_mkdir(name, prot) != 0)
8685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008686 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008687 return FAIL;
8688 }
8689 return OK;
8690}
8691#endif
8692
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693/*
8694 * Open a file for writing for an Ex command, with some checks.
8695 * Return file descriptor, or NULL on failure.
8696 */
8697 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008698open_exfile(
8699 char_u *fname,
8700 int forceit,
8701 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 FILE *fd;
8704
8705#ifdef UNIX
8706 /* with Unix it is possible to open a directory */
8707 if (mch_isdir(fname))
8708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008709 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 return NULL;
8711 }
8712#endif
8713 if (!forceit && *mode != 'a' && vim_fexists(fname))
8714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008715 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 return NULL;
8717 }
8718
8719 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008720 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721
8722 return fd;
8723}
8724
8725/*
8726 * ":mark" and ":k".
8727 */
8728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008729ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 pos_T pos;
8732
8733 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008734 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008736 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 else
8738 {
8739 pos = curwin->w_cursor; /* save curwin->w_cursor */
8740 curwin->w_cursor.lnum = eap->line2;
8741 beginline(BL_WHITE | BL_FIX);
8742 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008743 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8745 }
8746}
8747
8748/*
8749 * Update w_topline, w_leftcol and the cursor position.
8750 */
8751 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008752update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
8754 check_cursor(); /* put cursor on valid line */
8755 update_topline();
8756 if (!curwin->w_p_wrap)
8757 validate_cursor();
8758 update_curswant();
8759}
8760
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008762 * Save the current State and go to Normal mode.
8763 * Return TRUE if the typeahead could be saved.
8764 */
8765 int
8766save_current_state(save_state_T *sst)
8767{
8768 sst->save_msg_scroll = msg_scroll;
8769 sst->save_restart_edit = restart_edit;
8770 sst->save_msg_didout = msg_didout;
8771 sst->save_State = State;
8772 sst->save_insertmode = p_im;
8773 sst->save_finish_op = finish_op;
8774 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008775 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008776
8777 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8778 restart_edit = 0; /* don't go to Insert mode */
8779 p_im = FALSE; /* don't use 'insertmode' */
8780
8781 /*
8782 * Save the current typeahead. This is required to allow using ":normal"
8783 * from an event handler and makes sure we don't hang when the argument
8784 * ends with half a command.
8785 */
8786 save_typeahead(&sst->tabuf);
8787 return sst->tabuf.typebuf_valid;
8788}
8789
8790 void
8791restore_current_state(save_state_T *sst)
8792{
8793 /* Restore the previous typeahead. */
8794 restore_typeahead(&sst->tabuf);
8795
8796 msg_scroll = sst->save_msg_scroll;
8797 restart_edit = sst->save_restart_edit;
8798 p_im = sst->save_insertmode;
8799 finish_op = sst->save_finish_op;
8800 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008801 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008802 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8803
8804 /* Restore the state (needed when called from a function executed for
8805 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8806 State = sst->save_State;
8807#ifdef CURSOR_SHAPE
8808 ui_cursor_shape(); /* may show different cursor shape */
8809#endif
8810}
8811
8812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 * ":normal[!] {commands}": Execute normal mode commands.
8814 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008815 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008816ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008818 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 char_u *arg = NULL;
8820 int l;
8821 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008823 if (ex_normal_lock > 0)
8824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008825 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008826 return;
8827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 if (ex_normal_busy >= p_mmd)
8829 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008830 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 return;
8832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 /*
8835 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8836 * this for the K_SPECIAL leading byte, otherwise special keys will not
8837 * work.
8838 */
8839 if (has_mbyte)
8840 {
8841 int len = 0;
8842
8843 /* Count the number of characters to be escaped. */
8844 for (p = eap->arg; *p != NUL; ++p)
8845 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008846#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 if (*p == CSI) /* leadbyte CSI */
8848 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008849#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008850 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008852#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 )
8856 len += 2;
8857 }
8858 if (len > 0)
8859 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008860 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 if (arg != NULL)
8862 {
8863 len = 0;
8864 for (p = eap->arg; *p != NUL; ++p)
8865 {
8866 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008867#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 if (*p == CSI)
8869 {
8870 arg[len++] = KS_EXTRA;
8871 arg[len++] = (int)KE_CSI;
8872 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008873#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008874 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 {
8876 arg[len++] = *++p;
8877 if (*p == K_SPECIAL)
8878 {
8879 arg[len++] = KS_SPECIAL;
8880 arg[len++] = KE_FILLER;
8881 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008882#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 else if (*p == CSI)
8884 {
8885 arg[len++] = KS_EXTRA;
8886 arg[len++] = (int)KE_CSI;
8887 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 }
8890 arg[len] = NUL;
8891 }
8892 }
8893 }
8894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008896 ++ex_normal_busy;
8897 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 {
8899 /*
8900 * Repeat the :normal command for each line in the range. When no
8901 * range given, execute it just once, without positioning the cursor
8902 * first.
8903 */
8904 do
8905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 if (eap->addr_count != 0)
8907 {
8908 curwin->w_cursor.lnum = eap->line1++;
8909 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008910 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 }
8912
Bram Moolenaar13505972019-01-24 15:04:48 +01008913 exec_normal_cmd(arg != NULL
8914 ? arg
8915 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 }
8917 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8918 }
8919
8920 /* Might not return to the main loop when in an event handler. */
8921 update_topline_cursor();
8922
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008923 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008925#ifdef FEAT_MOUSE
8926 setmouse();
8927#endif
8928#ifdef CURSOR_SHAPE
8929 ui_cursor_shape(); /* may show different cursor shape */
8930#endif
8931
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933}
8934
8935/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008936 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 */
8938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008939ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008941 if (eap->forceit)
8942 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008943 /* cursor line can be zero on startup */
8944 if (!curwin->w_cursor.lnum)
8945 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008946 coladvance((colnr_T)MAXCOL);
8947 curwin->w_curswant = MAXCOL;
8948 curwin->w_set_curswant = FALSE;
8949 }
8950
Bram Moolenaara40c5002005-01-09 21:16:21 +00008951 /* Ignore the command when already in Insert mode. Inserting an
8952 * expression register that invokes a function can do this. */
8953 if (State & INSERT)
8954 return;
8955
Bram Moolenaar64969662005-12-14 21:59:55 +00008956 if (eap->cmdidx == CMD_startinsert)
8957 restart_edit = 'a';
8958 else if (eap->cmdidx == CMD_startreplace)
8959 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008961 restart_edit = 'V';
8962
8963 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008965 if (eap->cmdidx == CMD_startinsert)
8966 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 curwin->w_curswant = 0; /* avoid MAXCOL */
8968 }
8969}
8970
8971/*
8972 * ":stopinsert"
8973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008975ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 restart_edit = 0;
8978 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008979 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008982/*
8983 * Execute normal mode command "cmd".
8984 * "remap" can be REMAP_NONE or REMAP_YES.
8985 */
8986 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008987exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008988{
Bram Moolenaar25281632016-01-21 23:32:32 +01008989 /* Stuff the argument into the typeahead buffer. */
8990 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008991 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008992}
Bram Moolenaar25281632016-01-21 23:32:32 +01008993
Bram Moolenaar25281632016-01-21 23:32:32 +01008994/*
8995 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008996 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008997 */
8998 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008999exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01009000{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009001 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02009002 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009003
Bram Moolenaar905dd902019-04-07 14:21:47 +02009004 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9005 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009006 clear_oparg(&oa);
9007 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009008 while ((!stuff_empty()
9009 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009010 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009011 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009012 {
9013 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009014#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009015 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009016 && oa.op_type == OP_NOP && oa.regname == NUL
9017 && !VIsual_active)
9018 {
9019 /* If terminal_loop() returns OK we got a key that is handled
9020 * in Normal model. With FAIL we first need to position the
9021 * cursor and the screen needs to be redrawn. */
9022 if (terminal_loop(TRUE) == OK)
9023 normal_cmd(&oa, TRUE);
9024 }
9025 else
9026#endif
9027 /* execute a Normal mode cmd */
9028 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009029 }
9030}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009031
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032#ifdef FEAT_FIND_ID
9033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009034ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035{
9036 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9037 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9038 (linenr_T)1, (linenr_T)MAXLNUM);
9039}
9040
Bram Moolenaar4033c552017-09-16 20:54:51 +02009041#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042/*
9043 * ":psearch"
9044 */
9045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009046ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 g_do_tagpreview = p_pvh;
9049 ex_findpat(eap);
9050 g_do_tagpreview = 0;
9051}
9052#endif
9053
9054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009055ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056{
9057 int whole = TRUE;
9058 long n;
9059 char_u *p;
9060 int action;
9061
9062 switch (cmdnames[eap->cmdidx].cmd_name[2])
9063 {
9064 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9065 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9066 action = ACTION_GOTO;
9067 else
9068 action = ACTION_SHOW;
9069 break;
9070 case 'i': /* ":ilist" and ":dlist" */
9071 action = ACTION_SHOW_ALL;
9072 break;
9073 case 'u': /* ":ijump" and ":djump" */
9074 action = ACTION_GOTO;
9075 break;
9076 default: /* ":isplit" and ":dsplit" */
9077 action = ACTION_SPLIT;
9078 break;
9079 }
9080
9081 n = 1;
9082 if (vim_isdigit(*eap->arg)) /* get count */
9083 {
9084 n = getdigits(&eap->arg);
9085 eap->arg = skipwhite(eap->arg);
9086 }
9087 if (*eap->arg == '/') /* Match regexp, not just whole words */
9088 {
9089 whole = FALSE;
9090 ++eap->arg;
9091 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9092 if (*p)
9093 {
9094 *p++ = NUL;
9095 p = skipwhite(p);
9096
9097 /* Check for trailing illegal characters */
9098 if (!ends_excmd(*p))
9099 eap->errmsg = e_trailing;
9100 else
9101 eap->nextcmd = check_nextcmd(p);
9102 }
9103 }
9104 if (!eap->skip)
9105 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9106 whole, !eap->forceit,
9107 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9108 n, action, eap->line1, eap->line2);
9109}
9110#endif
9111
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112
Bram Moolenaar4033c552017-09-16 20:54:51 +02009113#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114/*
9115 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9116 */
9117 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009118ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009120 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9122}
9123
9124/*
9125 * ":pedit"
9126 */
9127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009128ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130 win_T *curwin_save = curwin;
9131
9132 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009133 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009134 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 do_exedit(eap, NULL);
9136 keep_help_flag = FALSE;
9137 if (curwin != curwin_save && win_valid(curwin_save))
9138 {
9139 /* Return cursor to where we were */
9140 validate_cursor();
9141 redraw_later(VALID);
9142 win_enter(curwin_save, TRUE);
9143 }
9144 g_do_tagpreview = 0;
9145}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147
9148/*
9149 * ":stag", ":stselect" and ":stjump".
9150 */
9151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009152ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153{
9154 postponed_split = -1;
9155 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009156 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9158 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009159 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
9162/*
9163 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9164 */
9165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009166ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167{
9168 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9169}
9170
9171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009172ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173{
9174 int cmd;
9175
9176 switch (name[1])
9177 {
9178 case 'j': cmd = DT_JUMP; /* ":tjump" */
9179 break;
9180 case 's': cmd = DT_SELECT; /* ":tselect" */
9181 break;
9182 case 'p': cmd = DT_PREV; /* ":tprevious" */
9183 break;
9184 case 'N': cmd = DT_PREV; /* ":tNext" */
9185 break;
9186 case 'n': cmd = DT_NEXT; /* ":tnext" */
9187 break;
9188 case 'o': cmd = DT_POP; /* ":pop" */
9189 break;
9190 case 'f': /* ":tfirst" */
9191 case 'r': cmd = DT_FIRST; /* ":trewind" */
9192 break;
9193 case 'l': cmd = DT_LAST; /* ":tlast" */
9194 break;
9195 default: /* ":tag" */
9196#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009197 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009199 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 return;
9201 }
9202#endif
9203 cmd = DT_TAG;
9204 break;
9205 }
9206
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009207 if (name[0] == 'l')
9208 {
9209#ifndef FEAT_QUICKFIX
9210 ex_ni(eap);
9211 return;
9212#else
9213 cmd = DT_LTAG;
9214#endif
9215 }
9216
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9218 eap->forceit, TRUE);
9219}
9220
9221/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009222 * Check "str" for starting with a special cmdline variable.
9223 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9224 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9225 */
9226 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009227find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009228{
9229 int len;
9230 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009231 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009232 "%",
9233#define SPEC_PERC 0
9234 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009235#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009236 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009237#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009238 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009239#define SPEC_CCWORD (SPEC_CWORD + 1)
9240 "<cexpr>", /* expr under cursor */
9241#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009242 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009243#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009244 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009245#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009246 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009247#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009248 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009249#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009250 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009251#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009252 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009253#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009254 "<sflnum>", /* script file line number */
9255#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009256#ifdef FEAT_CLIENTSERVER
9257 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009258# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009259#endif
9260 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009261
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009262 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009263 {
9264 len = (int)STRLEN(spec_str[i]);
9265 if (STRNCMP(src, spec_str[i], len) == 0)
9266 {
9267 *usedlen = len;
9268 return i;
9269 }
9270 }
9271 return -1;
9272}
9273
9274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 * Evaluate cmdline variables.
9276 *
9277 * change '%' to curbuf->b_ffname
9278 * '#' to curwin->w_altfile
9279 * '<cword>' to word under the cursor
9280 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02009281 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 * '<cfile>' to path name under the cursor
9283 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009284 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285 * '<afile>' to file name for autocommand
9286 * '<abuf>' to buffer number for autocommand
9287 * '<amatch>' to matching name for autocommand
9288 *
9289 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9290 * "" for error without a message) and NULL is returned.
9291 * Returns an allocated string if a valid match was found.
9292 * Returns NULL if no match was found. "usedlen" then still contains the
9293 * number of characters to skip.
9294 */
9295 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009296eval_vars(
9297 char_u *src, /* pointer into commandline */
9298 char_u *srcstart, /* beginning of valid memory for src */
9299 int *usedlen, /* characters after src that are used */
9300 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009301 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009302 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009303 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304{
9305 int i;
9306 char_u *s;
9307 char_u *result;
9308 char_u *resultbuf = NULL;
9309 int resultlen;
9310 buf_T *buf;
9311 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9312 int spec_idx;
9313#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009314 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 int skip_mod = FALSE;
9316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318
9319 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009320 if (escaped != NULL)
9321 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322
9323 /*
9324 * Check if there is something to do.
9325 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009326 spec_idx = find_cmdline_var(src, usedlen);
9327 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 {
9329 *usedlen = 1;
9330 return NULL;
9331 }
9332
9333 /*
9334 * Skip when preceded with a backslash "\%" and "\#".
9335 * Note: In "\\%" the % is also not recognized!
9336 */
9337 if (src > srcstart && src[-1] == '\\')
9338 {
9339 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009340 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 return NULL;
9342 }
9343
9344 /*
9345 * word or WORD under cursor
9346 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009347 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9348 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009350 resultlen = find_ident_under_cursor(&result,
9351 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9352 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9353 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (resultlen == 0)
9355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009356 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 return NULL;
9358 }
9359 }
9360
9361 /*
9362 * '#': Alternate file name
9363 * '%': Current file name
9364 * File name under the cursor
9365 * File name for autocommand
9366 * and following modifiers
9367 */
9368 else
9369 {
9370 switch (spec_idx)
9371 {
9372 case SPEC_PERC: /* '%': current file */
9373 if (curbuf->b_fname == NULL)
9374 {
9375 result = (char_u *)"";
9376 valid = 0; /* Must have ":p:h" to be valid */
9377 }
9378 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009381#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009382 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009383#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 break;
9386
9387 case SPEC_HASH: /* '#' or "#99": alternate file */
9388 if (src[1] == '#') /* "##": the argument list */
9389 {
9390 result = arg_all();
9391 resultbuf = result;
9392 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009393 if (escaped != NULL)
9394 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#ifdef FEAT_MODIFY_FNAME
9396 skip_mod = TRUE;
9397#endif
9398 break;
9399 }
9400 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009401 if (*s == '<') /* "#<99" uses v:oldfiles */
9402 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009404 if (s == src + 2 && src[1] == '-')
9405 /* just a minus sign, don't skip over it */
9406 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 *usedlen = (int)(s - src); /* length of what we expand */
9408
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009409 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009411 if (*usedlen < 2)
9412 {
9413 /* Should we give an error message for #<text? */
9414 *usedlen = 1;
9415 return NULL;
9416 }
9417#ifdef FEAT_EVAL
9418 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9419 (long)i);
9420 if (result == NULL)
9421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009422 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009423 return NULL;
9424 }
9425#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009426 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 }
9430 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009431 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009432 if (i == 0 && src[1] == '<' && *usedlen > 1)
9433 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009434 buf = buflist_findnr(i);
9435 if (buf == NULL)
9436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009437 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009438 return NULL;
9439 }
9440 if (lnump != NULL)
9441 *lnump = ECMD_LAST;
9442 if (buf->b_fname == NULL)
9443 {
9444 result = (char_u *)"";
9445 valid = 0; /* Must have ":p:h" to be valid */
9446 }
9447 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009448 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009449 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009450#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009451 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009452#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009453 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 break;
9456
9457#ifdef FEAT_SEARCHPATH
9458 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009459 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 if (result == NULL)
9461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009462 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 return NULL;
9464 }
9465 resultbuf = result; /* remember allocated string */
9466 break;
9467#endif
9468
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469 case SPEC_AFILE: /* file name for autocommand */
9470 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009471 if (result != NULL && !autocmd_fname_full)
9472 {
9473 /* Still need to turn the fname into a full path. It is
9474 * postponed to avoid a delay when <afile> is not used. */
9475 autocmd_fname_full = TRUE;
9476 result = FullName_save(autocmd_fname, FALSE);
9477 vim_free(autocmd_fname);
9478 autocmd_fname = result;
9479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009480 if (result == NULL)
9481 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009482 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 return NULL;
9484 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009485 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 break;
9487
9488 case SPEC_ABUF: /* buffer number for autocommand */
9489 if (autocmd_bufnr <= 0)
9490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009491 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 return NULL;
9493 }
9494 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9495 result = strbuf;
9496 break;
9497
9498 case SPEC_AMATCH: /* match name for autocommand */
9499 result = autocmd_match;
9500 if (result == NULL)
9501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009502 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 return NULL;
9504 }
9505 break;
9506
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 case SPEC_SFILE: /* file name for ":so" command */
9508 result = sourcing_name;
9509 if (result == NULL)
9510 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009511 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 return NULL;
9513 }
9514 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009515
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009516 case SPEC_SLNUM: /* line in file for ":so" command */
9517 if (sourcing_name == NULL || sourcing_lnum == 0)
9518 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009519 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009520 return NULL;
9521 }
9522 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9523 result = strbuf;
9524 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009525
9526#ifdef FEAT_EVAL
9527 case SPEC_SFLNUM: /* line in script file */
9528 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9529 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009530 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009531 return NULL;
9532 }
9533 sprintf((char *)strbuf, "%ld",
9534 (long)(current_sctx.sc_lnum + sourcing_lnum));
9535 result = strbuf;
9536 break;
9537#endif
9538
9539#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009541 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9542 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 result = strbuf;
9544 break;
9545#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009546
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009547 default:
9548 result = (char_u *)""; /* avoid gcc warning */
9549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 }
9551
9552 resultlen = (int)STRLEN(result); /* length of new string */
9553 if (src[*usedlen] == '<') /* remove the file name extension */
9554 {
9555 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 resultlen = (int)(s - result);
9558 }
9559#ifdef FEAT_MODIFY_FNAME
9560 else if (!skip_mod)
9561 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009562 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 &resultlen);
9564 if (result == NULL)
9565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009566 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 return NULL;
9568 }
9569 }
9570#endif
9571 }
9572
9573 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9574 {
9575 if (valid != VALID_HEAD + VALID_PATH)
9576 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009577 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009579 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 result = NULL;
9581 }
9582 else
9583 result = vim_strnsave(result, resultlen);
9584 vim_free(resultbuf);
9585 return result;
9586}
9587
9588/*
9589 * Concatenate all files in the argument list, separated by spaces, and return
9590 * it in one allocated string.
9591 * Spaces and backslashes in the file names are escaped with a backslash.
9592 * Returns NULL when out of memory.
9593 */
9594 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009595arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597 int len;
9598 int idx;
9599 char_u *retval = NULL;
9600 char_u *p;
9601
9602 /*
9603 * Do this loop two times:
9604 * first time: compute the total length
9605 * second time: concatenate the names
9606 */
9607 for (;;)
9608 {
9609 len = 0;
9610 for (idx = 0; idx < ARGCOUNT; ++idx)
9611 {
9612 p = alist_name(&ARGLIST[idx]);
9613 if (p != NULL)
9614 {
9615 if (len > 0)
9616 {
9617 /* insert a space in between names */
9618 if (retval != NULL)
9619 retval[len] = ' ';
9620 ++len;
9621 }
9622 for ( ; *p != NUL; ++p)
9623 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009624 if (*p == ' '
9625#ifndef BACKSLASH_IN_FILENAME
9626 || *p == '\\'
9627#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009628 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 {
9630 /* insert a backslash */
9631 if (retval != NULL)
9632 retval[len] = '\\';
9633 ++len;
9634 }
9635 if (retval != NULL)
9636 retval[len] = *p;
9637 ++len;
9638 }
9639 }
9640 }
9641
9642 /* second time: break here */
9643 if (retval != NULL)
9644 {
9645 retval[len] = NUL;
9646 break;
9647 }
9648
9649 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009650 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 if (retval == NULL)
9652 break;
9653 }
9654
9655 return retval;
9656}
9657
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658/*
9659 * Expand the <sfile> string in "arg".
9660 *
9661 * Returns an allocated string, or NULL for any error.
9662 */
9663 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009664expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009666 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 int len;
9668 char_u *result;
9669 char_u *newres;
9670 char_u *repl;
9671 int srclen;
9672 char_u *p;
9673
9674 result = vim_strsave(arg);
9675 if (result == NULL)
9676 return NULL;
9677
9678 for (p = result; *p; )
9679 {
9680 if (STRNCMP(p, "<sfile>", 7) != 0)
9681 ++p;
9682 else
9683 {
9684 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009685 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 if (errormsg != NULL)
9687 {
9688 if (*errormsg)
9689 emsg(errormsg);
9690 vim_free(result);
9691 return NULL;
9692 }
9693 if (repl == NULL) /* no match (cannot happen) */
9694 {
9695 p += srclen;
9696 continue;
9697 }
9698 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9699 newres = alloc(len);
9700 if (newres == NULL)
9701 {
9702 vim_free(repl);
9703 vim_free(result);
9704 return NULL;
9705 }
9706 mch_memmove(newres, result, (size_t)(p - result));
9707 STRCPY(newres + (p - result), repl);
9708 len = (int)STRLEN(newres);
9709 STRCAT(newres, p + srclen);
9710 vim_free(repl);
9711 vim_free(result);
9712 result = newres;
9713 p = newres + len; /* continue after the match */
9714 }
9715 }
9716
9717 return result;
9718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719
9720#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009721static int ses_winsizes(FILE *fd, int restore_size,
9722 win_T *tab_firstwin);
9723static int ses_win_rec(FILE *fd, frame_T *fr);
9724static frame_T *ses_skipframe(frame_T *fr);
9725static int ses_do_frame(frame_T *fr);
9726static int ses_do_win(win_T *wp);
9727static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9728static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009729static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
9731/*
9732 * Write openfile commands for the current buffers to an .exrc file.
9733 * Return FAIL on error, OK otherwise.
9734 */
9735 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009736makeopens(
9737 FILE *fd,
9738 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740 buf_T *buf;
9741 int only_save_windows = TRUE;
9742 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 int restore_size = TRUE;
9744 win_T *wp;
9745 char_u *sname;
9746 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009747 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009748 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009749 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009750 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009751 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009752 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753
9754 if (ssop_flags & SSOP_BUFFERS)
9755 only_save_windows = FALSE; /* Save ALL buffers */
9756
9757 /*
9758 * Begin by setting the this_session variable, and then other
9759 * sessionable variables.
9760 */
9761#ifdef FEAT_EVAL
9762 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9763 return FAIL;
9764 if (ssop_flags & SSOP_GLOBALS)
9765 if (store_session_globals(fd) == FAIL)
9766 return FAIL;
9767#endif
9768
9769 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009770 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 */
9772 if (put_line(fd, "silent only") == FAIL)
9773 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009774 if ((ssop_flags & SSOP_TABPAGES)
9775 && put_line(fd, "silent tabonly") == FAIL)
9776 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777
9778 /*
9779 * Now a :cd command to the session directory or the current directory
9780 */
9781 if (ssop_flags & SSOP_SESDIR)
9782 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009783 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9784 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 return FAIL;
9786 }
9787 else if (ssop_flags & SSOP_CURDIR)
9788 {
9789 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9790 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009791 || fputs("cd ", fd) < 0
9792 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9793 || put_eol(fd) == FAIL)
9794 {
9795 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798 vim_free(sname);
9799 }
9800
9801 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009802 * If there is an empty, unnamed buffer we will wipe it out later.
9803 * Remember the buffer number.
9804 */
9805 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9806 return FAIL;
9807 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9808 return FAIL;
9809 if (put_line(fd, "endif") == FAIL)
9810 return FAIL;
9811
9812 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 * Now save the current files, current buffer first.
9814 */
9815 if (put_line(fd, "set shortmess=aoO") == FAIL)
9816 return FAIL;
9817
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009819 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9821 return FAIL;
9822
9823 if (ssop_flags & SSOP_RESIZE)
9824 {
9825 /* Note: after the restore we still check it worked!*/
9826 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9827 || put_eol(fd) == FAIL)
9828 return FAIL;
9829 }
9830
9831#ifdef FEAT_GUI
9832 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9833 {
9834 int x, y;
9835
9836 if (gui_mch_get_winpos(&x, &y) == OK)
9837 {
9838 /* Note: after the restore we still check it worked!*/
9839 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9840 return FAIL;
9841 }
9842 }
9843#endif
9844
9845 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009846 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9847 * will be displayed when creating the next tab. That resizes the windows
9848 * in the first tab, which may cause problems. Set 'showtabline' to 2
9849 * temporarily to avoid that.
9850 */
9851 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9852 {
9853 if (put_line(fd, "set stal=2") == FAIL)
9854 return FAIL;
9855 restore_stal = TRUE;
9856 }
9857
9858 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009859 * May repeat putting Windows for each tab, when "tabpages" is in
9860 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009861 * Don't use goto_tabpage(), it may change directory and trigger
9862 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009864 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009865 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009866 if ((ssop_flags & SSOP_TABPAGES))
9867 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009868 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009869
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009870 // Similar to ses_win_rec() below, populate the tab pages first so
9871 // later local options won't be copied to the new tabs.
9872 FOR_ALL_TABPAGES(tp)
9873 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009874 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009875 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009876 return FAIL;
9877 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009878 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009880 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009881 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009882 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009883
Bram Moolenaar18144c82006-04-12 21:52:12 +00009884 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009886 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009887
9888 if (tp == NULL)
9889 break; /* done all tab pages */
9890 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009891 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009892 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009893 tab_topframe = topframe;
9894 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009895 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009896 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009897 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009898 tab_topframe = tp->tp_topframe;
9899 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009900 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009901 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
Bram Moolenaar18144c82006-04-12 21:52:12 +00009904 /*
9905 * Before creating the window layout, try loading one file. If this
9906 * is aborted we don't end up with a number of useless windows.
9907 * This may have side effects! (e.g., compressed or network file).
9908 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009909 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009910 {
9911 if (ses_do_win(wp)
9912 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009913 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009914#ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +02009915 && !bt_nofilename(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009916#endif
9917 )
9918 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009919 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9920 return FAIL;
9921 need_tabnext = FALSE;
9922
9923 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009924 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9925 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009926 return FAIL;
9927 if (!wp->w_arg_idx_invalid)
9928 edited_win = wp;
9929 break;
9930 }
9931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009933 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009934 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009935 return FAIL;
9936
Bram Moolenaar18144c82006-04-12 21:52:12 +00009937 /*
9938 * Save current window layout.
9939 */
9940 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009942 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009944 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9945 return FAIL;
9946 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9947 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948
Bram Moolenaar18144c82006-04-12 21:52:12 +00009949 /*
9950 * Check if window sizes can be restored (no windows omitted).
9951 * Remember the window number of the current window after restoring.
9952 */
9953 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009954 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009955 {
9956 if (ses_do_win(wp))
9957 ++nr;
9958 else
9959 restore_size = FALSE;
9960 if (curwin == wp)
9961 cnr = nr;
9962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963
Bram Moolenaar18144c82006-04-12 21:52:12 +00009964 /* Go to the first window. */
9965 if (put_line(fd, "wincmd t") == FAIL)
9966 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009967
Bram Moolenaar18144c82006-04-12 21:52:12 +00009968 /*
9969 * If more than one window, see if sizes can be restored.
9970 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9971 * resized when moving between windows.
9972 * Do this before restoring the view, so that the topline and the
9973 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009974 * winminheight and winminwidth need to be set to avoid an error if the
9975 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009976 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009977 if (put_line(fd, "set winminheight=0") == FAIL
9978 || put_line(fd, "set winheight=1") == FAIL
9979 || put_line(fd, "set winminwidth=0") == FAIL
9980 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009981 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009982 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009983 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009985 // Restore the tab-local working directory if specified
9986 // Do this before the windows, so that the window-local directory can
9987 // override the tab-local directory.
9988 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9989 {
9990 if (fputs("tcd ", fd) < 0
9991 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9992 || put_eol(fd) == FAIL)
9993 return FAIL;
9994 did_lcd = TRUE;
9995 }
9996
Bram Moolenaar18144c82006-04-12 21:52:12 +00009997 /*
9998 * Restore the view of the window (options, file, cursor, etc.).
9999 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010000 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010001 {
10002 if (!ses_do_win(wp))
10003 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010004 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10005 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010006 return FAIL;
10007 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10008 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010009 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010010 }
10011
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010012 /* The argument index in the first tab page is zero, need to set it in
10013 * each window. For further tab pages it's the window where we do
10014 * "tabedit". */
10015 cur_arg_idx = next_arg_idx;
10016
Bram Moolenaar18144c82006-04-12 21:52:12 +000010017 /*
10018 * Restore cursor to the current window if it's not the first one.
10019 */
10020 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10021 || put_eol(fd) == FAIL))
10022 return FAIL;
10023
10024 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010025 * Restore window sizes again after jumping around in windows, because
10026 * the current window has a minimum size while others may not.
10027 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010028 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010029 return FAIL;
10030
Bram Moolenaar18144c82006-04-12 21:52:12 +000010031 /* Don't continue in another tab page when doing only the current one
10032 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010033 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010034 break;
10035 }
10036
10037 if (ssop_flags & SSOP_TABPAGES)
10038 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010039 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10040 || put_eol(fd) == FAIL)
10041 return FAIL;
10042 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010043 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10044 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010045
Bram Moolenaard39e2752019-01-26 20:07:38 +010010046 // Now put the remaining buffers into the buffer list.
10047 // This is near the end, so that when 'hidden' is set we don't create extra
10048 // buffers. If the buffer was already created with another command the
10049 // ":badd" will have no effect.
10050 FOR_ALL_BUFFERS(buf)
10051 {
10052 if (!(only_save_windows && buf->b_nwindows == 0)
10053 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10054#ifdef FEAT_TERMINAL
10055 // Skip terminal buffers: finished ones are not useful, others
10056 // will be resurrected and result in a new buffer.
10057 && !bt_terminal(buf)
10058#endif
10059 && buf->b_fname != NULL
10060 && buf->b_p_bl)
10061 {
10062 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10063 : buf->b_wininfo->wi_fpos.lnum) < 0
10064 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
10065 return FAIL;
10066 }
10067 }
10068
Bram Moolenaar9c102382006-05-03 21:26:49 +000010069 /*
10070 * Wipe out an empty unnamed buffer we started in.
10071 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020010072 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010073 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010074 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010075 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010076 return FAIL;
10077 if (put_line(fd, "endif") == FAIL)
10078 return FAIL;
10079 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10080 return FAIL;
10081
10082 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10083 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10084 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10085 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010010086 /* Re-apply 'winminheight' and 'winminwidth'. */
10087 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
10088 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
10089 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090
10091 /*
10092 * Lastly, execute the x.vim file if it exists.
10093 */
10094 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10095 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010096 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 || put_line(fd, "endif") == FAIL)
10098 return FAIL;
10099
10100 return OK;
10101}
10102
10103 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010104ses_winsizes(
10105 FILE *fd,
10106 int restore_size,
10107 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108{
10109 int n = 0;
10110 win_T *wp;
10111
10112 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10113 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010114 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 {
10116 if (!ses_do_win(wp))
10117 continue;
10118 ++n;
10119
10120 /* restore height when not full height */
10121 if (wp->w_height + wp->w_status_height < topframe->fr_height
10122 && (fprintf(fd,
10123 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10124 n, (long)wp->w_height, Rows / 2, Rows) < 0
10125 || put_eol(fd) == FAIL))
10126 return FAIL;
10127
10128 /* restore width when not full width */
10129 if (wp->w_width < Columns && (fprintf(fd,
10130 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10131 n, (long)wp->w_width, Columns / 2, Columns) < 0
10132 || put_eol(fd) == FAIL))
10133 return FAIL;
10134 }
10135 }
10136 else
10137 {
10138 /* Just equalise window sizes */
10139 if (put_line(fd, "wincmd =") == FAIL)
10140 return FAIL;
10141 }
10142 return OK;
10143}
10144
10145/*
10146 * Write commands to "fd" to recursively create windows for frame "fr",
10147 * horizontally and vertically split.
10148 * After the commands the last window in the frame is the current window.
10149 * Returns FAIL when writing the commands to "fd" fails.
10150 */
10151 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010152ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153{
10154 frame_T *frc;
10155 int count = 0;
10156
10157 if (fr->fr_layout != FR_LEAF)
10158 {
10159 /* Find first frame that's not skipped and then create a window for
10160 * each following one (first frame is already there). */
10161 frc = ses_skipframe(fr->fr_child);
10162 if (frc != NULL)
10163 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10164 {
10165 /* Make window as big as possible so that we have lots of room
10166 * to split. */
10167 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10168 || put_line(fd, fr->fr_layout == FR_COL
10169 ? "split" : "vsplit") == FAIL)
10170 return FAIL;
10171 ++count;
10172 }
10173
10174 /* Go back to the first window. */
10175 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10176 ? "%dwincmd k" : "%dwincmd h", count) < 0
10177 || put_eol(fd) == FAIL))
10178 return FAIL;
10179
10180 /* Recursively create frames/windows in each window of this column or
10181 * row. */
10182 frc = ses_skipframe(fr->fr_child);
10183 while (frc != NULL)
10184 {
10185 ses_win_rec(fd, frc);
10186 frc = ses_skipframe(frc->fr_next);
10187 /* Go to next window. */
10188 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10189 return FAIL;
10190 }
10191 }
10192 return OK;
10193}
10194
10195/*
10196 * Skip frames that don't contain windows we want to save in the Session.
10197 * Returns NULL when there none.
10198 */
10199 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010200ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201{
10202 frame_T *frc;
10203
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010204 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 if (ses_do_frame(frc))
10206 break;
10207 return frc;
10208}
10209
10210/*
10211 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10212 * the Session.
10213 */
10214 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010215ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216{
10217 frame_T *frc;
10218
10219 if (fr->fr_layout == FR_LEAF)
10220 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010221 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 if (ses_do_frame(frc))
10223 return TRUE;
10224 return FALSE;
10225}
10226
10227/*
10228 * Return non-zero if window "wp" is to be stored in the Session.
10229 */
10230 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010231ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010233#ifdef FEAT_TERMINAL
10234 if (bt_terminal(wp->w_buffer))
10235 return !term_is_finished(wp->w_buffer)
10236 && (ssop_flags & SSOP_TERMINAL)
10237 && term_should_restore(wp->w_buffer);
10238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 if (wp->w_buffer->b_fname == NULL
10240#ifdef FEAT_QUICKFIX
10241 /* When 'buftype' is "nofile" can't restore the window contents. */
Bram Moolenaar26910de2019-06-15 19:37:15 +020010242 || bt_nofilename(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243#endif
10244 )
10245 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010246 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 return (ssop_flags & SSOP_HELP);
10248 return TRUE;
10249}
10250
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010251 static int
10252put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10253{
10254 int r;
10255
10256 if (wp->w_curswant == MAXCOL)
10257 r = fprintf(fd, "%snormal! $", spaces);
10258 else
10259 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10260 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10261}
10262
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263/*
10264 * Write commands to "fd" to restore the view of a window.
10265 * Caller must make sure 'scrolloff' is zero.
10266 */
10267 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010268put_view(
10269 FILE *fd,
10270 win_T *wp,
10271 int add_edit, /* add ":edit" command to view */
10272 unsigned *flagp, /* vop_flags or ssop_flags */
10273 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010274 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275{
10276 win_T *save_curwin;
10277 int f;
10278 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010279 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280
10281 /* Always restore cursor position for ":mksession". For ":mkview" only
10282 * when 'viewoptions' contains "cursor". */
10283 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10284
10285 /*
10286 * Local argument list.
10287 */
10288 if (wp->w_alist == &global_alist)
10289 {
10290 if (put_line(fd, "argglobal") == FAIL)
10291 return FAIL;
10292 }
10293 else
10294 {
10295 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10296 flagp == &vop_flags
10297 || !(*flagp & SSOP_CURDIR)
10298 || wp->w_localdir != NULL, flagp) == FAIL)
10299 return FAIL;
10300 }
10301
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010302 /* Only when part of a session: restore the argument index. Some
10303 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010304 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010305 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010307 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 || put_eol(fd) == FAIL)
10309 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010310 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 }
10312
10313 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010314 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010316# ifdef FEAT_TERMINAL
10317 if (bt_terminal(wp->w_buffer))
10318 {
10319 if (term_write_session(fd, wp) == FAIL)
10320 return FAIL;
10321 }
10322 else
10323# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 /*
10325 * Load the file.
10326 */
10327 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010328# ifdef FEAT_QUICKFIX
Bram Moolenaar26910de2019-06-15 19:37:15 +020010329 && !bt_nofilename(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331 )
10332 {
10333 /*
10334 * Editing a file in this buffer: use ":edit file".
10335 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010336 *
10337 * Note, if a buffer for that file already exists, use :badd to
10338 * edit that buffer, to not lose folding information (:edit resets
10339 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010341 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010342 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010343 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010344 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10345 || fputs(" | else | edit ", fd) < 0
10346 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10347 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010348 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 return FAIL;
10350 }
10351 else
10352 {
10353 /* No file in this buffer, just make it empty. */
10354 if (put_line(fd, "enew") == FAIL)
10355 return FAIL;
10356#ifdef FEAT_QUICKFIX
10357 if (wp->w_buffer->b_ffname != NULL)
10358 {
10359 /* The buffer does have a name, but it's not a file name. */
10360 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010361 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 return FAIL;
10363 }
10364#endif
10365 do_cursor = FALSE;
10366 }
10367 }
10368
10369 /*
10370 * Local mappings and abbreviations.
10371 */
10372 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10373 && makemap(fd, wp->w_buffer) == FAIL)
10374 return FAIL;
10375
10376 /*
10377 * Local options. Need to go to the window temporarily.
10378 * Store only local values when using ":mkview" and when ":mksession" is
10379 * used and 'sessionoptions' doesn't include "options".
10380 * Some folding options are always stored when "folds" is included,
10381 * otherwise the folds would not be restored correctly.
10382 */
10383 save_curwin = curwin;
10384 curwin = wp;
10385 curbuf = curwin->w_buffer;
10386 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10387 f = makeset(fd, OPT_LOCAL,
10388 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10389#ifdef FEAT_FOLDING
10390 else if (*flagp & SSOP_FOLDS)
10391 f = makefoldset(fd);
10392#endif
10393 else
10394 f = OK;
10395 curwin = save_curwin;
10396 curbuf = curwin->w_buffer;
10397 if (f == FAIL)
10398 return FAIL;
10399
10400#ifdef FEAT_FOLDING
10401 /*
10402 * Save Folds when 'buftype' is empty and for help files.
10403 */
10404 if ((*flagp & SSOP_FOLDS)
10405 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010406 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010407 {
10408 if (put_folds(fd, wp) == FAIL)
10409 return FAIL;
10410 }
10411#endif
10412
10413 /*
10414 * Set the cursor after creating folds, since that moves the cursor.
10415 */
10416 if (do_cursor)
10417 {
10418
10419 /* Restore the cursor line in the file and relatively in the
10420 * window. Don't use "G", it changes the jumplist. */
10421 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10422 (long)wp->w_cursor.lnum,
10423 (long)(wp->w_cursor.lnum - wp->w_topline),
10424 (long)wp->w_height / 2, (long)wp->w_height) < 0
10425 || put_eol(fd) == FAIL
10426 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10427 || put_line(fd, "exe s:l") == FAIL
10428 || put_line(fd, "normal! zt") == FAIL
10429 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10430 || put_eol(fd) == FAIL)
10431 return FAIL;
10432 /* Restore the cursor column and left offset when not wrapping. */
10433 if (wp->w_cursor.col == 0)
10434 {
10435 if (put_line(fd, "normal! 0") == FAIL)
10436 return FAIL;
10437 }
10438 else
10439 {
10440 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10441 {
10442 if (fprintf(fd,
10443 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010444 (long)wp->w_virtcol + 1,
10445 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 (long)wp->w_width / 2, (long)wp->w_width) < 0
10447 || put_eol(fd) == FAIL
10448 || put_line(fd, "if s:c > 0") == FAIL
10449 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010450 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10451 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010452 || put_eol(fd) == FAIL
10453 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010454 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 || put_line(fd, "endif") == FAIL)
10456 return FAIL;
10457 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010458 else if (put_view_curpos(fd, wp, "") == FAIL)
10459 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 }
10461 }
10462
10463 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010464 * Local directory, if the current flag is not view options or the "curdir"
10465 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010467 if (wp->w_localdir != NULL
10468 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 {
10470 if (fputs("lcd ", fd) < 0
10471 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10472 || put_eol(fd) == FAIL)
10473 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010474 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 }
10476
10477 return OK;
10478}
10479
10480/*
10481 * Write an argument list to the session file.
10482 * Returns FAIL if writing fails.
10483 */
10484 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010485ses_arglist(
10486 FILE *fd,
10487 char *cmd,
10488 garray_T *gap,
10489 int fullname, /* TRUE: use full path name */
10490 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491{
10492 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010493 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 char_u *s;
10495
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010496 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10497 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010498 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 return FAIL;
10500 for (i = 0; i < gap->ga_len; ++i)
10501 {
10502 /* NULL file names are skipped (only happens when out of memory). */
10503 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10504 if (s != NULL)
10505 {
10506 if (fullname)
10507 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010508 buf = alloc(MAXPATHL);
10509 if (buf != NULL)
10510 {
10511 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10512 s = buf;
10513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010515 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010516 || ses_put_fname(fd, s, flagp) == FAIL
10517 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010518 {
10519 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010520 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010521 }
10522 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 }
10524 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010525 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526}
10527
10528/*
10529 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010530 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 * Returns FAIL if writing fails.
10532 */
10533 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010534ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535{
10536 char_u *name;
10537
10538 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010539 * the session file will be sourced.
10540 * Don't do this for ":mkview", we don't know the current directory.
10541 * Don't do this after ":lcd", we don't keep track of what the current
10542 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 if (buf->b_sfname != NULL
10544 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010545 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010546#ifdef FEAT_AUTOCHDIR
10547 && !p_acd
10548#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010549 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 name = buf->b_sfname;
10551 else
10552 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010553 if (ses_put_fname(fd, name, flagp) == FAIL
10554 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 return FAIL;
10556 return OK;
10557}
10558
10559/*
10560 * Write a file name to the session file.
10561 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10562 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010563 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 */
10565 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010566ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567{
10568 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010569 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571
10572 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010573 if (sname == NULL)
10574 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010576 if (*flagp & SSOP_SLASH)
10577 {
10578 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010579 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010580 if (*p == '\\')
10581 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010583
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010584 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010585 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010586 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010587 if (p == NULL)
10588 return FAIL;
10589
10590 /* write the result */
10591 if (fputs((char *)p, fd) < 0)
10592 retval = FAIL;
10593
10594 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 return retval;
10596}
10597
10598/*
10599 * ":loadview [nr]"
10600 */
10601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010602ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 char_u *fname;
10605
10606 fname = get_view_file(*eap->arg);
10607 if (fname != NULL)
10608 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010609 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 vim_free(fname);
10611 }
10612}
10613
10614/*
10615 * Get the name of the view file for the current buffer.
10616 */
10617 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010618get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
10620 int len = 0;
10621 char_u *p, *s;
10622 char_u *retval;
10623 char_u *sname;
10624
10625 if (curbuf->b_ffname == NULL)
10626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010627 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010628 return NULL;
10629 }
10630 sname = home_replace_save(NULL, curbuf->b_ffname);
10631 if (sname == NULL)
10632 return NULL;
10633
10634 /*
10635 * We want a file name without separators, because we're not going to make
10636 * a directory.
10637 * "normal" path separator -> "=+"
10638 * "=" -> "=="
10639 * ":" path separator -> "=-"
10640 */
10641 for (p = sname; *p; ++p)
10642 if (*p == '=' || vim_ispathsep(*p))
10643 ++len;
Bram Moolenaar964b3742019-05-24 18:54:09 +020010644 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 if (retval != NULL)
10646 {
10647 STRCPY(retval, p_vdir);
10648 add_pathsep(retval);
10649 s = retval + STRLEN(retval);
10650 for (p = sname; *p; ++p)
10651 {
10652 if (*p == '=')
10653 {
10654 *s++ = '=';
10655 *s++ = '=';
10656 }
10657 else if (vim_ispathsep(*p))
10658 {
10659 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010660#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 if (*p == ':')
10662 *s++ = '-';
10663 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010665 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666 }
10667 else
10668 *s++ = *p;
10669 }
10670 *s++ = '=';
10671 *s++ = c;
10672 STRCPY(s, ".vim");
10673 }
10674
10675 vim_free(sname);
10676 return retval;
10677}
10678
10679#endif /* FEAT_SESSION */
10680
10681/*
10682 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10683 * Return FAIL for a write error.
10684 */
10685 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010686put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688 if (
10689#ifdef USE_CRNL
10690 (
10691# ifdef MKSESSION_NL
10692 !mksession_nl &&
10693# endif
10694 (putc('\r', fd) < 0)) ||
10695#endif
10696 (putc('\n', fd) < 0))
10697 return FAIL;
10698 return OK;
10699}
10700
10701/*
10702 * Write a line to "fd".
10703 * Return FAIL for a write error.
10704 */
10705 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010706put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
10708 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10709 return FAIL;
10710 return OK;
10711}
10712
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010714/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010715 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010716 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010719dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 if (fname == NULL)
10722 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010723 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724}
10725#endif
10726
10727/*
10728 * ":behave {mswin,xterm}"
10729 */
10730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010731ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732{
10733 if (STRCMP(eap->arg, "mswin") == 0)
10734 {
10735 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10736 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10737 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10738 set_option_value((char_u *)"keymodel", 0L,
10739 (char_u *)"startsel,stopsel", 0);
10740 }
10741 else if (STRCMP(eap->arg, "xterm") == 0)
10742 {
10743 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10744 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10745 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10746 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10747 }
10748 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010749 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750}
10751
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010752#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10753/*
10754 * Function given to ExpandGeneric() to obtain the possible arguments of the
10755 * ":behave {mswin,xterm}" command.
10756 */
10757 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010758get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010759{
10760 if (idx == 0)
10761 return (char_u *)"mswin";
10762 if (idx == 1)
10763 return (char_u *)"xterm";
10764 return NULL;
10765}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010766
10767/*
10768 * Function given to ExpandGeneric() to obtain the possible arguments of the
10769 * ":messages {clear}" command.
10770 */
10771 char_u *
10772get_messages_arg(expand_T *xp UNUSED, int idx)
10773{
10774 if (idx == 0)
10775 return (char_u *)"clear";
10776 return NULL;
10777}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010778#endif
10779
Bram Moolenaar113e1072019-01-20 15:30:40 +010010780#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010781 char_u *
10782get_mapclear_arg(expand_T *xp UNUSED, int idx)
10783{
10784 if (idx == 0)
10785 return (char_u *)"<buffer>";
10786 return NULL;
10787}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010788#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010789
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790static int filetype_detect = FALSE;
10791static int filetype_plugin = FALSE;
10792static int filetype_indent = FALSE;
10793
10794/*
10795 * ":filetype [plugin] [indent] {on,off,detect}"
10796 * on: Load the filetype.vim file to install autocommands for file types.
10797 * off: Load the ftoff.vim file to remove all autocommands for file types.
10798 * plugin on: load filetype.vim and ftplugin.vim
10799 * plugin off: load ftplugof.vim
10800 * indent on: load filetype.vim and indent.vim
10801 * indent off: load indoff.vim
10802 */
10803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010804ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805{
10806 char_u *arg = eap->arg;
10807 int plugin = FALSE;
10808 int indent = FALSE;
10809
10810 if (*eap->arg == NUL)
10811 {
10812 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010813 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 filetype_detect ? "ON" : "OFF",
10815 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10816 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10817 return;
10818 }
10819
10820 /* Accept "plugin" and "indent" in any order. */
10821 for (;;)
10822 {
10823 if (STRNCMP(arg, "plugin", 6) == 0)
10824 {
10825 plugin = TRUE;
10826 arg = skipwhite(arg + 6);
10827 continue;
10828 }
10829 if (STRNCMP(arg, "indent", 6) == 0)
10830 {
10831 indent = TRUE;
10832 arg = skipwhite(arg + 6);
10833 continue;
10834 }
10835 break;
10836 }
10837 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10838 {
10839 if (*arg == 'o' || !filetype_detect)
10840 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010841 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842 filetype_detect = TRUE;
10843 if (plugin)
10844 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010845 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 filetype_plugin = TRUE;
10847 }
10848 if (indent)
10849 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010850 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 filetype_indent = TRUE;
10852 }
10853 }
10854 if (*arg == 'd')
10855 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010856 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010857 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858 }
10859 }
10860 else if (STRCMP(arg, "off") == 0)
10861 {
10862 if (plugin || indent)
10863 {
10864 if (plugin)
10865 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010866 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 filetype_plugin = FALSE;
10868 }
10869 if (indent)
10870 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010871 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872 filetype_indent = FALSE;
10873 }
10874 }
10875 else
10876 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010877 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 filetype_detect = FALSE;
10879 }
10880 }
10881 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010882 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883}
10884
10885/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010886 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 */
10888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010889ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890{
10891 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010892 {
10893 char_u *arg = eap->arg;
10894
10895 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10896 arg += 9;
10897
10898 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10899 if (arg != eap->arg)
10900 did_filetype = FALSE;
10901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010905ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906{
10907#ifdef FEAT_DIGRAPHS
10908 if (*eap->arg != NUL)
10909 putdigraph(eap->arg);
10910 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010911 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010913 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914#endif
10915}
10916
10917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010918ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919{
10920 int flags = 0;
10921
10922 if (eap->cmdidx == CMD_setlocal)
10923 flags = OPT_LOCAL;
10924 else if (eap->cmdidx == CMD_setglobal)
10925 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010926#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 if (cmdmod.browse && flags == 0)
10928 ex_options(eap);
10929 else
10930#endif
10931 (void)do_set(eap->arg, flags);
10932}
10933
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010934#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10935 void
10936set_no_hlsearch(int flag)
10937{
10938 no_hlsearch = flag;
10939# ifdef FEAT_EVAL
10940 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10941# endif
10942}
10943
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944/*
10945 * ":nohlsearch"
10946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010948ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010950 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010951 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953#endif
10954
10955#ifdef FEAT_CRYPT
10956/*
10957 * ":X": Get crypt key
10958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010960ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010962 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010963 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964}
10965#endif
10966
10967#ifdef FEAT_FOLDING
10968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010969ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970{
10971 if (foldManualAllowed(TRUE))
10972 foldCreate(eap->line1, eap->line2);
10973}
10974
10975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010976ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977{
10978 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10979 eap->forceit, FALSE);
10980}
10981
10982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010983ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984{
10985 linenr_T lnum;
10986
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010987#ifdef FEAT_CLIPBOARD
10988 start_global_changes();
10989#endif
10990
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991 /* First set the marks for all lines closed/open. */
10992 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10993 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10994 ml_setmarked(lnum);
10995
10996 /* Execute the command on the marked lines. */
10997 global_exe(eap->arg);
10998 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010999#ifdef FEAT_CLIPBOARD
11000 end_global_changes();
11001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002}
11003#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011004
Bram Moolenaar39665952018-08-15 20:59:48 +020011005#ifdef FEAT_QUICKFIX
11006/*
11007 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11008 * instead of a quickfix command.
11009 */
11010 int
11011is_loclist_cmd(int cmdidx)
11012{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011013 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011014 return FALSE;
11015 return cmdnames[cmdidx].cmd_name[0] == 'l';
11016}
11017#endif
11018
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011019# if defined(FEAT_TIMERS) || defined(PROTO)
11020 int
11021get_pressedreturn(void)
11022{
11023 return ex_pressedreturn;
11024}
11025
11026 void
11027set_pressedreturn(int val)
11028{
11029 ex_pressedreturn = val;
11030}
11031#endif