blob: 6df1c3189dd38ea111a9f8e4fa17e819146762c9 [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 Moolenaar071d4272004-06-13 20:20:40 +000032#ifndef FEAT_MENU
33# define ex_emenu ex_ni
34# define ex_menu ex_ni
35# define ex_menutranslate ex_ni
36#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_autocmd(exarg_T *eap);
38static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static void ex_bunload(exarg_T *eap);
40static void ex_buffer(exarg_T *eap);
41static void ex_bmodified(exarg_T *eap);
42static void ex_bnext(exarg_T *eap);
43static void ex_bprevious(exarg_T *eap);
44static void ex_brewind(exarg_T *eap);
45static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static char_u *getargcmd(char_u **);
47static char_u *skip_cmd_arg(char_u *p, int rembs);
48static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#ifndef FEAT_QUICKFIX
50# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000051# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cc ex_ni
53# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020054# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000055# define ex_cfile ex_ni
56# define qf_list ex_ni
57# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020058# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000060# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020062#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000063# define ex_cclose ex_ni
64# define ex_copen ex_ni
65# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020066# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000068#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
69# define ex_cexpr ex_ni
70#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
Bram Moolenaarb7316892019-05-01 18:08:42 +020072static 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 +010073static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020074#if !defined(FEAT_PERL) \
75 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
76 || !defined(FEAT_TCL) \
77 || !defined(FEAT_RUBY) \
78 || !defined(FEAT_LUA) \
79 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000080# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010083static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000087#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
89static void ex_highlight(exarg_T *eap);
90static void ex_colorscheme(exarg_T *eap);
91static void ex_quit(exarg_T *eap);
92static void ex_cquit(exarg_T *eap);
93static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010094static void ex_close(exarg_T *eap);
95static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
96static void ex_only(exarg_T *eap);
97static void ex_resize(exarg_T *eap);
98static void ex_stag(exarg_T *eap);
99static void ex_tabclose(exarg_T *eap);
100static void ex_tabonly(exarg_T *eap);
101static void ex_tabnext(exarg_T *eap);
102static void ex_tabmove(exarg_T *eap);
103static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200104#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100105static void ex_pclose(exarg_T *eap);
106static void ex_ptag(exarg_T *eap);
107static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#else
109# define ex_pclose ex_ni
110# define ex_ptag ex_ni
111# define ex_pedit ex_ni
112#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100113static void ex_hide(exarg_T *eap);
114static void ex_stop(exarg_T *eap);
115static void ex_exit(exarg_T *eap);
116static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#else
120# define ex_goto ex_ni
121#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_shell(exarg_T *eap);
123static void ex_preserve(exarg_T *eap);
124static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100125static void ex_mode(exarg_T *eap);
126static void ex_wrongmodifier(exarg_T *eap);
127static void ex_find(exarg_T *eap);
128static void ex_open(exarg_T *eap);
129static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifndef FEAT_GUI
131# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100134#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100135static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#else
137# define ex_tearoff ex_ni
138#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100139#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
140 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#else
143# define ex_popup ex_ni
144#endif
145#ifndef FEAT_GUI_MSWIN
146# define ex_simalt ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define gui_mch_find_dialog ex_ni
150# define gui_mch_replace_dialog ex_ni
151#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000152#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define ex_helpfind ex_ni
154#endif
155#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100156# define ex_cscope ex_ni
157# define ex_scscope ex_ni
158# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
160#ifndef FEAT_SYN_HL
161# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200162# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200164#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200165# define ex_syntime ex_ni
166#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000167#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000168# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000170# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000171# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000172# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000173#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200174#ifndef FEAT_PERSISTENT_UNDO
175# define ex_rundo ex_ni
176# define ex_wundo ex_ni
177#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200178#ifndef FEAT_LUA
179# define ex_lua ex_script_ni
180# define ex_luado ex_ni
181# define ex_luafile ex_ni
182#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000183#ifndef FEAT_MZSCHEME
184# define ex_mzscheme ex_script_ni
185# define ex_mzfile ex_ni
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#ifndef FEAT_PERL
188# define ex_perl ex_script_ni
189# define ex_perldo ex_ni
190#endif
191#ifndef FEAT_PYTHON
192# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200193# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# define ex_pyfile ex_ni
195#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200197# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200198# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200199# define ex_py3file ex_ni
200#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100201#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
202# define ex_pyx ex_script_ni
203# define ex_pyxdo ex_ni
204# define ex_pyxfile ex_ni
205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifndef FEAT_TCL
207# define ex_tcl ex_script_ni
208# define ex_tcldo ex_ni
209# define ex_tclfile ex_ni
210#endif
211#ifndef FEAT_RUBY
212# define ex_ruby ex_script_ni
213# define ex_rubydo ex_ni
214# define ex_rubyfile ex_ni
215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifndef FEAT_KEYMAP
217# define ex_loadkeymap ex_ni
218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void ex_swapname(exarg_T *eap);
220static void ex_syncbind(exarg_T *eap);
221static void ex_read(exarg_T *eap);
222static void ex_pwd(exarg_T *eap);
223static void ex_equal(exarg_T *eap);
224static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000227#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#else
230# define ex_winpos ex_ni
231#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ex_operators(exarg_T *eap);
233static void ex_put(exarg_T *eap);
234static void ex_copymove(exarg_T *eap);
235static void ex_submagic(exarg_T *eap);
236static void ex_join(exarg_T *eap);
237static void ex_at(exarg_T *eap);
238static void ex_bang(exarg_T *eap);
239static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_wundo(exarg_T *eap);
242static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200243#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redo(exarg_T *eap);
245static void ex_later(exarg_T *eap);
246static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100248static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_startinsert(exarg_T *eap);
252static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ex_checkpath(exarg_T *eap);
255static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#else
257# define ex_findpat ex_ni
258# define ex_checkpath ex_ni
259#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200260#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#else
263# define ex_psearch ex_ni
264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static void ex_tag(exarg_T *eap);
266static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200268# define ex_break ex_ni
269# define ex_breakadd ex_ni
270# define ex_breakdel ex_ni
271# define ex_breaklist ex_ni
272# define ex_call ex_ni
273# define ex_catch ex_ni
274# define ex_compiler ex_ni
275# define ex_const ex_ni
276# define ex_continue ex_ni
277# define ex_debug ex_ni
278# define ex_debuggreedy ex_ni
279# define ex_delfunction ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
290# define ex_finish ex_ni
291# define ex_function ex_ni
292# define ex_if ex_ni
293# define ex_let ex_ni
294# define ex_lockvar ex_ni
295# define ex_oldfiles ex_ni
296# define ex_options ex_ni
297# define ex_packadd ex_ni
298# define ex_packloadall ex_ni
299# define ex_return ex_ni
300# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_throw ex_ni
302# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000304# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200305# define ex_while ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100307static char_u *arg_all(void);
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
326static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#else
330# define ex_nohlsearch ex_ni
331# define ex_match ex_ni
332#endif
333#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#else
336# define ex_X ex_ni
337#endif
338#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_fold(exarg_T *eap);
340static void ex_foldopen(exarg_T *eap);
341static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#else
343# define ex_fold ex_ni
344# define ex_foldopen ex_ni
345# define ex_folddo ex_ni
346#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100347#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348# define ex_language ex_ni
349#endif
350#ifndef FEAT_SIGNS
351# define ex_sign ex_ni
352#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000353#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200354# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifndef FEAT_CMDHIST
360# define ex_history ex_ni
361#endif
362#ifndef FEAT_JUMPLIST
363# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200364# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_changes ex_ni
366#endif
367
Bram Moolenaar05159a02005-02-26 23:04:13 +0000368#ifndef FEAT_PROFILE
369# define ex_profile ex_ni
370#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200371#ifndef FEAT_TERMINAL
372# define ex_terminal ex_ni
373#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200374#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
375# define ex_xrestore ex_ni
376#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200377#if !defined(FEAT_TEXT_PROP)
378# define ex_popupclear ex_ni
379#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381/*
382 * Declare cmdnames[].
383 */
384#define DO_DECLARE_EXCMD
385#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200386#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static char_u dollar_command[2] = {'$', 0};
389
390
391#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000392/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393typedef struct
394{
395 char_u *line; /* command line */
396 linenr_T lnum; /* sourcing_lnum of the line */
397} wcmd_T;
398
399/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000404struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
406 garray_T *lines_gap; /* growarray with line info */
407 int current_line; /* last read line from growarray */
408 int repeating; /* TRUE when looping a second time */
409 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 void *cookie;
412};
413
Bram Moolenaare96a2492019-06-25 04:12:16 +0200414static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static int store_loop_line(garray_T *gap, char_u *line);
416static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000417
418/* Struct to save a few things while debugging. Used in do_cmdline() only. */
419struct dbg_stuff
420{
421 int trylevel;
422 int force_abort;
423 except_T *caught_stack;
424 char_u *vv_exception;
425 char_u *vv_throwpoint;
426 int did_emsg;
427 int got_int;
428 int did_throw;
429 int need_rethrow;
430 int check_cstack;
431 except_T *current_exception;
432};
433
Bram Moolenaared203462004-06-16 11:19:22 +0000434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100435save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000436{
437 dsp->trylevel = trylevel; trylevel = 0;
438 dsp->force_abort = force_abort; force_abort = FALSE;
439 dsp->caught_stack = caught_stack; caught_stack = NULL;
440 dsp->vv_exception = v_exception(NULL);
441 dsp->vv_throwpoint = v_throwpoint(NULL);
442
443 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
444 dsp->did_emsg = did_emsg; did_emsg = FALSE;
445 dsp->got_int = got_int; got_int = FALSE;
446 dsp->did_throw = did_throw; did_throw = FALSE;
447 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
448 dsp->check_cstack = check_cstack; check_cstack = FALSE;
449 dsp->current_exception = current_exception; current_exception = NULL;
450}
451
452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100453restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000454{
455 suppress_errthrow = FALSE;
456 trylevel = dsp->trylevel;
457 force_abort = dsp->force_abort;
458 caught_stack = dsp->caught_stack;
459 (void)v_exception(dsp->vv_exception);
460 (void)v_throwpoint(dsp->vv_throwpoint);
461 did_emsg = dsp->did_emsg;
462 got_int = dsp->got_int;
463 did_throw = dsp->did_throw;
464 need_rethrow = dsp->need_rethrow;
465 check_cstack = dsp->check_cstack;
466 current_exception = dsp->current_exception;
467}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#endif
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470/*
471 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
472 * command is given.
473 */
474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100475do_exmode(
476 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 int save_msg_scroll;
479 int prev_msg_row;
480 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100481 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
483 if (improved)
484 exmode_active = EXMODE_VIM;
485 else
486 exmode_active = EXMODE_NORMAL;
487 State = NORMAL;
488
489 /* When using ":global /pat/ visual" and then "Q" we return to continue
490 * the :global command. */
491 if (global_busy)
492 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494 save_msg_scroll = msg_scroll;
495 ++RedrawingDisabled; /* don't redisplay the window */
496 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#ifdef FEAT_GUI
498 /* Ignore scrollbar and mouse events in Ex mode */
499 ++hold_gui_events;
500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
Bram Moolenaar32526b32019-01-19 17:43:09 +0100502 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 while (exmode_active)
504 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000505 /* Check for a ":normal" command and no more characters left. */
506 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
507 {
508 exmode_active = FALSE;
509 break;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 msg_scroll = TRUE;
512 need_wait_return = FALSE;
513 ex_pressedreturn = FALSE;
514 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100515 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 prev_msg_row = msg_row;
517 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (improved)
519 {
520 cmdline_row = msg_row;
521 do_cmdline(NULL, getexline, NULL, 0);
522 }
523 else
524 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
525 lines_left = Rows - 1;
526
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100528 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100531 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 if (ex_pressedreturn)
535 {
536 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000537 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000538 msg_row = prev_msg_row;
539 if (prev_msg_row == Rows - 1)
540 msg_row--;
541 }
542 msg_col = 0;
543 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
544 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
548 {
549 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100552 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555
556#ifdef FEAT_GUI
557 --hold_gui_events;
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 --RedrawingDisabled;
560 --no_wait_return;
561 update_screen(CLEAR);
562 need_wait_return = FALSE;
563 msg_scroll = save_msg_scroll;
564}
565
566/*
567 * Execute a simple command line. Used for translated commands like "*".
568 */
569 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100570do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571{
572 return do_cmdline(cmd, NULL, NULL,
573 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
574}
575
576/*
577 * do_cmdline(): execute one Ex command line
578 *
579 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100580 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 * 2. Split up in parts separated with '|'.
582 *
583 * This function can be called recursively!
584 *
585 * flags:
586 * DOCMD_VERBOSE - The command will be included in the error message.
587 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100588 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 * DOCMD_KEYTYPED - Don't reset KeyTyped.
590 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
591 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
592 *
593 * return FAIL if cmdline could not be executed, OK otherwise
594 */
595 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100596do_cmdline(
597 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200598 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100599 void *cookie, /* argument for fgetline() */
600 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601{
602 char_u *next_cmdline; /* next cmd to execute */
603 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100604 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 static int recursive = 0; /* recursive depth */
606 int msg_didout_before_start = 0;
607 int count = 0; /* line number count */
608 int did_inc = FALSE; /* incremented RedrawingDisabled */
609 int retval = OK;
610#ifdef FEAT_EVAL
611 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000612 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 int current_line = 0; /* active line in lines_ga */
614 char_u *fname = NULL; /* function or script name */
615 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
616 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000617 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 int initial_trylevel;
619 struct msglist **saved_msg_list = NULL;
620 struct msglist *private_msg_list;
621
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100622 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200623 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000625 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000627 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100629# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630# define cmd_cookie cookie
631#endif
632 static int call_depth = 0; /* recursiveness */
633
634#ifdef FEAT_EVAL
635 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
636 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000637 * This ensures that the do_errthrow() call in do_one_cmd() does not
638 * combine the messages stored by an earlier invocation of do_one_cmd()
639 * with the command name of the later one. This would happen when
640 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 saved_msg_list = msg_list;
642 msg_list = &private_msg_list;
643 private_msg_list = NULL;
644#endif
645
646 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100647 * here. The value of 200 allows nested function calls, ":source", etc.
648 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100649 if (call_depth >= 200
650#ifdef FEAT_EVAL
651 && call_depth >= p_mfd
652#endif
653 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100655 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656#ifdef FEAT_EVAL
657 /* When converting to an exception, we do not include the command name
658 * since this is not an error of the specific command. */
659 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
660 msg_list = saved_msg_list;
661#endif
662 return FAIL;
663 }
664 ++call_depth;
665
666#ifdef FEAT_EVAL
667 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000668 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 cstack.cs_trylevel = 0;
670 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000671 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
673
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100674 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100677 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000678 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 ++ex_nesting_level;
680
681 /* Get the function or script name and the address where the next breakpoint
682 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000683 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
685 fname = func_name(real_cookie);
686 breakpoint = func_breakpoint(real_cookie);
687 dbg_tick = func_dbg_tick(real_cookie);
688 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100689 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 {
691 fname = sourcing_name;
692 breakpoint = source_breakpoint(real_cookie);
693 dbg_tick = source_dbg_tick(real_cookie);
694 }
695
696 /*
697 * Initialize "force_abort" and "suppress_errthrow" at the top level.
698 */
699 if (!recursive)
700 {
701 force_abort = FALSE;
702 suppress_errthrow = FALSE;
703 }
704
705 /*
706 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000707 * exception handling (used when debugging). Otherwise clear it to avoid
708 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000710 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000711 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000712 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200713 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714
715 initial_trylevel = trylevel;
716
717 /*
718 * "did_throw" will be set to TRUE when an exception is being thrown.
719 */
720 did_throw = FALSE;
721#endif
722 /*
723 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000724 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 * If force_abort is set, we cancel everything.
726 */
727 did_emsg = FALSE;
728
729 /*
730 * KeyTyped is only set when calling vgetc(). Reset it here when not
731 * calling vgetc() (sourced command lines).
732 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100733 if (!(flags & DOCMD_KEYTYPED)
734 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 KeyTyped = FALSE;
736
737 /*
738 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000739 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * - for multiple commands on one line, separated with '|'
741 * - when repeating until there are no more lines (for ":source")
742 */
743 next_cmdline = cmdline;
744 do
745 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000746#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100747 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000748#endif
749
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000750 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (next_cmdline == NULL
752#ifdef FEAT_EVAL
753 && !force_abort
754 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000755 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756#endif
757 )
758 did_emsg = FALSE;
759
760 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000761 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100762 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 * 3. If a line is given: Make a copy, so we can mess with it.
764 */
765
766#ifdef FEAT_EVAL
767 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 {
770 /* Each '|' separated command is stored separately in lines_ga, to
771 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100772 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773
774 /* Check if a function has returned or, unless it has an unclosed
775 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000776 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000779 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780 func_line_end(real_cookie);
781# endif
782 if (func_has_ended(real_cookie))
783 {
784 retval = FAIL;
785 break;
786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000788#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000789 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100790 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791 script_line_end();
792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100795 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
797 retval = FAIL;
798 break;
799 }
800
801 /* If breakpoints have been added/deleted need to check for it. */
802 if (breakpoint != NULL && dbg_tick != NULL
803 && *dbg_tick != debug_tick)
804 {
805 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100806 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 fname, sourcing_lnum);
808 *dbg_tick = debug_tick;
809 }
810
811 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
812 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
813
814 /* Did we encounter a breakpoint? */
815 if (breakpoint != NULL && *breakpoint != 0
816 && *breakpoint <= sourcing_lnum)
817 {
818 dbg_breakpoint(fname, sourcing_lnum);
819 /* Find next breakpoint. */
820 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 fname, sourcing_lnum);
823 *dbg_tick = debug_tick;
824 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000825# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000826 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000827 {
828 if (getline_is_func)
829 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100830 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831 script_line_start();
832 }
833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 }
835
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000836 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 * below, so that it stores lines in or reads them from
841 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000842 * while/for loop. */
843 cmd_getline = get_loop_line;
844 cmd_cookie = (void *)&cmd_loop_cookie;
845 cmd_loop_cookie.lines_gap = &lines_ga;
846 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100847 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000848 cmd_loop_cookie.cookie = cookie;
849 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 else
852 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100853 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 cmd_cookie = cookie;
855 }
856#endif
857
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100858 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (next_cmdline == NULL)
860 {
861 /*
862 * Need to set msg_didout for the first line after an ":if",
863 * otherwise the ":if" will be overwritten.
864 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868#ifdef FEAT_EVAL
869 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
870#else
871 0
872#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200873 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
875 /* Don't call wait_return for aborted command line. The NULL
876 * returned for the end of a sourced file or executed function
877 * doesn't do this. */
878 if (KeyTyped && !(flags & DOCMD_REPEAT))
879 need_wait_return = FALSE;
880 retval = FAIL;
881 break;
882 }
883 used_getline = TRUE;
884
885 /*
886 * Keep the first typed line. Clear it when more lines are typed.
887 */
888 if (flags & DOCMD_KEEPLINE)
889 {
890 vim_free(repeat_cmdline);
891 if (count == 0)
892 repeat_cmdline = vim_strsave(next_cmdline);
893 else
894 repeat_cmdline = NULL;
895 }
896 }
897
898 /* 3. Make a copy of the command so we can mess with it. */
899 else if (cmdline_copy == NULL)
900 {
901 next_cmdline = vim_strsave(next_cmdline);
902 if (next_cmdline == NULL)
903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100904 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 retval = FAIL;
906 break;
907 }
908 }
909 cmdline_copy = next_cmdline;
910
911#ifdef FEAT_EVAL
912 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000913 * Save the current line when inside a ":while" or ":for", and when
914 * the command looks like a ":while" or ":for", because we may need it
915 * later. When there is a '|' and another command, it is stored
916 * separately, because we need to be able to jump back to it from an
917 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 if (current_line == lines_ga.ga_len
920 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000922 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 {
924 retval = FAIL;
925 break;
926 }
927 }
928 did_endif = FALSE;
929#endif
930
931 if (count++ == 0)
932 {
933 /*
934 * All output from the commands is put below each other, without
935 * waiting for a return. Don't do this when executing commands
936 * from a script or when being called recursive (e.g. for ":e
937 * +command file").
938 */
939 if (!(flags & DOCMD_NOWAIT) && !recursive)
940 {
941 msg_didout_before_start = msg_didout;
942 msg_didany = FALSE; /* no output yet */
943 msg_start();
944 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200945 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 ++RedrawingDisabled;
947 did_inc = TRUE;
948 }
949 }
950
951 if (p_verbose >= 15 && sourcing_name != NULL)
952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000954 verbose_enter_scroll();
955
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100956 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000958 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100959 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000960
961 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 --no_wait_return;
963 }
964
965 /*
966 * 2. Execute one '|' separated command.
967 * do_one_cmd() will return NULL if there is no trailing '|'.
968 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
969 */
970 ++recursive;
971 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
972#ifdef FEAT_EVAL
973 &cstack,
974#endif
975 cmd_getline, cmd_cookie);
976 --recursive;
977
978#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000979 if (cmd_cookie == (void *)&cmd_loop_cookie)
980 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000982 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983#endif
984
985 if (next_cmdline == NULL)
986 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100987 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#ifdef FEAT_CMDHIST
989 /*
990 * If the command was typed, remember it for the ':' register.
991 * Do this AFTER executing the command to make :@: work.
992 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100993 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 && new_last_cmdline != NULL)
995 {
996 vim_free(last_cmdline);
997 last_cmdline = new_last_cmdline;
998 new_last_cmdline = NULL;
999 }
1000#endif
1001 }
1002 else
1003 {
1004 /* need to copy the command after the '|' to cmdline_copy, for the
1005 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001006 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 next_cmdline = cmdline_copy;
1008 }
1009
1010
1011#ifdef FEAT_EVAL
1012 /* reset did_emsg for a function that is not aborted by an error */
1013 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 && !func_has_abort(real_cookie))
1016 did_emsg = FALSE;
1017
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001018 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 {
1020 ++current_line;
1021
1022 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001023 * An ":endwhile", ":endfor" and ":continue" is handled here.
1024 * If we were executing commands, jump back to the ":while" or
1025 * ":for".
1026 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001028 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001030 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 /* Jump back to the matching ":while" or ":for". Be careful
1033 * not to use a cs_line[] from an entry that isn't a ":while"
1034 * or ":for": It would make "current_line" invalid and can
1035 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (!did_emsg && !got_int && !did_throw
1037 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 && (cstack.cs_flags[cstack.cs_idx]
1039 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 && cstack.cs_line[cstack.cs_idx] >= 0
1041 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1042 {
1043 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 /* remember we jumped there */
1045 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 line_breakcheck(); /* check if CTRL-C typed */
1047
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 /* Check for the next breakpoint at or after the ":while"
1049 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (breakpoint != NULL)
1051 {
1052 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001053 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 fname,
1055 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1056 *dbg_tick = debug_tick;
1057 }
1058 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001063 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1064 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 }
1067
1068 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1075 }
1076 }
1077
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001078 /* Check for the next breakpoint after a watchexpression */
1079 if (breakpoint != NULL && has_watchexpr())
1080 {
1081 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1082 *dbg_tick = debug_tick;
1083 }
1084
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 /*
1086 * When not inside any ":while" loop, clear remembered lines.
1087 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001088 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
1090 if (lines_ga.ga_len > 0)
1091 {
1092 sourcing_lnum =
1093 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1094 free_cmdlines(&lines_ga);
1095 }
1096 current_line = 0;
1097 }
1098
1099 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1101 * being restored at the ":endtry". Reset them here and set the
1102 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1103 * This includes the case where a missing ":endif", ":endwhile" or
1104 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 cstack.cs_lflags &= ~CSL_HAD_FINA;
1109 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1110 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 did_throw ? (void *)current_exception : NULL);
1112 did_emsg = got_int = did_throw = FALSE;
1113 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1114 }
1115
1116 /* Update global "trylevel" for recursive calls to do_cmdline() from
1117 * within this loop. */
1118 trylevel = initial_trylevel + cstack.cs_trylevel;
1119
1120 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001121 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 * files) is aborted because of an error, an interrupt, or an uncaught
1123 * exception, cancel everything. If it is left normally, reset
1124 * force_abort to get the non-EH compatible abortion behavior for
1125 * the rest of the script.
1126 */
1127 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1128 force_abort = FALSE;
1129
1130 /* Convert an interrupt to an exception if appropriate. */
1131 (void)do_intthrow(&cstack);
1132#endif /* FEAT_EVAL */
1133
1134 }
1135 /*
1136 * Continue executing command lines when:
1137 * - no CTRL-C typed, no aborting error, no exception thrown or try
1138 * conditionals need to be checked for executing finally clauses or
1139 * catching an interrupt exception
1140 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 * looping for ":source" command or function call.
1143 */
1144 while (!((got_int
1145#ifdef FEAT_EVAL
1146 || (did_emsg && force_abort) || did_throw
1147#endif
1148 )
1149#ifdef FEAT_EVAL
1150 && cstack.cs_trylevel == 0
1151#endif
1152 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001153 && !(did_emsg
1154#ifdef FEAT_EVAL
1155 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001156 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001157 * the :endtry to be missed. */
1158 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1159#endif
1160 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001161 && (getline_equal(fgetline, cookie, getexmodeline)
1162 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 && (next_cmdline != NULL
1164#ifdef FEAT_EVAL
1165 || cstack.cs_idx >= 0
1166#endif
1167 || (flags & DOCMD_REPEAT)));
1168
1169 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001170 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171#ifdef FEAT_EVAL
1172 free_cmdlines(&lines_ga);
1173 ga_clear(&lines_ga);
1174
1175 if (cstack.cs_idx >= 0)
1176 {
1177 /*
1178 * If a sourced file or executed function ran to its end, report the
1179 * unclosed conditional.
1180 */
1181 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001182 && ((getline_equal(fgetline, cookie, getsourceline)
1183 && !source_finished(fgetline, cookie))
1184 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 && !func_has_ended(real_cookie))))
1186 {
1187 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001188 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001190 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001191 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 }
1196
1197 /*
1198 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1199 * ":endtry" in a sourced file or executed function. If the try
1200 * conditional is in its finally clause, ignore anything pending.
1201 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001202 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 */
1204 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001205 {
1206 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1207
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001208 if (idx >= 0)
1209 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001210 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1211 &cstack.cs_looplevel);
1212 }
1213 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 trylevel = initial_trylevel;
1215 }
1216
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001217 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1218 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001220 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 ? (char_u *)"endfunction" : (char_u *)NULL);
1222
1223 if (trylevel == 0)
1224 {
1225 /*
1226 * When an exception is being thrown out of the outermost try
1227 * conditional, discard the uncaught exception, disable the conversion
1228 * of interrupts or errors to exceptions, and ensure that no more
1229 * commands are executed.
1230 */
1231 if (did_throw)
1232 {
1233 void *p = NULL;
1234 char_u *saved_sourcing_name;
1235 int saved_sourcing_lnum;
1236 struct msglist *messages = NULL, *next;
1237
1238 /*
1239 * If the uncaught exception is a user exception, report it as an
1240 * error. If it is an error exception, display the saved error
1241 * message now. For an interrupt exception, do nothing; the
1242 * interrupt message is given elsewhere.
1243 */
1244 switch (current_exception->type)
1245 {
1246 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001247 vim_snprintf((char *)IObuff, IOSIZE,
1248 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 current_exception->value);
1250 p = vim_strsave(IObuff);
1251 break;
1252 case ET_ERROR:
1253 messages = current_exception->messages;
1254 current_exception->messages = NULL;
1255 break;
1256 case ET_INTERRUPT:
1257 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
1259
1260 saved_sourcing_name = sourcing_name;
1261 saved_sourcing_lnum = sourcing_lnum;
1262 sourcing_name = current_exception->throw_name;
1263 sourcing_lnum = current_exception->throw_lnum;
1264 current_exception->throw_name = NULL;
1265
1266 discard_current_exception(); /* uses IObuff if 'verbose' */
1267 suppress_errthrow = TRUE;
1268 force_abort = TRUE;
1269
1270 if (messages != NULL)
1271 {
1272 do
1273 {
1274 next = messages->next;
1275 emsg(messages->msg);
1276 vim_free(messages->msg);
1277 vim_free(messages);
1278 messages = next;
1279 }
1280 while (messages != NULL);
1281 }
1282 else if (p != NULL)
1283 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001284 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 vim_free(p);
1286 }
1287 vim_free(sourcing_name);
1288 sourcing_name = saved_sourcing_name;
1289 sourcing_lnum = saved_sourcing_lnum;
1290 }
1291
1292 /*
1293 * On an interrupt or an aborting error not converted to an exception,
1294 * disable the conversion of errors to exceptions. (Interrupts are not
1295 * converted any more, here.) This enables also the interrupt message
1296 * when force_abort is set and did_emsg unset in case of an interrupt
1297 * from a finally clause after an error.
1298 */
1299 else if (got_int || (did_emsg && force_abort))
1300 suppress_errthrow = TRUE;
1301 }
1302
1303 /*
1304 * The current cstack will be freed when do_cmdline() returns. An uncaught
1305 * exception will have to be rethrown in the previous cstack. If a function
1306 * has just returned or a script file was just finished and the previous
1307 * cstack belongs to the same function or, respectively, script file, it
1308 * will have to be checked for finally clauses to be executed due to the
1309 * ":return" or ":finish". This is done in do_one_cmd().
1310 */
1311 if (did_throw)
1312 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001313 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001315 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 && ex_nesting_level > func_level(real_cookie) + 1))
1317 {
1318 if (!did_throw)
1319 check_cstack = TRUE;
1320 }
1321 else
1322 {
1323 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 --ex_nesting_level;
1326 /*
1327 * Go to debug mode when returning from a function in which we are
1328 * single-stepping.
1329 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 if ((getline_equal(fgetline, cookie, getsourceline)
1331 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 ? (char_u *)_("End of sourced file")
1335 : (char_u *)_("End of function"));
1336 }
1337
1338 /*
1339 * Restore the exception environment (done after returning from the
1340 * debugger).
1341 */
1342 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001343 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345 msg_list = saved_msg_list;
1346#endif /* FEAT_EVAL */
1347
1348 /*
1349 * If there was too much output to fit on the command line, ask the user to
1350 * hit return before redrawing the screen. With the ":global" command we do
1351 * this only once after the command is finished.
1352 */
1353 if (did_inc)
1354 {
1355 --RedrawingDisabled;
1356 --no_wait_return;
1357 msg_scroll = FALSE;
1358
1359 /*
1360 * When just finished an ":if"-":else" which was typed, no need to
1361 * wait for hit-return. Also for an error situation.
1362 */
1363 if (retval == FAIL
1364#ifdef FEAT_EVAL
1365 || (did_endif && KeyTyped && !did_emsg)
1366#endif
1367 )
1368 {
1369 need_wait_return = FALSE;
1370 msg_didany = FALSE; /* don't wait when restarting edit */
1371 }
1372 else if (need_wait_return)
1373 {
1374 /*
1375 * The msg_start() above clears msg_didout. The wait_return we do
1376 * here should not overwrite the command that may be shown before
1377 * doing that.
1378 */
1379 msg_didout |= msg_didout_before_start;
1380 wait_return(FALSE);
1381 }
1382 }
1383
Bram Moolenaar2a942252012-11-28 23:03:07 +01001384#ifdef FEAT_EVAL
1385 did_endif = FALSE; /* in case do_cmdline used recursively */
1386#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /*
1388 * Reset if_level, in case a sourced script file contains more ":if" than
1389 * ":endif" (could be ":if x | foo | endif").
1390 */
1391 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001392#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 --call_depth;
1395 return retval;
1396}
1397
1398#ifdef FEAT_EVAL
1399/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001400 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 */
1402 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001403get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001405 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 wcmd_T *wp;
1407 char_u *line;
1408
1409 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1410 {
1411 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001412 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001414 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001416 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001418 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001419 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 ++cp->current_line;
1421
1422 return line;
1423 }
1424
1425 KeyTyped = FALSE;
1426 ++cp->current_line;
1427 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1428 sourcing_lnum = wp->lnum;
1429 return vim_strsave(wp->line);
1430}
1431
1432/*
1433 * Store a line in "gap" so that a ":while" loop can execute it again.
1434 */
1435 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001436store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 if (ga_grow(gap, 1) == FAIL)
1439 return FAIL;
1440 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1441 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1442 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 return OK;
1444}
1445
1446/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001447 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 */
1449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001450free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 while (gap->ga_len > 0)
1453 {
1454 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1455 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457}
1458#endif
1459
1460/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001461 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1462 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001465getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001466 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001467 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001468 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001471 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001472 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar89d40322006-08-29 15:30:07 +00001474 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001475 * function that's originally used to obtain the lines. This may be
1476 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001477 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001478 cp = (struct loop_cookie *)cookie;
1479 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 gp = cp->getline;
1482 cp = cp->cookie;
1483 }
1484 return gp == func;
1485#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001486 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487#endif
1488}
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001491 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 * getline function. Otherwise return "cookie".
1493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001495getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001496 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001497 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
Bram Moolenaar13505972019-01-24 15:04:48 +01001499#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001500 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaar89d40322006-08-29 15:30:07 +00001503 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001504 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001507 cp = (struct loop_cookie *)cookie;
1508 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
1510 gp = cp->getline;
1511 cp = cp->cookie;
1512 }
1513 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001514#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001517}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001519
1520/*
1521 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1522 * ":bwipeout", etc.
1523 * Returns the buffer number.
1524 */
1525 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001526compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001527{
1528 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001529 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001530 int count = offset;
1531
1532 buf = firstbuf;
1533 while (buf->b_next != NULL && buf->b_fnum < lnum)
1534 buf = buf->b_next;
1535 while (count != 0)
1536 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001537 count += (offset < 0) ? 1 : -1;
1538 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1539 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001540 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001541 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001542 if (addr_type == ADDR_LOADED_BUFFERS)
1543 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 while (buf->b_ml.ml_mfp == NULL)
1545 {
1546 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1547 if (nextbuf == NULL)
1548 break;
1549 buf = nextbuf;
1550 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001551 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001552 /* we might have gone too far, last buffer is not loadedd */
1553 if (addr_type == ADDR_LOADED_BUFFERS)
1554 while (buf->b_ml.ml_mfp == NULL)
1555 {
1556 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1557 if (nextbuf == NULL)
1558 break;
1559 buf = nextbuf;
1560 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001561 return buf->b_fnum;
1562}
1563
Bram Moolenaarb7316892019-05-01 18:08:42 +02001564/*
1565 * Return the window number of "win".
1566 * When "win" is NULL return the number of windows.
1567 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001568 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001569current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001570{
1571 win_T *wp;
1572 int nr = 0;
1573
Bram Moolenaar29323592016-07-24 22:04:11 +02001574 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001575 {
1576 ++nr;
1577 if (wp == win)
1578 break;
1579 }
1580 return nr;
1581}
1582
1583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001584current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585{
1586 tabpage_T *tp;
1587 int nr = 0;
1588
Bram Moolenaar29323592016-07-24 22:04:11 +02001589 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001590 {
1591 ++nr;
1592 if (tp == tab)
1593 break;
1594 }
1595 return nr;
1596}
1597
1598# define CURRENT_WIN_NR current_win_nr(curwin)
1599# define LAST_WIN_NR current_win_nr(NULL)
1600# define CURRENT_TAB_NR current_tab_nr(curtab)
1601# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603/*
1604 * Execute one Ex command.
1605 *
1606 * If 'sourcing' is TRUE, the command will be included in the error message.
1607 *
1608 * 1. skip comment lines and leading space
1609 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001610 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001611 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001612 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 * 6. parse arguments
1614 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 *
1618 * This function may be called recursively!
1619 */
1620#if (_MSC_VER == 1200)
1621/*
Bram Moolenaared203462004-06-16 11:19:22 +00001622 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001624 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625#endif
1626 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001627do_one_cmd(
1628 char_u **cmdlinep,
1629 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001631 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001633 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001634 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
1636 char_u *p;
1637 linenr_T lnum;
1638 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001639 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001640 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001642 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001644 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001646 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 vim_memset(&ea, 0, sizeof(ea));
1649 ea.line1 = 1;
1650 ea.line2 = 1;
1651#ifdef FEAT_EVAL
1652 ++ex_nesting_level;
1653#endif
1654
Bram Moolenaar21691f82012-12-05 19:13:18 +01001655 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (quitmore
1657#ifdef FEAT_EVAL
1658 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001659 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001660#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001661 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001662 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 --quitmore;
1664
1665 /*
1666 * Reset browse, confirm, etc.. They are restored when returning, for
1667 * recursive calls.
1668 */
1669 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001671 /* "#!anything" is handled like a comment. */
1672 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1673 goto doend;
1674
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001675/*
1676 * 1. Skip comment lines and leading white space and colons.
1677 * 2. Handle command modifiers.
1678 */
1679 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001681 ea.cmdlinep = cmdlinep;
1682 ea.getline = fgetline;
1683 ea.cookie = cookie;
1684#ifdef FEAT_EVAL
1685 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001687 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001688 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001690 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692#ifdef FEAT_EVAL
1693 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1694 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1695#else
1696 ea.skip = (if_level > 0);
1697#endif
1698
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001700 * 3. Skip over the range to find the command. Let "p" point to after it.
1701 *
1702 * We need the command to know what kind of range it uses.
1703 */
1704 cmd = ea.cmd;
1705 ea.cmd = skip_range(ea.cmd, NULL);
1706 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1707 ea.cmd = skipwhite(ea.cmd + 1);
1708 p = find_command(&ea, NULL);
1709
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001710#ifdef FEAT_EVAL
1711# ifdef FEAT_PROFILE
1712 // Count this line for profiling if skip is TRUE.
1713 if (do_profiling == PROF_YES
1714 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1715 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1716 {
1717 int skip = did_emsg || got_int || did_throw;
1718
1719 if (ea.cmdidx == CMD_catch)
1720 skip = !skip && !(cstack->cs_idx >= 0
1721 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1722 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1723 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1724 skip = skip || !(cstack->cs_idx >= 0
1725 && !(cstack->cs_flags[cstack->cs_idx]
1726 & (CSF_ACTIVE | CSF_TRUE)));
1727 else if (ea.cmdidx == CMD_finally)
1728 skip = FALSE;
1729 else if (ea.cmdidx != CMD_endif
1730 && ea.cmdidx != CMD_endfor
1731 && ea.cmdidx != CMD_endtry
1732 && ea.cmdidx != CMD_endwhile)
1733 skip = ea.skip;
1734
1735 if (!skip)
1736 {
1737 if (getline_equal(fgetline, cookie, get_func_line))
1738 func_line_exec(getline_cookie(fgetline, cookie));
1739 else if (getline_equal(fgetline, cookie, getsourceline))
1740 script_line_exec();
1741 }
1742 }
1743# endif
1744
1745 /* May go to debug mode. If this happens and the ">quit" debug command is
1746 * used, throw an interrupt exception and skip the next command. */
1747 dbg_check_breakpoint(&ea);
1748 if (!ea.skip && got_int)
1749 {
1750 ea.skip = TRUE;
1751 (void)do_intthrow(cstack);
1752 }
1753#endif
1754
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001755/*
1756 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 *
1758 * where 'addr' is:
1759 *
1760 * % (entire file)
1761 * $ [+-NUM]
1762 * 'x [+-NUM] (where x denotes a currently defined mark)
1763 * . [+-NUM]
1764 * [+-NUM]..
1765 * NUM
1766 *
1767 * The ea.cmd pointer is updated to point to the first character following the
1768 * range spec. If an initial address is found, but no second, the upper bound
1769 * is equal to the lower.
1770 */
1771
Bram Moolenaar25190db2019-05-04 15:05:28 +02001772 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001773 if (!IS_USER_CMDIDX(ea.cmdidx))
1774 {
1775 if (ea.cmdidx != CMD_SIZE)
1776 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1777 else
1778 ea.addr_type = ADDR_LINES;
1779
Bram Moolenaar25190db2019-05-04 15:05:28 +02001780 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001781 if (ea.cmdidx == CMD_wincmd && p != NULL)
1782 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001783#ifdef FEAT_QUICKFIX
1784 // :.cc in quickfix window uses line number
1785 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1786 ea.addr_type = ADDR_OTHER;
1787#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001788 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001789
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001790 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001791 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001792 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001795 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 */
1797
1798 /*
1799 * Skip ':' and any white space
1800 */
1801 ea.cmd = skipwhite(ea.cmd);
1802 while (*ea.cmd == ':')
1803 ea.cmd = skipwhite(ea.cmd + 1);
1804
1805 /*
1806 * If we got a line, but no command, then go to the line.
1807 * If we find a '|' or '\n' we set ea.nextcmd.
1808 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001809 if (*ea.cmd == NUL || *ea.cmd == '"'
1810 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 {
1812 /*
1813 * strange vi behaviour:
1814 * ":3" jumps to line 3
1815 * ":3|..." prints line 3
1816 * ":|" prints current line
1817 */
1818 if (ea.skip) /* skip this if inside :if */
1819 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001820 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
1822 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001823 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 if ((errormsg = invalid_range(&ea)) == NULL)
1825 {
1826 correct_range(&ea);
1827 ex_print(&ea);
1828 }
1829 }
1830 else if (ea.addr_count != 0)
1831 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001832 if (ea.line2 > curbuf->b_ml.ml_line_count)
1833 {
1834 /* With '-' in 'cpoptions' a line number past the file is an
1835 * error, otherwise put it at the end of the file. */
1836 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1837 ea.line2 = -1;
1838 else
1839 ea.line2 = curbuf->b_ml.ml_line_count;
1840 }
1841
1842 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001843 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 else
1845 {
1846 if (ea.line2 == 0)
1847 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 else
1849 curwin->w_cursor.lnum = ea.line2;
1850 beginline(BL_SOL | BL_FIX);
1851 }
1852 }
1853 goto doend;
1854 }
1855
Bram Moolenaard5005162014-08-22 23:05:54 +02001856 /* If this looks like an undefined user command and there are CmdUndefined
1857 * autocommands defined, trigger the matching autocommands. */
1858 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1859 && ASCII_ISUPPER(*ea.cmd)
1860 && has_cmdundefined())
1861 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001862 int ret;
1863
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001864 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001865 while (ASCII_ISALNUM(*p))
1866 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001867 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001868 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1869 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001870 /* If the autocommands did something and didn't cause an error, try
1871 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001872 p = (ret
1873#ifdef FEAT_EVAL
1874 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001875#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001876 ) ? find_command(&ea, NULL) : ea.cmd;
1877 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001878
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (p == NULL)
1880 {
1881 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001882 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 goto doend;
1884 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001885 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001886 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1887 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 errormsg = uc_fun_cmd();
1890 goto doend;
1891 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001892
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (ea.cmdidx == CMD_SIZE)
1894 {
1895 if (!ea.skip)
1896 {
1897 STRCPY(IObuff, _("E492: Not an editor command"));
1898 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001899 {
1900 /* If the modifier was parsed OK the error must be in the
1901 * following command */
1902 if (after_modifier != NULL)
1903 append_command(after_modifier);
1904 else
1905 append_command(*cmdlinep);
1906 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001907 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001908 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 goto doend;
1911 }
1912
Bram Moolenaar958636c2014-10-21 20:01:58 +02001913 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1914 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001915#ifdef HAVE_EX_SCRIPT_NI
1916 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1917#endif
1918 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920#ifndef FEAT_EVAL
1921 /*
1922 * When the expression evaluation is disabled, recognize the ":if" and
1923 * ":endif" commands and ignore everything in between it.
1924 */
1925 if (ea.cmdidx == CMD_if)
1926 ++if_level;
1927 if (if_level)
1928 {
1929 if (ea.cmdidx == CMD_endif)
1930 --if_level;
1931 goto doend;
1932 }
1933
1934#endif
1935
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001936 /* forced commands */
1937 if (*p == '!' && ea.cmdidx != CMD_substitute
1938 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 {
1940 ++p;
1941 ea.forceit = TRUE;
1942 }
1943 else
1944 ea.forceit = FALSE;
1945
1946/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001947 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001949 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001950 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
1952 if (!ea.skip)
1953 {
1954#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001955 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001957 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 goto doend;
1960 }
1961#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001962 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001963 {
1964 errormsg = _("E981: Command not allowed in rvim");
1965 goto doend;
1966 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001967 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001970 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 goto doend;
1972 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001973
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001974 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001975 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001977 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 goto doend;
1980 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001981
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001982 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001983 * Do allow ":checktime" (it is postponed).
1984 * Do allow ":edit" (check for an argument later).
1985 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001986 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001987 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001988 && ea.cmdidx != CMD_edit
1989 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001990 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001991 && curbuf_locked())
1992 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001994 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 {
1996 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001997 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 goto doend;
1999 }
2000 }
2001
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002002 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002004 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 goto doend;
2006 }
2007
2008 /*
2009 * Don't complain about the range if it is not used
2010 * (could happen if line_count is accidentally set to 0).
2011 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002012 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
2014 /*
2015 * If the range is backwards, ask for confirmation and, if given, swap
2016 * ea.line1 & ea.line2 so it's forwards again.
2017 * When global command is busy, don't ask, will fail below.
2018 */
2019 if (!global_busy && ea.line1 > ea.line2)
2020 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002021 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002023 if (sourcing || exmode_active)
2024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002025 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002026 goto doend;
2027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 if (ask_yesno((char_u *)
2029 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002030 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 }
2032 lnum = ea.line1;
2033 ea.line1 = ea.line2;
2034 ea.line2 = lnum;
2035 }
2036 if ((errormsg = invalid_range(&ea)) != NULL)
2037 goto doend;
2038 }
2039
Bram Moolenaarb7316892019-05-01 18:08:42 +02002040 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2041 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 ea.line2 = 1;
2043
2044 correct_range(&ea);
2045
2046#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002047 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002048 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 {
2050 /* Put the first line at the start of a closed fold, put the last line
2051 * at the end of a closed fold. */
2052 (void)hasFolding(ea.line1, &ea.line1, NULL);
2053 (void)hasFolding(ea.line2, NULL, &ea.line2);
2054 }
2055#endif
2056
2057#ifdef FEAT_QUICKFIX
2058 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002059 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 * option here, so things like % get expanded.
2061 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002062 p = replace_makeprg(&ea, p, cmdlinep);
2063 if (p == NULL)
2064 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065#endif
2066
2067 /*
2068 * Skip to start of argument.
2069 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2070 */
2071 if (ea.cmdidx == CMD_bang)
2072 ea.arg = p;
2073 else
2074 ea.arg = skipwhite(p);
2075
Bram Moolenaar379fb762018-08-30 15:58:28 +02002076 // ":file" cannot be run with an argument when "curbuf_lock" is set
2077 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2078 goto doend;
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 /*
2081 * Check for "++opt=val" argument.
2082 * Must be first, allow ":w ++enc=utf8 !cmd"
2083 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002084 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2086 if (getargopt(&ea) == FAIL && !ni)
2087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002088 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 goto doend;
2090 }
2091
2092 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2093 {
2094 if (*ea.arg == '>') /* append */
2095 {
2096 if (*++ea.arg != '>') /* typed wrong */
2097 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002098 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 goto doend;
2100 }
2101 ea.arg = skipwhite(ea.arg + 1);
2102 ea.append = TRUE;
2103 }
2104 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2105 {
2106 ++ea.arg;
2107 ea.usefilter = TRUE;
2108 }
2109 }
2110
2111 if (ea.cmdidx == CMD_read)
2112 {
2113 if (ea.forceit)
2114 {
2115 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2116 ea.forceit = FALSE;
2117 }
2118 else if (*ea.arg == '!') /* :r !filter */
2119 {
2120 ++ea.arg;
2121 ea.usefilter = TRUE;
2122 }
2123 }
2124
2125 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2126 {
2127 ea.amount = 1;
2128 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2129 {
2130 ++ea.arg;
2131 ++ea.amount;
2132 }
2133 ea.arg = skipwhite(ea.arg);
2134 }
2135
2136 /*
2137 * Check for "+command" argument, before checking for next command.
2138 * Don't do this for ":read !cmd" and ":write !cmd".
2139 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002140 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2142
2143 /*
2144 * Check for '|' to separate commands and '"' to start comments.
2145 * Don't do this for ":read !cmd" and ":write !cmd".
2146 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002147 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 separate_nextcmd(&ea);
2149
2150 /*
2151 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002152 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2153 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002155 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002156 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002157 || ea.cmdidx == CMD_global
2158 || ea.cmdidx == CMD_vglobal
2159 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
2161 for (p = ea.arg; *p; ++p)
2162 {
2163 /* Remove one backslash before a newline, so that it's possible to
2164 * pass a newline to the shell and also a newline that is preceded
2165 * with a backslash. This makes it impossible to end a shell
2166 * command in a backslash, but that doesn't appear useful.
2167 * Halving the number of backslashes is incompatible with previous
2168 * versions. */
2169 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002170 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 else if (*p == '\n')
2172 {
2173 ea.nextcmd = p + 1;
2174 *p = NUL;
2175 break;
2176 }
2177 }
2178 }
2179
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002180 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002182 buf_T *buf;
2183
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002185 switch (ea.addr_type)
2186 {
2187 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002188 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002189 ea.line2 = curbuf->b_ml.ml_line_count;
2190 break;
2191 case ADDR_LOADED_BUFFERS:
2192 buf = firstbuf;
2193 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2194 buf = buf->b_next;
2195 ea.line1 = buf->b_fnum;
2196 buf = lastbuf;
2197 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2198 buf = buf->b_prev;
2199 ea.line2 = buf->b_fnum;
2200 break;
2201 case ADDR_BUFFERS:
2202 ea.line1 = firstbuf->b_fnum;
2203 ea.line2 = lastbuf->b_fnum;
2204 break;
2205 case ADDR_WINDOWS:
2206 ea.line2 = LAST_WIN_NR;
2207 break;
2208 case ADDR_TABS:
2209 ea.line2 = LAST_TAB_NR;
2210 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002211 case ADDR_TABS_RELATIVE:
2212 ea.line2 = 1;
2213 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002214 case ADDR_ARGUMENTS:
2215 if (ARGCOUNT == 0)
2216 ea.line1 = ea.line2 = 0;
2217 else
2218 ea.line2 = ARGCOUNT;
2219 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002220 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002221#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002222 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002223 if (ea.line2 == 0)
2224 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002225#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002226 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002227 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002228 case ADDR_UNSIGNED:
2229 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002230 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002231 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234
2235 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002236 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002238 /* Do not allow register = for user commands */
2239 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002240 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002242#ifndef FEAT_CLIPBOARD
2243 /* check these explicitly for a more specific error message */
2244 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002246 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002247 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002250 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2251 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002252 {
2253 ea.regname = *ea.arg++;
2254#ifdef FEAT_EVAL
2255 /* for '=' register: accept the rest of the line as an expression */
2256 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2257 {
2258 set_expr_line(vim_strsave(ea.arg));
2259 ea.arg += STRLEN(ea.arg);
2260 }
2261#endif
2262 ea.arg = skipwhite(ea.arg);
2263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265
2266 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002267 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 * count, it's a buffer name.
2269 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2271 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002272 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274 n = getdigits(&ea.arg);
2275 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002276 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002278 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 goto doend;
2280 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002281 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 ea.line2 = n;
2284 if (ea.addr_count == 0)
2285 ea.addr_count = 1;
2286 }
2287 else
2288 {
2289 ea.line1 = ea.line2;
2290 ea.line2 += n - 1;
2291 ++ea.addr_count;
2292 /*
2293 * Be vi compatible: no error message for out of range.
2294 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002295 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 ea.line2 = curbuf->b_ml.ml_line_count;
2297 }
2298 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002299
2300 /*
2301 * Check for flags: 'l', 'p' and '#'.
2302 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002303 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002304 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002305 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2306 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002308 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 goto doend;
2311 }
2312
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002313 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002315 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 goto doend;
2317 }
2318
2319#ifdef FEAT_EVAL
2320 /*
2321 * Skip the command when it's not going to be executed.
2322 * The commands like :if, :endif, etc. always need to be executed.
2323 * Also make an exception for commands that handle a trailing command
2324 * themselves.
2325 */
2326 if (ea.skip)
2327 {
2328 switch (ea.cmdidx)
2329 {
2330 /* commands that need evaluation */
2331 case CMD_while:
2332 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002333 case CMD_for:
2334 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 case CMD_if:
2336 case CMD_elseif:
2337 case CMD_else:
2338 case CMD_endif:
2339 case CMD_try:
2340 case CMD_catch:
2341 case CMD_finally:
2342 case CMD_endtry:
2343 case CMD_function:
2344 break;
2345
2346 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002347 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 * the list at ":help :bar". */
2349 case CMD_aboveleft:
2350 case CMD_and:
2351 case CMD_belowright:
2352 case CMD_botright:
2353 case CMD_browse:
2354 case CMD_call:
2355 case CMD_confirm:
2356 case CMD_delfunction:
2357 case CMD_djump:
2358 case CMD_dlist:
2359 case CMD_dsearch:
2360 case CMD_dsplit:
2361 case CMD_echo:
2362 case CMD_echoerr:
2363 case CMD_echomsg:
2364 case CMD_echon:
2365 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002366 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 case CMD_help:
2368 case CMD_hide:
2369 case CMD_ijump:
2370 case CMD_ilist:
2371 case CMD_isearch:
2372 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002373 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 case CMD_keepjumps:
2375 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002376 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 case CMD_leftabove:
2378 case CMD_let:
2379 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002380 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002382 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002383 case CMD_noautocmd:
2384 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 case CMD_perl:
2386 case CMD_psearch:
2387 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002388 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002389 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_return:
2391 case CMD_rightbelow:
2392 case CMD_ruby:
2393 case CMD_silent:
2394 case CMD_smagic:
2395 case CMD_snomagic:
2396 case CMD_substitute:
2397 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002398 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 case CMD_tcl:
2400 case CMD_throw:
2401 case CMD_tilde:
2402 case CMD_topleft:
2403 case CMD_unlet:
2404 case CMD_verbose:
2405 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002406 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 break;
2408
2409 default: goto doend;
2410 }
2411 }
2412#endif
2413
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002414 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
2416 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2417 goto doend;
2418 }
2419
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 /*
2421 * Accept buffer name. Cannot be used at the same time with a buffer
2422 * number. Don't do this for a user command.
2423 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002424 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002425 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
2427 /*
2428 * :bdelete, :bwipeout and :bunload take several arguments, separated
2429 * by spaces: find next space (skipping over escaped characters).
2430 * The others take one argument: ignore trailing spaces.
2431 */
2432 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2433 || ea.cmdidx == CMD_bunload)
2434 p = skiptowhite_esc(ea.arg);
2435 else
2436 {
2437 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002438 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 --p;
2440 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002441 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002442 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (ea.line2 < 0) /* failed */
2444 goto doend;
2445 ea.addr_count = 1;
2446 ea.arg = skipwhite(p);
2447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Bram Moolenaar2be57332018-02-13 18:05:18 +01002449 /* The :try command saves the emsg_silent flag, reset it here when
2450 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002451 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002452 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002453 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002454 if (emsg_silent < 0)
2455 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002456 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002457 }
2458
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002460 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462
Bram Moolenaar958636c2014-10-21 20:01:58 +02002463 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
2465 /*
2466 * Execute a user-defined command.
2467 */
2468 do_ucmd(&ea);
2469 }
2470 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002473 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 */
2475 ea.errmsg = NULL;
2476 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2477 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002478 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 }
2480
2481#ifdef FEAT_EVAL
2482 /*
2483 * If the command just executed called do_cmdline(), any throw or ":return"
2484 * or ":finish" encountered there must also check the cstack of the still
2485 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2486 * exception, or reanimate a returned function or finished script file and
2487 * return or finish it again.
2488 */
2489 if (need_rethrow)
2490 do_throw(cstack);
2491 else if (check_cstack)
2492 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002493 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002495 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 && current_func_returned())
2497 do_return(&ea, TRUE, FALSE, NULL);
2498 }
2499 need_rethrow = check_cstack = FALSE;
2500#endif
2501
2502doend:
2503 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002504 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002506 curwin->w_cursor.col = 0;
2507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2510 {
2511 if (sourcing)
2512 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002513 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002516 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002518 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
2520 emsg(errormsg);
2521 }
2522#ifdef FEAT_EVAL
2523 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002524 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2525 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526#endif
2527
Bram Moolenaareffed932018-08-14 13:38:17 +02002528 if (ea.verbose_save >= 0)
2529 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002530
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002531 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002533 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
Bram Moolenaareffed932018-08-14 13:38:17 +02002535 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 {
2537 /* messages could be enabled for a serious error, need to check if the
2538 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002539 if (!did_emsg || msg_silent > ea.save_msg_silent)
2540 msg_silent = ea.save_msg_silent;
2541 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (emsg_silent < 0)
2543 emsg_silent = 0;
2544 /* Restore msg_scroll, it's set by file I/O commands, even when no
2545 * message is actually displayed. */
2546 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002547
2548 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2549 * somewhere in the line. Put it back in the first column. */
2550 if (redirecting())
2551 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 }
2553
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002554#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002555 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002556 --sandbox;
2557#endif
2558
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2560 ea.nextcmd = NULL;
2561
2562#ifdef FEAT_EVAL
2563 --ex_nesting_level;
2564#endif
2565
2566 return ea.nextcmd;
2567}
2568#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002569 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570#endif
2571
2572/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002573 * Parse and skip over command modifiers:
2574 * - update eap->cmd
2575 * - store flags in "cmdmod".
2576 * - Set ex_pressedreturn for an empty command line.
2577 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002578 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002579 * - set p_verbose for ":verbose"
2580 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002581 * When "skip_only" is TRUE the global variables are not changed, except for
2582 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002583 * Return FAIL when the command is not to be executed.
2584 * May set "errormsg" to an error message.
2585 */
2586 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002587parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002588{
2589 char_u *p;
2590
2591 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2592 eap->verbose_save = -1;
2593 eap->save_msg_silent = -1;
2594
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002595 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002596 for (;;)
2597 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002598 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2599 ++eap->cmd;
2600
2601 /* in ex mode, an empty line works like :+ */
2602 if (*eap->cmd == NUL && exmode_active
2603 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2604 || getline_equal(eap->getline, eap->cookie, getexline))
2605 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2606 {
2607 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002608 if (!skip_only)
2609 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002610 }
2611
2612 /* ignore comment and empty lines */
2613 if (*eap->cmd == '"')
2614 return FAIL;
2615 if (*eap->cmd == NUL)
2616 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002617 if (!skip_only)
2618 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002619 return FAIL;
2620 }
2621
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 p = skip_range(eap->cmd, NULL);
2623 switch (*p)
2624 {
2625 /* When adding an entry, also modify cmd_exists(). */
2626 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2627 break;
2628 cmdmod.split |= WSP_ABOVE;
2629 continue;
2630
2631 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2632 {
2633 cmdmod.split |= WSP_BELOW;
2634 continue;
2635 }
2636 if (checkforcmd(&eap->cmd, "browse", 3))
2637 {
2638#ifdef FEAT_BROWSE_CMD
2639 cmdmod.browse = TRUE;
2640#endif
2641 continue;
2642 }
2643 if (!checkforcmd(&eap->cmd, "botright", 2))
2644 break;
2645 cmdmod.split |= WSP_BOT;
2646 continue;
2647
2648 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2649 break;
2650#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2651 cmdmod.confirm = TRUE;
2652#endif
2653 continue;
2654
2655 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2656 {
2657 cmdmod.keepmarks = TRUE;
2658 continue;
2659 }
2660 if (checkforcmd(&eap->cmd, "keepalt", 5))
2661 {
2662 cmdmod.keepalt = TRUE;
2663 continue;
2664 }
2665 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2666 {
2667 cmdmod.keeppatterns = TRUE;
2668 continue;
2669 }
2670 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2671 break;
2672 cmdmod.keepjumps = TRUE;
2673 continue;
2674
2675 case 'f': /* only accept ":filter {pat} cmd" */
2676 {
2677 char_u *reg_pat;
2678
2679 if (!checkforcmd(&p, "filter", 4)
2680 || *p == NUL || ends_excmd(*p))
2681 break;
2682 if (*p == '!')
2683 {
2684 cmdmod.filter_force = TRUE;
2685 p = skipwhite(p + 1);
2686 if (*p == NUL || ends_excmd(*p))
2687 break;
2688 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002689 if (skip_only)
2690 p = skip_vimgrep_pat(p, NULL, NULL);
2691 else
2692 // NOTE: This puts a NUL after the pattern.
2693 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002694 if (p == NULL || *p == NUL)
2695 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002696 if (!skip_only)
2697 {
2698 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002699 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002700 if (cmdmod.filter_regmatch.regprog == NULL)
2701 break;
2702 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002703 eap->cmd = p;
2704 continue;
2705 }
2706
2707 /* ":hide" and ":hide | cmd" are not modifiers */
2708 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2709 || *p == NUL || ends_excmd(*p))
2710 break;
2711 eap->cmd = p;
2712 cmdmod.hide = TRUE;
2713 continue;
2714
2715 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2716 {
2717 cmdmod.lockmarks = TRUE;
2718 continue;
2719 }
2720
2721 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2722 break;
2723 cmdmod.split |= WSP_ABOVE;
2724 continue;
2725
2726 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2727 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002728 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002729 {
2730 /* Set 'eventignore' to "all". Restore the
2731 * existing option value later. */
2732 cmdmod.save_ei = vim_strsave(p_ei);
2733 set_string_option_direct((char_u *)"ei", -1,
2734 (char_u *)"all", OPT_FREE, SID_NONE);
2735 }
2736 continue;
2737 }
2738 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2739 break;
2740 cmdmod.noswapfile = TRUE;
2741 continue;
2742
2743 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2744 break;
2745 cmdmod.split |= WSP_BELOW;
2746 continue;
2747
2748 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2749 {
2750#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002751 if (!skip_only)
2752 {
2753 if (!eap->did_sandbox)
2754 ++sandbox;
2755 eap->did_sandbox = TRUE;
2756 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002757#endif
2758 continue;
2759 }
2760 if (!checkforcmd(&eap->cmd, "silent", 3))
2761 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002762 if (!skip_only)
2763 {
2764 if (eap->save_msg_silent == -1)
2765 eap->save_msg_silent = msg_silent;
2766 ++msg_silent;
2767 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2769 {
2770 /* ":silent!", but not "silent !cmd" */
2771 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002772 if (!skip_only)
2773 {
2774 ++emsg_silent;
2775 ++eap->did_esilent;
2776 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 }
2778 continue;
2779
2780 case 't': if (checkforcmd(&p, "tab", 3))
2781 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002782 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002784 long tabnr = get_address(eap, &eap->cmd,
2785 ADDR_TABS, eap->skip,
2786 skip_only, FALSE, 1);
2787 if (tabnr == MAXLNUM)
2788 cmdmod.tab = tabpage_index(curtab) + 1;
2789 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002791 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2792 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002793 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002794 return FAIL;
2795 }
2796 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 }
2799 eap->cmd = p;
2800 continue;
2801 }
2802 if (!checkforcmd(&eap->cmd, "topleft", 2))
2803 break;
2804 cmdmod.split |= WSP_TOP;
2805 continue;
2806
2807 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2808 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002809 if (!skip_only)
2810 {
2811 if (eap->save_msg_silent == -1)
2812 eap->save_msg_silent = msg_silent;
2813 msg_silent = 0;
2814 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002815 continue;
2816
2817 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2818 {
2819 cmdmod.split |= WSP_VERT;
2820 continue;
2821 }
2822 if (!checkforcmd(&p, "verbose", 4))
2823 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002824 if (!skip_only)
2825 {
2826 if (eap->verbose_save < 0)
2827 eap->verbose_save = p_verbose;
2828 if (vim_isdigit(*eap->cmd))
2829 p_verbose = atoi((char *)eap->cmd);
2830 else
2831 p_verbose = 1;
2832 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 eap->cmd = p;
2834 continue;
2835 }
2836 break;
2837 }
2838
2839 return OK;
2840}
2841
2842/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002843 * Free contents of "cmdmod".
2844 */
2845 static void
2846free_cmdmod(void)
2847{
2848 if (cmdmod.save_ei != NULL)
2849 {
2850 /* Restore 'eventignore' to the value before ":noautocmd". */
2851 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2852 OPT_FREE, SID_NONE);
2853 free_string_option(cmdmod.save_ei);
2854 }
2855
2856 if (cmdmod.filter_regmatch.regprog != NULL)
2857 vim_regfree(cmdmod.filter_regmatch.regprog);
2858}
2859
2860/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002861 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002862 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002863 * Return FAIL and set "errormsg" or return OK.
2864 */
2865 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002866parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002867{
2868 int address_count = 1;
2869 linenr_T lnum;
2870
2871 // Repeat for all ',' or ';' separated addresses.
2872 for (;;)
2873 {
2874 eap->line1 = eap->line2;
2875 switch (eap->addr_type)
2876 {
2877 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002878 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002879 // default is current line number
2880 eap->line2 = curwin->w_cursor.lnum;
2881 break;
2882 case ADDR_WINDOWS:
2883 eap->line2 = CURRENT_WIN_NR;
2884 break;
2885 case ADDR_ARGUMENTS:
2886 eap->line2 = curwin->w_arg_idx + 1;
2887 if (eap->line2 > ARGCOUNT)
2888 eap->line2 = ARGCOUNT;
2889 break;
2890 case ADDR_LOADED_BUFFERS:
2891 case ADDR_BUFFERS:
2892 eap->line2 = curbuf->b_fnum;
2893 break;
2894 case ADDR_TABS:
2895 eap->line2 = CURRENT_TAB_NR;
2896 break;
2897 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002898 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002899 eap->line2 = 1;
2900 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002901 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002902#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002903 eap->line2 = qf_get_cur_idx(eap);
2904#endif
2905 break;
2906 case ADDR_QUICKFIX_VALID:
2907#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002908 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002909#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002910 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002911 case ADDR_NONE:
2912 // Will give an error later if a range is found.
2913 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002914 }
2915 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002916 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002917 eap->addr_count == 0, address_count++);
2918 if (eap->cmd == NULL) // error detected
2919 return FAIL;
2920 if (lnum == MAXLNUM)
2921 {
2922 if (*eap->cmd == '%') // '%' - all lines
2923 {
2924 ++eap->cmd;
2925 switch (eap->addr_type)
2926 {
2927 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002928 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002929 eap->line1 = 1;
2930 eap->line2 = curbuf->b_ml.ml_line_count;
2931 break;
2932 case ADDR_LOADED_BUFFERS:
2933 {
2934 buf_T *buf = firstbuf;
2935
2936 while (buf->b_next != NULL
2937 && buf->b_ml.ml_mfp == NULL)
2938 buf = buf->b_next;
2939 eap->line1 = buf->b_fnum;
2940 buf = lastbuf;
2941 while (buf->b_prev != NULL
2942 && buf->b_ml.ml_mfp == NULL)
2943 buf = buf->b_prev;
2944 eap->line2 = buf->b_fnum;
2945 break;
2946 }
2947 case ADDR_BUFFERS:
2948 eap->line1 = firstbuf->b_fnum;
2949 eap->line2 = lastbuf->b_fnum;
2950 break;
2951 case ADDR_WINDOWS:
2952 case ADDR_TABS:
2953 if (IS_USER_CMDIDX(eap->cmdidx))
2954 {
2955 eap->line1 = 1;
2956 eap->line2 = eap->addr_type == ADDR_WINDOWS
2957 ? LAST_WIN_NR : LAST_TAB_NR;
2958 }
2959 else
2960 {
2961 // there is no Vim command which uses '%' and
2962 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002963 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002964 return FAIL;
2965 }
2966 break;
2967 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002968 case ADDR_UNSIGNED:
2969 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002970 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002971 return FAIL;
2972 case ADDR_ARGUMENTS:
2973 if (ARGCOUNT == 0)
2974 eap->line1 = eap->line2 = 0;
2975 else
2976 {
2977 eap->line1 = 1;
2978 eap->line2 = ARGCOUNT;
2979 }
2980 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002981 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002982#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002983 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002984 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002985 if (eap->line2 == 0)
2986 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002987#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002988 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002989 case ADDR_NONE:
2990 // Will give an error later if a range is found.
2991 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002992 }
2993 ++eap->addr_count;
2994 }
2995 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2996 {
2997 pos_T *fp;
2998
2999 // '*' - visual area
3000 if (eap->addr_type != ADDR_LINES)
3001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003002 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003003 return FAIL;
3004 }
3005
3006 ++eap->cmd;
3007 if (!eap->skip)
3008 {
3009 fp = getmark('<', FALSE);
3010 if (check_mark(fp) == FAIL)
3011 return FAIL;
3012 eap->line1 = fp->lnum;
3013 fp = getmark('>', FALSE);
3014 if (check_mark(fp) == FAIL)
3015 return FAIL;
3016 eap->line2 = fp->lnum;
3017 ++eap->addr_count;
3018 }
3019 }
3020 }
3021 else
3022 eap->line2 = lnum;
3023 eap->addr_count++;
3024
3025 if (*eap->cmd == ';')
3026 {
3027 if (!eap->skip)
3028 {
3029 curwin->w_cursor.lnum = eap->line2;
3030 // don't leave the cursor on an illegal line or column
3031 check_cursor();
3032 }
3033 }
3034 else if (*eap->cmd != ',')
3035 break;
3036 ++eap->cmd;
3037 }
3038
3039 // One address given: set start and end lines.
3040 if (eap->addr_count == 1)
3041 {
3042 eap->line1 = eap->line2;
3043 // ... but only implicit: really no address given
3044 if (lnum == MAXLNUM)
3045 eap->addr_count = 0;
3046 }
3047 return OK;
3048}
3049
3050/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003051 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 * If there is a match advance "pp" to the argument and return TRUE.
3053 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003054 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003055checkforcmd(
3056 char_u **pp, /* start of command */
3057 char *cmd, /* name of command */
3058 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059{
3060 int i;
3061
3062 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003063 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 break;
3065 if (i >= len && !isalpha((*pp)[i]))
3066 {
3067 *pp = skipwhite(*pp + i);
3068 return TRUE;
3069 }
3070 return FALSE;
3071}
3072
3073/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003074 * Append "cmd" to the error message in IObuff.
3075 * Takes care of limiting the length and handling 0xa0, which would be
3076 * invisible otherwise.
3077 */
3078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003079append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003080{
3081 char_u *s = cmd;
3082 char_u *d;
3083
3084 STRCAT(IObuff, ": ");
3085 d = IObuff + STRLEN(IObuff);
3086 while (*s != NUL && d - IObuff < IOSIZE - 7)
3087 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003088 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003089 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003090 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003091 STRCPY(d, "<a0>");
3092 d += 4;
3093 }
3094 else
3095 MB_COPY_CHAR(s, d);
3096 }
3097 *d = NUL;
3098}
3099
3100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003102 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003103 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003104 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 * Returns NULL for an ambiguous user command.
3106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003108find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 int len;
3111 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003112 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 /*
3115 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003116 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 * - the 'k' command can directly be followed by any character.
3118 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003119 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003121 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 */
3123 p = eap->cmd;
3124 if (*p == 'k')
3125 {
3126 eap->cmdidx = CMD_k;
3127 ++p;
3128 }
3129 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003130 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3131 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 || p[1] == 'g'
3133 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3134 || p[1] == 'I'
3135 || (p[1] == 'r' && p[2] != 'e')))
3136 {
3137 eap->cmdidx = CMD_substitute;
3138 ++p;
3139 }
3140 else
3141 {
3142 while (ASCII_ISALPHA(*p))
3143 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003144 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003145 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003146 while (ASCII_ISALNUM(*p))
3147 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003148
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 /* check for non-alpha command */
3150 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3151 ++p;
3152 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003153 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3154 {
3155 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3156 * :delete with the 'l' flag. Same for 'p'. */
3157 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003158 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003159 break;
3160 if (i == len - 1)
3161 {
3162 --len;
3163 if (p[-1] == 'l')
3164 eap->flags |= EXFLAG_LIST;
3165 else
3166 eap->flags |= EXFLAG_PRINT;
3167 }
3168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003170 if (ASCII_ISLOWER(eap->cmd[0]))
3171 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003172 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003173 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003174
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003175 if (command_count != (int)CMD_SIZE)
3176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003177 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003178 getout(1);
3179 }
3180
3181 /* Use a precomputed index for fast look-up in cmdnames[]
3182 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003183 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3184 if (ASCII_ISLOWER(c2))
3185 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003188 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3191 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3192 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3193 (size_t)len) == 0)
3194 {
3195#ifdef FEAT_EVAL
3196 if (full != NULL
3197 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3198 *full = TRUE;
3199#endif
3200 break;
3201 }
3202
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003203 // Look for a user defined command as a last resort. Let ":Print" be
3204 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003205 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3206 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003208 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 while (ASCII_ISALNUM(*p))
3210 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003211 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003213 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 eap->cmdidx = CMD_SIZE;
3215 }
3216
3217 return p;
3218}
3219
3220#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003221static struct cmdmod
3222{
3223 char *name;
3224 int minlen;
3225 int has_count; /* :123verbose :3tab */
3226} cmdmods[] = {
3227 {"aboveleft", 3, FALSE},
3228 {"belowright", 3, FALSE},
3229 {"botright", 2, FALSE},
3230 {"browse", 3, FALSE},
3231 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003232 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003233 {"hide", 3, FALSE},
3234 {"keepalt", 5, FALSE},
3235 {"keepjumps", 5, FALSE},
3236 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003237 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003238 {"leftabove", 5, FALSE},
3239 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003240 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003241 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003242 {"rightbelow", 6, FALSE},
3243 {"sandbox", 3, FALSE},
3244 {"silent", 3, FALSE},
3245 {"tab", 3, TRUE},
3246 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003247 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003248 {"verbose", 4, TRUE},
3249 {"vertical", 4, FALSE},
3250};
3251
3252/*
3253 * Return length of a command modifier (including optional count).
3254 * Return zero when it's not a modifier.
3255 */
3256 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003257modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003258{
3259 int i, j;
3260 char_u *p = cmd;
3261
3262 if (VIM_ISDIGIT(*cmd))
3263 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003264 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003265 {
3266 for (j = 0; p[j] != NUL; ++j)
3267 if (p[j] != cmdmods[i].name[j])
3268 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003269 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003270 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003271 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003272 }
3273 return 0;
3274}
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276/*
3277 * Return > 0 if an Ex command "name" exists.
3278 * Return 2 if there is an exact match.
3279 * Return 3 if there is an ambiguous match.
3280 */
3281 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003282cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 exarg_T ea;
3285 int full = FALSE;
3286 int i;
3287 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003288 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003291 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 for (j = 0; name[j] != NUL; ++j)
3294 if (name[j] != cmdmods[i].name[j])
3295 break;
3296 if (name[j] == NUL && j >= cmdmods[i].minlen)
3297 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3298 }
3299
Bram Moolenaara9587612006-05-04 21:47:50 +00003300 /* Check built-in commands and user defined commands.
3301 * For ":2match" and ":3match" we need to skip the number. */
3302 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003304 p = find_command(&ea, &full);
3305 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003307 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3308 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003309 if (*skipwhite(p) != NUL)
3310 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3312}
3313#endif
3314
3315/*
3316 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3317 * we don't need/want deleted. Maybe this could be done better if we didn't
3318 * repeat all this stuff. The only problem is that they may not stay
3319 * perfectly compatible with each other, but then the command line syntax
3320 * probably won't change that much -- webb.
3321 */
3322 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003323set_one_cmd_context(
3324 expand_T *xp,
3325 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327 char_u *p;
3328 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003329 int len = 0;
3330 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003332 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 int delim;
3334#endif
3335 int forceit = FALSE;
3336 int usefilter = FALSE; /* filter instead of file name */
3337
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003338 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 xp->xp_pattern = buff;
3340 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003341 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
3343/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003344 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 */
3346 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3347 ;
3348 xp->xp_pattern = cmd;
3349
3350 if (*cmd == NUL)
3351 return NULL;
3352 if (*cmd == '"') /* ignore comment lines */
3353 {
3354 xp->xp_context = EXPAND_NOTHING;
3355 return NULL;
3356 }
3357
3358/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003359 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 */
3361 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 xp->xp_pattern = cmd;
3363 if (*cmd == NUL)
3364 return NULL;
3365 if (*cmd == '"')
3366 {
3367 xp->xp_context = EXPAND_NOTHING;
3368 return NULL;
3369 }
3370
3371 if (*cmd == '|' || *cmd == '\n')
3372 return cmd + 1; /* There's another command */
3373
3374 /*
3375 * Isolate the command and search for it in the command table.
3376 * Exceptions:
3377 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003378 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3380 */
3381 if (*cmd == 'k' && cmd[1] != 'e')
3382 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003383 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 p = cmd + 1;
3385 }
3386 else
3387 {
3388 p = cmd;
3389 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3390 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003391 /* a user command may contain digits */
3392 if (ASCII_ISUPPER(cmd[0]))
3393 while (ASCII_ISALNUM(*p) || *p == '*')
3394 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003395 /* for python 3.x: ":py3*" commands completion */
3396 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003397 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003398 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003399 while (ASCII_ISALPHA(*p) || *p == '*')
3400 ++p;
3401 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003402 /* check for non-alpha command */
3403 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3404 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003405 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003407 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
3409 xp->xp_context = EXPAND_UNSUCCESSFUL;
3410 return NULL;
3411 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003412 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003413 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3414 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3415 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 break;
3417
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003419 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 }
3422
3423 /*
3424 * If the cursor is touching the command, and it ends in an alpha-numeric
3425 * character, complete the command name.
3426 */
3427 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3428 return NULL;
3429
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003430 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
3432 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3433 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003434 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 p = cmd + 1;
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3438 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003439 ea.cmd = cmd;
3440 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003441#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003442 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003443#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003444 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003445#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003446 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003447 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003448 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003451 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 {
3453 /* Not still touching the command and it was an illegal one */
3454 xp->xp_context = EXPAND_UNSUCCESSFUL;
3455 return NULL;
3456 }
3457
3458 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3459
3460 if (*p == '!') /* forced commands */
3461 {
3462 forceit = TRUE;
3463 ++p;
3464 }
3465
3466/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003467 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003469 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003470 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 arg = skipwhite(p);
3473
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003474 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 if (*arg == '>') /* append */
3477 {
3478 if (*++arg == '>')
3479 ++arg;
3480 arg = skipwhite(arg);
3481 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003482 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
3484 ++arg;
3485 usefilter = TRUE;
3486 }
3487 }
3488
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003489 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
3491 usefilter = forceit; /* :r! filter if forced */
3492 if (*arg == '!') /* :r !filter */
3493 {
3494 ++arg;
3495 usefilter = TRUE;
3496 }
3497 }
3498
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 {
3501 while (*arg == *cmd) /* allow any number of '>' or '<' */
3502 ++arg;
3503 arg = skipwhite(arg);
3504 }
3505
3506 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003507 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 /* Check if we're in the +command */
3510 p = arg + 1;
3511 arg = skip_cmd_arg(arg, FALSE);
3512
3513 /* Still touching the command after '+'? */
3514 if (*arg == NUL)
3515 return p;
3516
3517 /* Skip space(s) after +command to get to the real argument */
3518 arg = skipwhite(arg);
3519 }
3520
3521 /*
3522 * Check for '|' to separate commands and '"' to start comments.
3523 * Don't do this for ":read !cmd" and ":write !cmd".
3524 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003525 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
3527 p = arg;
3528 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003529 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 p += 2;
3531 while (*p)
3532 {
3533 if (*p == Ctrl_V)
3534 {
3535 if (p[1] != NUL)
3536 ++p;
3537 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003538 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 || *p == '|' || *p == '\n')
3540 {
3541 if (*(p - 1) != '\\')
3542 {
3543 if (*p == '|' || *p == '\n')
3544 return p + 1;
3545 return NULL; /* It's a comment */
3546 }
3547 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003548 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550 }
3551
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003552 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3553 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3554 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 return NULL;
3556
3557 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003558 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003560 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003561 while (*p && p < buff + len)
3562 {
3563 if (*p == ' ' || *p == TAB)
3564 {
3565 /* argument starts after a space */
3566 xp->xp_pattern = ++p;
3567 }
3568 else
3569 {
3570 if (*p == '\\' && *(p + 1) != NUL)
3571 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003572 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003573 }
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003576 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003578 int c;
3579 int in_quote = FALSE;
3580 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
3582 /*
3583 * Allow spaces within back-quotes to count as part of the argument
3584 * being expanded.
3585 */
3586 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003587 p = xp->xp_pattern;
3588 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003590 if (has_mbyte)
3591 c = mb_ptr2char(p);
3592 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003593 c = *p;
3594 if (c == '\\' && p[1] != NUL)
3595 ++p;
3596 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 if (!in_quote)
3599 {
3600 xp->xp_pattern = p;
3601 bow = p + 1;
3602 }
3603 in_quote = !in_quote;
3604 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003605 /* An argument can contain just about everything, except
3606 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003607 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003608#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003609 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003610#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003611 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003612 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003613 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003614 while (*p != NUL)
3615 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003616 if (has_mbyte)
3617 c = mb_ptr2char(p);
3618 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003619 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003620 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003621 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003622 if (has_mbyte)
3623 len = (*mb_ptr2len)(p);
3624 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003625 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003626 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003627 }
3628 if (in_quote)
3629 bow = p;
3630 else
3631 xp->xp_pattern = p;
3632 p -= len;
3633 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003634 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 }
3636
3637 /*
3638 * If we are still inside the quotes, and we passed a space, just
3639 * expand from there.
3640 */
3641 if (bow != NULL && in_quote)
3642 xp->xp_pattern = bow;
3643 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003644
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003645 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003646 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003647 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003648#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003649 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003650#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003651 /* When still after the command name expand executables. */
3652 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003653 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 /* Check for environment variable */
3657 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003658#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 || *xp->xp_pattern == '%'
3660#endif
3661 )
3662 {
3663 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3664 if (!vim_isIDc(*p))
3665 break;
3666 if (*p == NUL)
3667 {
3668 xp->xp_context = EXPAND_ENV_VARS;
3669 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003670#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003671 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003672 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003673 compl = EXPAND_ENV_VARS;
3674#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003677#if defined(FEAT_CMDL_COMPL)
3678 /* Check for user names */
3679 if (*xp->xp_pattern == '~')
3680 {
3681 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3682 ;
3683 /* Complete ~user only if it partially matches a user name.
3684 * A full match ~user<Tab> will be replaced by user's home
3685 * directory i.e. something like ~user<Tab> -> /home/user/ */
3686 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003687 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003688 {
3689 xp->xp_context = EXPAND_USER;
3690 ++xp->xp_pattern;
3691 }
3692 }
3693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 }
3695
3696/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003697 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003699 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003701 case CMD_find:
3702 case CMD_sfind:
3703 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003704 if (xp->xp_context == EXPAND_FILES)
3705 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 case CMD_cd:
3708 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003709 case CMD_tcd:
3710 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 case CMD_lcd:
3712 case CMD_lchdir:
3713 if (xp->xp_context == EXPAND_FILES)
3714 xp->xp_context = EXPAND_DIRECTORIES;
3715 break;
3716 case CMD_help:
3717 xp->xp_context = EXPAND_HELP;
3718 xp->xp_pattern = arg;
3719 break;
3720
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003721 /* Command modifiers: return the argument.
3722 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003724 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 case CMD_belowright:
3726 case CMD_botright:
3727 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003728 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003729 case CMD_cdo:
3730 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003732 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 case CMD_folddoclosed:
3734 case CMD_folddoopen:
3735 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003736 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 case CMD_keepjumps:
3738 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003739 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003740 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003742 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003744 case CMD_noautocmd:
3745 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003747 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003749 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003750 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 case CMD_topleft:
3752 case CMD_verbose:
3753 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003754 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 return arg;
3756
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003757 case CMD_filter:
3758 if (*arg != NUL)
3759 arg = skip_vimgrep_pat(arg, NULL, NULL);
3760 if (arg == NULL || *arg == NUL)
3761 {
3762 xp->xp_context = EXPAND_NOTHING;
3763 return NULL;
3764 }
3765 return skipwhite(arg);
3766
Bram Moolenaar4f688582007-07-24 12:34:30 +00003767#ifdef FEAT_CMDL_COMPL
3768# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 case CMD_match:
3770 if (*arg == NUL || !ends_excmd(*arg))
3771 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003772 /* also complete "None" */
3773 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 arg = skipwhite(skiptowhite(arg));
3775 if (*arg != NUL)
3776 {
3777 xp->xp_context = EXPAND_NOTHING;
3778 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3779 }
3780 }
3781 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784/*
3785 * All completion for the +cmdline_compl feature goes here.
3786 */
3787
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003789 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 case CMD_delcommand:
3792 xp->xp_context = EXPAND_USER_COMMANDS;
3793 xp->xp_pattern = arg;
3794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
3796 case CMD_global:
3797 case CMD_vglobal:
3798 delim = *arg; /* get the delimiter */
3799 if (delim)
3800 ++arg; /* skip delimiter if there is one */
3801
3802 while (arg[0] != NUL && arg[0] != delim)
3803 {
3804 if (arg[0] == '\\' && arg[1] != NUL)
3805 ++arg;
3806 ++arg;
3807 }
3808 if (arg[0] != NUL)
3809 return arg + 1;
3810 break;
3811 case CMD_and:
3812 case CMD_substitute:
3813 delim = *arg;
3814 if (delim)
3815 {
3816 /* skip "from" part */
3817 ++arg;
3818 arg = skip_regexp(arg, delim, p_magic, NULL);
3819 }
3820 /* skip "to" part */
3821 while (arg[0] != NUL && arg[0] != delim)
3822 {
3823 if (arg[0] == '\\' && arg[1] != NUL)
3824 ++arg;
3825 ++arg;
3826 }
3827 if (arg[0] != NUL) /* skip delimiter */
3828 ++arg;
3829 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3830 ++arg;
3831 if (arg[0] != NUL)
3832 return arg;
3833 break;
3834 case CMD_isearch:
3835 case CMD_dsearch:
3836 case CMD_ilist:
3837 case CMD_dlist:
3838 case CMD_ijump:
3839 case CMD_psearch:
3840 case CMD_djump:
3841 case CMD_isplit:
3842 case CMD_dsplit:
3843 arg = skipwhite(skipdigits(arg)); /* skip count */
3844 if (*arg == '/') /* Match regexp, not just whole words */
3845 {
3846 for (++arg; *arg && *arg != '/'; arg++)
3847 if (*arg == '\\' && arg[1] != NUL)
3848 arg++;
3849 if (*arg)
3850 {
3851 arg = skipwhite(arg + 1);
3852
3853 /* Check for trailing illegal characters */
3854 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3855 xp->xp_context = EXPAND_NOTHING;
3856 else
3857 return arg;
3858 }
3859 }
3860 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 case CMD_autocmd:
3863 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003865 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 case CMD_set:
3868 set_context_in_set_cmd(xp, arg, 0);
3869 break;
3870 case CMD_setglobal:
3871 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3872 break;
3873 case CMD_setlocal:
3874 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3875 break;
3876 case CMD_tag:
3877 case CMD_stag:
3878 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003879 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 case CMD_tselect:
3881 case CMD_stselect:
3882 case CMD_ptselect:
3883 case CMD_tjump:
3884 case CMD_stjump:
3885 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003886 if (*p_wop != NUL)
3887 xp->xp_context = EXPAND_TAGS_LISTFILES;
3888 else
3889 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 xp->xp_pattern = arg;
3891 break;
3892 case CMD_augroup:
3893 xp->xp_context = EXPAND_AUGROUP;
3894 xp->xp_pattern = arg;
3895 break;
3896#ifdef FEAT_SYN_HL
3897 case CMD_syntax:
3898 set_context_in_syntax_cmd(xp, arg);
3899 break;
3900#endif
3901#ifdef FEAT_EVAL
3902 case CMD_let:
3903 case CMD_if:
3904 case CMD_elseif:
3905 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003906 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 case CMD_echo:
3908 case CMD_echon:
3909 case CMD_execute:
3910 case CMD_echomsg:
3911 case CMD_echoerr:
3912 case CMD_call:
3913 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003914 case CMD_cexpr:
3915 case CMD_caddexpr:
3916 case CMD_cgetexpr:
3917 case CMD_lexpr:
3918 case CMD_laddexpr:
3919 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003920 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 break;
3922
3923 case CMD_unlet:
3924 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3925 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003926
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 xp->xp_context = EXPAND_USER_VARS;
3928 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003929
3930 if (*xp->xp_pattern == '$')
3931 {
3932 xp->xp_context = EXPAND_ENV_VARS;
3933 ++xp->xp_pattern;
3934 }
3935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 break;
3937
3938 case CMD_function:
3939 case CMD_delfunction:
3940 xp->xp_context = EXPAND_USER_FUNC;
3941 xp->xp_pattern = arg;
3942 break;
3943
3944 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003945 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 break;
3947#endif
3948 case CMD_highlight:
3949 set_context_in_highlight_cmd(xp, arg);
3950 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003951#ifdef FEAT_CSCOPE
3952 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003953 case CMD_lcscope:
3954 case CMD_scscope:
3955 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003956 break;
3957#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003958#ifdef FEAT_SIGNS
3959 case CMD_sign:
3960 set_context_in_sign_cmd(xp, arg);
3961 break;
3962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 case CMD_bdelete:
3964 case CMD_bwipeout:
3965 case CMD_bunload:
3966 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3967 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003968 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 case CMD_buffer:
3970 case CMD_sbuffer:
3971 case CMD_checktime:
3972 xp->xp_context = EXPAND_BUFFERS;
3973 xp->xp_pattern = arg;
3974 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 case CMD_USER:
3977 case CMD_USER_BUF:
3978 if (compl != EXPAND_NOTHING)
3979 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003980 // EX_XFILE: file names are handled above
3981 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003983#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 if (compl == EXPAND_MENUS)
3985 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 if (compl == EXPAND_COMMANDS)
3988 return arg;
3989 if (compl == EXPAND_MAPPINGS)
3990 return set_context_in_map_cmd(xp, (char_u *)"map",
3991 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003992 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02003993 p = arg;
3994 while (*p)
3995 {
3996 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003997 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02003998 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003999 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004000 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004001 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 xp->xp_pattern = arg;
4004 }
4005 xp->xp_context = compl;
4006 }
4007 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004008
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 case CMD_map: case CMD_noremap:
4010 case CMD_nmap: case CMD_nnoremap:
4011 case CMD_vmap: case CMD_vnoremap:
4012 case CMD_omap: case CMD_onoremap:
4013 case CMD_imap: case CMD_inoremap:
4014 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004015 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004016 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004017 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004018 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004020 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 case CMD_unmap:
4022 case CMD_nunmap:
4023 case CMD_vunmap:
4024 case CMD_ounmap:
4025 case CMD_iunmap:
4026 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004027 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004028 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004029 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004030 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004032 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004033 case CMD_mapclear:
4034 case CMD_nmapclear:
4035 case CMD_vmapclear:
4036 case CMD_omapclear:
4037 case CMD_imapclear:
4038 case CMD_cmapclear:
4039 case CMD_lmapclear:
4040 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004041 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004042 case CMD_xmapclear:
4043 xp->xp_context = EXPAND_MAPCLEAR;
4044 xp->xp_pattern = arg;
4045 break;
4046
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 case CMD_abbreviate: case CMD_noreabbrev:
4048 case CMD_cabbrev: case CMD_cnoreabbrev:
4049 case CMD_iabbrev: case CMD_inoreabbrev:
4050 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004051 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 case CMD_unabbreviate:
4053 case CMD_cunabbrev:
4054 case CMD_iunabbrev:
4055 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004056 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057#ifdef FEAT_MENU
4058 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4059 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4060 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4061 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4062 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4063 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4064 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004065 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 case CMD_tmenu: case CMD_tunmenu:
4067 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4068 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4069#endif
4070
4071 case CMD_colorscheme:
4072 xp->xp_context = EXPAND_COLORS;
4073 xp->xp_pattern = arg;
4074 break;
4075
4076 case CMD_compiler:
4077 xp->xp_context = EXPAND_COMPILER;
4078 xp->xp_pattern = arg;
4079 break;
4080
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004081 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004082 xp->xp_context = EXPAND_OWNSYNTAX;
4083 xp->xp_pattern = arg;
4084 break;
4085
4086 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004087 xp->xp_context = EXPAND_FILETYPE;
4088 xp->xp_pattern = arg;
4089 break;
4090
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004091 case CMD_packadd:
4092 xp->xp_context = EXPAND_PACKADD;
4093 xp->xp_pattern = arg;
4094 break;
4095
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004096#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004098 p = skiptowhite(arg);
4099 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
4101 xp->xp_context = EXPAND_LANGUAGE;
4102 xp->xp_pattern = arg;
4103 }
4104 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004105 {
4106 if ( STRNCMP(arg, "messages", p - arg) == 0
4107 || STRNCMP(arg, "ctype", p - arg) == 0
4108 || STRNCMP(arg, "time", p - arg) == 0)
4109 {
4110 xp->xp_context = EXPAND_LOCALES;
4111 xp->xp_pattern = skipwhite(p);
4112 }
4113 else
4114 xp->xp_context = EXPAND_NOTHING;
4115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 break;
4117#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004118#if defined(FEAT_PROFILE)
4119 case CMD_profile:
4120 set_context_in_profile_cmd(xp, arg);
4121 break;
4122#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004123 case CMD_behave:
4124 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004125 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004128 case CMD_messages:
4129 xp->xp_context = EXPAND_MESSAGES;
4130 xp->xp_pattern = arg;
4131 break;
4132
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004133#if defined(FEAT_CMDHIST)
4134 case CMD_history:
4135 xp->xp_context = EXPAND_HISTORY;
4136 xp->xp_pattern = arg;
4137 break;
4138#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004139#if defined(FEAT_PROFILE)
4140 case CMD_syntime:
4141 xp->xp_context = EXPAND_SYNTIME;
4142 xp->xp_pattern = arg;
4143 break;
4144#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004145
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004146 case CMD_argdelete:
4147 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4148 arg = xp->xp_pattern + 1;
4149 xp->xp_context = EXPAND_ARGLIST;
4150 xp->xp_pattern = arg;
4151 break;
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153#endif /* FEAT_CMDL_COMPL */
4154
4155 default:
4156 break;
4157 }
4158 return NULL;
4159}
4160
4161/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004162 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 *
4164 * Backslashed delimiters after / or ? will be skipped, and commands will
4165 * not be expanded between /'s and ?'s or after "'".
4166 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004167 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 * Returns the "cmd" pointer advanced to beyond the range.
4169 */
4170 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004171skip_range(
4172 char_u *cmd,
4173 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004175 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004177 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004179 if (*cmd == '\\')
4180 {
4181 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4182 ++cmd;
4183 else
4184 break;
4185 }
4186 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
4188 if (*++cmd == NUL && ctx != NULL)
4189 *ctx = EXPAND_NOTHING;
4190 }
4191 else if (*cmd == '/' || *cmd == '?')
4192 {
4193 delim = *cmd++;
4194 while (*cmd != NUL && *cmd != delim)
4195 if (*cmd++ == '\\' && *cmd != NUL)
4196 ++cmd;
4197 if (*cmd == NUL && ctx != NULL)
4198 *ctx = EXPAND_NOTHING;
4199 }
4200 if (*cmd != NUL)
4201 ++cmd;
4202 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004203
4204 /* Skip ":" and white space. */
4205 while (*cmd == ':')
4206 cmd = skipwhite(cmd + 1);
4207
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return cmd;
4209}
4210
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004211 static void
4212addr_error(cmd_addr_T addr_type)
4213{
4214 if (addr_type == ADDR_NONE)
4215 emsg(_(e_norange));
4216 else
4217 emsg(_(e_invrange));
4218}
4219
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004221 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 *
4223 * Set ptr to the next character after the part that was interpreted.
4224 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004225 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 *
4227 * Return MAXLNUM when no Ex address was found.
4228 */
4229 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004230get_address(
4231 exarg_T *eap UNUSED,
4232 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004233 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004234 int skip, // only skip the address, don't use it
4235 int silent, // no errors or side effects
4236 int to_other_file, // flag: may jump to other file
4237 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004239 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 int c;
4241 int i;
4242 long n;
4243 char_u *cmd;
4244 pos_T pos;
4245 pos_T *fp;
4246 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004247 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 cmd = skipwhite(*ptr);
4250 lnum = MAXLNUM;
4251 do
4252 {
4253 switch (*cmd)
4254 {
4255 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004256 ++cmd;
4257 switch (addr_type)
4258 {
4259 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004260 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 lnum = curwin->w_cursor.lnum;
4262 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004263 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004264 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004265 break;
4266 case ADDR_ARGUMENTS:
4267 lnum = curwin->w_arg_idx + 1;
4268 break;
4269 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004270 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 lnum = curbuf->b_fnum;
4272 break;
4273 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004274 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004275 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004276 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004277 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004278 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004279 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004280 cmd = NULL;
4281 goto error;
4282 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004283 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004284#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004285 lnum = qf_get_cur_idx(eap);
4286#endif
4287 break;
4288 case ADDR_QUICKFIX_VALID:
4289#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004290 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004291#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004292 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004293 }
4294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004297 ++cmd;
4298 switch (addr_type)
4299 {
4300 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004301 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 lnum = curbuf->b_ml.ml_line_count;
4303 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004304 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004305 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004306 break;
4307 case ADDR_ARGUMENTS:
4308 lnum = ARGCOUNT;
4309 break;
4310 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004311 buf = lastbuf;
4312 while (buf->b_ml.ml_mfp == NULL)
4313 {
4314 if (buf->b_prev == NULL)
4315 break;
4316 buf = buf->b_prev;
4317 }
4318 lnum = buf->b_fnum;
4319 break;
4320 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004321 lnum = lastbuf->b_fnum;
4322 break;
4323 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004324 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004325 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004326 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004327 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004328 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004329 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004330 cmd = NULL;
4331 goto error;
4332 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004333 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004334#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004335 lnum = qf_get_size(eap);
4336 if (lnum == 0)
4337 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004338#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004339 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004340 case ADDR_QUICKFIX_VALID:
4341#ifdef FEAT_QUICKFIX
4342 lnum = qf_get_valid_size(eap);
4343 if (lnum == 0)
4344 lnum = 1;
4345#endif
4346 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004347 }
4348 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004351 if (*++cmd == NUL)
4352 {
4353 cmd = NULL;
4354 goto error;
4355 }
4356 if (addr_type != ADDR_LINES)
4357 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004358 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004359 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004360 goto error;
4361 }
4362 if (skip)
4363 ++cmd;
4364 else
4365 {
4366 /* Only accept a mark in another file when it is
4367 * used by itself: ":'M". */
4368 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4369 ++cmd;
4370 if (fp == (pos_T *)-1)
4371 /* Jumped to another file. */
4372 lnum = curwin->w_cursor.lnum;
4373 else
4374 {
4375 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
4377 cmd = NULL;
4378 goto error;
4379 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 lnum = fp->lnum;
4381 }
4382 }
4383 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385 case '/':
4386 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 c = *cmd++;
4388 if (addr_type != ADDR_LINES)
4389 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004390 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004391 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004392 goto error;
4393 }
4394 if (skip) /* skip "/pat/" */
4395 {
4396 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4397 if (*cmd == c)
4398 ++cmd;
4399 }
4400 else
4401 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004402 int flags;
4403
4404 pos = curwin->w_cursor; // save curwin->w_cursor
4405
4406 // When '/' or '?' follows another address, start from
4407 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004408 if (lnum != MAXLNUM)
4409 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004410
4411 // Start a forward search at the end of the line (unless
4412 // before the first line).
4413 // Start a backward search at the start of the line.
4414 // This makes sure we never match in the current
4415 // line, and can match anywhere in the
4416 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004417 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 curwin->w_cursor.col = MAXCOL;
4419 else
4420 curwin->w_cursor.col = 0;
4421 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004422 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4423 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004424 {
4425 curwin->w_cursor = pos;
4426 cmd = NULL;
4427 goto error;
4428 }
4429 lnum = curwin->w_cursor.lnum;
4430 curwin->w_cursor = pos;
4431 /* adjust command string pointer */
4432 cmd += searchcmdlen;
4433 }
4434 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004437 ++cmd;
4438 if (addr_type != ADDR_LINES)
4439 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004440 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004441 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 goto error;
4443 }
4444 if (*cmd == '&')
4445 i = RE_SUBST;
4446 else if (*cmd == '?' || *cmd == '/')
4447 i = RE_SEARCH;
4448 else
4449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004450 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004451 cmd = NULL;
4452 goto error;
4453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004455 if (!skip)
4456 {
4457 /*
4458 * When search follows another address, start from
4459 * there.
4460 */
4461 if (lnum != MAXLNUM)
4462 pos.lnum = lnum;
4463 else
4464 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 /*
4467 * Start the search just like for the above
4468 * do_search().
4469 */
4470 if (*cmd != '?')
4471 pos.col = MAXCOL;
4472 else
4473 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004474 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004475 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 *cmd == '?' ? BACKWARD : FORWARD,
4477 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004478 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 lnum = pos.lnum;
4480 else
4481 {
4482 cmd = NULL;
4483 goto error;
4484 }
4485 }
4486 ++cmd;
4487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4491 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493
4494 for (;;)
4495 {
4496 cmd = skipwhite(cmd);
4497 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4498 break;
4499
4500 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 {
4502 switch (addr_type)
4503 {
4504 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004505 case ADDR_OTHER:
4506 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004507 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 break;
4509 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004510 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004511 break;
4512 case ADDR_ARGUMENTS:
4513 lnum = curwin->w_arg_idx + 1;
4514 break;
4515 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004516 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 lnum = curbuf->b_fnum;
4518 break;
4519 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004520 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004521 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004522 case ADDR_TABS_RELATIVE:
4523 lnum = 1;
4524 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004525 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004526#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004527 lnum = qf_get_cur_idx(eap);
4528#endif
4529 break;
4530 case ADDR_QUICKFIX_VALID:
4531#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004532 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004533#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004534 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004535 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004536 case ADDR_UNSIGNED:
4537 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004538 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004539 }
4540 }
4541
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (VIM_ISDIGIT(*cmd))
4543 i = '+'; /* "number" is same as "+number" */
4544 else
4545 i = *cmd++;
4546 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4547 n = 1;
4548 else
4549 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004550
4551 if (addr_type == ADDR_TABS_RELATIVE)
4552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004553 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004554 cmd = NULL;
4555 goto error;
4556 }
4557 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004558 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004559 lnum = compute_buffer_local_count(
4560 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004562 {
4563#ifdef FEAT_FOLDING
4564 /* Relative line addressing, need to adjust for folded lines
4565 * now, but only do it after the first address. */
4566 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4567 && address_count >= 2)
4568 (void)hasFolding(lnum, NULL, &lnum);
4569#endif
4570 if (i == '-')
4571 lnum -= n;
4572 else
4573 lnum += n;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576 } while (*cmd == '/' || *cmd == '?');
4577
4578error:
4579 *ptr = cmd;
4580 return lnum;
4581}
4582
4583/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004584 * Get flags from an Ex command argument.
4585 */
4586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004587get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004588{
4589 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4590 {
4591 if (*eap->arg == 'l')
4592 eap->flags |= EXFLAG_LIST;
4593 else if (*eap->arg == 'p')
4594 eap->flags |= EXFLAG_PRINT;
4595 else
4596 eap->flags |= EXFLAG_NR;
4597 eap->arg = skipwhite(eap->arg + 1);
4598 }
4599}
4600
4601/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 * Function called for command which is Not Implemented. NI!
4603 */
4604 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004605ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004608 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609}
4610
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004611#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612/*
4613 * Function called for script command which is Not Implemented. NI!
4614 * Skips over ":perl <<EOF" constructs.
4615 */
4616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004617ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618{
4619 if (!eap->skip)
4620 ex_ni(eap);
4621 else
4622 vim_free(script_get(eap, eap->arg));
4623}
4624#endif
4625
4626/*
4627 * Check range in Ex command for validity.
4628 * Return NULL when valid, error message when invalid.
4629 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004630 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004633 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if ( eap->line1 < 0
4636 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004637 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004638 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004639
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004640 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004641 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004642 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004643 {
4644 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004645 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004646#ifdef FEAT_DIFF
4647 + (eap->cmdidx == CMD_diffget)
4648#endif
4649 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004650 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004651 break;
4652 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004653 /* add 1 if ARGCOUNT is 0 */
4654 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004655 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004656 break;
4657 case ADDR_BUFFERS:
4658 if (eap->line1 < firstbuf->b_fnum
4659 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004660 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004661 break;
4662 case ADDR_LOADED_BUFFERS:
4663 buf = firstbuf;
4664 while (buf->b_ml.ml_mfp == NULL)
4665 {
4666 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004667 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004668 buf = buf->b_next;
4669 }
4670 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004671 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004672 buf = lastbuf;
4673 while (buf->b_ml.ml_mfp == NULL)
4674 {
4675 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004676 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004677 buf = buf->b_prev;
4678 }
4679 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004680 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004681 break;
4682 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004683 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004684 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004685 break;
4686 case ADDR_TABS:
4687 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004688 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004689 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004690 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004691 case ADDR_OTHER:
4692 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004693 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004694 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004695#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004696 // No error for value that is too big, will use the last entry.
4697 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004698 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004699#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004700 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004701 case ADDR_QUICKFIX_VALID:
4702#ifdef FEAT_QUICKFIX
4703 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4704 || eap->line2 < 0)
4705 return _(e_invrange);
4706#endif
4707 break;
4708 case ADDR_UNSIGNED:
4709 if (eap->line2 < 0)
4710 return _(e_invrange);
4711 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004712 case ADDR_NONE:
4713 // Will give an error elsewhere.
4714 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004715 }
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 return NULL;
4718}
4719
4720/*
4721 * Correct the range for zero line number, if required.
4722 */
4723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004724correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004726 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 {
4728 if (eap->line1 == 0)
4729 eap->line1 = 1;
4730 if (eap->line2 == 0)
4731 eap->line2 = 1;
4732 }
4733}
4734
Bram Moolenaar748bf032005-02-02 23:04:36 +00004735#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004736/*
4737 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4738 * pattern. Otherwise return eap->arg.
4739 */
4740 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004741skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004742{
4743 char_u *p = eap->arg;
4744
Bram Moolenaara37420f2006-02-04 22:37:47 +00004745 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4746 || eap->cmdidx == CMD_vimgrepadd
4747 || eap->cmdidx == CMD_lvimgrepadd
4748 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004749 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004750 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004751 if (p == NULL)
4752 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004753 }
4754 return p;
4755}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004756
4757/*
4758 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4759 * in the command line, so that things like % get expanded.
4760 */
4761 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004762replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004763{
4764 char_u *new_cmdline;
4765 char_u *program;
4766 char_u *pos;
4767 char_u *ptr;
4768 int len;
4769 int i;
4770
4771 /*
4772 * Don't do it when ":vimgrep" is used for ":grep".
4773 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004774 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4775 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4776 || eap->cmdidx == CMD_grepadd
4777 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004778 && !grep_internal(eap->cmdidx))
4779 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004780 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4781 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004782 {
4783 if (*curbuf->b_p_gp == NUL)
4784 program = p_gp;
4785 else
4786 program = curbuf->b_p_gp;
4787 }
4788 else
4789 {
4790 if (*curbuf->b_p_mp == NUL)
4791 program = p_mp;
4792 else
4793 program = curbuf->b_p_mp;
4794 }
4795
4796 p = skipwhite(p);
4797
4798 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4799 {
4800 /* replace $* by given arguments */
4801 i = 1;
4802 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4803 ++i;
4804 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004805 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004806 if (new_cmdline == NULL)
4807 return NULL; /* out of memory */
4808 ptr = new_cmdline;
4809 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4810 {
4811 i = (int)(pos - program);
4812 STRNCPY(ptr, program, i);
4813 STRCPY(ptr += i, p);
4814 ptr += len;
4815 program = pos + 2;
4816 }
4817 STRCPY(ptr, program);
4818 }
4819 else
4820 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004821 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004822 if (new_cmdline == NULL)
4823 return NULL; /* out of memory */
4824 STRCPY(new_cmdline, program);
4825 STRCAT(new_cmdline, " ");
4826 STRCAT(new_cmdline, p);
4827 }
4828 msg_make(p);
4829
4830 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4831 vim_free(*cmdlinep);
4832 *cmdlinep = new_cmdline;
4833 p = new_cmdline;
4834 }
4835 return p;
4836}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004837#endif
4838
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839/*
4840 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004841 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 * Return FAIL for failure, OK otherwise.
4843 */
4844 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004845expand_filename(
4846 exarg_T *eap,
4847 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004848 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850 int has_wildcards; /* need to expand wildcards */
4851 char_u *repl;
4852 int srclen;
4853 char_u *p;
4854 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004855 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
Bram Moolenaar748bf032005-02-02 23:04:36 +00004857#ifdef FEAT_QUICKFIX
4858 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4859 p = skip_grep_pat(eap);
4860#else
4861 p = eap->arg;
4862#endif
4863
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 /*
4865 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4866 * the file name contains a wildcard it should not cause expanding.
4867 * (it will be expanded anyway if there is a wildcard before replacing).
4868 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004869 has_wildcards = mch_has_wildcard(p);
4870 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004872#ifdef FEAT_EVAL
4873 /* Skip over `=expr`, wildcards in it are not expanded. */
4874 if (p[0] == '`' && p[1] == '=')
4875 {
4876 p += 2;
4877 (void)skip_expr(&p);
4878 if (*p == '`')
4879 ++p;
4880 continue;
4881 }
4882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 /*
4884 * Quick check if this cannot be the start of a special string.
4885 * Also removes backslash before '%', '#' and '<'.
4886 */
4887 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4888 {
4889 ++p;
4890 continue;
4891 }
4892
4893 /*
4894 * Try to find a match at this position.
4895 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004896 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4897 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (*errormsgp != NULL) /* error detected */
4899 return FAIL;
4900 if (repl == NULL) /* no match found */
4901 {
4902 p += srclen;
4903 continue;
4904 }
4905
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004906 /* Wildcards won't be expanded below, the replacement is taken
4907 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4908 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4909 {
4910 char_u *l = repl;
4911
4912 repl = expand_env_save(repl);
4913 vim_free(l);
4914 }
4915
Bram Moolenaar63b92542007-03-27 14:57:09 +00004916 /* Need to escape white space et al. with a backslash.
4917 * Don't do this for:
4918 * - replacement that already has been escaped: "##"
4919 * - shell commands (may have to use quotes instead).
4920 * - non-unix systems when there is a single argument (spaces don't
4921 * separate arguments then).
4922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004924 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 && eap->cmdidx != CMD_grep
4927 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004928 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004929 && eap->cmdidx != CMD_lgrep
4930 && eap->cmdidx != CMD_lgrepadd
4931 && eap->cmdidx != CMD_lmake
4932 && eap->cmdidx != CMD_make
4933 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004935 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#endif
4937 )
4938 {
4939 char_u *l;
4940#ifdef BACKSLASH_IN_FILENAME
4941 /* Don't escape a backslash here, because rem_backslash() doesn't
4942 * remove it later. */
4943 static char_u *nobslash = (char_u *)" \t\"|";
4944# define ESCAPE_CHARS nobslash
4945#else
4946# define ESCAPE_CHARS escape_chars
4947#endif
4948
4949 for (l = repl; *l; ++l)
4950 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4951 {
4952 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4953 if (l != NULL)
4954 {
4955 vim_free(repl);
4956 repl = l;
4957 }
4958 break;
4959 }
4960 }
4961
4962 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004963 if ((eap->usefilter || eap->cmdidx == CMD_bang
4964 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004965 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 char_u *l;
4968
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004969 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 if (l != NULL)
4971 {
4972 vim_free(repl);
4973 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
4975 }
4976
4977 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4978 vim_free(repl);
4979 if (p == NULL)
4980 return FAIL;
4981 }
4982
4983 /*
4984 * One file argument: Expand wildcards.
4985 * Don't do this with ":r !command" or ":w !command".
4986 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004987 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 /*
4990 * May do this twice:
4991 * 1. Replace environment variables.
4992 * 2. Replace any other wildcards, remove backslashes.
4993 */
4994 for (n = 1; n <= 2; ++n)
4995 {
4996 if (n == 2)
4997 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 /*
4999 * Halve the number of backslashes (this is Vi compatible).
5000 * For Unix and OS/2, when wildcards are expanded, this is
5001 * done by ExpandOne() below.
5002 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005003#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 if (!has_wildcards)
5005#endif
5006 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
5008
5009 if (has_wildcards)
5010 {
5011 if (n == 1)
5012 {
5013 /*
5014 * First loop: May expand environment variables. This
5015 * can be done much faster with expand_env() than with
5016 * something else (e.g., calling a shell).
5017 * After expanding environment variables, check again
5018 * if there are still wildcards present.
5019 */
5020 if (vim_strchr(eap->arg, '$') != NULL
5021 || vim_strchr(eap->arg, '~') != NULL)
5022 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005023 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005024 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 has_wildcards = mch_has_wildcard(NameBuff);
5026 p = NameBuff;
5027 }
5028 else
5029 p = NULL;
5030 }
5031 else /* n == 2 */
5032 {
5033 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005034 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
5036 ExpandInit(&xpc);
5037 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005038 if (p_wic)
5039 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005041 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (p == NULL)
5043 return FAIL;
5044 }
5045 if (p != NULL)
5046 {
5047 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5048 p, cmdlinep);
5049 if (n == 2) /* p came from ExpandOne() */
5050 vim_free(p);
5051 }
5052 }
5053 }
5054 }
5055 return OK;
5056}
5057
5058/*
5059 * Replace part of the command line, keeping eap->cmd, eap->arg and
5060 * eap->nextcmd correct.
5061 * "src" points to the part that is to be replaced, of length "srclen".
5062 * "repl" is the replacement string.
5063 * Returns a pointer to the character after the replaced string.
5064 * Returns NULL for failure.
5065 */
5066 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005067repl_cmdline(
5068 exarg_T *eap,
5069 char_u *src,
5070 int srclen,
5071 char_u *repl,
5072 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
5074 int len;
5075 int i;
5076 char_u *new_cmdline;
5077
5078 /*
5079 * The new command line is build in new_cmdline[].
5080 * First allocate it.
5081 * Careful: a "+cmd" argument may have been NUL terminated.
5082 */
5083 len = (int)STRLEN(repl);
5084 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005085 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005087 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 return NULL; /* out of memory! */
5089
5090 /*
5091 * Copy the stuff before the expanded part.
5092 * Copy the expanded stuff.
5093 * Copy what came after the expanded part.
5094 * Copy the next commands, if there are any.
5095 */
5096 i = (int)(src - *cmdlinep); /* length of part before match */
5097 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 mch_memmove(new_cmdline + i, repl, (size_t)len);
5100 i += len; /* remember the end of the string */
5101 STRCPY(new_cmdline + i, src + srclen);
5102 src = new_cmdline + i; /* remember where to continue */
5103
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005104 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 {
5106 i = (int)STRLEN(new_cmdline) + 1;
5107 STRCPY(new_cmdline + i, eap->nextcmd);
5108 eap->nextcmd = new_cmdline + i;
5109 }
5110 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5111 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5112 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5113 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5114 vim_free(*cmdlinep);
5115 *cmdlinep = new_cmdline;
5116
5117 return src;
5118}
5119
5120/*
5121 * Check for '|' to separate commands and '"' to start comments.
5122 */
5123 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005124separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125{
5126 char_u *p;
5127
Bram Moolenaar86b68352004-12-27 21:59:20 +00005128#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005129 p = skip_grep_pat(eap);
5130#else
5131 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005132#endif
5133
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005134 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
5136 if (*p == Ctrl_V)
5137 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005138 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 ++p; /* skip CTRL-V and next char */
5140 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005141 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005142 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 if (*p == NUL) /* stop at NUL after CTRL-V */
5144 break;
5145 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005146
5147#ifdef FEAT_EVAL
5148 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005149 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005150 {
5151 p += 2;
5152 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005153 }
5154#endif
5155
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 /* Check for '"': start of comment or '|': next command */
5157 /* :@" and :*" do not start a comment!
5158 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005159 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5161 || p != eap->arg)
5162 && (eap->cmdidx != CMD_redir
5163 || p != eap->arg + 1 || p[-1] != '@'))
5164 || *p == '|' || *p == '\n')
5165 {
5166 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005167 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 * AND 'b' is present in 'cpoptions'.
5169 */
5170 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005171 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005173 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 --p;
5175 }
5176 else
5177 {
5178 eap->nextcmd = check_nextcmd(p);
5179 *p = NUL;
5180 break;
5181 }
5182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005184
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005185 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 del_trailing_spaces(eap->arg);
5187}
5188
5189/*
5190 * get + command from ex argument
5191 */
5192 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005193getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 char_u *arg = *argp;
5196 char_u *command = NULL;
5197
5198 if (*arg == '+') /* +[command] */
5199 {
5200 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005201 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 command = dollar_command;
5203 else
5204 {
5205 command = arg;
5206 arg = skip_cmd_arg(command, TRUE);
5207 if (*arg != NUL)
5208 *arg++ = NUL; /* terminate command with NUL */
5209 }
5210
5211 arg = skipwhite(arg); /* skip over spaces */
5212 *argp = arg;
5213 }
5214 return command;
5215}
5216
5217/*
5218 * Find end of "+command" argument. Skip over "\ " and "\\".
5219 */
5220 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221skip_cmd_arg(
5222 char_u *p,
5223 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 while (*p && !vim_isspace(*p))
5226 {
5227 if (*p == '\\' && p[1] != NUL)
5228 {
5229 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005230 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 else
5232 ++p;
5233 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005234 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
5236 return p;
5237}
5238
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005239 int
5240get_bad_opt(char_u *p, exarg_T *eap)
5241{
5242 if (STRICMP(p, "keep") == 0)
5243 eap->bad_char = BAD_KEEP;
5244 else if (STRICMP(p, "drop") == 0)
5245 eap->bad_char = BAD_DROP;
5246 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5247 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005248 else
5249 return FAIL;
5250 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005251}
5252
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253/*
5254 * Get "++opt=arg" argument.
5255 * Return FAIL or OK.
5256 */
5257 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005258getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 char_u *arg = eap->arg + 2;
5261 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005262 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
5265 /* ":edit ++[no]bin[ary] file" */
5266 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5267 {
5268 if (*arg == 'n')
5269 {
5270 arg += 2;
5271 eap->force_bin = FORCE_NOBIN;
5272 }
5273 else
5274 eap->force_bin = FORCE_BIN;
5275 if (!checkforcmd(&arg, "binary", 3))
5276 return FAIL;
5277 eap->arg = skipwhite(arg);
5278 return OK;
5279 }
5280
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005281 /* ":read ++edit file" */
5282 if (STRNCMP(arg, "edit", 4) == 0)
5283 {
5284 eap->read_edit = TRUE;
5285 eap->arg = skipwhite(arg + 4);
5286 return OK;
5287 }
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (STRNCMP(arg, "ff", 2) == 0)
5290 {
5291 arg += 2;
5292 pp = &eap->force_ff;
5293 }
5294 else if (STRNCMP(arg, "fileformat", 10) == 0)
5295 {
5296 arg += 10;
5297 pp = &eap->force_ff;
5298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 else if (STRNCMP(arg, "enc", 3) == 0)
5300 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005301 if (STRNCMP(arg, "encoding", 8) == 0)
5302 arg += 8;
5303 else
5304 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 pp = &eap->force_enc;
5306 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005307 else if (STRNCMP(arg, "bad", 3) == 0)
5308 {
5309 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005310 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
5313 if (pp == NULL || *arg != '=')
5314 return FAIL;
5315
5316 ++arg;
5317 *pp = (int)(arg - eap->cmd);
5318 arg = skip_cmd_arg(arg, FALSE);
5319 eap->arg = skipwhite(arg);
5320 *arg = NUL;
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 if (pp == &eap->force_ff)
5323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5325 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005326 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005328 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
5330 /* Make 'fileencoding' lower case. */
5331 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5332 *p = TOLOWER_ASC(*p);
5333 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005334 else
5335 {
5336 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5337 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005338 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005339 return FAIL;
5340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
5342 return OK;
5343}
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005346ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
5348 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005349 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 * directory for security reasons.
5351 */
5352 if (secure)
5353 {
5354 secure = 2;
5355 eap->errmsg = e_curdir;
5356 }
5357 else if (eap->cmdidx == CMD_autocmd)
5358 do_autocmd(eap->arg, eap->forceit);
5359 else
5360 do_augroup(eap->arg, eap->forceit);
5361}
5362
5363/*
5364 * ":doautocmd": Apply the automatic commands to the current buffer.
5365 */
5366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005367ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005369 char_u *arg = eap->arg;
5370 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005371 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005372
Bram Moolenaar1610d052016-06-09 22:53:01 +02005373 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5374 /* Only when there is no <nomodeline>. */
5375 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005376 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379/*
5380 * :[N]bunload[!] [N] [bufname] unload buffer
5381 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5382 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5383 */
5384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005385ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005387 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005388 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 eap->errmsg = do_bufdel(
5390 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5391 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5392 : DOBUF_UNLOAD, eap->arg,
5393 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5394}
5395
5396/*
5397 * :[N]buffer [N] to buffer N
5398 * :[N]sbuffer [N] to buffer N
5399 */
5400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005403 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005404 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 if (*eap->arg)
5406 eap->errmsg = e_trailing;
5407 else
5408 {
5409 if (eap->addr_count == 0) /* default is current buffer */
5410 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5411 else
5412 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005413 if (eap->do_ecmd_cmd != NULL)
5414 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 }
5416}
5417
5418/*
5419 * :[N]bmodified [N] to next mod. buffer
5420 * :[N]sbmodified [N] to next mod. buffer
5421 */
5422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005423ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424{
5425 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005426 if (eap->do_ecmd_cmd != NULL)
5427 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428}
5429
5430/*
5431 * :[N]bnext [N] to next buffer
5432 * :[N]sbnext [N] split and to next buffer
5433 */
5434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005435ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
5437 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005438 if (eap->do_ecmd_cmd != NULL)
5439 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440}
5441
5442/*
5443 * :[N]bNext [N] to previous buffer
5444 * :[N]bprevious [N] to previous buffer
5445 * :[N]sbNext [N] split and to previous buffer
5446 * :[N]sbprevious [N] split and to previous buffer
5447 */
5448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005449ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450{
5451 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005452 if (eap->do_ecmd_cmd != NULL)
5453 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454}
5455
5456/*
5457 * :brewind to first buffer
5458 * :bfirst to first buffer
5459 * :sbrewind split and to first buffer
5460 * :sbfirst split and to first buffer
5461 */
5462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005466 if (eap->do_ecmd_cmd != NULL)
5467 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468}
5469
5470/*
5471 * :blast to last buffer
5472 * :sblast split and to last buffer
5473 */
5474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005475ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005478 if (eap->do_ecmd_cmd != NULL)
5479 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 return (c == NUL || c == '|' || c == '"' || c == '\n');
5486}
5487
5488#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5489 || defined(PROTO)
5490/*
5491 * Return the next command, after the first '|' or '\n'.
5492 * Return NULL if not found.
5493 */
5494 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005495find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
5497 while (*p != '|' && *p != '\n')
5498 {
5499 if (*p == NUL)
5500 return NULL;
5501 ++p;
5502 }
5503 return (p + 1);
5504}
5505#endif
5506
5507/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005508 * Check if *p is a separator between Ex commands, skipping over white space.
5509 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 */
5511 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005514 char_u *s = skipwhite(p);
5515
5516 if (*s == '|' || *s == '\n')
5517 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 else
5519 return NULL;
5520}
5521
5522/*
5523 * - if there are more files to edit
5524 * - and this is the last window
5525 * - and forceit not used
5526 * - and not repeated twice on a row
5527 * return FAIL and give error message if 'message' TRUE
5528 * return OK otherwise
5529 */
5530 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531check_more(
5532 int message, /* when FALSE check only, no messages */
5533 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534{
5535 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5536
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005537 if (!forceit && only_one_window()
5538 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 if (message)
5541 {
5542#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5543 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5544 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005545 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005547 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5548 NGETTEXT("%d more file to edit. Quit anyway?",
5549 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5551 return OK;
5552 return FAIL;
5553 }
5554#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005555 semsg(NGETTEXT("E173: %d more file to edit",
5556 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 quitmore = 2; /* next try to quit is allowed */
5558 }
5559 return FAIL;
5560 }
5561 return OK;
5562}
5563
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005564#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565/*
5566 * Function given to ExpandGeneric() to obtain the list of command names.
5567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005569get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
5571 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 return cmdnames[idx].cmd_name;
5574}
5575#endif
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579{
Bram Moolenaare6850792010-05-14 15:28:44 +02005580 if (*eap->arg == NUL)
5581 {
5582#ifdef FEAT_EVAL
5583 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5584 char_u *p = NULL;
5585
5586 if (expr != NULL)
5587 {
5588 ++emsg_off;
5589 p = eval_to_string(expr, NULL, FALSE);
5590 --emsg_off;
5591 vim_free(expr);
5592 }
5593 if (p != NULL)
5594 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005595 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005596 vim_free(p);
5597 }
5598 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005599 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005600#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005601 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005602#endif
5603 }
5604 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005605 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005606
5607#ifdef FEAT_VTP
5608 else if (has_vtp_working())
5609 {
5610 // background color change requires clear + redraw
5611 update_screen(CLEAR);
5612 redrawcmd();
5613 }
5614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616
5617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
5620 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005621 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 do_highlight(eap->arg, eap->forceit, FALSE);
5623}
5624
5625
5626/*
5627 * Call this function if we thought we were going to exit, but we won't
5628 * (because of an error). May need to restore the terminal mode.
5629 */
5630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005631not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632{
5633 exiting = FALSE;
5634 settmode(TMODE_RAW);
5635}
5636
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005637 static int
5638before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5639{
5640 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5641
5642 /* Bail out when autocommands closed the window.
5643 * Refuse to quit when the buffer in the last window is being closed (can
5644 * only happen in autocommands). */
5645 if (!win_valid(wp)
5646 || curbuf_locked()
5647 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5648 return TRUE;
5649
5650 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5651 {
5652 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5653 /* Refuse to quit when locked or when the buffer in the last window is
5654 * being closed (can only happen in autocommands). */
5655 if (curbuf_locked()
5656 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5657 return TRUE;
5658 }
5659
5660 return FALSE;
5661}
5662
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005664 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005665 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 */
5667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005668ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005670 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672#ifdef FEAT_CMDWIN
5673 if (cmdwin_type != 0)
5674 {
5675 cmdwin_result = Ctrl_C;
5676 return;
5677 }
5678#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005679 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005680 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005681 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005682 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005683 return;
5684 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005685 if (eap->addr_count > 0)
5686 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005687 int wnr = eap->line2;
5688
5689 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5690 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005691 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005692 }
5693 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005694 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005695
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005696 /* Refuse to quit when locked. */
5697 if (curbuf_locked())
5698 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005699
5700 /* Trigger QuitPre and maybe ExitPre */
5701 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005702 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
5704#ifdef FEAT_NETBEANS_INTG
5705 netbeansForcedQuit = eap->forceit;
5706#endif
5707
5708 /*
5709 * If there are more files or windows we won't exit.
5710 */
5711 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5712 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005713 if ((!buf_hide(wp->w_buffer)
5714 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005715 | (eap->forceit ? CCGD_FORCEIT : 0)
5716 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005718 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 {
5720 not_exiting();
5721 }
5722 else
5723 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005724 /* quit last window
5725 * Note: only_one_window() returns true, even so a help window is
5726 * still open. In that case only quit, if no address has been
5727 * specified. Example:
5728 * :h|wincmd w|1q - don't quit
5729 * :h|wincmd w|q - quit
5730 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005731 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005733 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005734#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005738 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
5740}
5741
5742/*
5743 * ":cquit".
5744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005746ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 getout(1); /* this does not always pass on the exit code to the Manx
5749 compiler. why? */
5750}
5751
5752/*
5753 * ":qall": try to quit all windows
5754 */
5755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005756ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757{
5758# ifdef FEAT_CMDWIN
5759 if (cmdwin_type != 0)
5760 {
5761 if (eap->forceit)
5762 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5763 else
5764 cmdwin_result = K_XF2;
5765 return;
5766 }
5767# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005768
5769 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005770 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005771 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005772 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005773 return;
5774 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005775
5776 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005777 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005780 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 getout(0);
5782 not_exiting();
5783}
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785/*
5786 * ":close": close current window, unless it is the last one
5787 */
5788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005791 win_T *win;
5792 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005793#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005795 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005797#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005798 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005799 {
5800 if (eap->addr_count == 0)
5801 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005802 else
5803 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005804 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005805 {
5806 winnr++;
5807 if (winnr == eap->line2)
5808 break;
5809 }
5810 if (win == NULL)
5811 win = lastwin;
5812 ex_win_close(eap->forceit, win, NULL);
5813 }
5814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815}
5816
Bram Moolenaar4033c552017-09-16 20:54:51 +02005817#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005818/*
5819 * ":pclose": Close any preview window.
5820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005822ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005823{
5824 win_T *win;
5825
Bram Moolenaar79648732019-07-18 21:43:07 +02005826 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005827 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005828 if (win->w_p_pvw)
5829 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005830 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005831 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005832 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005833# ifdef FEAT_TEXT_PROP
5834 // Also when 'previewpopup' is empty, it might have been cleared.
5835 popup_close_preview();
5836# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005837}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005838#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005839
Bram Moolenaarf740b292006-02-16 22:11:02 +00005840/*
5841 * Close window "win" and take care of handling closing the last window for a
5842 * modified buffer.
5843 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005845ex_win_close(
5846 int forceit,
5847 win_T *win,
5848 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 int need_hide;
5851 buf_T *buf = win->w_buffer;
5852
5853 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005854 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005856#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 if ((p_confirm || cmdmod.confirm) && p_write)
5858 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005859 bufref_T bufref;
5860
5861 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005863 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 return;
5865 need_hide = FALSE;
5866 }
5867 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005870 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 return;
5872 }
5873 }
5874
Bram Moolenaar4033c552017-09-16 20:54:51 +02005875#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005877#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005880 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005881 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005882 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005883 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884}
5885
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005887 * Handle the argument for a tabpage related ex command.
5888 * Returns a tabpage number.
5889 * When an error is encountered then eap->errmsg is set.
5890 */
5891 static int
5892get_tabpage_arg(exarg_T *eap)
5893{
5894 int tab_number;
5895 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5896
5897 if (eap->arg && *eap->arg != NUL)
5898 {
5899 char_u *p = eap->arg;
5900 char_u *p_save;
5901 int relative = 0; /* argument +N/-N means: go to N places to the
5902 * right/left relative to the current position. */
5903
5904 if (*p == '-')
5905 {
5906 relative = -1;
5907 p++;
5908 }
5909 else if (*p == '+')
5910 {
5911 relative = 1;
5912 p++;
5913 }
5914
5915 p_save = p;
5916 tab_number = getdigits(&p);
5917
5918 if (relative == 0)
5919 {
5920 if (STRCMP(p, "$") == 0)
5921 tab_number = LAST_TAB_NR;
5922 else if (p == p_save || *p_save == '-' || *p != NUL
5923 || tab_number > LAST_TAB_NR)
5924 {
5925 /* No numbers as argument. */
5926 eap->errmsg = e_invarg;
5927 goto theend;
5928 }
5929 }
5930 else
5931 {
5932 if (*p_save == NUL)
5933 tab_number = 1;
5934 else if (p == p_save || *p_save == '-' || *p != NUL
5935 || tab_number == 0)
5936 {
5937 /* No numbers as argument. */
5938 eap->errmsg = e_invarg;
5939 goto theend;
5940 }
5941 tab_number = tab_number * relative + tabpage_index(curtab);
5942 if (!unaccept_arg0 && relative == -1)
5943 --tab_number;
5944 }
5945 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5946 eap->errmsg = e_invarg;
5947 }
5948 else if (eap->addr_count > 0)
5949 {
5950 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005951 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005952 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005953 tab_number = 0;
5954 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005955 else
5956 {
5957 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005958 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005959 {
5960 --tab_number;
5961 if (tab_number < unaccept_arg0)
5962 eap->errmsg = e_invarg;
5963 }
5964 }
5965 }
5966 else
5967 {
5968 switch (eap->cmdidx)
5969 {
5970 case CMD_tabnext:
5971 tab_number = tabpage_index(curtab) + 1;
5972 if (tab_number > LAST_TAB_NR)
5973 tab_number = 1;
5974 break;
5975 case CMD_tabmove:
5976 tab_number = LAST_TAB_NR;
5977 break;
5978 default:
5979 tab_number = tabpage_index(curtab);
5980 }
5981 }
5982
5983theend:
5984 return tab_number;
5985}
5986
5987/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005988 * ":tabclose": close current tab page, unless it is the last one.
5989 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
5991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005994 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005995 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005996
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005997# ifdef FEAT_CMDWIN
5998 if (cmdwin_type != 0)
5999 cmdwin_result = K_IGNORE;
6000 else
6001# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006002 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006003 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006006 tab_number = get_tabpage_arg(eap);
6007 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006008 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006009 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006010 if (tp == NULL)
6011 {
6012 beep_flush();
6013 return;
6014 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006015 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006016 {
6017 tabpage_close_other(tp, eap->forceit);
6018 return;
6019 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006020 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006021 tabpage_close(eap->forceit);
6022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006024}
6025
6026/*
6027 * ":tabonly": close all tab pages except the current one
6028 */
6029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006030ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006031{
6032 tabpage_T *tp;
6033 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006034 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006035
6036# ifdef FEAT_CMDWIN
6037 if (cmdwin_type != 0)
6038 cmdwin_result = K_IGNORE;
6039 else
6040# endif
6041 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006042 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006043 else
6044 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006045 tab_number = get_tabpage_arg(eap);
6046 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006047 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006048 goto_tabpage(tab_number);
6049 /* Repeat this up to a 1000 times, because autocommands may
6050 * mess up the lists. */
6051 for (done = 0; done < 1000; ++done)
6052 {
6053 FOR_ALL_TABPAGES(tp)
6054 if (tp->tp_topframe != topframe)
6055 {
6056 tabpage_close_other(tp, eap->forceit);
6057 /* if we failed to close it quit */
6058 if (valid_tabpage(tp))
6059 done = 1000;
6060 /* start over, "tp" is now invalid */
6061 break;
6062 }
6063 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006064 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006065 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006066 }
6067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
6070/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006071 * Close the current tab page.
6072 */
6073 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006074tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006075{
6076 /* First close all the windows but the current one. If that worked then
6077 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006078 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006079 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006080 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006081 ex_win_close(forceit, curwin, NULL);
6082# ifdef FEAT_GUI
6083 need_mouse_correct = TRUE;
6084# endif
6085}
6086
6087/*
6088 * Close tab page "tp", which is not the current tab page.
6089 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006090 * Also takes care of the tab pages line disappearing when closing the
6091 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006092 */
6093 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006094tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006095{
6096 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006097 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006098 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006099
6100 /* Limit to 1000 windows, autocommands may add a window while we close
6101 * one. OK, so I'm paranoid... */
6102 while (++done < 1000)
6103 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006104 wp = tp->tp_firstwin;
6105 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006106
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006107 /* Autocommands may delete the tab page under our fingers and we may
6108 * fail to close a window with a modified buffer. */
6109 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006110 break;
6111 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006112
Bram Moolenaar29323592016-07-24 22:04:11 +02006113 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006114
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006115 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006116 if (h != tabline_height())
6117 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006118}
6119
6120/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 * ":only".
6122 */
6123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006124ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006126 win_T *wp;
6127 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128# ifdef FEAT_GUI
6129 need_mouse_correct = TRUE;
6130# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006131 if (eap->addr_count > 0)
6132 {
6133 wnr = eap->line2;
6134 for (wp = firstwin; --wnr > 0; )
6135 {
6136 if (wp->w_next == NULL)
6137 break;
6138 else
6139 wp = wp->w_next;
6140 }
6141 win_goto(wp);
6142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 close_others(TRUE, eap->forceit);
6144}
6145
6146/*
6147 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006148 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006151ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
6153 if (eap->addr_count == 0)
6154 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006155 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006159ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006161 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006162 if (!eap->skip)
6163 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006164#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006165 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006166#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006167 if (eap->addr_count == 0)
6168 win_close(curwin, FALSE); /* don't free buffer */
6169 else
6170 {
6171 int winnr = 0;
6172 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006173
Bram Moolenaar2256c992016-11-15 21:17:07 +01006174 FOR_ALL_WINDOWS(win)
6175 {
6176 winnr++;
6177 if (winnr == eap->line2)
6178 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006179 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006180 if (win == NULL)
6181 win = lastwin;
6182 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185}
6186
6187/*
6188 * ":stop" and ":suspend": Suspend Vim.
6189 */
6190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006191ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
6193 /*
6194 * Disallow suspending for "rvim".
6195 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006196 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {
6198 if (!eap->forceit)
6199 autowrite_all();
6200 windgoto((int)Rows - 1, 0);
6201 out_char('\n');
6202 out_flush();
6203 stoptermcap();
6204 out_flush(); /* needed for SUN to restore xterm buffer */
6205#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006206 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207#endif
6208 ui_suspend(); /* call machine specific function */
6209#ifdef FEAT_TITLE
6210 maketitle();
6211 resettitle(); /* force updating the title */
6212#endif
6213 starttermcap();
6214 scroll_start(); /* scroll screen before redrawing */
6215 redraw_later_clear();
6216 shell_resized(); /* may have resized window */
6217 }
6218}
6219
6220/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006221 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 */
6223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006224ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226#ifdef FEAT_CMDWIN
6227 if (cmdwin_type != 0)
6228 {
6229 cmdwin_result = Ctrl_C;
6230 return;
6231 }
6232#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006233 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006234 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006235 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006236 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006237 return;
6238 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006239
6240 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006241 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242
6243 /*
6244 * if more files or windows we won't exit
6245 */
6246 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6247 exiting = TRUE;
6248 if ( ((eap->cmdidx == CMD_wq
6249 || curbufIsChanged())
6250 && do_write(eap) == FAIL)
6251 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006252 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
6254 not_exiting();
6255 }
6256 else
6257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006260 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261# ifdef FEAT_GUI
6262 need_mouse_correct = TRUE;
6263# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006264 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006265 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 }
6267}
6268
6269/*
6270 * ":print", ":list", ":number".
6271 */
6272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006273ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006275 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006276 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006277 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006279 for ( ;!got_int; ui_breakcheck())
6280 {
6281 print_line(eap->line1,
6282 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6283 || (eap->flags & EXFLAG_NR)),
6284 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6285 if (++eap->line1 > eap->line2)
6286 break;
6287 out_flush(); /* show one line at a time */
6288 }
6289 setpcmark();
6290 /* put cursor at last line */
6291 curwin->w_cursor.lnum = eap->line2;
6292 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 }
6294
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296}
6297
6298#ifdef FEAT_BYTEOFF
6299 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006300ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 goto_byte(eap->line2);
6303}
6304#endif
6305
6306/*
6307 * ":shell".
6308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006310ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311{
6312 do_shell(NULL, 0);
6313}
6314
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006315#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006317static int drop_busy = FALSE;
6318static int drop_filec;
6319static char_u **drop_filev = NULL;
6320static int drop_split;
6321static void (*drop_callback)(void *);
6322static void *drop_cookie;
6323
6324 static void
6325handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 exarg_T ea;
6328 int save_msg_scroll = msg_scroll;
6329
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006330 // Setting the argument list may cause screen updates and being called
6331 // recursively. Avoid that by setting drop_busy.
6332 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334 /* Check whether the current buffer is changed. If so, we will need
6335 * to split the current window or data could be lost.
6336 * We don't need to check if the 'hidden' option is set, as in this
6337 * case the buffer won't be lost.
6338 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006339 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
6341 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006342 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 --emsg_off;
6344 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006345 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 if (win_split(0, 0) == FAIL)
6348 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006349 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350
6351 /* When splitting the window, create a new alist. Otherwise the
6352 * existing one is overwritten. */
6353 alist_unlink(curwin->w_alist);
6354 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 }
6356
6357 /*
6358 * Set up the new argument list.
6359 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006360 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 /*
6363 * Move to the first file.
6364 */
6365 /* Fake up a minimal "next" command for do_argfile() */
6366 vim_memset(&ea, 0, sizeof(ea));
6367 ea.cmd = (char_u *)"next";
6368 do_argfile(&ea, 0);
6369
6370 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6371 * mode that is not needed here. */
6372 need_start_insertmode = FALSE;
6373
6374 /* Restore msg_scroll, otherwise a following command may cause scrolling
6375 * unexpectedly. The screen will be redrawn by the caller, thus
6376 * msg_scroll being set by displaying a message is irrelevant. */
6377 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006378
6379 if (drop_callback != NULL)
6380 drop_callback(drop_cookie);
6381
6382 drop_filev = NULL;
6383 drop_busy = FALSE;
6384}
6385
6386/*
6387 * Handle a file drop. The code is here because a drop is *nearly* like an
6388 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006389 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006390 * code is very similar to :args and hence needs access to a lot of the static
6391 * functions in this file.
6392 *
6393 * The "filev" list must have been allocated using alloc(), as should each item
6394 * in the list. This function takes over responsibility for freeing the "filev"
6395 * list.
6396 */
6397 void
6398handle_drop(
6399 int filec, // the number of files dropped
6400 char_u **filev, // the list of files dropped
6401 int split, // force splitting the window
6402 void (*callback)(void *), // to be called after setting the argument
6403 // list
6404 void *cookie) // argument for "callback" (allocated)
6405{
6406 // Cannot handle recursive drops, finish the pending one.
6407 if (drop_busy)
6408 {
6409 FreeWild(filec, filev);
6410 vim_free(cookie);
6411 return;
6412 }
6413
6414 // When calling handle_drop() more than once in a row we only use the last
6415 // one.
6416 if (drop_filev != NULL)
6417 {
6418 FreeWild(drop_filec, drop_filev);
6419 vim_free(drop_cookie);
6420 }
6421
6422 drop_filec = filec;
6423 drop_filev = filev;
6424 drop_split = split;
6425 drop_callback = callback;
6426 drop_cookie = cookie;
6427
6428 // Postpone this when:
6429 // - editing the command line
6430 // - not possible to change the current buffer
6431 // - updating the screen
6432 // As it may change buffers and window structures that are in use and cause
6433 // freed memory to be used.
6434 if (text_locked() || curbuf_locked() || updating_screen)
6435 return;
6436
6437 handle_drop_internal();
6438}
6439
6440/*
6441 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6442 * reset: Handle a postponed drop.
6443 */
6444 void
6445handle_any_postponed_drop(void)
6446{
6447 if (!drop_busy && drop_filev != NULL
6448 && !text_locked() && !curbuf_locked() && !updating_screen)
6449 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450}
6451#endif
6452
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453/*
6454 * Clear an argument list: free all file names and reset it to zero entries.
6455 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006457alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459 while (--al->al_ga.ga_len >= 0)
6460 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6461 ga_clear(&al->al_ga);
6462}
6463
6464/*
6465 * Init an argument list.
6466 */
6467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006468alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6471}
6472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473/*
6474 * Remove a reference from an argument list.
6475 * Ignored when the argument list is the global one.
6476 * If the argument list is no longer used by any window, free it.
6477 */
6478 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006479alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
6481 if (al != &global_alist && --al->al_refcount <= 0)
6482 {
6483 alist_clear(al);
6484 vim_free(al);
6485 }
6486}
6487
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488/*
6489 * Create a new argument list and use it for the current window.
6490 */
6491 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006492alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006494 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 if (curwin->w_alist == NULL)
6496 {
6497 curwin->w_alist = &global_alist;
6498 ++global_alist.al_refcount;
6499 }
6500 else
6501 {
6502 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006503 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 alist_init(curwin->w_alist);
6505 }
6506}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006508#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509/*
6510 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006511 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6512 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 */
6514 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006515alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006518 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 char_u **new_arg_files;
6520 int new_arg_file_count;
6521 char_u *save_p_su = p_su;
6522 int i;
6523
6524 /* Don't use 'suffixes' here. This should work like the shell did the
6525 * expansion. Also, the vimrc file isn't read yet, thus the user
6526 * can't set the options. */
6527 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006528 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 if (old_arg_files != NULL)
6530 {
6531 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006532 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6533 old_arg_count = GARGCOUNT;
6534 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006536 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 && new_arg_file_count > 0)
6538 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006539 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6540 TRUE, fnum_list, fnum_len);
6541 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 }
6544 p_su = save_p_su;
6545}
6546#endif
6547
6548/*
6549 * Set the argument list for the current window.
6550 * Takes over the allocated files[] and the allocated fnames in it.
6551 */
6552 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006553alist_set(
6554 alist_T *al,
6555 int count,
6556 char_u **files,
6557 int use_curbuf,
6558 int *fnum_list,
6559 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560{
6561 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006562 static int recursive = 0;
6563
6564 if (recursive)
6565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006566 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006567 return;
6568 }
6569 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
6571 alist_clear(al);
6572 if (ga_grow(&al->al_ga, count) == OK)
6573 {
6574 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006575 {
6576 if (got_int)
6577 {
6578 /* When adding many buffers this can take a long time. Allow
6579 * interrupting here. */
6580 while (i < count)
6581 vim_free(files[i++]);
6582 break;
6583 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006584
6585 /* May set buffer name of a buffer previously used for the
6586 * argument list, so that it's re-used by alist_add. */
6587 if (fnum_list != NULL && i < fnum_len)
6588 buf_set_name(fnum_list[i], files[i]);
6589
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006591 ui_breakcheck();
6592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 vim_free(files);
6594 }
6595 else
6596 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006599
6600 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601}
6602
6603/*
6604 * Add file "fname" to argument list "al".
6605 * "fname" must have been allocated and "al" must have been checked for room.
6606 */
6607 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006608alist_add(
6609 alist_T *al,
6610 char_u *fname,
6611 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612{
6613 if (fname == NULL) /* don't add NULL file names */
6614 return;
6615#ifdef BACKSLASH_IN_FILENAME
6616 slash_adjust(fname);
6617#endif
6618 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6619 if (set_fnum > 0)
6620 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6621 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6622 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623}
6624
6625#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6626/*
6627 * Adjust slashes in file names. Called after 'shellslash' was set.
6628 */
6629 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006630alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006634 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636 for (i = 0; i < GARGCOUNT; ++i)
6637 if (GARGLIST[i].ae_fname != NULL)
6638 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006639 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 if (wp->w_alist != &global_alist)
6641 for (i = 0; i < WARGCOUNT(wp); ++i)
6642 if (WARGLIST(wp)[i].ae_fname != NULL)
6643 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644}
6645#endif
6646
6647/*
6648 * ":preserve".
6649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006651ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006653 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 ml_preserve(curbuf, TRUE);
6655}
6656
6657/*
6658 * ":recover".
6659 */
6660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006661ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662{
6663 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6664 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006665 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6666 | CCGD_MULTWIN
6667 | (eap->forceit ? CCGD_FORCEIT : 0)
6668 | CCGD_EXCMD)
6669
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 && (*eap->arg == NUL
6671 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006672 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 recoverymode = FALSE;
6674}
6675
6676/*
6677 * Command modifier used in a wrong way.
6678 */
6679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006680ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681{
6682 eap->errmsg = e_invcmd;
6683}
6684
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685/*
6686 * :sview [+command] file split window with new file, read-only
6687 * :split [[+command] file] split window with current or new file
6688 * :vsplit [[+command] file] split window vertically with current or new file
6689 * :new [[+command] file] split window with no or new file
6690 * :vnew [[+command] file] split vertically window with no or new file
6691 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006692 *
6693 * :tabedit open new Tab page with empty window
6694 * :tabedit [+command] file open new Tab page and edit "file"
6695 * :tabnew [[+command] file] just like :tabedit
6696 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 */
6698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006699ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006701 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006702#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006704#endif
6705#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006707#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006708 int use_tab = eap->cmdidx == CMD_tabedit
6709 || eap->cmdidx == CMD_tabfind
6710 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006712 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006713 return;
6714
Bram Moolenaar4033c552017-09-16 20:54:51 +02006715#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
Bram Moolenaar4033c552017-09-16 20:54:51 +02006719#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006721 * quickfix windows. But it's OK when doing ":tab split". */
6722 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {
6724 if (eap->cmdidx == CMD_split)
6725 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (eap->cmdidx == CMD_vsplit)
6727 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
Bram Moolenaar4033c552017-09-16 20:54:51 +02006731#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006732 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 {
6734 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6735 FNAME_MESS, TRUE, curbuf->b_ffname);
6736 if (fname == NULL)
6737 goto theend;
6738 eap->arg = fname;
6739 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006740# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006741 else
6742# endif
6743#endif
6744#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 && eap->cmdidx != CMD_new)
6748 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006749 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006750# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006751 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006752# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006753 au_has_group((char_u *)"FileExplorer"))
6754 {
6755 /* No browsing supported but we do have the file explorer:
6756 * Edit the directory. */
6757 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6758 eap->arg = (char_u *)".";
6759 }
6760 else
6761 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006762 fname = do_browse(0, (char_u *)(use_tab
6763 ? _("Edit File in new tab page")
6764 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006766 if (fname == NULL)
6767 goto theend;
6768 eap->arg = fname;
6769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 }
6771 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006774 /*
6775 * Either open new tab page or split the window.
6776 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006777 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006778 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006779 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6780 : eap->addr_count == 0 ? 0
6781 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006782 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006783 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006784
6785 /* set the alternate buffer for the window we came from */
6786 if (curwin != old_curwin
6787 && win_valid(old_curwin)
6788 && old_curwin->w_buffer != curbuf
6789 && !cmdmod.keepalt)
6790 old_curwin->w_alt_fnum = curbuf->b_fnum;
6791 }
6792 }
6793 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 /* Reset 'scrollbind' when editing another file, but keep it when
6797 * doing ":split" without arguments. */
6798 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006799# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006803 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 else
6805 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 do_exedit(eap, old_curwin);
6807 }
6808
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006809# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006813# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814theend:
6815 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006818
6819/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006820 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006821 */
6822 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006823tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006824{
6825 exarg_T ea;
6826
6827 vim_memset(&ea, 0, sizeof(ea));
6828 ea.cmdidx = CMD_tabnew;
6829 ea.cmd = (char_u *)"tabn";
6830 ea.arg = (char_u *)"";
6831 ex_splitview(&ea);
6832}
6833
6834/*
6835 * :tabnext command
6836 */
6837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006838ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006839{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006840 int tab_number;
6841
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006842 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006843 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006844 switch (eap->cmdidx)
6845 {
6846 case CMD_tabfirst:
6847 case CMD_tabrewind:
6848 goto_tabpage(1);
6849 break;
6850 case CMD_tablast:
6851 goto_tabpage(9999);
6852 break;
6853 case CMD_tabprevious:
6854 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006855 if (eap->arg && *eap->arg != NUL)
6856 {
6857 char_u *p = eap->arg;
6858 char_u *p_save = p;
6859
6860 tab_number = getdigits(&p);
6861 if (p == p_save || *p_save == '-' || *p != NUL
6862 || tab_number == 0)
6863 {
6864 /* No numbers as argument. */
6865 eap->errmsg = e_invarg;
6866 return;
6867 }
6868 }
6869 else
6870 {
6871 if (eap->addr_count == 0)
6872 tab_number = 1;
6873 else
6874 {
6875 tab_number = eap->line2;
6876 if (tab_number < 1)
6877 {
6878 eap->errmsg = e_invrange;
6879 return;
6880 }
6881 }
6882 }
6883 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006884 break;
6885 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006886 tab_number = get_tabpage_arg(eap);
6887 if (eap->errmsg == NULL)
6888 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006889 break;
6890 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006891}
6892
6893/*
6894 * :tabmove command
6895 */
6896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006897ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006898{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006899 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006900
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006901 tab_number = get_tabpage_arg(eap);
6902 if (eap->errmsg == NULL)
6903 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006904}
6905
6906/*
6907 * :tabs command: List tabs and their contents.
6908 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006910ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006911{
6912 tabpage_T *tp;
6913 win_T *wp;
6914 int tabcount = 1;
6915
6916 msg_start();
6917 msg_scroll = TRUE;
6918 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6919 {
6920 msg_putchar('\n');
6921 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006922 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006923 out_flush(); /* output one line at a time */
6924 ui_breakcheck();
6925
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006926 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006927 wp = firstwin;
6928 else
6929 wp = tp->tp_firstwin;
6930 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6931 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006932 msg_putchar('\n');
6933 msg_putchar(wp == curwin ? '>' : ' ');
6934 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006935 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6936 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006937 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006938 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006939 else
6940 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6941 IObuff, IOSIZE, TRUE);
6942 msg_outtrans(IObuff);
6943 out_flush(); /* output one line at a time */
6944 ui_breakcheck();
6945 }
6946 }
6947}
6948
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949/*
6950 * ":mode": Set screen mode.
6951 * If no argument given, just get the screen size and redraw.
6952 */
6953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006954ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955{
6956 if (*eap->arg == NUL)
6957 shell_resized();
6958 else
6959 mch_screenmode(eap->arg);
6960}
6961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962/*
6963 * ":resize".
6964 * set, increment or decrement current window height
6965 */
6966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006967ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
6969 int n;
6970 win_T *wp = curwin;
6971
6972 if (eap->addr_count > 0)
6973 {
6974 n = eap->line2;
6975 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6976 ;
6977 }
6978
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006979# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006981# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 if (cmdmod.split & WSP_VERT)
6984 {
6985 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006986 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6988 n = 9999;
6989 win_setwidth_win((int)n, wp);
6990 }
6991 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 {
6993 if (*eap->arg == '-' || *eap->arg == '+')
6994 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006995 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 n = 9999;
6997 win_setheight_win((int)n, wp);
6998 }
6999}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001/*
7002 * ":find [+command] <file>" command.
7003 */
7004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007005ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006{
7007#ifdef FEAT_SEARCHPATH
7008 char_u *fname;
7009 int count;
7010
7011 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7012 TRUE, curbuf->b_ffname);
7013 if (eap->addr_count > 0)
7014 {
7015 /* Repeat finding the file "count" times. This matters when it
7016 * appears several times in the path. */
7017 count = eap->line2;
7018 while (fname != NULL && --count > 0)
7019 {
7020 vim_free(fname);
7021 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7022 FALSE, curbuf->b_ffname);
7023 }
7024 }
7025
7026 if (fname != NULL)
7027 {
7028 eap->arg = fname;
7029#endif
7030 do_exedit(eap, NULL);
7031#ifdef FEAT_SEARCHPATH
7032 vim_free(fname);
7033 }
7034#endif
7035}
7036
7037/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007038 * ":open" simulation: for now just work like ":visual".
7039 */
7040 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007042{
7043 regmatch_T regmatch;
7044 char_u *p;
7045
7046 curwin->w_cursor.lnum = eap->line2;
7047 beginline(BL_SOL | BL_FIX);
7048 if (*eap->arg == '/')
7049 {
7050 /* ":open /pattern/": put cursor in column found with pattern */
7051 ++eap->arg;
7052 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7053 *p = NUL;
7054 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7055 if (regmatch.regprog != NULL)
7056 {
7057 regmatch.rm_ic = p_ic;
7058 p = ml_get_curline();
7059 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007060 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007061 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007062 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007063 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007064 }
7065 /* Move to the NUL, ignore any other arguments. */
7066 eap->arg += STRLEN(eap->arg);
7067 }
7068 check_cursor();
7069
7070 eap->cmdidx = CMD_visual;
7071 do_exedit(eap, NULL);
7072}
7073
7074/*
7075 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 */
7077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007078ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079{
7080 do_exedit(eap, NULL);
7081}
7082
7083/*
7084 * ":edit <file>" command and alikes.
7085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007087do_exedit(
7088 exarg_T *eap,
7089 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
7091 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007093 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007095 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007096 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 /*
7098 * ":vi" command ends Ex mode.
7099 */
7100 if (exmode_active && (eap->cmdidx == CMD_visual
7101 || eap->cmdidx == CMD_view))
7102 {
7103 exmode_active = FALSE;
7104 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007105 {
7106 /* Special case: ":global/pat/visual\NLvi-commands" */
7107 if (global_busy)
7108 {
7109 int rd = RedrawingDisabled;
7110 int nwr = no_wait_return;
7111 int ms = msg_scroll;
7112#ifdef FEAT_GUI
7113 int he = hold_gui_events;
7114#endif
7115
7116 if (eap->nextcmd != NULL)
7117 {
7118 stuffReadbuff(eap->nextcmd);
7119 eap->nextcmd = NULL;
7120 }
7121
7122 if (exmode_was != EXMODE_VIM)
7123 settmode(TMODE_RAW);
7124 RedrawingDisabled = 0;
7125 no_wait_return = 0;
7126 need_wait_return = FALSE;
7127 msg_scroll = 0;
7128#ifdef FEAT_GUI
7129 hold_gui_events = 0;
7130#endif
7131 must_redraw = CLEAR;
7132
7133 main_loop(FALSE, TRUE);
7134
7135 RedrawingDisabled = rd;
7136 no_wait_return = nwr;
7137 msg_scroll = ms;
7138#ifdef FEAT_GUI
7139 hold_gui_events = he;
7140#endif
7141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 }
7145
7146 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007147 || eap->cmdidx == CMD_tabnew
7148 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007149 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007151 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 setpcmark();
7153 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007154 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7155 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007157 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 || *eap->arg != NUL
7159#ifdef FEAT_BROWSE
7160 || cmdmod.browse
7161#endif
7162 )
7163 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007164 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7165 * can bring us here, others are stopped earlier. */
7166 if (*eap->arg != NUL && curbuf_locked())
7167 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 n = readonlymode;
7170 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7171 readonlymode = TRUE;
7172 else if (eap->cmdidx == CMD_enew)
7173 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7174 empty buffer */
7175 setpcmark();
7176 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7177 NULL, eap,
7178 /* ":edit" goes to first line if Vi compatible */
7179 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7180 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7181 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007182 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007184 /* after a split we can use an existing buffer */
7185 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007187 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
7189 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (old_curwin != NULL)
7191 {
7192 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007193 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007195#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007196 cleanup_T cs;
7197
7198 /* Reset the error/interrupt/exception state here so that
7199 * aborting() returns FALSE when closing a window. */
7200 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007201#endif
7202#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007204#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007205 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007206
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007207#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007208 /* Restore the error/interrupt/exception state if not
7209 * discarded by a new aborting error, interrupt, or
7210 * uncaught exception. */
7211 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 }
7214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 }
7216 else if (readonlymode && curbuf->b_nwindows == 1)
7217 {
7218 /* When editing an already visited buffer, 'readonly' won't be set
7219 * but the previous value is kept. With ":view" and ":sview" we
7220 * want the file to be readonly, except when another window is
7221 * editing the same buffer. */
7222 curbuf->b_p_ro = TRUE;
7223 }
7224 readonlymode = n;
7225 }
7226 else
7227 {
7228 if (eap->do_ecmd_cmd != NULL)
7229 do_cmdline_cmd(eap->do_ecmd_cmd);
7230#ifdef FEAT_TITLE
7231 n = curwin->w_arg_idx_invalid;
7232#endif
7233 check_arg_idx(curwin);
7234#ifdef FEAT_TITLE
7235 if (n != curwin->w_arg_idx_invalid)
7236 maketitle();
7237#endif
7238 }
7239
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 /*
7241 * if ":split file" worked, set alternate file name in old window to new
7242 * file
7243 */
7244 if (old_curwin != NULL
7245 && *eap->arg != NUL
7246 && curwin != old_curwin
7247 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007248 && old_curwin->w_buffer != curbuf
7249 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
7252 ex_no_reprint = TRUE;
7253}
7254
7255#ifndef FEAT_GUI
7256/*
7257 * ":gui" and ":gvim" when there is no GUI.
7258 */
7259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007260ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
7262 eap->errmsg = e_nogvim;
7263}
7264#endif
7265
Bram Moolenaar4f974752019-02-17 17:44:42 +01007266#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007268ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
7270 gui_make_tearoff(eap->arg);
7271}
7272#endif
7273
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007274#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7275 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007277ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007279# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7280 if (gui.in_use)
7281 gui_make_popup(eap->arg, eap->forceit);
7282# ifdef FEAT_TERM_POPUP_MENU
7283 else
7284# endif
7285# endif
7286# ifdef FEAT_TERM_POPUP_MENU
7287 pum_make_popup(eap->arg, eap->forceit);
7288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289}
7290#endif
7291
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007293ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
7295 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007296 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007298 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299}
7300
7301/*
7302 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7303 * offset.
7304 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007307ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007310 win_T *save_curwin = curwin;
7311 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 long topline;
7313 long y;
7314 linenr_T old_linenr = curwin->w_cursor.lnum;
7315
7316 setpcmark();
7317
7318 /*
7319 * determine max topline
7320 */
7321 if (curwin->w_p_scb)
7322 {
7323 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007324 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 {
7326 if (wp->w_p_scb && wp->w_buffer)
7327 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007328 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 if (topline > y)
7330 topline = y;
7331 }
7332 }
7333 if (topline < 1)
7334 topline = 1;
7335 }
7336 else
7337 {
7338 topline = 1;
7339 }
7340
7341
7342 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007343 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007345 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
7347 if (curwin->w_p_scb)
7348 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007349 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 y = topline - curwin->w_topline;
7351 if (y > 0)
7352 scrollup(y, TRUE);
7353 else
7354 scrolldown(-y, TRUE);
7355 curwin->w_scbind_pos = topline;
7356 redraw_later(VALID);
7357 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 }
7360 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007361 curwin = save_curwin;
7362 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (curwin->w_p_scb)
7364 {
7365 did_syncbind = TRUE;
7366 checkpcmark();
7367 if (old_linenr != curwin->w_cursor.lnum)
7368 {
7369 char_u ctrl_o[2];
7370
7371 ctrl_o[0] = Ctrl_O;
7372 ctrl_o[1] = 0;
7373 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7374 }
7375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376}
7377
7378
7379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007380ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007382 int i;
7383 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7384 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
7386 if (eap->usefilter) /* :r!cmd */
7387 do_bang(1, eap, FALSE, FALSE, TRUE);
7388 else
7389 {
7390 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7391 return;
7392
7393#ifdef FEAT_BROWSE
7394 if (cmdmod.browse)
7395 {
7396 char_u *browseFile;
7397
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007398 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 NULL, NULL, NULL, curbuf);
7400 if (browseFile != NULL)
7401 {
7402 i = readfile(browseFile, NULL,
7403 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7404 vim_free(browseFile);
7405 }
7406 else
7407 i = OK;
7408 }
7409 else
7410#endif
7411 if (*eap->arg == NUL)
7412 {
7413 if (check_fname() == FAIL) /* check for no file name */
7414 return;
7415 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7416 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7417 }
7418 else
7419 {
7420 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7421 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7422 i = readfile(eap->arg, NULL,
7423 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7424
7425 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007426 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007428#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 if (!aborting())
7430#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007431 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 }
7433 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007434 {
7435 if (empty && exmode_active)
7436 {
7437 /* Delete the empty line that remains. Historically ex does
7438 * this but vi doesn't. */
7439 if (eap->line2 == 0)
7440 lnum = curbuf->b_ml.ml_line_count;
7441 else
7442 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007443 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007444 {
7445 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007446 if (curwin->w_cursor.lnum > 1
7447 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007448 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007449 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007450 }
7451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 }
7455}
7456
Bram Moolenaarea408852005-06-25 22:49:46 +00007457static char_u *prev_dir = NULL;
7458
7459#if defined(EXITFREE) || defined(PROTO)
7460 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007461free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007462{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007463 VIM_CLEAR(prev_dir);
7464 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007465}
7466#endif
7467
Bram Moolenaarf4258302013-06-02 18:20:17 +02007468/*
7469 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007470 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7471 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007472 */
7473 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007474post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007475{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007476 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007477 // Clear tab local directory for both :cd and :tcd
7478 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007479 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007480 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007481 {
7482 /* If still in global directory, need to remember current
7483 * directory as global directory. */
7484 if (globaldir == NULL && prev_dir != NULL)
7485 globaldir = vim_strsave(prev_dir);
7486 /* Remember this local directory for the window. */
7487 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007488 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007489 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007490 curtab->tp_localdir = vim_strsave(NameBuff);
7491 else
7492 curwin->w_localdir = vim_strsave(NameBuff);
7493 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007494 }
7495 else
7496 {
7497 /* We are now in the global directory, no need to remember its
7498 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007499 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007500 }
7501
7502 shorten_fnames(TRUE);
7503}
7504
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007505/*
7506 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7507 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7508 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7509 * Otherwise changes the global directory.
7510 * Returns TRUE if the directory is successfully changed.
7511 */
7512 int
7513changedir_func(
7514 char_u *new_dir,
7515 int forceit,
7516 cdscope_T scope)
7517{
7518 char_u *tofree;
7519 int dir_differs;
7520 int retval = FALSE;
7521
7522 if (allbuf_locked())
7523 return FALSE;
7524
7525 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7526 {
7527 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7528 return FALSE;
7529 }
7530
7531 // ":cd -": Change to previous directory
7532 if (STRCMP(new_dir, "-") == 0)
7533 {
7534 if (prev_dir == NULL)
7535 {
7536 emsg(_("E186: No previous directory"));
7537 return FALSE;
7538 }
7539 new_dir = prev_dir;
7540 }
7541
7542 // Save current directory for next ":cd -"
7543 tofree = prev_dir;
7544 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7545 prev_dir = vim_strsave(NameBuff);
7546 else
7547 prev_dir = NULL;
7548
7549#if defined(UNIX) || defined(VMS)
7550 // for UNIX ":cd" means: go to home directory
7551 if (*new_dir == NUL)
7552 {
7553 // use NameBuff for home directory name
7554# ifdef VMS
7555 char_u *p;
7556
7557 p = mch_getenv((char_u *)"SYS$LOGIN");
7558 if (p == NULL || *p == NUL) // empty is the same as not set
7559 NameBuff[0] = NUL;
7560 else
7561 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7562# else
7563 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7564# endif
7565 new_dir = NameBuff;
7566 }
7567#endif
7568 dir_differs = new_dir == NULL || prev_dir == NULL
7569 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7570 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7571 emsg(_(e_failed));
7572 else
7573 {
7574 char_u *acmd_fname;
7575
7576 post_chdir(scope);
7577
7578 if (dir_differs)
7579 {
7580 if (scope == CDSCOPE_WINDOW)
7581 acmd_fname = (char_u *)"window";
7582 else if (scope == CDSCOPE_TABPAGE)
7583 acmd_fname = (char_u *)"tabpage";
7584 else
7585 acmd_fname = (char_u *)"global";
7586 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7587 curbuf);
7588 }
7589 retval = TRUE;
7590 }
7591 vim_free(tofree);
7592
7593 return retval;
7594}
Bram Moolenaarea408852005-06-25 22:49:46 +00007595
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007597 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007599 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007600ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007602 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603
7604 new_dir = eap->arg;
7605#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007606 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (*new_dir == NUL)
7608 ex_pwd(NULL);
7609 else
7610#endif
7611 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007612 cdscope_T scope = CDSCOPE_GLOBAL;
7613
7614 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7615 scope = CDSCOPE_WINDOW;
7616 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7617 scope = CDSCOPE_TABPAGE;
7618
7619 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007620 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007621 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007622 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 ex_pwd(eap);
7624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
7626}
7627
7628/*
7629 * ":pwd".
7630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007632ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7635 {
7636#ifdef BACKSLASH_IN_FILENAME
7637 slash_adjust(NameBuff);
7638#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007639 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 }
7641 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007642 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643}
7644
7645/*
7646 * ":=".
7647 */
7648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007649ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007651 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007652 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653}
7654
7655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007656ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007658 int n;
7659 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660
7661 if (cursor_valid())
7662 {
7663 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7664 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007665 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007667
7668 len = eap->line2;
7669 switch (*eap->arg)
7670 {
7671 case 'm': break;
7672 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007673 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007674 }
7675 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676}
7677
7678/*
7679 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7680 */
7681 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007682do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007685 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686
7687 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007688 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007689 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007691 wait_now = msec - done > 1000L ? 1000L : msec - done;
7692#ifdef FEAT_TIMERS
7693 {
7694 long due_time = check_due_timer();
7695
7696 if (due_time > 0 && due_time < wait_now)
7697 wait_now = due_time;
7698 }
7699#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007700#ifdef FEAT_JOB_CHANNEL
7701 if (has_any_channel() && wait_now > 100L)
7702 wait_now = 100L;
7703#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007704 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007705#ifdef FEAT_JOB_CHANNEL
7706 if (has_any_channel())
7707 ui_breakcheck_force(TRUE);
7708 else
7709#endif
7710 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007711#ifdef MESSAGE_QUEUE
7712 /* Process the netbeans and clientserver messages that may have been
7713 * received in the call to ui_breakcheck() when the GUI is in use. This
7714 * may occur when running a test case. */
7715 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007718
7719 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7720 // input buffer, otherwise a following call to input() fails.
7721 if (got_int)
7722 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723}
7724
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725/*
7726 * ":winsize" command (obsolete).
7727 */
7728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007729ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
7731 int w, h;
7732 char_u *arg = eap->arg;
7733 char_u *p;
7734
7735 w = getdigits(&arg);
7736 arg = skipwhite(arg);
7737 p = arg;
7738 h = getdigits(&arg);
7739 if (*p != NUL && *arg == NUL)
7740 set_shellsize(w, h, TRUE);
7741 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007742 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007746ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747{
7748 int xchar = NUL;
7749 char_u *p;
7750
7751 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7752 {
7753 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7754 if (eap->arg[1] == NUL)
7755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007756 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 return;
7758 }
7759 xchar = eap->arg[1];
7760 p = eap->arg + 2;
7761 }
7762 else
7763 p = eap->arg + 1;
7764
7765 eap->nextcmd = check_nextcmd(p);
7766 p = skipwhite(p);
7767 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007768 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007769 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {
7771 /* Pass flags on for ":vertical wincmd ]". */
7772 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007773 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7775 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007776 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 }
7778}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779
Bram Moolenaar843ee412004-06-30 16:16:41 +00007780#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781/*
7782 * ":winpos".
7783 */
7784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007785ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 int x, y;
7788 char_u *arg = eap->arg;
7789 char_u *p;
7790
7791 if (*arg == NUL)
7792 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007793# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007794# ifdef VIMDLL
7795 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7796 mch_get_winpos(&x, &y) != FAIL)
7797# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007799# else
7800 if (mch_get_winpos(&x, &y) != FAIL)
7801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {
7803 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007804 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 }
7806 else
7807# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007808 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 }
7810 else
7811 {
7812 x = getdigits(&arg);
7813 arg = skipwhite(arg);
7814 p = arg;
7815 y = getdigits(&arg);
7816 if (*p == NUL || *arg != NUL)
7817 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007818 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 return;
7820 }
7821# ifdef FEAT_GUI
7822 if (gui.in_use)
7823 gui_mch_set_winpos(x, y);
7824 else if (gui.starting)
7825 {
7826 /* Remember the coordinates for when the window is opened. */
7827 gui_win_x = x;
7828 gui_win_y = y;
7829 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007830# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 else
7832# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007833# endif
7834# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007835 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836# endif
7837# ifdef HAVE_TGETENT
7838 if (*T_CWP)
7839 term_set_winpos(x, y);
7840# endif
7841 }
7842}
7843#endif
7844
7845/*
7846 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7847 */
7848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007849ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850{
7851 oparg_T oa;
7852
7853 clear_oparg(&oa);
7854 oa.regname = eap->regname;
7855 oa.start.lnum = eap->line1;
7856 oa.end.lnum = eap->line2;
7857 oa.line_count = eap->line2 - eap->line1 + 1;
7858 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7861 {
7862 setpcmark();
7863 curwin->w_cursor.lnum = eap->line1;
7864 beginline(BL_SOL | BL_FIX);
7865 }
7866
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007867 if (VIsual_active)
7868 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007869
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 switch (eap->cmdidx)
7871 {
7872 case CMD_delete:
7873 oa.op_type = OP_DELETE;
7874 op_delete(&oa);
7875 break;
7876
7877 case CMD_yank:
7878 oa.op_type = OP_YANK;
7879 (void)op_yank(&oa, FALSE, TRUE);
7880 break;
7881
7882 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007883 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007885 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7886#else
7887 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007889 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 oa.op_type = OP_RSHIFT;
7891 else
7892 oa.op_type = OP_LSHIFT;
7893 op_shift(&oa, FALSE, eap->amount);
7894 break;
7895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007897 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898}
7899
7900/*
7901 * ":put".
7902 */
7903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007904ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 /* ":0put" works like ":1put!". */
7907 if (eap->line2 == 0)
7908 {
7909 eap->line2 = 1;
7910 eap->forceit = TRUE;
7911 }
7912 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007913 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7914 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915}
7916
7917/*
7918 * Handle ":copy" and ":move".
7919 */
7920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007921ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 long n;
7924
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007925 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 if (eap->arg == NULL) /* error detected */
7927 {
7928 eap->nextcmd = NULL;
7929 return;
7930 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007931 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
7933 /*
7934 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7935 */
7936 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7937 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007938 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 return;
7940 }
7941
7942 if (eap->cmdidx == CMD_move)
7943 {
7944 if (do_move(eap->line1, eap->line2, n) == FAIL)
7945 return;
7946 }
7947 else
7948 ex_copy(eap->line1, eap->line2, n);
7949 u_clearline();
7950 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007951 ex_may_print(eap);
7952}
7953
7954/*
7955 * Print the current line if flags were given to the Ex command.
7956 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007957 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007958ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007959{
7960 if (eap->flags != 0)
7961 {
7962 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7963 (eap->flags & EXFLAG_LIST));
7964 ex_no_reprint = TRUE;
7965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966}
7967
7968/*
7969 * ":smagic" and ":snomagic".
7970 */
7971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007972ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
7974 int magic_save = p_magic;
7975
7976 p_magic = (eap->cmdidx == CMD_smagic);
7977 do_sub(eap);
7978 p_magic = magic_save;
7979}
7980
7981/*
7982 * ":join".
7983 */
7984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007985ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987 curwin->w_cursor.lnum = eap->line1;
7988 if (eap->line1 == eap->line2)
7989 {
7990 if (eap->addr_count >= 2) /* :2,2join does nothing */
7991 return;
7992 if (eap->line2 == curbuf->b_ml.ml_line_count)
7993 {
7994 beep_flush();
7995 return;
7996 }
7997 ++eap->line2;
7998 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007999 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008001 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002}
8003
8004/*
8005 * ":[addr]@r" or ":[addr]*r": execute register
8006 */
8007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008008ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009{
8010 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008011 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
8013 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008014 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015
8016#ifdef USE_ON_FLY_SCROLL
8017 dont_scroll = TRUE; /* disallow scrolling here */
8018#endif
8019
8020 /* get the register name. No name means to use the previous one */
8021 c = *eap->arg;
8022 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8023 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008024 /* Put the register in the typeahead buffer with the "silent" flag. */
8025 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8026 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 else
8031 {
8032 int save_efr = exec_from_reg;
8033
8034 exec_from_reg = TRUE;
8035
8036 /*
8037 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008038 * Continue until the stuff buffer is empty and all added characters
8039 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008041 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8043
8044 exec_from_reg = save_efr;
8045 }
8046}
8047
8048/*
8049 * ":!".
8050 */
8051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008052ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8055}
8056
8057/*
8058 * ":undo".
8059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008061ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008063 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008064 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008065 else
8066 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067}
8068
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008069#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008072{
8073 char_u hash[UNDO_HASH_SIZE];
8074
8075 u_compute_hash(hash);
8076 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8077}
8078
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008080ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008081{
8082 char_u hash[UNDO_HASH_SIZE];
8083
8084 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008085 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008086}
8087#endif
8088
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089/*
8090 * ":redo".
8091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008093ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
8095 u_redo(1);
8096}
8097
8098/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008099 * ":earlier" and ":later".
8100 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008102ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008103{
8104 long count = 0;
8105 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008106 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008107 char_u *p = eap->arg;
8108
8109 if (*p == NUL)
8110 count = 1;
8111 else if (isdigit(*p))
8112 {
8113 count = getdigits(&p);
8114 switch (*p)
8115 {
8116 case 's': ++p; sec = TRUE; break;
8117 case 'm': ++p; sec = TRUE; count *= 60; break;
8118 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008119 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8120 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008121 }
8122 }
8123
8124 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008125 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008126 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008127 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8128 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008129}
8130
8131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 * ":redir": start/stop redirection.
8133 */
8134 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008135ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136{
8137 char *mode;
8138 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008139 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
Bram Moolenaarba768492016-07-08 15:32:54 +02008141#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008142 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008143 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008144 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008145 return;
8146 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008147#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008148
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 if (STRICMP(eap->arg, "END") == 0)
8150 close_redir();
8151 else
8152 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008153 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008155 ++arg;
8156 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008158 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 mode = "a";
8160 }
8161 else
8162 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008163 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
8165 close_redir();
8166
8167 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008168 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 if (fname == NULL)
8170 return;
8171#ifdef FEAT_BROWSE
8172 if (cmdmod.browse)
8173 {
8174 char_u *browseFile;
8175
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008176 browseFile = do_browse(BROWSE_SAVE,
8177 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008178 fname, NULL, NULL,
8179 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 if (browseFile == NULL)
8181 return; /* operation cancelled */
8182 vim_free(fname);
8183 fname = browseFile;
8184 eap->forceit = TRUE; /* since dialog already asked */
8185 }
8186#endif
8187
8188 redir_fd = open_exfile(fname, eap->forceit, mode);
8189 vim_free(fname);
8190 }
8191#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008192 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 {
8194 /* redirect to a register a-z (resp. A-Z for appending) */
8195 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008196 ++arg;
8197 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008199 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008200 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008202 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008204 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008205 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008206 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008207 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008209 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008210 if (*arg == '>')
8211 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008212 /* Make register empty when not using @A-@Z and the
8213 * command is valid. */
8214 if (*arg == NUL && !isupper(redir_reg))
8215 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008217 }
8218 if (*arg != NUL)
8219 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008220 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008221 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008222 }
8223 }
8224 else if (*arg == '=' && arg[1] == '>')
8225 {
8226 int append;
8227
8228 /* redirect to a variable */
8229 close_redir();
8230 arg += 2;
8231
8232 if (*arg == '>')
8233 {
8234 ++arg;
8235 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008238 append = FALSE;
8239
8240 if (var_redir_start(skipwhite(arg), append) == OK)
8241 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 }
8243#endif
8244
8245 /* TODO: redirect to a buffer */
8246
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008248 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008250
8251 /* Make sure redirection is not off. Can happen for cmdline completion
8252 * that indirectly invokes a command to catch its output. */
8253 if (redir_fd != NULL
8254#ifdef FEAT_EVAL
8255 || redir_reg || redir_vname
8256#endif
8257 )
8258 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259}
8260
8261/*
8262 * ":redraw": force redraw
8263 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008264 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008265ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266{
8267 int r = RedrawingDisabled;
8268 int p = p_lz;
8269
8270 RedrawingDisabled = 0;
8271 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008272 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008274 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275#ifdef FEAT_TITLE
8276 if (need_maketitle)
8277 maketitle();
8278#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008279#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8280# ifdef VIMDLL
8281 if (!gui.in_use)
8282# endif
8283 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 RedrawingDisabled = r;
8286 p_lz = p;
8287
8288 /* Reset msg_didout, so that a message that's there is overwritten. */
8289 msg_didout = FALSE;
8290 msg_col = 0;
8291
Bram Moolenaar56667a52013-07-17 11:54:28 +02008292 /* No need to wait after an intentional redraw. */
8293 need_wait_return = FALSE;
8294
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 out_flush();
8296}
8297
8298/*
8299 * ":redrawstatus": force redraw of status line(s)
8300 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008302ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 int r = RedrawingDisabled;
8305 int p = p_lz;
8306
8307 RedrawingDisabled = 0;
8308 p_lz = FALSE;
8309 if (eap->forceit)
8310 status_redraw_all();
8311 else
8312 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008313 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 RedrawingDisabled = r;
8315 p_lz = p;
8316 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317}
8318
Bram Moolenaare12bab32019-01-08 22:02:56 +01008319/*
8320 * ":redrawtabline": force redraw of the tabline
8321 */
8322 static void
8323ex_redrawtabline(exarg_T *eap UNUSED)
8324{
8325 int r = RedrawingDisabled;
8326 int p = p_lz;
8327
8328 RedrawingDisabled = 0;
8329 p_lz = FALSE;
8330
8331 draw_tabline();
8332
8333 RedrawingDisabled = r;
8334 p_lz = p;
8335 out_flush();
8336}
8337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008339close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 if (redir_fd != NULL)
8342 {
8343 fclose(redir_fd);
8344 redir_fd = NULL;
8345 }
8346#ifdef FEAT_EVAL
8347 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008348 if (redir_vname)
8349 {
8350 var_redir_stop();
8351 redir_vname = 0;
8352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353#endif
8354}
8355
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008356#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008357 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008358vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008359{
8360 if (vim_mkdir(name, prot) != 0)
8361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008362 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008363 return FAIL;
8364 }
8365 return OK;
8366}
8367#endif
8368
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369/*
8370 * Open a file for writing for an Ex command, with some checks.
8371 * Return file descriptor, or NULL on failure.
8372 */
8373 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008374open_exfile(
8375 char_u *fname,
8376 int forceit,
8377 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
8379 FILE *fd;
8380
8381#ifdef UNIX
8382 /* with Unix it is possible to open a directory */
8383 if (mch_isdir(fname))
8384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008385 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 return NULL;
8387 }
8388#endif
8389 if (!forceit && *mode != 'a' && vim_fexists(fname))
8390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008391 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 return NULL;
8393 }
8394
8395 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008396 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397
8398 return fd;
8399}
8400
8401/*
8402 * ":mark" and ":k".
8403 */
8404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008405ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407 pos_T pos;
8408
8409 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008410 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008412 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 else
8414 {
8415 pos = curwin->w_cursor; /* save curwin->w_cursor */
8416 curwin->w_cursor.lnum = eap->line2;
8417 beginline(BL_WHITE | BL_FIX);
8418 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008419 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8421 }
8422}
8423
8424/*
8425 * Update w_topline, w_leftcol and the cursor position.
8426 */
8427 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008428update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429{
8430 check_cursor(); /* put cursor on valid line */
8431 update_topline();
8432 if (!curwin->w_p_wrap)
8433 validate_cursor();
8434 update_curswant();
8435}
8436
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008438 * Save the current State and go to Normal mode.
8439 * Return TRUE if the typeahead could be saved.
8440 */
8441 int
8442save_current_state(save_state_T *sst)
8443{
8444 sst->save_msg_scroll = msg_scroll;
8445 sst->save_restart_edit = restart_edit;
8446 sst->save_msg_didout = msg_didout;
8447 sst->save_State = State;
8448 sst->save_insertmode = p_im;
8449 sst->save_finish_op = finish_op;
8450 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008451 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008452
8453 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8454 restart_edit = 0; /* don't go to Insert mode */
8455 p_im = FALSE; /* don't use 'insertmode' */
8456
8457 /*
8458 * Save the current typeahead. This is required to allow using ":normal"
8459 * from an event handler and makes sure we don't hang when the argument
8460 * ends with half a command.
8461 */
8462 save_typeahead(&sst->tabuf);
8463 return sst->tabuf.typebuf_valid;
8464}
8465
8466 void
8467restore_current_state(save_state_T *sst)
8468{
8469 /* Restore the previous typeahead. */
8470 restore_typeahead(&sst->tabuf);
8471
8472 msg_scroll = sst->save_msg_scroll;
8473 restart_edit = sst->save_restart_edit;
8474 p_im = sst->save_insertmode;
8475 finish_op = sst->save_finish_op;
8476 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008477 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008478 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8479
8480 /* Restore the state (needed when called from a function executed for
8481 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8482 State = sst->save_State;
8483#ifdef CURSOR_SHAPE
8484 ui_cursor_shape(); /* may show different cursor shape */
8485#endif
8486}
8487
8488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 * ":normal[!] {commands}": Execute normal mode commands.
8490 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008491 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008492ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008494 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 char_u *arg = NULL;
8496 int l;
8497 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008499 if (ex_normal_lock > 0)
8500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008501 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008502 return;
8503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 if (ex_normal_busy >= p_mmd)
8505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008506 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 return;
8508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 /*
8511 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8512 * this for the K_SPECIAL leading byte, otherwise special keys will not
8513 * work.
8514 */
8515 if (has_mbyte)
8516 {
8517 int len = 0;
8518
8519 /* Count the number of characters to be escaped. */
8520 for (p = eap->arg; *p != NUL; ++p)
8521 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008522#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 if (*p == CSI) /* leadbyte CSI */
8524 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008525#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008526 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008528#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 )
8532 len += 2;
8533 }
8534 if (len > 0)
8535 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008536 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 if (arg != NULL)
8538 {
8539 len = 0;
8540 for (p = eap->arg; *p != NUL; ++p)
8541 {
8542 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008543#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 if (*p == CSI)
8545 {
8546 arg[len++] = KS_EXTRA;
8547 arg[len++] = (int)KE_CSI;
8548 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008549#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008550 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 {
8552 arg[len++] = *++p;
8553 if (*p == K_SPECIAL)
8554 {
8555 arg[len++] = KS_SPECIAL;
8556 arg[len++] = KE_FILLER;
8557 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008558#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 else if (*p == CSI)
8560 {
8561 arg[len++] = KS_EXTRA;
8562 arg[len++] = (int)KE_CSI;
8563 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 }
8566 arg[len] = NUL;
8567 }
8568 }
8569 }
8570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008572 ++ex_normal_busy;
8573 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {
8575 /*
8576 * Repeat the :normal command for each line in the range. When no
8577 * range given, execute it just once, without positioning the cursor
8578 * first.
8579 */
8580 do
8581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 if (eap->addr_count != 0)
8583 {
8584 curwin->w_cursor.lnum = eap->line1++;
8585 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008586 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 }
8588
Bram Moolenaar13505972019-01-24 15:04:48 +01008589 exec_normal_cmd(arg != NULL
8590 ? arg
8591 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 }
8593 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8594 }
8595
8596 /* Might not return to the main loop when in an event handler. */
8597 update_topline_cursor();
8598
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008599 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008601#ifdef FEAT_MOUSE
8602 setmouse();
8603#endif
8604#ifdef CURSOR_SHAPE
8605 ui_cursor_shape(); /* may show different cursor shape */
8606#endif
8607
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
8611/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008612 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 */
8614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008615ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008617 if (eap->forceit)
8618 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008619 /* cursor line can be zero on startup */
8620 if (!curwin->w_cursor.lnum)
8621 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008622 coladvance((colnr_T)MAXCOL);
8623 curwin->w_curswant = MAXCOL;
8624 curwin->w_set_curswant = FALSE;
8625 }
8626
Bram Moolenaara40c5002005-01-09 21:16:21 +00008627 /* Ignore the command when already in Insert mode. Inserting an
8628 * expression register that invokes a function can do this. */
8629 if (State & INSERT)
8630 return;
8631
Bram Moolenaar64969662005-12-14 21:59:55 +00008632 if (eap->cmdidx == CMD_startinsert)
8633 restart_edit = 'a';
8634 else if (eap->cmdidx == CMD_startreplace)
8635 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008637 restart_edit = 'V';
8638
8639 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008641 if (eap->cmdidx == CMD_startinsert)
8642 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 curwin->w_curswant = 0; /* avoid MAXCOL */
8644 }
8645}
8646
8647/*
8648 * ":stopinsert"
8649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008651ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652{
8653 restart_edit = 0;
8654 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008655 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008658/*
8659 * Execute normal mode command "cmd".
8660 * "remap" can be REMAP_NONE or REMAP_YES.
8661 */
8662 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008663exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008664{
Bram Moolenaar25281632016-01-21 23:32:32 +01008665 /* Stuff the argument into the typeahead buffer. */
8666 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008667 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008668}
Bram Moolenaar25281632016-01-21 23:32:32 +01008669
Bram Moolenaar25281632016-01-21 23:32:32 +01008670/*
8671 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008672 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008673 */
8674 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008675exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008676{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008677 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008678 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008679
Bram Moolenaar905dd902019-04-07 14:21:47 +02008680 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8681 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008682 clear_oparg(&oa);
8683 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008684 while ((!stuff_empty()
8685 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008686 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008687 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008688 {
8689 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008690#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008691 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008692 && oa.op_type == OP_NOP && oa.regname == NUL
8693 && !VIsual_active)
8694 {
8695 /* If terminal_loop() returns OK we got a key that is handled
8696 * in Normal model. With FAIL we first need to position the
8697 * cursor and the screen needs to be redrawn. */
8698 if (terminal_loop(TRUE) == OK)
8699 normal_cmd(&oa, TRUE);
8700 }
8701 else
8702#endif
8703 /* execute a Normal mode cmd */
8704 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008705 }
8706}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708#ifdef FEAT_FIND_ID
8709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008710ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8713 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8714 (linenr_T)1, (linenr_T)MAXLNUM);
8715}
8716
Bram Moolenaar4033c552017-09-16 20:54:51 +02008717#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718/*
8719 * ":psearch"
8720 */
8721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008722ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 g_do_tagpreview = p_pvh;
8725 ex_findpat(eap);
8726 g_do_tagpreview = 0;
8727}
8728#endif
8729
8730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008731ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732{
8733 int whole = TRUE;
8734 long n;
8735 char_u *p;
8736 int action;
8737
8738 switch (cmdnames[eap->cmdidx].cmd_name[2])
8739 {
8740 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8741 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8742 action = ACTION_GOTO;
8743 else
8744 action = ACTION_SHOW;
8745 break;
8746 case 'i': /* ":ilist" and ":dlist" */
8747 action = ACTION_SHOW_ALL;
8748 break;
8749 case 'u': /* ":ijump" and ":djump" */
8750 action = ACTION_GOTO;
8751 break;
8752 default: /* ":isplit" and ":dsplit" */
8753 action = ACTION_SPLIT;
8754 break;
8755 }
8756
8757 n = 1;
8758 if (vim_isdigit(*eap->arg)) /* get count */
8759 {
8760 n = getdigits(&eap->arg);
8761 eap->arg = skipwhite(eap->arg);
8762 }
8763 if (*eap->arg == '/') /* Match regexp, not just whole words */
8764 {
8765 whole = FALSE;
8766 ++eap->arg;
8767 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8768 if (*p)
8769 {
8770 *p++ = NUL;
8771 p = skipwhite(p);
8772
8773 /* Check for trailing illegal characters */
8774 if (!ends_excmd(*p))
8775 eap->errmsg = e_trailing;
8776 else
8777 eap->nextcmd = check_nextcmd(p);
8778 }
8779 }
8780 if (!eap->skip)
8781 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8782 whole, !eap->forceit,
8783 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8784 n, action, eap->line1, eap->line2);
8785}
8786#endif
8787
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788
Bram Moolenaar4033c552017-09-16 20:54:51 +02008789#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790/*
8791 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8792 */
8793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008794ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008796 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8798}
8799
8800/*
8801 * ":pedit"
8802 */
8803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008804ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
8806 win_T *curwin_save = curwin;
8807
8808 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00008809 prepare_tagpreview(TRUE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008810
Bram Moolenaar67883b42017-07-27 22:57:00 +02008811 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 do_exedit(eap, NULL);
8813 keep_help_flag = FALSE;
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008814
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 if (curwin != curwin_save && win_valid(curwin_save))
8816 {
8817 /* Return cursor to where we were */
8818 validate_cursor();
8819 redraw_later(VALID);
8820 win_enter(curwin_save, TRUE);
8821 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008822# ifdef FEAT_TEXT_PROP
8823 else if (WIN_IS_POPUP(curwin))
8824 {
8825 // can't keep focus in popup window
8826 win_enter(firstwin, TRUE);
8827 }
8828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 g_do_tagpreview = 0;
8830}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832
8833/*
8834 * ":stag", ":stselect" and ":stjump".
8835 */
8836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008837ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838{
8839 postponed_split = -1;
8840 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008841 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8843 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008844 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846
8847/*
8848 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8849 */
8850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008851ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8854}
8855
8856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008857ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858{
8859 int cmd;
8860
8861 switch (name[1])
8862 {
8863 case 'j': cmd = DT_JUMP; /* ":tjump" */
8864 break;
8865 case 's': cmd = DT_SELECT; /* ":tselect" */
8866 break;
8867 case 'p': cmd = DT_PREV; /* ":tprevious" */
8868 break;
8869 case 'N': cmd = DT_PREV; /* ":tNext" */
8870 break;
8871 case 'n': cmd = DT_NEXT; /* ":tnext" */
8872 break;
8873 case 'o': cmd = DT_POP; /* ":pop" */
8874 break;
8875 case 'f': /* ":tfirst" */
8876 case 'r': cmd = DT_FIRST; /* ":trewind" */
8877 break;
8878 case 'l': cmd = DT_LAST; /* ":tlast" */
8879 break;
8880 default: /* ":tag" */
8881#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008882 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008884 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 return;
8886 }
8887#endif
8888 cmd = DT_TAG;
8889 break;
8890 }
8891
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008892 if (name[0] == 'l')
8893 {
8894#ifndef FEAT_QUICKFIX
8895 ex_ni(eap);
8896 return;
8897#else
8898 cmd = DT_LTAG;
8899#endif
8900 }
8901
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8903 eap->forceit, TRUE);
8904}
8905
8906/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008907 * Check "str" for starting with a special cmdline variable.
8908 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8909 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8910 */
8911 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008912find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008913{
8914 int len;
8915 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008916 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008917 "%",
8918#define SPEC_PERC 0
8919 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008920#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008921 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008922#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008923 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008924#define SPEC_CCWORD (SPEC_CWORD + 1)
8925 "<cexpr>", /* expr under cursor */
8926#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008927 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008928#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008929 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008930#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008931 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008932#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008933 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008934#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008935 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008936#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008937 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008938#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008939 "<sflnum>", /* script file line number */
8940#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008941#ifdef FEAT_CLIENTSERVER
8942 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008943# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008944#endif
8945 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008946
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008947 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008948 {
8949 len = (int)STRLEN(spec_str[i]);
8950 if (STRNCMP(src, spec_str[i], len) == 0)
8951 {
8952 *usedlen = len;
8953 return i;
8954 }
8955 }
8956 return -1;
8957}
8958
8959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 * Evaluate cmdline variables.
8961 *
8962 * change '%' to curbuf->b_ffname
8963 * '#' to curwin->w_altfile
8964 * '<cword>' to word under the cursor
8965 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008966 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 * '<cfile>' to path name under the cursor
8968 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008969 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 * '<afile>' to file name for autocommand
8971 * '<abuf>' to buffer number for autocommand
8972 * '<amatch>' to matching name for autocommand
8973 *
8974 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8975 * "" for error without a message) and NULL is returned.
8976 * Returns an allocated string if a valid match was found.
8977 * Returns NULL if no match was found. "usedlen" then still contains the
8978 * number of characters to skip.
8979 */
8980 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008981eval_vars(
8982 char_u *src, /* pointer into commandline */
8983 char_u *srcstart, /* beginning of valid memory for src */
8984 int *usedlen, /* characters after src that are used */
8985 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008986 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008987 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00008988 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
8990 int i;
8991 char_u *s;
8992 char_u *result;
8993 char_u *resultbuf = NULL;
8994 int resultlen;
8995 buf_T *buf;
8996 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8997 int spec_idx;
8998#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02008999 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 int skip_mod = FALSE;
9001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003
9004 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009005 if (escaped != NULL)
9006 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007
9008 /*
9009 * Check if there is something to do.
9010 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009011 spec_idx = find_cmdline_var(src, usedlen);
9012 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 {
9014 *usedlen = 1;
9015 return NULL;
9016 }
9017
9018 /*
9019 * Skip when preceded with a backslash "\%" and "\#".
9020 * Note: In "\\%" the % is also not recognized!
9021 */
9022 if (src > srcstart && src[-1] == '\\')
9023 {
9024 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009025 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 return NULL;
9027 }
9028
9029 /*
9030 * word or WORD under cursor
9031 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009032 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9033 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009035 resultlen = find_ident_under_cursor(&result,
9036 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9037 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9038 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 if (resultlen == 0)
9040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009041 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 return NULL;
9043 }
9044 }
9045
9046 /*
9047 * '#': Alternate file name
9048 * '%': Current file name
9049 * File name under the cursor
9050 * File name for autocommand
9051 * and following modifiers
9052 */
9053 else
9054 {
9055 switch (spec_idx)
9056 {
9057 case SPEC_PERC: /* '%': current file */
9058 if (curbuf->b_fname == NULL)
9059 {
9060 result = (char_u *)"";
9061 valid = 0; /* Must have ":p:h" to be valid */
9062 }
9063 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009064 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009066#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009067 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009068#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 break;
9071
9072 case SPEC_HASH: /* '#' or "#99": alternate file */
9073 if (src[1] == '#') /* "##": the argument list */
9074 {
9075 result = arg_all();
9076 resultbuf = result;
9077 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009078 if (escaped != NULL)
9079 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080#ifdef FEAT_MODIFY_FNAME
9081 skip_mod = TRUE;
9082#endif
9083 break;
9084 }
9085 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009086 if (*s == '<') /* "#<99" uses v:oldfiles */
9087 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009089 if (s == src + 2 && src[1] == '-')
9090 /* just a minus sign, don't skip over it */
9091 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 *usedlen = (int)(s - src); /* length of what we expand */
9093
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009094 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009096 if (*usedlen < 2)
9097 {
9098 /* Should we give an error message for #<text? */
9099 *usedlen = 1;
9100 return NULL;
9101 }
9102#ifdef FEAT_EVAL
9103 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9104 (long)i);
9105 if (result == NULL)
9106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009107 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009108 return NULL;
9109 }
9110#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009111 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 }
9115 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009116 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009117 if (i == 0 && src[1] == '<' && *usedlen > 1)
9118 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009119 buf = buflist_findnr(i);
9120 if (buf == NULL)
9121 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009122 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009123 return NULL;
9124 }
9125 if (lnump != NULL)
9126 *lnump = ECMD_LAST;
9127 if (buf->b_fname == NULL)
9128 {
9129 result = (char_u *)"";
9130 valid = 0; /* Must have ":p:h" to be valid */
9131 }
9132 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009133 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009134 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009135#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009136 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009137#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009138 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 break;
9141
9142#ifdef FEAT_SEARCHPATH
9143 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009144 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 if (result == NULL)
9146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009147 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 return NULL;
9149 }
9150 resultbuf = result; /* remember allocated string */
9151 break;
9152#endif
9153
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 case SPEC_AFILE: /* file name for autocommand */
9155 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009156 if (result != NULL && !autocmd_fname_full)
9157 {
9158 /* Still need to turn the fname into a full path. It is
9159 * postponed to avoid a delay when <afile> is not used. */
9160 autocmd_fname_full = TRUE;
9161 result = FullName_save(autocmd_fname, FALSE);
9162 vim_free(autocmd_fname);
9163 autocmd_fname = result;
9164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 if (result == NULL)
9166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009167 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 return NULL;
9169 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009170 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 break;
9172
9173 case SPEC_ABUF: /* buffer number for autocommand */
9174 if (autocmd_bufnr <= 0)
9175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009176 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 return NULL;
9178 }
9179 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9180 result = strbuf;
9181 break;
9182
9183 case SPEC_AMATCH: /* match name for autocommand */
9184 result = autocmd_match;
9185 if (result == NULL)
9186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009187 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 return NULL;
9189 }
9190 break;
9191
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 case SPEC_SFILE: /* file name for ":so" command */
9193 result = sourcing_name;
9194 if (result == NULL)
9195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009196 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 return NULL;
9198 }
9199 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009200
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009201 case SPEC_SLNUM: /* line in file for ":so" command */
9202 if (sourcing_name == NULL || sourcing_lnum == 0)
9203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009204 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009205 return NULL;
9206 }
9207 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9208 result = strbuf;
9209 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009210
9211#ifdef FEAT_EVAL
9212 case SPEC_SFLNUM: /* line in script file */
9213 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009215 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009216 return NULL;
9217 }
9218 sprintf((char *)strbuf, "%ld",
9219 (long)(current_sctx.sc_lnum + sourcing_lnum));
9220 result = strbuf;
9221 break;
9222#endif
9223
9224#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009226 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9227 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 result = strbuf;
9229 break;
9230#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009231
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009232 default:
9233 result = (char_u *)""; /* avoid gcc warning */
9234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 }
9236
9237 resultlen = (int)STRLEN(result); /* length of new string */
9238 if (src[*usedlen] == '<') /* remove the file name extension */
9239 {
9240 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 resultlen = (int)(s - result);
9243 }
9244#ifdef FEAT_MODIFY_FNAME
9245 else if (!skip_mod)
9246 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009247 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 &resultlen);
9249 if (result == NULL)
9250 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009251 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 return NULL;
9253 }
9254 }
9255#endif
9256 }
9257
9258 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9259 {
9260 if (valid != VALID_HEAD + VALID_PATH)
9261 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009262 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009264 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 result = NULL;
9266 }
9267 else
9268 result = vim_strnsave(result, resultlen);
9269 vim_free(resultbuf);
9270 return result;
9271}
9272
9273/*
9274 * Concatenate all files in the argument list, separated by spaces, and return
9275 * it in one allocated string.
9276 * Spaces and backslashes in the file names are escaped with a backslash.
9277 * Returns NULL when out of memory.
9278 */
9279 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009280arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281{
9282 int len;
9283 int idx;
9284 char_u *retval = NULL;
9285 char_u *p;
9286
9287 /*
9288 * Do this loop two times:
9289 * first time: compute the total length
9290 * second time: concatenate the names
9291 */
9292 for (;;)
9293 {
9294 len = 0;
9295 for (idx = 0; idx < ARGCOUNT; ++idx)
9296 {
9297 p = alist_name(&ARGLIST[idx]);
9298 if (p != NULL)
9299 {
9300 if (len > 0)
9301 {
9302 /* insert a space in between names */
9303 if (retval != NULL)
9304 retval[len] = ' ';
9305 ++len;
9306 }
9307 for ( ; *p != NUL; ++p)
9308 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009309 if (*p == ' '
9310#ifndef BACKSLASH_IN_FILENAME
9311 || *p == '\\'
9312#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009313 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 {
9315 /* insert a backslash */
9316 if (retval != NULL)
9317 retval[len] = '\\';
9318 ++len;
9319 }
9320 if (retval != NULL)
9321 retval[len] = *p;
9322 ++len;
9323 }
9324 }
9325 }
9326
9327 /* second time: break here */
9328 if (retval != NULL)
9329 {
9330 retval[len] = NUL;
9331 break;
9332 }
9333
9334 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009335 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (retval == NULL)
9337 break;
9338 }
9339
9340 return retval;
9341}
9342
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343/*
9344 * Expand the <sfile> string in "arg".
9345 *
9346 * Returns an allocated string, or NULL for any error.
9347 */
9348 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009349expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009351 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 int len;
9353 char_u *result;
9354 char_u *newres;
9355 char_u *repl;
9356 int srclen;
9357 char_u *p;
9358
9359 result = vim_strsave(arg);
9360 if (result == NULL)
9361 return NULL;
9362
9363 for (p = result; *p; )
9364 {
9365 if (STRNCMP(p, "<sfile>", 7) != 0)
9366 ++p;
9367 else
9368 {
9369 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009370 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 if (errormsg != NULL)
9372 {
9373 if (*errormsg)
9374 emsg(errormsg);
9375 vim_free(result);
9376 return NULL;
9377 }
9378 if (repl == NULL) /* no match (cannot happen) */
9379 {
9380 p += srclen;
9381 continue;
9382 }
9383 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9384 newres = alloc(len);
9385 if (newres == NULL)
9386 {
9387 vim_free(repl);
9388 vim_free(result);
9389 return NULL;
9390 }
9391 mch_memmove(newres, result, (size_t)(p - result));
9392 STRCPY(newres + (p - result), repl);
9393 len = (int)STRLEN(newres);
9394 STRCAT(newres, p + srclen);
9395 vim_free(repl);
9396 vim_free(result);
9397 result = newres;
9398 p = newres + len; /* continue after the match */
9399 }
9400 }
9401
9402 return result;
9403}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009406/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009407 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009408 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009411dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 if (fname == NULL)
9414 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009415 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416}
9417#endif
9418
9419/*
9420 * ":behave {mswin,xterm}"
9421 */
9422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009423ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424{
9425 if (STRCMP(eap->arg, "mswin") == 0)
9426 {
9427 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9428 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9429 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9430 set_option_value((char_u *)"keymodel", 0L,
9431 (char_u *)"startsel,stopsel", 0);
9432 }
9433 else if (STRCMP(eap->arg, "xterm") == 0)
9434 {
9435 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9436 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9437 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9438 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9439 }
9440 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009441 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442}
9443
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009444#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9445/*
9446 * Function given to ExpandGeneric() to obtain the possible arguments of the
9447 * ":behave {mswin,xterm}" command.
9448 */
9449 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009450get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009451{
9452 if (idx == 0)
9453 return (char_u *)"mswin";
9454 if (idx == 1)
9455 return (char_u *)"xterm";
9456 return NULL;
9457}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02009458
9459/*
9460 * Function given to ExpandGeneric() to obtain the possible arguments of the
9461 * ":messages {clear}" command.
9462 */
9463 char_u *
9464get_messages_arg(expand_T *xp UNUSED, int idx)
9465{
9466 if (idx == 0)
9467 return (char_u *)"clear";
9468 return NULL;
9469}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009470#endif
9471
Bram Moolenaar113e1072019-01-20 15:30:40 +01009472#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009473 char_u *
9474get_mapclear_arg(expand_T *xp UNUSED, int idx)
9475{
9476 if (idx == 0)
9477 return (char_u *)"<buffer>";
9478 return NULL;
9479}
Bram Moolenaar113e1072019-01-20 15:30:40 +01009480#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009481
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482static int filetype_detect = FALSE;
9483static int filetype_plugin = FALSE;
9484static int filetype_indent = FALSE;
9485
9486/*
9487 * ":filetype [plugin] [indent] {on,off,detect}"
9488 * on: Load the filetype.vim file to install autocommands for file types.
9489 * off: Load the ftoff.vim file to remove all autocommands for file types.
9490 * plugin on: load filetype.vim and ftplugin.vim
9491 * plugin off: load ftplugof.vim
9492 * indent on: load filetype.vim and indent.vim
9493 * indent off: load indoff.vim
9494 */
9495 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009496ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497{
9498 char_u *arg = eap->arg;
9499 int plugin = FALSE;
9500 int indent = FALSE;
9501
9502 if (*eap->arg == NUL)
9503 {
9504 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009505 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 filetype_detect ? "ON" : "OFF",
9507 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9508 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9509 return;
9510 }
9511
9512 /* Accept "plugin" and "indent" in any order. */
9513 for (;;)
9514 {
9515 if (STRNCMP(arg, "plugin", 6) == 0)
9516 {
9517 plugin = TRUE;
9518 arg = skipwhite(arg + 6);
9519 continue;
9520 }
9521 if (STRNCMP(arg, "indent", 6) == 0)
9522 {
9523 indent = TRUE;
9524 arg = skipwhite(arg + 6);
9525 continue;
9526 }
9527 break;
9528 }
9529 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9530 {
9531 if (*arg == 'o' || !filetype_detect)
9532 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009533 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 filetype_detect = TRUE;
9535 if (plugin)
9536 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009537 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 filetype_plugin = TRUE;
9539 }
9540 if (indent)
9541 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009542 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 filetype_indent = TRUE;
9544 }
9545 }
9546 if (*arg == 'd')
9547 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009548 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009549 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 }
9551 }
9552 else if (STRCMP(arg, "off") == 0)
9553 {
9554 if (plugin || indent)
9555 {
9556 if (plugin)
9557 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009558 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 filetype_plugin = FALSE;
9560 }
9561 if (indent)
9562 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009563 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 filetype_indent = FALSE;
9565 }
9566 }
9567 else
9568 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009569 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 filetype_detect = FALSE;
9571 }
9572 }
9573 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009574 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575}
9576
9577/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009578 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 */
9580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009581ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582{
9583 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009584 {
9585 char_u *arg = eap->arg;
9586
9587 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9588 arg += 9;
9589
9590 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9591 if (arg != eap->arg)
9592 did_filetype = FALSE;
9593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009597ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599#ifdef FEAT_DIGRAPHS
9600 if (*eap->arg != NUL)
9601 putdigraph(eap->arg);
9602 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009603 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009605 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606#endif
9607}
9608
9609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009610ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611{
9612 int flags = 0;
9613
9614 if (eap->cmdidx == CMD_setlocal)
9615 flags = OPT_LOCAL;
9616 else if (eap->cmdidx == CMD_setglobal)
9617 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009618#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 if (cmdmod.browse && flags == 0)
9620 ex_options(eap);
9621 else
9622#endif
9623 (void)do_set(eap->arg, flags);
9624}
9625
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009626#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9627 void
9628set_no_hlsearch(int flag)
9629{
9630 no_hlsearch = flag;
9631# ifdef FEAT_EVAL
9632 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9633# endif
9634}
9635
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636/*
9637 * ":nohlsearch"
9638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009640ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009642 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009643 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645#endif
9646
9647#ifdef FEAT_CRYPT
9648/*
9649 * ":X": Get crypt key
9650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009652ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009654 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009655 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656}
9657#endif
9658
9659#ifdef FEAT_FOLDING
9660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009661ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662{
9663 if (foldManualAllowed(TRUE))
9664 foldCreate(eap->line1, eap->line2);
9665}
9666
9667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009668ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669{
9670 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9671 eap->forceit, FALSE);
9672}
9673
9674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009675ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676{
9677 linenr_T lnum;
9678
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009679#ifdef FEAT_CLIPBOARD
9680 start_global_changes();
9681#endif
9682
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 /* First set the marks for all lines closed/open. */
9684 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9685 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9686 ml_setmarked(lnum);
9687
9688 /* Execute the command on the marked lines. */
9689 global_exe(eap->arg);
9690 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009691#ifdef FEAT_CLIPBOARD
9692 end_global_changes();
9693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694}
9695#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009696
Bram Moolenaar39665952018-08-15 20:59:48 +02009697#ifdef FEAT_QUICKFIX
9698/*
9699 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9700 * instead of a quickfix command.
9701 */
9702 int
9703is_loclist_cmd(int cmdidx)
9704{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009705 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009706 return FALSE;
9707 return cmdnames[cmdidx].cmd_name[0] == 'l';
9708}
9709#endif
9710
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009711# if defined(FEAT_TIMERS) || defined(PROTO)
9712 int
9713get_pressedreturn(void)
9714{
9715 return ex_pressedreturn;
9716}
9717
9718 void
9719set_pressedreturn(int val)
9720{
9721 ex_pressedreturn = val;
9722}
9723#endif