blob: 3cd615a1fe4a149312a1fa266487b50045503279 [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
4211/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004212 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 *
4214 * Set ptr to the next character after the part that was interpreted.
4215 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004216 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 *
4218 * Return MAXLNUM when no Ex address was found.
4219 */
4220 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004221get_address(
4222 exarg_T *eap UNUSED,
4223 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004224 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004225 int skip, // only skip the address, don't use it
4226 int silent, // no errors or side effects
4227 int to_other_file, // flag: may jump to other file
4228 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004230 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 int c;
4232 int i;
4233 long n;
4234 char_u *cmd;
4235 pos_T pos;
4236 pos_T *fp;
4237 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004238 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
4240 cmd = skipwhite(*ptr);
4241 lnum = MAXLNUM;
4242 do
4243 {
4244 switch (*cmd)
4245 {
4246 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004247 ++cmd;
4248 switch (addr_type)
4249 {
4250 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004251 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 lnum = curwin->w_cursor.lnum;
4253 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004254 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004255 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004256 break;
4257 case ADDR_ARGUMENTS:
4258 lnum = curwin->w_arg_idx + 1;
4259 break;
4260 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004261 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004262 lnum = curbuf->b_fnum;
4263 break;
4264 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004265 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004266 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004267 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004268 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004269 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004270 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004271 cmd = NULL;
4272 goto error;
4273 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004274 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004275#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004276 lnum = qf_get_cur_idx(eap);
4277#endif
4278 break;
4279 case ADDR_QUICKFIX_VALID:
4280#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004281 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004282#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004283 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004284 }
4285 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004288 ++cmd;
4289 switch (addr_type)
4290 {
4291 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004292 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 lnum = curbuf->b_ml.ml_line_count;
4294 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004295 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004296 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004297 break;
4298 case ADDR_ARGUMENTS:
4299 lnum = ARGCOUNT;
4300 break;
4301 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004302 buf = lastbuf;
4303 while (buf->b_ml.ml_mfp == NULL)
4304 {
4305 if (buf->b_prev == NULL)
4306 break;
4307 buf = buf->b_prev;
4308 }
4309 lnum = buf->b_fnum;
4310 break;
4311 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004312 lnum = lastbuf->b_fnum;
4313 break;
4314 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004315 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004316 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004317 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004318 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004319 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004320 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004321 cmd = NULL;
4322 goto error;
4323 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004324 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004325#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004326 lnum = qf_get_size(eap);
4327 if (lnum == 0)
4328 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004329#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004330 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004331 case ADDR_QUICKFIX_VALID:
4332#ifdef FEAT_QUICKFIX
4333 lnum = qf_get_valid_size(eap);
4334 if (lnum == 0)
4335 lnum = 1;
4336#endif
4337 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 }
4339 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
4341 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004342 if (*++cmd == NUL)
4343 {
4344 cmd = NULL;
4345 goto error;
4346 }
4347 if (addr_type != ADDR_LINES)
4348 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004349 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004350 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004351 goto error;
4352 }
4353 if (skip)
4354 ++cmd;
4355 else
4356 {
4357 /* Only accept a mark in another file when it is
4358 * used by itself: ":'M". */
4359 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4360 ++cmd;
4361 if (fp == (pos_T *)-1)
4362 /* Jumped to another file. */
4363 lnum = curwin->w_cursor.lnum;
4364 else
4365 {
4366 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 {
4368 cmd = NULL;
4369 goto error;
4370 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004371 lnum = fp->lnum;
4372 }
4373 }
4374 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
4376 case '/':
4377 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004378 c = *cmd++;
4379 if (addr_type != ADDR_LINES)
4380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004381 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004382 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004383 goto error;
4384 }
4385 if (skip) /* skip "/pat/" */
4386 {
4387 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4388 if (*cmd == c)
4389 ++cmd;
4390 }
4391 else
4392 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004393 int flags;
4394
4395 pos = curwin->w_cursor; // save curwin->w_cursor
4396
4397 // When '/' or '?' follows another address, start from
4398 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004399 if (lnum != MAXLNUM)
4400 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004401
4402 // Start a forward search at the end of the line (unless
4403 // before the first line).
4404 // Start a backward search at the start of the line.
4405 // This makes sure we never match in the current
4406 // line, and can match anywhere in the
4407 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004408 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004409 curwin->w_cursor.col = MAXCOL;
4410 else
4411 curwin->w_cursor.col = 0;
4412 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004413 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4414 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 {
4416 curwin->w_cursor = pos;
4417 cmd = NULL;
4418 goto error;
4419 }
4420 lnum = curwin->w_cursor.lnum;
4421 curwin->w_cursor = pos;
4422 /* adjust command string pointer */
4423 cmd += searchcmdlen;
4424 }
4425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004428 ++cmd;
4429 if (addr_type != ADDR_LINES)
4430 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004431 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004432 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 goto error;
4434 }
4435 if (*cmd == '&')
4436 i = RE_SUBST;
4437 else if (*cmd == '?' || *cmd == '/')
4438 i = RE_SEARCH;
4439 else
4440 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004441 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004442 cmd = NULL;
4443 goto error;
4444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 if (!skip)
4447 {
4448 /*
4449 * When search follows another address, start from
4450 * there.
4451 */
4452 if (lnum != MAXLNUM)
4453 pos.lnum = lnum;
4454 else
4455 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 /*
4458 * Start the search just like for the above
4459 * do_search().
4460 */
4461 if (*cmd != '?')
4462 pos.col = MAXCOL;
4463 else
4464 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004465 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004466 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467 *cmd == '?' ? BACKWARD : FORWARD,
4468 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004469 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470 lnum = pos.lnum;
4471 else
4472 {
4473 cmd = NULL;
4474 goto error;
4475 }
4476 }
4477 ++cmd;
4478 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004481 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4482 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484
4485 for (;;)
4486 {
4487 cmd = skipwhite(cmd);
4488 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4489 break;
4490
4491 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004492 {
4493 switch (addr_type)
4494 {
4495 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004496 case ADDR_OTHER:
4497 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004498 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 break;
4500 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004501 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004502 break;
4503 case ADDR_ARGUMENTS:
4504 lnum = curwin->w_arg_idx + 1;
4505 break;
4506 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004507 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004508 lnum = curbuf->b_fnum;
4509 break;
4510 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004511 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004512 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004513 case ADDR_TABS_RELATIVE:
4514 lnum = 1;
4515 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004516 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004517#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004518 lnum = qf_get_cur_idx(eap);
4519#endif
4520 break;
4521 case ADDR_QUICKFIX_VALID:
4522#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004523 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004524#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004525 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004526 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004527 case ADDR_UNSIGNED:
4528 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004529 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004530 }
4531 }
4532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 if (VIM_ISDIGIT(*cmd))
4534 i = '+'; /* "number" is same as "+number" */
4535 else
4536 i = *cmd++;
4537 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4538 n = 1;
4539 else
4540 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004541
4542 if (addr_type == ADDR_TABS_RELATIVE)
4543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004544 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004545 cmd = NULL;
4546 goto error;
4547 }
4548 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004549 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004550 lnum = compute_buffer_local_count(
4551 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004553 {
4554#ifdef FEAT_FOLDING
4555 /* Relative line addressing, need to adjust for folded lines
4556 * now, but only do it after the first address. */
4557 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4558 && address_count >= 2)
4559 (void)hasFolding(lnum, NULL, &lnum);
4560#endif
4561 if (i == '-')
4562 lnum -= n;
4563 else
4564 lnum += n;
4565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 } while (*cmd == '/' || *cmd == '?');
4568
4569error:
4570 *ptr = cmd;
4571 return lnum;
4572}
4573
4574/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004575 * Get flags from an Ex command argument.
4576 */
4577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004578get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004579{
4580 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4581 {
4582 if (*eap->arg == 'l')
4583 eap->flags |= EXFLAG_LIST;
4584 else if (*eap->arg == 'p')
4585 eap->flags |= EXFLAG_PRINT;
4586 else
4587 eap->flags |= EXFLAG_NR;
4588 eap->arg = skipwhite(eap->arg + 1);
4589 }
4590}
4591
4592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 * Function called for command which is Not Implemented. NI!
4594 */
4595 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004596ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597{
4598 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004599 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600}
4601
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004602#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603/*
4604 * Function called for script command which is Not Implemented. NI!
4605 * Skips over ":perl <<EOF" constructs.
4606 */
4607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004608ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 if (!eap->skip)
4611 ex_ni(eap);
4612 else
4613 vim_free(script_get(eap, eap->arg));
4614}
4615#endif
4616
4617/*
4618 * Check range in Ex command for validity.
4619 * Return NULL when valid, error message when invalid.
4620 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004621 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004622invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004624 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004625
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if ( eap->line1 < 0
4627 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004628 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004629 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004630
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004631 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004632 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004633 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004634 {
4635 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004636 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004637#ifdef FEAT_DIFF
4638 + (eap->cmdidx == CMD_diffget)
4639#endif
4640 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004641 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004642 break;
4643 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004644 /* add 1 if ARGCOUNT is 0 */
4645 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004646 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004647 break;
4648 case ADDR_BUFFERS:
4649 if (eap->line1 < firstbuf->b_fnum
4650 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004651 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004652 break;
4653 case ADDR_LOADED_BUFFERS:
4654 buf = firstbuf;
4655 while (buf->b_ml.ml_mfp == NULL)
4656 {
4657 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004658 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659 buf = buf->b_next;
4660 }
4661 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004662 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004663 buf = lastbuf;
4664 while (buf->b_ml.ml_mfp == NULL)
4665 {
4666 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004667 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004668 buf = buf->b_prev;
4669 }
4670 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004671 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004672 break;
4673 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004674 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004675 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004676 break;
4677 case ADDR_TABS:
4678 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004679 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004680 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004681 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004682 case ADDR_OTHER:
4683 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004684 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004685 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004686#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004687 // No error for value that is too big, will use the last entry.
4688 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004689 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004690#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004691 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004692 case ADDR_QUICKFIX_VALID:
4693#ifdef FEAT_QUICKFIX
4694 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4695 || eap->line2 < 0)
4696 return _(e_invrange);
4697#endif
4698 break;
4699 case ADDR_UNSIGNED:
4700 if (eap->line2 < 0)
4701 return _(e_invrange);
4702 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004703 case ADDR_NONE:
4704 // Will give an error elsewhere.
4705 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004706 }
4707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 return NULL;
4709}
4710
4711/*
4712 * Correct the range for zero line number, if required.
4713 */
4714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004715correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004717 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
4719 if (eap->line1 == 0)
4720 eap->line1 = 1;
4721 if (eap->line2 == 0)
4722 eap->line2 = 1;
4723 }
4724}
4725
Bram Moolenaar748bf032005-02-02 23:04:36 +00004726#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004727/*
4728 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4729 * pattern. Otherwise return eap->arg.
4730 */
4731 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004732skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004733{
4734 char_u *p = eap->arg;
4735
Bram Moolenaara37420f2006-02-04 22:37:47 +00004736 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4737 || eap->cmdidx == CMD_vimgrepadd
4738 || eap->cmdidx == CMD_lvimgrepadd
4739 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004740 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004741 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004742 if (p == NULL)
4743 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004744 }
4745 return p;
4746}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004747
4748/*
4749 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4750 * in the command line, so that things like % get expanded.
4751 */
4752 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004753replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004754{
4755 char_u *new_cmdline;
4756 char_u *program;
4757 char_u *pos;
4758 char_u *ptr;
4759 int len;
4760 int i;
4761
4762 /*
4763 * Don't do it when ":vimgrep" is used for ":grep".
4764 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004765 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4766 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4767 || eap->cmdidx == CMD_grepadd
4768 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004769 && !grep_internal(eap->cmdidx))
4770 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004771 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4772 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004773 {
4774 if (*curbuf->b_p_gp == NUL)
4775 program = p_gp;
4776 else
4777 program = curbuf->b_p_gp;
4778 }
4779 else
4780 {
4781 if (*curbuf->b_p_mp == NUL)
4782 program = p_mp;
4783 else
4784 program = curbuf->b_p_mp;
4785 }
4786
4787 p = skipwhite(p);
4788
4789 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4790 {
4791 /* replace $* by given arguments */
4792 i = 1;
4793 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4794 ++i;
4795 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004796 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004797 if (new_cmdline == NULL)
4798 return NULL; /* out of memory */
4799 ptr = new_cmdline;
4800 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4801 {
4802 i = (int)(pos - program);
4803 STRNCPY(ptr, program, i);
4804 STRCPY(ptr += i, p);
4805 ptr += len;
4806 program = pos + 2;
4807 }
4808 STRCPY(ptr, program);
4809 }
4810 else
4811 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004812 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004813 if (new_cmdline == NULL)
4814 return NULL; /* out of memory */
4815 STRCPY(new_cmdline, program);
4816 STRCAT(new_cmdline, " ");
4817 STRCAT(new_cmdline, p);
4818 }
4819 msg_make(p);
4820
4821 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4822 vim_free(*cmdlinep);
4823 *cmdlinep = new_cmdline;
4824 p = new_cmdline;
4825 }
4826 return p;
4827}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004828#endif
4829
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830/*
4831 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004832 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * Return FAIL for failure, OK otherwise.
4834 */
4835 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004836expand_filename(
4837 exarg_T *eap,
4838 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004839 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
4841 int has_wildcards; /* need to expand wildcards */
4842 char_u *repl;
4843 int srclen;
4844 char_u *p;
4845 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004846 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
Bram Moolenaar748bf032005-02-02 23:04:36 +00004848#ifdef FEAT_QUICKFIX
4849 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4850 p = skip_grep_pat(eap);
4851#else
4852 p = eap->arg;
4853#endif
4854
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 /*
4856 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4857 * the file name contains a wildcard it should not cause expanding.
4858 * (it will be expanded anyway if there is a wildcard before replacing).
4859 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004860 has_wildcards = mch_has_wildcard(p);
4861 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004863#ifdef FEAT_EVAL
4864 /* Skip over `=expr`, wildcards in it are not expanded. */
4865 if (p[0] == '`' && p[1] == '=')
4866 {
4867 p += 2;
4868 (void)skip_expr(&p);
4869 if (*p == '`')
4870 ++p;
4871 continue;
4872 }
4873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 /*
4875 * Quick check if this cannot be the start of a special string.
4876 * Also removes backslash before '%', '#' and '<'.
4877 */
4878 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4879 {
4880 ++p;
4881 continue;
4882 }
4883
4884 /*
4885 * Try to find a match at this position.
4886 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004887 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4888 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 if (*errormsgp != NULL) /* error detected */
4890 return FAIL;
4891 if (repl == NULL) /* no match found */
4892 {
4893 p += srclen;
4894 continue;
4895 }
4896
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004897 /* Wildcards won't be expanded below, the replacement is taken
4898 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4899 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4900 {
4901 char_u *l = repl;
4902
4903 repl = expand_env_save(repl);
4904 vim_free(l);
4905 }
4906
Bram Moolenaar63b92542007-03-27 14:57:09 +00004907 /* Need to escape white space et al. with a backslash.
4908 * Don't do this for:
4909 * - replacement that already has been escaped: "##"
4910 * - shell commands (may have to use quotes instead).
4911 * - non-unix systems when there is a single argument (spaces don't
4912 * separate arguments then).
4913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004915 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 && eap->cmdidx != CMD_grep
4918 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004919 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004920 && eap->cmdidx != CMD_lgrep
4921 && eap->cmdidx != CMD_lgrepadd
4922 && eap->cmdidx != CMD_lmake
4923 && eap->cmdidx != CMD_make
4924 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004926 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927#endif
4928 )
4929 {
4930 char_u *l;
4931#ifdef BACKSLASH_IN_FILENAME
4932 /* Don't escape a backslash here, because rem_backslash() doesn't
4933 * remove it later. */
4934 static char_u *nobslash = (char_u *)" \t\"|";
4935# define ESCAPE_CHARS nobslash
4936#else
4937# define ESCAPE_CHARS escape_chars
4938#endif
4939
4940 for (l = repl; *l; ++l)
4941 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4942 {
4943 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4944 if (l != NULL)
4945 {
4946 vim_free(repl);
4947 repl = l;
4948 }
4949 break;
4950 }
4951 }
4952
4953 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004954 if ((eap->usefilter || eap->cmdidx == CMD_bang
4955 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004956 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
4958 char_u *l;
4959
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004960 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (l != NULL)
4962 {
4963 vim_free(repl);
4964 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 }
4966 }
4967
4968 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4969 vim_free(repl);
4970 if (p == NULL)
4971 return FAIL;
4972 }
4973
4974 /*
4975 * One file argument: Expand wildcards.
4976 * Don't do this with ":r !command" or ":w !command".
4977 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004978 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 /*
4981 * May do this twice:
4982 * 1. Replace environment variables.
4983 * 2. Replace any other wildcards, remove backslashes.
4984 */
4985 for (n = 1; n <= 2; ++n)
4986 {
4987 if (n == 2)
4988 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 /*
4990 * Halve the number of backslashes (this is Vi compatible).
4991 * For Unix and OS/2, when wildcards are expanded, this is
4992 * done by ExpandOne() below.
4993 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004994#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (!has_wildcards)
4996#endif
4997 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999
5000 if (has_wildcards)
5001 {
5002 if (n == 1)
5003 {
5004 /*
5005 * First loop: May expand environment variables. This
5006 * can be done much faster with expand_env() than with
5007 * something else (e.g., calling a shell).
5008 * After expanding environment variables, check again
5009 * if there are still wildcards present.
5010 */
5011 if (vim_strchr(eap->arg, '$') != NULL
5012 || vim_strchr(eap->arg, '~') != NULL)
5013 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005014 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005015 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 has_wildcards = mch_has_wildcard(NameBuff);
5017 p = NameBuff;
5018 }
5019 else
5020 p = NULL;
5021 }
5022 else /* n == 2 */
5023 {
5024 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005025 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027 ExpandInit(&xpc);
5028 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005029 if (p_wic)
5030 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005032 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (p == NULL)
5034 return FAIL;
5035 }
5036 if (p != NULL)
5037 {
5038 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5039 p, cmdlinep);
5040 if (n == 2) /* p came from ExpandOne() */
5041 vim_free(p);
5042 }
5043 }
5044 }
5045 }
5046 return OK;
5047}
5048
5049/*
5050 * Replace part of the command line, keeping eap->cmd, eap->arg and
5051 * eap->nextcmd correct.
5052 * "src" points to the part that is to be replaced, of length "srclen".
5053 * "repl" is the replacement string.
5054 * Returns a pointer to the character after the replaced string.
5055 * Returns NULL for failure.
5056 */
5057 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005058repl_cmdline(
5059 exarg_T *eap,
5060 char_u *src,
5061 int srclen,
5062 char_u *repl,
5063 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064{
5065 int len;
5066 int i;
5067 char_u *new_cmdline;
5068
5069 /*
5070 * The new command line is build in new_cmdline[].
5071 * First allocate it.
5072 * Careful: a "+cmd" argument may have been NUL terminated.
5073 */
5074 len = (int)STRLEN(repl);
5075 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005076 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005078 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 return NULL; /* out of memory! */
5080
5081 /*
5082 * Copy the stuff before the expanded part.
5083 * Copy the expanded stuff.
5084 * Copy what came after the expanded part.
5085 * Copy the next commands, if there are any.
5086 */
5087 i = (int)(src - *cmdlinep); /* length of part before match */
5088 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 mch_memmove(new_cmdline + i, repl, (size_t)len);
5091 i += len; /* remember the end of the string */
5092 STRCPY(new_cmdline + i, src + srclen);
5093 src = new_cmdline + i; /* remember where to continue */
5094
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005095 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 i = (int)STRLEN(new_cmdline) + 1;
5098 STRCPY(new_cmdline + i, eap->nextcmd);
5099 eap->nextcmd = new_cmdline + i;
5100 }
5101 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5102 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5103 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5104 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5105 vim_free(*cmdlinep);
5106 *cmdlinep = new_cmdline;
5107
5108 return src;
5109}
5110
5111/*
5112 * Check for '|' to separate commands and '"' to start comments.
5113 */
5114 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005115separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
5117 char_u *p;
5118
Bram Moolenaar86b68352004-12-27 21:59:20 +00005119#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005120 p = skip_grep_pat(eap);
5121#else
5122 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005123#endif
5124
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005125 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 if (*p == Ctrl_V)
5128 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005129 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 ++p; /* skip CTRL-V and next char */
5131 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005132 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005133 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (*p == NUL) /* stop at NUL after CTRL-V */
5135 break;
5136 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005137
5138#ifdef FEAT_EVAL
5139 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005140 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005141 {
5142 p += 2;
5143 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005144 }
5145#endif
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 /* Check for '"': start of comment or '|': next command */
5148 /* :@" and :*" do not start a comment!
5149 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005150 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5152 || p != eap->arg)
5153 && (eap->cmdidx != CMD_redir
5154 || p != eap->arg + 1 || p[-1] != '@'))
5155 || *p == '|' || *p == '\n')
5156 {
5157 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005158 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 * AND 'b' is present in 'cpoptions'.
5160 */
5161 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005162 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005164 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 --p;
5166 }
5167 else
5168 {
5169 eap->nextcmd = check_nextcmd(p);
5170 *p = NUL;
5171 break;
5172 }
5173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005175
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005176 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 del_trailing_spaces(eap->arg);
5178}
5179
5180/*
5181 * get + command from ex argument
5182 */
5183 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005184getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 char_u *arg = *argp;
5187 char_u *command = NULL;
5188
5189 if (*arg == '+') /* +[command] */
5190 {
5191 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005192 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 command = dollar_command;
5194 else
5195 {
5196 command = arg;
5197 arg = skip_cmd_arg(command, TRUE);
5198 if (*arg != NUL)
5199 *arg++ = NUL; /* terminate command with NUL */
5200 }
5201
5202 arg = skipwhite(arg); /* skip over spaces */
5203 *argp = arg;
5204 }
5205 return command;
5206}
5207
5208/*
5209 * Find end of "+command" argument. Skip over "\ " and "\\".
5210 */
5211 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005212skip_cmd_arg(
5213 char_u *p,
5214 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
5216 while (*p && !vim_isspace(*p))
5217 {
5218 if (*p == '\\' && p[1] != NUL)
5219 {
5220 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005221 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 else
5223 ++p;
5224 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005225 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 return p;
5228}
5229
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005230 int
5231get_bad_opt(char_u *p, exarg_T *eap)
5232{
5233 if (STRICMP(p, "keep") == 0)
5234 eap->bad_char = BAD_KEEP;
5235 else if (STRICMP(p, "drop") == 0)
5236 eap->bad_char = BAD_DROP;
5237 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5238 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005239 else
5240 return FAIL;
5241 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005242}
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244/*
5245 * Get "++opt=arg" argument.
5246 * Return FAIL or OK.
5247 */
5248 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
5251 char_u *arg = eap->arg + 2;
5252 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005253 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255
5256 /* ":edit ++[no]bin[ary] file" */
5257 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5258 {
5259 if (*arg == 'n')
5260 {
5261 arg += 2;
5262 eap->force_bin = FORCE_NOBIN;
5263 }
5264 else
5265 eap->force_bin = FORCE_BIN;
5266 if (!checkforcmd(&arg, "binary", 3))
5267 return FAIL;
5268 eap->arg = skipwhite(arg);
5269 return OK;
5270 }
5271
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005272 /* ":read ++edit file" */
5273 if (STRNCMP(arg, "edit", 4) == 0)
5274 {
5275 eap->read_edit = TRUE;
5276 eap->arg = skipwhite(arg + 4);
5277 return OK;
5278 }
5279
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (STRNCMP(arg, "ff", 2) == 0)
5281 {
5282 arg += 2;
5283 pp = &eap->force_ff;
5284 }
5285 else if (STRNCMP(arg, "fileformat", 10) == 0)
5286 {
5287 arg += 10;
5288 pp = &eap->force_ff;
5289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 else if (STRNCMP(arg, "enc", 3) == 0)
5291 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005292 if (STRNCMP(arg, "encoding", 8) == 0)
5293 arg += 8;
5294 else
5295 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 pp = &eap->force_enc;
5297 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005298 else if (STRNCMP(arg, "bad", 3) == 0)
5299 {
5300 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005301 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 if (pp == NULL || *arg != '=')
5305 return FAIL;
5306
5307 ++arg;
5308 *pp = (int)(arg - eap->cmd);
5309 arg = skip_cmd_arg(arg, FALSE);
5310 eap->arg = skipwhite(arg);
5311 *arg = NUL;
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 if (pp == &eap->force_ff)
5314 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5316 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005317 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005319 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
5321 /* Make 'fileencoding' lower case. */
5322 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5323 *p = TOLOWER_ASC(*p);
5324 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005325 else
5326 {
5327 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5328 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005329 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005330 return FAIL;
5331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332
5333 return OK;
5334}
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005337ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338{
5339 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005340 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 * directory for security reasons.
5342 */
5343 if (secure)
5344 {
5345 secure = 2;
5346 eap->errmsg = e_curdir;
5347 }
5348 else if (eap->cmdidx == CMD_autocmd)
5349 do_autocmd(eap->arg, eap->forceit);
5350 else
5351 do_augroup(eap->arg, eap->forceit);
5352}
5353
5354/*
5355 * ":doautocmd": Apply the automatic commands to the current buffer.
5356 */
5357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005358ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005360 char_u *arg = eap->arg;
5361 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005362 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005363
Bram Moolenaar1610d052016-06-09 22:53:01 +02005364 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5365 /* Only when there is no <nomodeline>. */
5366 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005367 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370/*
5371 * :[N]bunload[!] [N] [bufname] unload buffer
5372 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5373 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5374 */
5375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005376ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005378 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005379 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 eap->errmsg = do_bufdel(
5381 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5382 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5383 : DOBUF_UNLOAD, eap->arg,
5384 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5385}
5386
5387/*
5388 * :[N]buffer [N] to buffer N
5389 * :[N]sbuffer [N] to buffer N
5390 */
5391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005392ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005394 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005395 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if (*eap->arg)
5397 eap->errmsg = e_trailing;
5398 else
5399 {
5400 if (eap->addr_count == 0) /* default is current buffer */
5401 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5402 else
5403 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005404 if (eap->do_ecmd_cmd != NULL)
5405 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407}
5408
5409/*
5410 * :[N]bmodified [N] to next mod. buffer
5411 * :[N]sbmodified [N] to next mod. buffer
5412 */
5413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005417 if (eap->do_ecmd_cmd != NULL)
5418 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419}
5420
5421/*
5422 * :[N]bnext [N] to next buffer
5423 * :[N]sbnext [N] split and to next buffer
5424 */
5425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005426ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427{
5428 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005429 if (eap->do_ecmd_cmd != NULL)
5430 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431}
5432
5433/*
5434 * :[N]bNext [N] to previous buffer
5435 * :[N]bprevious [N] to previous buffer
5436 * :[N]sbNext [N] split and to previous buffer
5437 * :[N]sbprevious [N] split and to previous buffer
5438 */
5439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005440ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441{
5442 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005443 if (eap->do_ecmd_cmd != NULL)
5444 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445}
5446
5447/*
5448 * :brewind to first buffer
5449 * :bfirst to first buffer
5450 * :sbrewind split and to first buffer
5451 * :sbfirst split and to first buffer
5452 */
5453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005457 if (eap->do_ecmd_cmd != NULL)
5458 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
5460
5461/*
5462 * :blast to last buffer
5463 * :sblast split and to last buffer
5464 */
5465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005466ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005469 if (eap->do_ecmd_cmd != NULL)
5470 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005474ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 return (c == NUL || c == '|' || c == '"' || c == '\n');
5477}
5478
5479#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5480 || defined(PROTO)
5481/*
5482 * Return the next command, after the first '|' or '\n'.
5483 * Return NULL if not found.
5484 */
5485 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005486find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487{
5488 while (*p != '|' && *p != '\n')
5489 {
5490 if (*p == NUL)
5491 return NULL;
5492 ++p;
5493 }
5494 return (p + 1);
5495}
5496#endif
5497
5498/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005499 * Check if *p is a separator between Ex commands, skipping over white space.
5500 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 */
5502 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005503check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005505 char_u *s = skipwhite(p);
5506
5507 if (*s == '|' || *s == '\n')
5508 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 else
5510 return NULL;
5511}
5512
5513/*
5514 * - if there are more files to edit
5515 * - and this is the last window
5516 * - and forceit not used
5517 * - and not repeated twice on a row
5518 * return FAIL and give error message if 'message' TRUE
5519 * return OK otherwise
5520 */
5521 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005522check_more(
5523 int message, /* when FALSE check only, no messages */
5524 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525{
5526 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5527
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005528 if (!forceit && only_one_window()
5529 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
5531 if (message)
5532 {
5533#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5534 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5535 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005536 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005538 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5539 NGETTEXT("%d more file to edit. Quit anyway?",
5540 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5542 return OK;
5543 return FAIL;
5544 }
5545#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005546 semsg(NGETTEXT("E173: %d more file to edit",
5547 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 quitmore = 2; /* next try to quit is allowed */
5549 }
5550 return FAIL;
5551 }
5552 return OK;
5553}
5554
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005555#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556/*
5557 * Function given to ExpandGeneric() to obtain the list of command names.
5558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 return cmdnames[idx].cmd_name;
5565}
5566#endif
5567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005569ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570{
Bram Moolenaare6850792010-05-14 15:28:44 +02005571 if (*eap->arg == NUL)
5572 {
5573#ifdef FEAT_EVAL
5574 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5575 char_u *p = NULL;
5576
5577 if (expr != NULL)
5578 {
5579 ++emsg_off;
5580 p = eval_to_string(expr, NULL, FALSE);
5581 --emsg_off;
5582 vim_free(expr);
5583 }
5584 if (p != NULL)
5585 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005586 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005587 vim_free(p);
5588 }
5589 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005590 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005591#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005592 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005593#endif
5594 }
5595 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005596 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005597
5598#ifdef FEAT_VTP
5599 else if (has_vtp_working())
5600 {
5601 // background color change requires clear + redraw
5602 update_screen(CLEAR);
5603 redrawcmd();
5604 }
5605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606}
5607
5608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005609ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610{
5611 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005612 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 do_highlight(eap->arg, eap->forceit, FALSE);
5614}
5615
5616
5617/*
5618 * Call this function if we thought we were going to exit, but we won't
5619 * (because of an error). May need to restore the terminal mode.
5620 */
5621 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
5624 exiting = FALSE;
5625 settmode(TMODE_RAW);
5626}
5627
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005628 static int
5629before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5630{
5631 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5632
5633 /* Bail out when autocommands closed the window.
5634 * Refuse to quit when the buffer in the last window is being closed (can
5635 * only happen in autocommands). */
5636 if (!win_valid(wp)
5637 || curbuf_locked()
5638 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5639 return TRUE;
5640
5641 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5642 {
5643 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5644 /* Refuse to quit when locked or when the buffer in the last window is
5645 * being closed (can only happen in autocommands). */
5646 if (curbuf_locked()
5647 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5648 return TRUE;
5649 }
5650
5651 return FALSE;
5652}
5653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005655 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005656 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 */
5658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005661 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005662
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663#ifdef FEAT_CMDWIN
5664 if (cmdwin_type != 0)
5665 {
5666 cmdwin_result = Ctrl_C;
5667 return;
5668 }
5669#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005670 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005671 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005672 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005673 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005674 return;
5675 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005676 if (eap->addr_count > 0)
5677 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005678 int wnr = eap->line2;
5679
5680 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5681 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005682 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005683 }
5684 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005685 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005686
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005687 /* Refuse to quit when locked. */
5688 if (curbuf_locked())
5689 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005690
5691 /* Trigger QuitPre and maybe ExitPre */
5692 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005693 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694
5695#ifdef FEAT_NETBEANS_INTG
5696 netbeansForcedQuit = eap->forceit;
5697#endif
5698
5699 /*
5700 * If there are more files or windows we won't exit.
5701 */
5702 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5703 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005704 if ((!buf_hide(wp->w_buffer)
5705 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005706 | (eap->forceit ? CCGD_FORCEIT : 0)
5707 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005709 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
5711 not_exiting();
5712 }
5713 else
5714 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005715 /* quit last window
5716 * Note: only_one_window() returns true, even so a help window is
5717 * still open. In that case only quit, if no address has been
5718 * specified. Example:
5719 * :h|wincmd w|1q - don't quit
5720 * :h|wincmd w|q - quit
5721 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005722 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005724 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005725#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005729 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731}
5732
5733/*
5734 * ":cquit".
5735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005737ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 getout(1); /* this does not always pass on the exit code to the Manx
5740 compiler. why? */
5741}
5742
5743/*
5744 * ":qall": try to quit all windows
5745 */
5746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005747ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749# ifdef FEAT_CMDWIN
5750 if (cmdwin_type != 0)
5751 {
5752 if (eap->forceit)
5753 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5754 else
5755 cmdwin_result = K_XF2;
5756 return;
5757 }
5758# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005759
5760 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005761 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005762 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005763 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005764 return;
5765 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005766
5767 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005768 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005771 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 getout(0);
5773 not_exiting();
5774}
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776/*
5777 * ":close": close current window, unless it is the last one
5778 */
5779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005780ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005782 win_T *win;
5783 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005784#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005786 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005788#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005789 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005790 {
5791 if (eap->addr_count == 0)
5792 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005793 else
5794 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005795 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005796 {
5797 winnr++;
5798 if (winnr == eap->line2)
5799 break;
5800 }
5801 if (win == NULL)
5802 win = lastwin;
5803 ex_win_close(eap->forceit, win, NULL);
5804 }
5805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806}
5807
Bram Moolenaar4033c552017-09-16 20:54:51 +02005808#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005809/*
5810 * ":pclose": Close any preview window.
5811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005813ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005814{
5815 win_T *win;
5816
Bram Moolenaar79648732019-07-18 21:43:07 +02005817 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005818 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005819 if (win->w_p_pvw)
5820 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005821 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005822 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005823 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005824# ifdef FEAT_TEXT_PROP
5825 // Also when 'previewpopup' is empty, it might have been cleared.
5826 popup_close_preview();
5827# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005828}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005829#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005830
Bram Moolenaarf740b292006-02-16 22:11:02 +00005831/*
5832 * Close window "win" and take care of handling closing the last window for a
5833 * modified buffer.
5834 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005836ex_win_close(
5837 int forceit,
5838 win_T *win,
5839 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 int need_hide;
5842 buf_T *buf = win->w_buffer;
5843
5844 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005845 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005847#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 if ((p_confirm || cmdmod.confirm) && p_write)
5849 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005850 bufref_T bufref;
5851
5852 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005854 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 return;
5856 need_hide = FALSE;
5857 }
5858 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005861 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 return;
5863 }
5864 }
5865
Bram Moolenaar4033c552017-09-16 20:54:51 +02005866#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005868#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005871 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005872 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005873 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005874 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875}
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005878 * Handle the argument for a tabpage related ex command.
5879 * Returns a tabpage number.
5880 * When an error is encountered then eap->errmsg is set.
5881 */
5882 static int
5883get_tabpage_arg(exarg_T *eap)
5884{
5885 int tab_number;
5886 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5887
5888 if (eap->arg && *eap->arg != NUL)
5889 {
5890 char_u *p = eap->arg;
5891 char_u *p_save;
5892 int relative = 0; /* argument +N/-N means: go to N places to the
5893 * right/left relative to the current position. */
5894
5895 if (*p == '-')
5896 {
5897 relative = -1;
5898 p++;
5899 }
5900 else if (*p == '+')
5901 {
5902 relative = 1;
5903 p++;
5904 }
5905
5906 p_save = p;
5907 tab_number = getdigits(&p);
5908
5909 if (relative == 0)
5910 {
5911 if (STRCMP(p, "$") == 0)
5912 tab_number = LAST_TAB_NR;
5913 else if (p == p_save || *p_save == '-' || *p != NUL
5914 || tab_number > LAST_TAB_NR)
5915 {
5916 /* No numbers as argument. */
5917 eap->errmsg = e_invarg;
5918 goto theend;
5919 }
5920 }
5921 else
5922 {
5923 if (*p_save == NUL)
5924 tab_number = 1;
5925 else if (p == p_save || *p_save == '-' || *p != NUL
5926 || tab_number == 0)
5927 {
5928 /* No numbers as argument. */
5929 eap->errmsg = e_invarg;
5930 goto theend;
5931 }
5932 tab_number = tab_number * relative + tabpage_index(curtab);
5933 if (!unaccept_arg0 && relative == -1)
5934 --tab_number;
5935 }
5936 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5937 eap->errmsg = e_invarg;
5938 }
5939 else if (eap->addr_count > 0)
5940 {
5941 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005942 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005943 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005944 tab_number = 0;
5945 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005946 else
5947 {
5948 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005949 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005950 {
5951 --tab_number;
5952 if (tab_number < unaccept_arg0)
5953 eap->errmsg = e_invarg;
5954 }
5955 }
5956 }
5957 else
5958 {
5959 switch (eap->cmdidx)
5960 {
5961 case CMD_tabnext:
5962 tab_number = tabpage_index(curtab) + 1;
5963 if (tab_number > LAST_TAB_NR)
5964 tab_number = 1;
5965 break;
5966 case CMD_tabmove:
5967 tab_number = LAST_TAB_NR;
5968 break;
5969 default:
5970 tab_number = tabpage_index(curtab);
5971 }
5972 }
5973
5974theend:
5975 return tab_number;
5976}
5977
5978/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005979 * ":tabclose": close current tab page, unless it is the last one.
5980 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 */
5982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005983ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005985 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005986 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005987
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005988# ifdef FEAT_CMDWIN
5989 if (cmdwin_type != 0)
5990 cmdwin_result = K_IGNORE;
5991 else
5992# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005993 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005994 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005995 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005997 tab_number = get_tabpage_arg(eap);
5998 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006000 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006001 if (tp == NULL)
6002 {
6003 beep_flush();
6004 return;
6005 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006006 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006007 {
6008 tabpage_close_other(tp, eap->forceit);
6009 return;
6010 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006011 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006012 tabpage_close(eap->forceit);
6013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006015}
6016
6017/*
6018 * ":tabonly": close all tab pages except the current one
6019 */
6020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006021ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006022{
6023 tabpage_T *tp;
6024 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006025 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006026
6027# ifdef FEAT_CMDWIN
6028 if (cmdwin_type != 0)
6029 cmdwin_result = K_IGNORE;
6030 else
6031# endif
6032 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006033 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006034 else
6035 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006036 tab_number = get_tabpage_arg(eap);
6037 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006038 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006039 goto_tabpage(tab_number);
6040 /* Repeat this up to a 1000 times, because autocommands may
6041 * mess up the lists. */
6042 for (done = 0; done < 1000; ++done)
6043 {
6044 FOR_ALL_TABPAGES(tp)
6045 if (tp->tp_topframe != topframe)
6046 {
6047 tabpage_close_other(tp, eap->forceit);
6048 /* if we failed to close it quit */
6049 if (valid_tabpage(tp))
6050 done = 1000;
6051 /* start over, "tp" is now invalid */
6052 break;
6053 }
6054 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006055 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006056 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006057 }
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060
6061/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006062 * Close the current tab page.
6063 */
6064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006065tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006066{
6067 /* First close all the windows but the current one. If that worked then
6068 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006069 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006070 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006071 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006072 ex_win_close(forceit, curwin, NULL);
6073# ifdef FEAT_GUI
6074 need_mouse_correct = TRUE;
6075# endif
6076}
6077
6078/*
6079 * Close tab page "tp", which is not the current tab page.
6080 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006081 * Also takes care of the tab pages line disappearing when closing the
6082 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006083 */
6084 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006085tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006086{
6087 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006088 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006089 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006090
6091 /* Limit to 1000 windows, autocommands may add a window while we close
6092 * one. OK, so I'm paranoid... */
6093 while (++done < 1000)
6094 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006095 wp = tp->tp_firstwin;
6096 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006097
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006098 /* Autocommands may delete the tab page under our fingers and we may
6099 * fail to close a window with a modified buffer. */
6100 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006101 break;
6102 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006103
Bram Moolenaar29323592016-07-24 22:04:11 +02006104 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006105
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006106 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006107 if (h != tabline_height())
6108 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006109}
6110
6111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 * ":only".
6113 */
6114 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006115ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006117 win_T *wp;
6118 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119# ifdef FEAT_GUI
6120 need_mouse_correct = TRUE;
6121# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006122 if (eap->addr_count > 0)
6123 {
6124 wnr = eap->line2;
6125 for (wp = firstwin; --wnr > 0; )
6126 {
6127 if (wp->w_next == NULL)
6128 break;
6129 else
6130 wp = wp->w_next;
6131 }
6132 win_goto(wp);
6133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 close_others(TRUE, eap->forceit);
6135}
6136
6137/*
6138 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006139 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006141 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006142ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144 if (eap->addr_count == 0)
6145 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006146 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148
6149 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006150ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006152 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006153 if (!eap->skip)
6154 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006155#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006156 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006157#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006158 if (eap->addr_count == 0)
6159 win_close(curwin, FALSE); /* don't free buffer */
6160 else
6161 {
6162 int winnr = 0;
6163 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006164
Bram Moolenaar2256c992016-11-15 21:17:07 +01006165 FOR_ALL_WINDOWS(win)
6166 {
6167 winnr++;
6168 if (winnr == eap->line2)
6169 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006170 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006171 if (win == NULL)
6172 win = lastwin;
6173 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 }
6176}
6177
6178/*
6179 * ":stop" and ":suspend": Suspend Vim.
6180 */
6181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006182ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
6184 /*
6185 * Disallow suspending for "rvim".
6186 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006187 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 {
6189 if (!eap->forceit)
6190 autowrite_all();
6191 windgoto((int)Rows - 1, 0);
6192 out_char('\n');
6193 out_flush();
6194 stoptermcap();
6195 out_flush(); /* needed for SUN to restore xterm buffer */
6196#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006197 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198#endif
6199 ui_suspend(); /* call machine specific function */
6200#ifdef FEAT_TITLE
6201 maketitle();
6202 resettitle(); /* force updating the title */
6203#endif
6204 starttermcap();
6205 scroll_start(); /* scroll screen before redrawing */
6206 redraw_later_clear();
6207 shell_resized(); /* may have resized window */
6208 }
6209}
6210
6211/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006212 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 */
6214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006215ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217#ifdef FEAT_CMDWIN
6218 if (cmdwin_type != 0)
6219 {
6220 cmdwin_result = Ctrl_C;
6221 return;
6222 }
6223#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006224 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006225 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006226 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006227 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006228 return;
6229 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006230
6231 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006232 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
6234 /*
6235 * if more files or windows we won't exit
6236 */
6237 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6238 exiting = TRUE;
6239 if ( ((eap->cmdidx == CMD_wq
6240 || curbufIsChanged())
6241 && do_write(eap) == FAIL)
6242 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006243 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 {
6245 not_exiting();
6246 }
6247 else
6248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006251 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252# ifdef FEAT_GUI
6253 need_mouse_correct = TRUE;
6254# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006255 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006256 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 }
6258}
6259
6260/*
6261 * ":print", ":list", ":number".
6262 */
6263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006264ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006266 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006267 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006268 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006270 for ( ;!got_int; ui_breakcheck())
6271 {
6272 print_line(eap->line1,
6273 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6274 || (eap->flags & EXFLAG_NR)),
6275 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6276 if (++eap->line1 > eap->line2)
6277 break;
6278 out_flush(); /* show one line at a time */
6279 }
6280 setpcmark();
6281 /* put cursor at last line */
6282 curwin->w_cursor.lnum = eap->line2;
6283 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 }
6285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287}
6288
6289#ifdef FEAT_BYTEOFF
6290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 goto_byte(eap->line2);
6294}
6295#endif
6296
6297/*
6298 * ":shell".
6299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006301ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 do_shell(NULL, 0);
6304}
6305
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006306#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006308static int drop_busy = FALSE;
6309static int drop_filec;
6310static char_u **drop_filev = NULL;
6311static int drop_split;
6312static void (*drop_callback)(void *);
6313static void *drop_cookie;
6314
6315 static void
6316handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 exarg_T ea;
6319 int save_msg_scroll = msg_scroll;
6320
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006321 // Setting the argument list may cause screen updates and being called
6322 // recursively. Avoid that by setting drop_busy.
6323 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324
6325 /* Check whether the current buffer is changed. If so, we will need
6326 * to split the current window or data could be lost.
6327 * We don't need to check if the 'hidden' option is set, as in this
6328 * case the buffer won't be lost.
6329 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006330 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 {
6332 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006333 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 --emsg_off;
6335 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006336 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 if (win_split(0, 0) == FAIL)
6339 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006340 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
6342 /* When splitting the window, create a new alist. Otherwise the
6343 * existing one is overwritten. */
6344 alist_unlink(curwin->w_alist);
6345 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 }
6347
6348 /*
6349 * Set up the new argument list.
6350 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006351 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352
6353 /*
6354 * Move to the first file.
6355 */
6356 /* Fake up a minimal "next" command for do_argfile() */
6357 vim_memset(&ea, 0, sizeof(ea));
6358 ea.cmd = (char_u *)"next";
6359 do_argfile(&ea, 0);
6360
6361 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6362 * mode that is not needed here. */
6363 need_start_insertmode = FALSE;
6364
6365 /* Restore msg_scroll, otherwise a following command may cause scrolling
6366 * unexpectedly. The screen will be redrawn by the caller, thus
6367 * msg_scroll being set by displaying a message is irrelevant. */
6368 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006369
6370 if (drop_callback != NULL)
6371 drop_callback(drop_cookie);
6372
6373 drop_filev = NULL;
6374 drop_busy = FALSE;
6375}
6376
6377/*
6378 * Handle a file drop. The code is here because a drop is *nearly* like an
6379 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006380 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006381 * code is very similar to :args and hence needs access to a lot of the static
6382 * functions in this file.
6383 *
6384 * The "filev" list must have been allocated using alloc(), as should each item
6385 * in the list. This function takes over responsibility for freeing the "filev"
6386 * list.
6387 */
6388 void
6389handle_drop(
6390 int filec, // the number of files dropped
6391 char_u **filev, // the list of files dropped
6392 int split, // force splitting the window
6393 void (*callback)(void *), // to be called after setting the argument
6394 // list
6395 void *cookie) // argument for "callback" (allocated)
6396{
6397 // Cannot handle recursive drops, finish the pending one.
6398 if (drop_busy)
6399 {
6400 FreeWild(filec, filev);
6401 vim_free(cookie);
6402 return;
6403 }
6404
6405 // When calling handle_drop() more than once in a row we only use the last
6406 // one.
6407 if (drop_filev != NULL)
6408 {
6409 FreeWild(drop_filec, drop_filev);
6410 vim_free(drop_cookie);
6411 }
6412
6413 drop_filec = filec;
6414 drop_filev = filev;
6415 drop_split = split;
6416 drop_callback = callback;
6417 drop_cookie = cookie;
6418
6419 // Postpone this when:
6420 // - editing the command line
6421 // - not possible to change the current buffer
6422 // - updating the screen
6423 // As it may change buffers and window structures that are in use and cause
6424 // freed memory to be used.
6425 if (text_locked() || curbuf_locked() || updating_screen)
6426 return;
6427
6428 handle_drop_internal();
6429}
6430
6431/*
6432 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6433 * reset: Handle a postponed drop.
6434 */
6435 void
6436handle_any_postponed_drop(void)
6437{
6438 if (!drop_busy && drop_filev != NULL
6439 && !text_locked() && !curbuf_locked() && !updating_screen)
6440 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
6442#endif
6443
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444/*
6445 * Clear an argument list: free all file names and reset it to zero entries.
6446 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006447 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006448alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 while (--al->al_ga.ga_len >= 0)
6451 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6452 ga_clear(&al->al_ga);
6453}
6454
6455/*
6456 * Init an argument list.
6457 */
6458 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006459alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
6461 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6462}
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464/*
6465 * Remove a reference from an argument list.
6466 * Ignored when the argument list is the global one.
6467 * If the argument list is no longer used by any window, free it.
6468 */
6469 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006470alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 if (al != &global_alist && --al->al_refcount <= 0)
6473 {
6474 alist_clear(al);
6475 vim_free(al);
6476 }
6477}
6478
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479/*
6480 * Create a new argument list and use it for the current window.
6481 */
6482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006483alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006485 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 if (curwin->w_alist == NULL)
6487 {
6488 curwin->w_alist = &global_alist;
6489 ++global_alist.al_refcount;
6490 }
6491 else
6492 {
6493 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006494 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 alist_init(curwin->w_alist);
6496 }
6497}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006499#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500/*
6501 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006502 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6503 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 */
6505 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006506alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507{
6508 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006509 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 char_u **new_arg_files;
6511 int new_arg_file_count;
6512 char_u *save_p_su = p_su;
6513 int i;
6514
6515 /* Don't use 'suffixes' here. This should work like the shell did the
6516 * expansion. Also, the vimrc file isn't read yet, thus the user
6517 * can't set the options. */
6518 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006519 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if (old_arg_files != NULL)
6521 {
6522 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006523 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6524 old_arg_count = GARGCOUNT;
6525 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006527 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 && new_arg_file_count > 0)
6529 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006530 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6531 TRUE, fnum_list, fnum_len);
6532 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 }
6535 p_su = save_p_su;
6536}
6537#endif
6538
6539/*
6540 * Set the argument list for the current window.
6541 * Takes over the allocated files[] and the allocated fnames in it.
6542 */
6543 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006544alist_set(
6545 alist_T *al,
6546 int count,
6547 char_u **files,
6548 int use_curbuf,
6549 int *fnum_list,
6550 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551{
6552 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006553 static int recursive = 0;
6554
6555 if (recursive)
6556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006557 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006558 return;
6559 }
6560 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
6562 alist_clear(al);
6563 if (ga_grow(&al->al_ga, count) == OK)
6564 {
6565 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006566 {
6567 if (got_int)
6568 {
6569 /* When adding many buffers this can take a long time. Allow
6570 * interrupting here. */
6571 while (i < count)
6572 vim_free(files[i++]);
6573 break;
6574 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006575
6576 /* May set buffer name of a buffer previously used for the
6577 * argument list, so that it's re-used by alist_add. */
6578 if (fnum_list != NULL && i < fnum_len)
6579 buf_set_name(fnum_list[i], files[i]);
6580
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006582 ui_breakcheck();
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 vim_free(files);
6585 }
6586 else
6587 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006590
6591 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592}
6593
6594/*
6595 * Add file "fname" to argument list "al".
6596 * "fname" must have been allocated and "al" must have been checked for room.
6597 */
6598 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006599alist_add(
6600 alist_T *al,
6601 char_u *fname,
6602 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 if (fname == NULL) /* don't add NULL file names */
6605 return;
6606#ifdef BACKSLASH_IN_FILENAME
6607 slash_adjust(fname);
6608#endif
6609 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6610 if (set_fnum > 0)
6611 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6612 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6613 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614}
6615
6616#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6617/*
6618 * Adjust slashes in file names. Called after 'shellslash' was set.
6619 */
6620 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006621alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
6623 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006625 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627 for (i = 0; i < GARGCOUNT; ++i)
6628 if (GARGLIST[i].ae_fname != NULL)
6629 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006630 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (wp->w_alist != &global_alist)
6632 for (i = 0; i < WARGCOUNT(wp); ++i)
6633 if (WARGLIST(wp)[i].ae_fname != NULL)
6634 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635}
6636#endif
6637
6638/*
6639 * ":preserve".
6640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006642ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006644 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 ml_preserve(curbuf, TRUE);
6646}
6647
6648/*
6649 * ":recover".
6650 */
6651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006652ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653{
6654 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6655 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006656 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6657 | CCGD_MULTWIN
6658 | (eap->forceit ? CCGD_FORCEIT : 0)
6659 | CCGD_EXCMD)
6660
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 && (*eap->arg == NUL
6662 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006663 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 recoverymode = FALSE;
6665}
6666
6667/*
6668 * Command modifier used in a wrong way.
6669 */
6670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006671ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 eap->errmsg = e_invcmd;
6674}
6675
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676/*
6677 * :sview [+command] file split window with new file, read-only
6678 * :split [[+command] file] split window with current or new file
6679 * :vsplit [[+command] file] split window vertically with current or new file
6680 * :new [[+command] file] split window with no or new file
6681 * :vnew [[+command] file] split vertically window with no or new file
6682 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006683 *
6684 * :tabedit open new Tab page with empty window
6685 * :tabedit [+command] file open new Tab page and edit "file"
6686 * :tabnew [[+command] file] just like :tabedit
6687 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 */
6689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006690ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006692 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006693#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006695#endif
6696#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006698#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006699 int use_tab = eap->cmdidx == CMD_tabedit
6700 || eap->cmdidx == CMD_tabfind
6701 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006703 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006704 return;
6705
Bram Moolenaar4033c552017-09-16 20:54:51 +02006706#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
Bram Moolenaar4033c552017-09-16 20:54:51 +02006710#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006712 * quickfix windows. But it's OK when doing ":tab split". */
6713 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {
6715 if (eap->cmdidx == CMD_split)
6716 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 if (eap->cmdidx == CMD_vsplit)
6718 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
Bram Moolenaar4033c552017-09-16 20:54:51 +02006722#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006723 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 {
6725 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6726 FNAME_MESS, TRUE, curbuf->b_ffname);
6727 if (fname == NULL)
6728 goto theend;
6729 eap->arg = fname;
6730 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006731# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006732 else
6733# endif
6734#endif
6735#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 && eap->cmdidx != CMD_new)
6739 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006740 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006741# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006742 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006743# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006744 au_has_group((char_u *)"FileExplorer"))
6745 {
6746 /* No browsing supported but we do have the file explorer:
6747 * Edit the directory. */
6748 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6749 eap->arg = (char_u *)".";
6750 }
6751 else
6752 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006753 fname = do_browse(0, (char_u *)(use_tab
6754 ? _("Edit File in new tab page")
6755 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006757 if (fname == NULL)
6758 goto theend;
6759 eap->arg = fname;
6760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 }
6762 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006765 /*
6766 * Either open new tab page or split the window.
6767 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006768 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006769 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006770 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6771 : eap->addr_count == 0 ? 0
6772 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006773 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006774 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006775
6776 /* set the alternate buffer for the window we came from */
6777 if (curwin != old_curwin
6778 && win_valid(old_curwin)
6779 && old_curwin->w_buffer != curbuf
6780 && !cmdmod.keepalt)
6781 old_curwin->w_alt_fnum = curbuf->b_fnum;
6782 }
6783 }
6784 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6786 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 /* Reset 'scrollbind' when editing another file, but keep it when
6788 * doing ":split" without arguments. */
6789 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006790# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006794 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 else
6796 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 do_exedit(eap, old_curwin);
6798 }
6799
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006800# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006804# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805theend:
6806 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006809
6810/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006811 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006812 */
6813 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006814tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006815{
6816 exarg_T ea;
6817
6818 vim_memset(&ea, 0, sizeof(ea));
6819 ea.cmdidx = CMD_tabnew;
6820 ea.cmd = (char_u *)"tabn";
6821 ea.arg = (char_u *)"";
6822 ex_splitview(&ea);
6823}
6824
6825/*
6826 * :tabnext command
6827 */
6828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006829ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006830{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006831 int tab_number;
6832
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006833 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006834 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006835 switch (eap->cmdidx)
6836 {
6837 case CMD_tabfirst:
6838 case CMD_tabrewind:
6839 goto_tabpage(1);
6840 break;
6841 case CMD_tablast:
6842 goto_tabpage(9999);
6843 break;
6844 case CMD_tabprevious:
6845 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006846 if (eap->arg && *eap->arg != NUL)
6847 {
6848 char_u *p = eap->arg;
6849 char_u *p_save = p;
6850
6851 tab_number = getdigits(&p);
6852 if (p == p_save || *p_save == '-' || *p != NUL
6853 || tab_number == 0)
6854 {
6855 /* No numbers as argument. */
6856 eap->errmsg = e_invarg;
6857 return;
6858 }
6859 }
6860 else
6861 {
6862 if (eap->addr_count == 0)
6863 tab_number = 1;
6864 else
6865 {
6866 tab_number = eap->line2;
6867 if (tab_number < 1)
6868 {
6869 eap->errmsg = e_invrange;
6870 return;
6871 }
6872 }
6873 }
6874 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006875 break;
6876 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006877 tab_number = get_tabpage_arg(eap);
6878 if (eap->errmsg == NULL)
6879 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006880 break;
6881 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006882}
6883
6884/*
6885 * :tabmove command
6886 */
6887 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006888ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006889{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006890 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006891
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006892 tab_number = get_tabpage_arg(eap);
6893 if (eap->errmsg == NULL)
6894 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006895}
6896
6897/*
6898 * :tabs command: List tabs and their contents.
6899 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006901ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006902{
6903 tabpage_T *tp;
6904 win_T *wp;
6905 int tabcount = 1;
6906
6907 msg_start();
6908 msg_scroll = TRUE;
6909 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6910 {
6911 msg_putchar('\n');
6912 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006913 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006914 out_flush(); /* output one line at a time */
6915 ui_breakcheck();
6916
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006917 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006918 wp = firstwin;
6919 else
6920 wp = tp->tp_firstwin;
6921 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6922 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006923 msg_putchar('\n');
6924 msg_putchar(wp == curwin ? '>' : ' ');
6925 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006926 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6927 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006928 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006929 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006930 else
6931 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6932 IObuff, IOSIZE, TRUE);
6933 msg_outtrans(IObuff);
6934 out_flush(); /* output one line at a time */
6935 ui_breakcheck();
6936 }
6937 }
6938}
6939
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940/*
6941 * ":mode": Set screen mode.
6942 * If no argument given, just get the screen size and redraw.
6943 */
6944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006945ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
6947 if (*eap->arg == NUL)
6948 shell_resized();
6949 else
6950 mch_screenmode(eap->arg);
6951}
6952
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953/*
6954 * ":resize".
6955 * set, increment or decrement current window height
6956 */
6957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006958ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959{
6960 int n;
6961 win_T *wp = curwin;
6962
6963 if (eap->addr_count > 0)
6964 {
6965 n = eap->line2;
6966 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6967 ;
6968 }
6969
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006970# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 if (cmdmod.split & WSP_VERT)
6975 {
6976 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006977 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6979 n = 9999;
6980 win_setwidth_win((int)n, wp);
6981 }
6982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
6984 if (*eap->arg == '-' || *eap->arg == '+')
6985 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006986 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 n = 9999;
6988 win_setheight_win((int)n, wp);
6989 }
6990}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991
6992/*
6993 * ":find [+command] <file>" command.
6994 */
6995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006996ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998#ifdef FEAT_SEARCHPATH
6999 char_u *fname;
7000 int count;
7001
7002 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7003 TRUE, curbuf->b_ffname);
7004 if (eap->addr_count > 0)
7005 {
7006 /* Repeat finding the file "count" times. This matters when it
7007 * appears several times in the path. */
7008 count = eap->line2;
7009 while (fname != NULL && --count > 0)
7010 {
7011 vim_free(fname);
7012 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7013 FALSE, curbuf->b_ffname);
7014 }
7015 }
7016
7017 if (fname != NULL)
7018 {
7019 eap->arg = fname;
7020#endif
7021 do_exedit(eap, NULL);
7022#ifdef FEAT_SEARCHPATH
7023 vim_free(fname);
7024 }
7025#endif
7026}
7027
7028/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007029 * ":open" simulation: for now just work like ":visual".
7030 */
7031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007032ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007033{
7034 regmatch_T regmatch;
7035 char_u *p;
7036
7037 curwin->w_cursor.lnum = eap->line2;
7038 beginline(BL_SOL | BL_FIX);
7039 if (*eap->arg == '/')
7040 {
7041 /* ":open /pattern/": put cursor in column found with pattern */
7042 ++eap->arg;
7043 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7044 *p = NUL;
7045 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7046 if (regmatch.regprog != NULL)
7047 {
7048 regmatch.rm_ic = p_ic;
7049 p = ml_get_curline();
7050 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007051 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007052 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007053 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007054 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007055 }
7056 /* Move to the NUL, ignore any other arguments. */
7057 eap->arg += STRLEN(eap->arg);
7058 }
7059 check_cursor();
7060
7061 eap->cmdidx = CMD_visual;
7062 do_exedit(eap, NULL);
7063}
7064
7065/*
7066 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 */
7068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007069ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 do_exedit(eap, NULL);
7072}
7073
7074/*
7075 * ":edit <file>" command and alikes.
7076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007078do_exedit(
7079 exarg_T *eap,
7080 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
7082 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007084 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007086 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007087 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 /*
7089 * ":vi" command ends Ex mode.
7090 */
7091 if (exmode_active && (eap->cmdidx == CMD_visual
7092 || eap->cmdidx == CMD_view))
7093 {
7094 exmode_active = FALSE;
7095 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007096 {
7097 /* Special case: ":global/pat/visual\NLvi-commands" */
7098 if (global_busy)
7099 {
7100 int rd = RedrawingDisabled;
7101 int nwr = no_wait_return;
7102 int ms = msg_scroll;
7103#ifdef FEAT_GUI
7104 int he = hold_gui_events;
7105#endif
7106
7107 if (eap->nextcmd != NULL)
7108 {
7109 stuffReadbuff(eap->nextcmd);
7110 eap->nextcmd = NULL;
7111 }
7112
7113 if (exmode_was != EXMODE_VIM)
7114 settmode(TMODE_RAW);
7115 RedrawingDisabled = 0;
7116 no_wait_return = 0;
7117 need_wait_return = FALSE;
7118 msg_scroll = 0;
7119#ifdef FEAT_GUI
7120 hold_gui_events = 0;
7121#endif
7122 must_redraw = CLEAR;
7123
7124 main_loop(FALSE, TRUE);
7125
7126 RedrawingDisabled = rd;
7127 no_wait_return = nwr;
7128 msg_scroll = ms;
7129#ifdef FEAT_GUI
7130 hold_gui_events = he;
7131#endif
7132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 }
7136
7137 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007138 || eap->cmdidx == CMD_tabnew
7139 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007140 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007142 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 setpcmark();
7144 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007145 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7146 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007148 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 || *eap->arg != NUL
7150#ifdef FEAT_BROWSE
7151 || cmdmod.browse
7152#endif
7153 )
7154 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007155 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7156 * can bring us here, others are stopped earlier. */
7157 if (*eap->arg != NUL && curbuf_locked())
7158 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007159
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 n = readonlymode;
7161 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7162 readonlymode = TRUE;
7163 else if (eap->cmdidx == CMD_enew)
7164 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7165 empty buffer */
7166 setpcmark();
7167 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7168 NULL, eap,
7169 /* ":edit" goes to first line if Vi compatible */
7170 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7171 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7172 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007173 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007175 /* after a split we can use an existing buffer */
7176 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007178 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 {
7180 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 if (old_curwin != NULL)
7182 {
7183 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007184 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007186#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007187 cleanup_T cs;
7188
7189 /* Reset the error/interrupt/exception state here so that
7190 * aborting() returns FALSE when closing a window. */
7191 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007192#endif
7193#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007195#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007196 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007197
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007198#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007199 /* Restore the error/interrupt/exception state if not
7200 * discarded by a new aborting error, interrupt, or
7201 * uncaught exception. */
7202 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
7205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207 else if (readonlymode && curbuf->b_nwindows == 1)
7208 {
7209 /* When editing an already visited buffer, 'readonly' won't be set
7210 * but the previous value is kept. With ":view" and ":sview" we
7211 * want the file to be readonly, except when another window is
7212 * editing the same buffer. */
7213 curbuf->b_p_ro = TRUE;
7214 }
7215 readonlymode = n;
7216 }
7217 else
7218 {
7219 if (eap->do_ecmd_cmd != NULL)
7220 do_cmdline_cmd(eap->do_ecmd_cmd);
7221#ifdef FEAT_TITLE
7222 n = curwin->w_arg_idx_invalid;
7223#endif
7224 check_arg_idx(curwin);
7225#ifdef FEAT_TITLE
7226 if (n != curwin->w_arg_idx_invalid)
7227 maketitle();
7228#endif
7229 }
7230
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 /*
7232 * if ":split file" worked, set alternate file name in old window to new
7233 * file
7234 */
7235 if (old_curwin != NULL
7236 && *eap->arg != NUL
7237 && curwin != old_curwin
7238 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007239 && old_curwin->w_buffer != curbuf
7240 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
7243 ex_no_reprint = TRUE;
7244}
7245
7246#ifndef FEAT_GUI
7247/*
7248 * ":gui" and ":gvim" when there is no GUI.
7249 */
7250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007251ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252{
7253 eap->errmsg = e_nogvim;
7254}
7255#endif
7256
Bram Moolenaar4f974752019-02-17 17:44:42 +01007257#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007259ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 gui_make_tearoff(eap->arg);
7262}
7263#endif
7264
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007265#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7266 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007268ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007270# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7271 if (gui.in_use)
7272 gui_make_popup(eap->arg, eap->forceit);
7273# ifdef FEAT_TERM_POPUP_MENU
7274 else
7275# endif
7276# endif
7277# ifdef FEAT_TERM_POPUP_MENU
7278 pum_make_popup(eap->arg, eap->forceit);
7279# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280}
7281#endif
7282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007284ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285{
7286 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007287 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007289 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290}
7291
7292/*
7293 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7294 * offset.
7295 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007298ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007301 win_T *save_curwin = curwin;
7302 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 long topline;
7304 long y;
7305 linenr_T old_linenr = curwin->w_cursor.lnum;
7306
7307 setpcmark();
7308
7309 /*
7310 * determine max topline
7311 */
7312 if (curwin->w_p_scb)
7313 {
7314 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007315 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
7317 if (wp->w_p_scb && wp->w_buffer)
7318 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007319 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 if (topline > y)
7321 topline = y;
7322 }
7323 }
7324 if (topline < 1)
7325 topline = 1;
7326 }
7327 else
7328 {
7329 topline = 1;
7330 }
7331
7332
7333 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007334 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007336 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 {
7338 if (curwin->w_p_scb)
7339 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007340 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 y = topline - curwin->w_topline;
7342 if (y > 0)
7343 scrollup(y, TRUE);
7344 else
7345 scrolldown(-y, TRUE);
7346 curwin->w_scbind_pos = topline;
7347 redraw_later(VALID);
7348 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 }
7351 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007352 curwin = save_curwin;
7353 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 if (curwin->w_p_scb)
7355 {
7356 did_syncbind = TRUE;
7357 checkpcmark();
7358 if (old_linenr != curwin->w_cursor.lnum)
7359 {
7360 char_u ctrl_o[2];
7361
7362 ctrl_o[0] = Ctrl_O;
7363 ctrl_o[1] = 0;
7364 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7365 }
7366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367}
7368
7369
7370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007371ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007373 int i;
7374 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7375 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376
7377 if (eap->usefilter) /* :r!cmd */
7378 do_bang(1, eap, FALSE, FALSE, TRUE);
7379 else
7380 {
7381 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7382 return;
7383
7384#ifdef FEAT_BROWSE
7385 if (cmdmod.browse)
7386 {
7387 char_u *browseFile;
7388
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007389 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 NULL, NULL, NULL, curbuf);
7391 if (browseFile != NULL)
7392 {
7393 i = readfile(browseFile, NULL,
7394 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7395 vim_free(browseFile);
7396 }
7397 else
7398 i = OK;
7399 }
7400 else
7401#endif
7402 if (*eap->arg == NUL)
7403 {
7404 if (check_fname() == FAIL) /* check for no file name */
7405 return;
7406 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7407 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7408 }
7409 else
7410 {
7411 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7412 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7413 i = readfile(eap->arg, NULL,
7414 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7415
7416 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007417 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007419#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (!aborting())
7421#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007422 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
7424 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007425 {
7426 if (empty && exmode_active)
7427 {
7428 /* Delete the empty line that remains. Historically ex does
7429 * this but vi doesn't. */
7430 if (eap->line2 == 0)
7431 lnum = curbuf->b_ml.ml_line_count;
7432 else
7433 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007434 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007435 {
7436 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007437 if (curwin->w_cursor.lnum > 1
7438 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007439 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007440 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007441 }
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 }
7446}
7447
Bram Moolenaarea408852005-06-25 22:49:46 +00007448static char_u *prev_dir = NULL;
7449
7450#if defined(EXITFREE) || defined(PROTO)
7451 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007452free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007453{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007454 VIM_CLEAR(prev_dir);
7455 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007456}
7457#endif
7458
Bram Moolenaarf4258302013-06-02 18:20:17 +02007459/*
7460 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007461 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7462 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007463 */
7464 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007465post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007466{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007467 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007468 // Clear tab local directory for both :cd and :tcd
7469 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007470 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007471 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007472 {
7473 /* If still in global directory, need to remember current
7474 * directory as global directory. */
7475 if (globaldir == NULL && prev_dir != NULL)
7476 globaldir = vim_strsave(prev_dir);
7477 /* Remember this local directory for the window. */
7478 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007479 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007480 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007481 curtab->tp_localdir = vim_strsave(NameBuff);
7482 else
7483 curwin->w_localdir = vim_strsave(NameBuff);
7484 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007485 }
7486 else
7487 {
7488 /* We are now in the global directory, no need to remember its
7489 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007490 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007491 }
7492
7493 shorten_fnames(TRUE);
7494}
7495
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007496/*
7497 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7498 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7499 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7500 * Otherwise changes the global directory.
7501 * Returns TRUE if the directory is successfully changed.
7502 */
7503 int
7504changedir_func(
7505 char_u *new_dir,
7506 int forceit,
7507 cdscope_T scope)
7508{
7509 char_u *tofree;
7510 int dir_differs;
7511 int retval = FALSE;
7512
7513 if (allbuf_locked())
7514 return FALSE;
7515
7516 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7517 {
7518 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7519 return FALSE;
7520 }
7521
7522 // ":cd -": Change to previous directory
7523 if (STRCMP(new_dir, "-") == 0)
7524 {
7525 if (prev_dir == NULL)
7526 {
7527 emsg(_("E186: No previous directory"));
7528 return FALSE;
7529 }
7530 new_dir = prev_dir;
7531 }
7532
7533 // Save current directory for next ":cd -"
7534 tofree = prev_dir;
7535 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7536 prev_dir = vim_strsave(NameBuff);
7537 else
7538 prev_dir = NULL;
7539
7540#if defined(UNIX) || defined(VMS)
7541 // for UNIX ":cd" means: go to home directory
7542 if (*new_dir == NUL)
7543 {
7544 // use NameBuff for home directory name
7545# ifdef VMS
7546 char_u *p;
7547
7548 p = mch_getenv((char_u *)"SYS$LOGIN");
7549 if (p == NULL || *p == NUL) // empty is the same as not set
7550 NameBuff[0] = NUL;
7551 else
7552 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7553# else
7554 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7555# endif
7556 new_dir = NameBuff;
7557 }
7558#endif
7559 dir_differs = new_dir == NULL || prev_dir == NULL
7560 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7561 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7562 emsg(_(e_failed));
7563 else
7564 {
7565 char_u *acmd_fname;
7566
7567 post_chdir(scope);
7568
7569 if (dir_differs)
7570 {
7571 if (scope == CDSCOPE_WINDOW)
7572 acmd_fname = (char_u *)"window";
7573 else if (scope == CDSCOPE_TABPAGE)
7574 acmd_fname = (char_u *)"tabpage";
7575 else
7576 acmd_fname = (char_u *)"global";
7577 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7578 curbuf);
7579 }
7580 retval = TRUE;
7581 }
7582 vim_free(tofree);
7583
7584 return retval;
7585}
Bram Moolenaarea408852005-06-25 22:49:46 +00007586
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007588 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007590 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007591ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007593 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594
7595 new_dir = eap->arg;
7596#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007597 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 if (*new_dir == NUL)
7599 ex_pwd(NULL);
7600 else
7601#endif
7602 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007603 cdscope_T scope = CDSCOPE_GLOBAL;
7604
7605 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7606 scope = CDSCOPE_WINDOW;
7607 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7608 scope = CDSCOPE_TABPAGE;
7609
7610 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007611 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007612 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007613 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 ex_pwd(eap);
7615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 }
7617}
7618
7619/*
7620 * ":pwd".
7621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007623ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
7625 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7626 {
7627#ifdef BACKSLASH_IN_FILENAME
7628 slash_adjust(NameBuff);
7629#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007630 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634}
7635
7636/*
7637 * ":=".
7638 */
7639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007640ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007642 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007643 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644}
7645
7646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007647ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007649 int n;
7650 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651
7652 if (cursor_valid())
7653 {
7654 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7655 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007656 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007658
7659 len = eap->line2;
7660 switch (*eap->arg)
7661 {
7662 case 'm': break;
7663 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007664 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007665 }
7666 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667}
7668
7669/*
7670 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7671 */
7672 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007673do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007676 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677
7678 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007679 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007680 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007682 wait_now = msec - done > 1000L ? 1000L : msec - done;
7683#ifdef FEAT_TIMERS
7684 {
7685 long due_time = check_due_timer();
7686
7687 if (due_time > 0 && due_time < wait_now)
7688 wait_now = due_time;
7689 }
7690#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007691#ifdef FEAT_JOB_CHANNEL
7692 if (has_any_channel() && wait_now > 100L)
7693 wait_now = 100L;
7694#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007695 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007696#ifdef FEAT_JOB_CHANNEL
7697 if (has_any_channel())
7698 ui_breakcheck_force(TRUE);
7699 else
7700#endif
7701 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007702#ifdef MESSAGE_QUEUE
7703 /* Process the netbeans and clientserver messages that may have been
7704 * received in the call to ui_breakcheck() when the GUI is in use. This
7705 * may occur when running a test case. */
7706 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007709
7710 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7711 // input buffer, otherwise a following call to input() fails.
7712 if (got_int)
7713 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714}
7715
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716/*
7717 * ":winsize" command (obsolete).
7718 */
7719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007720ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
7722 int w, h;
7723 char_u *arg = eap->arg;
7724 char_u *p;
7725
7726 w = getdigits(&arg);
7727 arg = skipwhite(arg);
7728 p = arg;
7729 h = getdigits(&arg);
7730 if (*p != NUL && *arg == NUL)
7731 set_shellsize(w, h, TRUE);
7732 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007733 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734}
7735
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007737ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 int xchar = NUL;
7740 char_u *p;
7741
7742 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7743 {
7744 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7745 if (eap->arg[1] == NUL)
7746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007747 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 return;
7749 }
7750 xchar = eap->arg[1];
7751 p = eap->arg + 2;
7752 }
7753 else
7754 p = eap->arg + 1;
7755
7756 eap->nextcmd = check_nextcmd(p);
7757 p = skipwhite(p);
7758 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007759 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007760 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {
7762 /* Pass flags on for ":vertical wincmd ]". */
7763 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007764 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7766 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007767 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
7769}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
Bram Moolenaar843ee412004-06-30 16:16:41 +00007771#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772/*
7773 * ":winpos".
7774 */
7775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007776ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 int x, y;
7779 char_u *arg = eap->arg;
7780 char_u *p;
7781
7782 if (*arg == NUL)
7783 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007784# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007785# ifdef VIMDLL
7786 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7787 mch_get_winpos(&x, &y) != FAIL)
7788# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007790# else
7791 if (mch_get_winpos(&x, &y) != FAIL)
7792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {
7794 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007795 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 }
7797 else
7798# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007799 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 }
7801 else
7802 {
7803 x = getdigits(&arg);
7804 arg = skipwhite(arg);
7805 p = arg;
7806 y = getdigits(&arg);
7807 if (*p == NUL || *arg != NUL)
7808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007809 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 return;
7811 }
7812# ifdef FEAT_GUI
7813 if (gui.in_use)
7814 gui_mch_set_winpos(x, y);
7815 else if (gui.starting)
7816 {
7817 /* Remember the coordinates for when the window is opened. */
7818 gui_win_x = x;
7819 gui_win_y = y;
7820 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007821# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 else
7823# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007824# endif
7825# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007826 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827# endif
7828# ifdef HAVE_TGETENT
7829 if (*T_CWP)
7830 term_set_winpos(x, y);
7831# endif
7832 }
7833}
7834#endif
7835
7836/*
7837 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7838 */
7839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007840ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 oparg_T oa;
7843
7844 clear_oparg(&oa);
7845 oa.regname = eap->regname;
7846 oa.start.lnum = eap->line1;
7847 oa.end.lnum = eap->line2;
7848 oa.line_count = eap->line2 - eap->line1 + 1;
7849 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7852 {
7853 setpcmark();
7854 curwin->w_cursor.lnum = eap->line1;
7855 beginline(BL_SOL | BL_FIX);
7856 }
7857
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007858 if (VIsual_active)
7859 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007860
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 switch (eap->cmdidx)
7862 {
7863 case CMD_delete:
7864 oa.op_type = OP_DELETE;
7865 op_delete(&oa);
7866 break;
7867
7868 case CMD_yank:
7869 oa.op_type = OP_YANK;
7870 (void)op_yank(&oa, FALSE, TRUE);
7871 break;
7872
7873 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007874 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007876 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7877#else
7878 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007880 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 oa.op_type = OP_RSHIFT;
7882 else
7883 oa.op_type = OP_LSHIFT;
7884 op_shift(&oa, FALSE, eap->amount);
7885 break;
7886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007888 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891/*
7892 * ":put".
7893 */
7894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007895ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 /* ":0put" works like ":1put!". */
7898 if (eap->line2 == 0)
7899 {
7900 eap->line2 = 1;
7901 eap->forceit = TRUE;
7902 }
7903 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007904 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7905 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906}
7907
7908/*
7909 * Handle ":copy" and ":move".
7910 */
7911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007912ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 long n;
7915
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007916 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 if (eap->arg == NULL) /* error detected */
7918 {
7919 eap->nextcmd = NULL;
7920 return;
7921 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007922 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
7924 /*
7925 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7926 */
7927 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007929 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 return;
7931 }
7932
7933 if (eap->cmdidx == CMD_move)
7934 {
7935 if (do_move(eap->line1, eap->line2, n) == FAIL)
7936 return;
7937 }
7938 else
7939 ex_copy(eap->line1, eap->line2, n);
7940 u_clearline();
7941 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007942 ex_may_print(eap);
7943}
7944
7945/*
7946 * Print the current line if flags were given to the Ex command.
7947 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007949ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950{
7951 if (eap->flags != 0)
7952 {
7953 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7954 (eap->flags & EXFLAG_LIST));
7955 ex_no_reprint = TRUE;
7956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957}
7958
7959/*
7960 * ":smagic" and ":snomagic".
7961 */
7962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007963ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 int magic_save = p_magic;
7966
7967 p_magic = (eap->cmdidx == CMD_smagic);
7968 do_sub(eap);
7969 p_magic = magic_save;
7970}
7971
7972/*
7973 * ":join".
7974 */
7975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
7978 curwin->w_cursor.lnum = eap->line1;
7979 if (eap->line1 == eap->line2)
7980 {
7981 if (eap->addr_count >= 2) /* :2,2join does nothing */
7982 return;
7983 if (eap->line2 == curbuf->b_ml.ml_line_count)
7984 {
7985 beep_flush();
7986 return;
7987 }
7988 ++eap->line2;
7989 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007990 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007992 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993}
7994
7995/*
7996 * ":[addr]@r" or ":[addr]*r": execute register
7997 */
7998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007999ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
8001 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008002 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008005 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
8007#ifdef USE_ON_FLY_SCROLL
8008 dont_scroll = TRUE; /* disallow scrolling here */
8009#endif
8010
8011 /* get the register name. No name means to use the previous one */
8012 c = *eap->arg;
8013 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8014 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008015 /* Put the register in the typeahead buffer with the "silent" flag. */
8016 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8017 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 else
8022 {
8023 int save_efr = exec_from_reg;
8024
8025 exec_from_reg = TRUE;
8026
8027 /*
8028 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008029 * Continue until the stuff buffer is empty and all added characters
8030 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008032 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8034
8035 exec_from_reg = save_efr;
8036 }
8037}
8038
8039/*
8040 * ":!".
8041 */
8042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8046}
8047
8048/*
8049 * ":undo".
8050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008052ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008054 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008055 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008056 else
8057 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058}
8059
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008060#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008063{
8064 char_u hash[UNDO_HASH_SIZE];
8065
8066 u_compute_hash(hash);
8067 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8068}
8069
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008072{
8073 char_u hash[UNDO_HASH_SIZE];
8074
8075 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008076 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008077}
8078#endif
8079
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080/*
8081 * ":redo".
8082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 u_redo(1);
8087}
8088
8089/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008090 * ":earlier" and ":later".
8091 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008093ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008094{
8095 long count = 0;
8096 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008097 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008098 char_u *p = eap->arg;
8099
8100 if (*p == NUL)
8101 count = 1;
8102 else if (isdigit(*p))
8103 {
8104 count = getdigits(&p);
8105 switch (*p)
8106 {
8107 case 's': ++p; sec = TRUE; break;
8108 case 'm': ++p; sec = TRUE; count *= 60; break;
8109 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008110 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8111 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008112 }
8113 }
8114
8115 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008116 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008117 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008118 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8119 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008120}
8121
8122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 * ":redir": start/stop redirection.
8124 */
8125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008126ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128 char *mode;
8129 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008130 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131
Bram Moolenaarba768492016-07-08 15:32:54 +02008132#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008133 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008135 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008136 return;
8137 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008138#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008139
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 if (STRICMP(eap->arg, "END") == 0)
8141 close_redir();
8142 else
8143 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008144 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008146 ++arg;
8147 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008149 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 mode = "a";
8151 }
8152 else
8153 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008154 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
8156 close_redir();
8157
8158 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008159 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 if (fname == NULL)
8161 return;
8162#ifdef FEAT_BROWSE
8163 if (cmdmod.browse)
8164 {
8165 char_u *browseFile;
8166
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008167 browseFile = do_browse(BROWSE_SAVE,
8168 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008169 fname, NULL, NULL,
8170 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 if (browseFile == NULL)
8172 return; /* operation cancelled */
8173 vim_free(fname);
8174 fname = browseFile;
8175 eap->forceit = TRUE; /* since dialog already asked */
8176 }
8177#endif
8178
8179 redir_fd = open_exfile(fname, eap->forceit, mode);
8180 vim_free(fname);
8181 }
8182#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008183 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {
8185 /* redirect to a register a-z (resp. A-Z for appending) */
8186 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008187 ++arg;
8188 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008190 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008191 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008193 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008195 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008196 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008197 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008200 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008201 if (*arg == '>')
8202 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008203 /* Make register empty when not using @A-@Z and the
8204 * command is valid. */
8205 if (*arg == NUL && !isupper(redir_reg))
8206 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008208 }
8209 if (*arg != NUL)
8210 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008211 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008212 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008213 }
8214 }
8215 else if (*arg == '=' && arg[1] == '>')
8216 {
8217 int append;
8218
8219 /* redirect to a variable */
8220 close_redir();
8221 arg += 2;
8222
8223 if (*arg == '>')
8224 {
8225 ++arg;
8226 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
8228 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008229 append = FALSE;
8230
8231 if (var_redir_start(skipwhite(arg), append) == OK)
8232 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
8234#endif
8235
8236 /* TODO: redirect to a buffer */
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008239 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008241
8242 /* Make sure redirection is not off. Can happen for cmdline completion
8243 * that indirectly invokes a command to catch its output. */
8244 if (redir_fd != NULL
8245#ifdef FEAT_EVAL
8246 || redir_reg || redir_vname
8247#endif
8248 )
8249 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250}
8251
8252/*
8253 * ":redraw": force redraw
8254 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008255 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008256ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258 int r = RedrawingDisabled;
8259 int p = p_lz;
8260
8261 RedrawingDisabled = 0;
8262 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008263 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008265 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266#ifdef FEAT_TITLE
8267 if (need_maketitle)
8268 maketitle();
8269#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008270#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8271# ifdef VIMDLL
8272 if (!gui.in_use)
8273# endif
8274 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 RedrawingDisabled = r;
8277 p_lz = p;
8278
8279 /* Reset msg_didout, so that a message that's there is overwritten. */
8280 msg_didout = FALSE;
8281 msg_col = 0;
8282
Bram Moolenaar56667a52013-07-17 11:54:28 +02008283 /* No need to wait after an intentional redraw. */
8284 need_wait_return = FALSE;
8285
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 out_flush();
8287}
8288
8289/*
8290 * ":redrawstatus": force redraw of status line(s)
8291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 int r = RedrawingDisabled;
8296 int p = p_lz;
8297
8298 RedrawingDisabled = 0;
8299 p_lz = FALSE;
8300 if (eap->forceit)
8301 status_redraw_all();
8302 else
8303 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008304 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 RedrawingDisabled = r;
8306 p_lz = p;
8307 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308}
8309
Bram Moolenaare12bab32019-01-08 22:02:56 +01008310/*
8311 * ":redrawtabline": force redraw of the tabline
8312 */
8313 static void
8314ex_redrawtabline(exarg_T *eap UNUSED)
8315{
8316 int r = RedrawingDisabled;
8317 int p = p_lz;
8318
8319 RedrawingDisabled = 0;
8320 p_lz = FALSE;
8321
8322 draw_tabline();
8323
8324 RedrawingDisabled = r;
8325 p_lz = p;
8326 out_flush();
8327}
8328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008330close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 if (redir_fd != NULL)
8333 {
8334 fclose(redir_fd);
8335 redir_fd = NULL;
8336 }
8337#ifdef FEAT_EVAL
8338 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008339 if (redir_vname)
8340 {
8341 var_redir_stop();
8342 redir_vname = 0;
8343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344#endif
8345}
8346
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008347#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008348 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008349vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008350{
8351 if (vim_mkdir(name, prot) != 0)
8352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008353 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008354 return FAIL;
8355 }
8356 return OK;
8357}
8358#endif
8359
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360/*
8361 * Open a file for writing for an Ex command, with some checks.
8362 * Return file descriptor, or NULL on failure.
8363 */
8364 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008365open_exfile(
8366 char_u *fname,
8367 int forceit,
8368 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370 FILE *fd;
8371
8372#ifdef UNIX
8373 /* with Unix it is possible to open a directory */
8374 if (mch_isdir(fname))
8375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008376 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 return NULL;
8378 }
8379#endif
8380 if (!forceit && *mode != 'a' && vim_fexists(fname))
8381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008382 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 return NULL;
8384 }
8385
8386 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008387 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388
8389 return fd;
8390}
8391
8392/*
8393 * ":mark" and ":k".
8394 */
8395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008396ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397{
8398 pos_T pos;
8399
8400 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008401 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008403 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 else
8405 {
8406 pos = curwin->w_cursor; /* save curwin->w_cursor */
8407 curwin->w_cursor.lnum = eap->line2;
8408 beginline(BL_WHITE | BL_FIX);
8409 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008410 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8412 }
8413}
8414
8415/*
8416 * Update w_topline, w_leftcol and the cursor position.
8417 */
8418 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008419update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421 check_cursor(); /* put cursor on valid line */
8422 update_topline();
8423 if (!curwin->w_p_wrap)
8424 validate_cursor();
8425 update_curswant();
8426}
8427
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008429 * Save the current State and go to Normal mode.
8430 * Return TRUE if the typeahead could be saved.
8431 */
8432 int
8433save_current_state(save_state_T *sst)
8434{
8435 sst->save_msg_scroll = msg_scroll;
8436 sst->save_restart_edit = restart_edit;
8437 sst->save_msg_didout = msg_didout;
8438 sst->save_State = State;
8439 sst->save_insertmode = p_im;
8440 sst->save_finish_op = finish_op;
8441 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008442 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008443
8444 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8445 restart_edit = 0; /* don't go to Insert mode */
8446 p_im = FALSE; /* don't use 'insertmode' */
8447
8448 /*
8449 * Save the current typeahead. This is required to allow using ":normal"
8450 * from an event handler and makes sure we don't hang when the argument
8451 * ends with half a command.
8452 */
8453 save_typeahead(&sst->tabuf);
8454 return sst->tabuf.typebuf_valid;
8455}
8456
8457 void
8458restore_current_state(save_state_T *sst)
8459{
8460 /* Restore the previous typeahead. */
8461 restore_typeahead(&sst->tabuf);
8462
8463 msg_scroll = sst->save_msg_scroll;
8464 restart_edit = sst->save_restart_edit;
8465 p_im = sst->save_insertmode;
8466 finish_op = sst->save_finish_op;
8467 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008468 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008469 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8470
8471 /* Restore the state (needed when called from a function executed for
8472 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8473 State = sst->save_State;
8474#ifdef CURSOR_SHAPE
8475 ui_cursor_shape(); /* may show different cursor shape */
8476#endif
8477}
8478
8479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 * ":normal[!] {commands}": Execute normal mode commands.
8481 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008483ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008485 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 char_u *arg = NULL;
8487 int l;
8488 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008490 if (ex_normal_lock > 0)
8491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008492 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008493 return;
8494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 if (ex_normal_busy >= p_mmd)
8496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008497 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 return;
8499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 /*
8502 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8503 * this for the K_SPECIAL leading byte, otherwise special keys will not
8504 * work.
8505 */
8506 if (has_mbyte)
8507 {
8508 int len = 0;
8509
8510 /* Count the number of characters to be escaped. */
8511 for (p = eap->arg; *p != NUL; ++p)
8512 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008513#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (*p == CSI) /* leadbyte CSI */
8515 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008516#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008517 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008519#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 )
8523 len += 2;
8524 }
8525 if (len > 0)
8526 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008527 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 if (arg != NULL)
8529 {
8530 len = 0;
8531 for (p = eap->arg; *p != NUL; ++p)
8532 {
8533 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008534#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 if (*p == CSI)
8536 {
8537 arg[len++] = KS_EXTRA;
8538 arg[len++] = (int)KE_CSI;
8539 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008540#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008541 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {
8543 arg[len++] = *++p;
8544 if (*p == K_SPECIAL)
8545 {
8546 arg[len++] = KS_SPECIAL;
8547 arg[len++] = KE_FILLER;
8548 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008549#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 else if (*p == CSI)
8551 {
8552 arg[len++] = KS_EXTRA;
8553 arg[len++] = (int)KE_CSI;
8554 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 }
8557 arg[len] = NUL;
8558 }
8559 }
8560 }
8561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008563 ++ex_normal_busy;
8564 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {
8566 /*
8567 * Repeat the :normal command for each line in the range. When no
8568 * range given, execute it just once, without positioning the cursor
8569 * first.
8570 */
8571 do
8572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 if (eap->addr_count != 0)
8574 {
8575 curwin->w_cursor.lnum = eap->line1++;
8576 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008577 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
8579
Bram Moolenaar13505972019-01-24 15:04:48 +01008580 exec_normal_cmd(arg != NULL
8581 ? arg
8582 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 }
8584 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8585 }
8586
8587 /* Might not return to the main loop when in an event handler. */
8588 update_topline_cursor();
8589
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008590 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008592#ifdef FEAT_MOUSE
8593 setmouse();
8594#endif
8595#ifdef CURSOR_SHAPE
8596 ui_cursor_shape(); /* may show different cursor shape */
8597#endif
8598
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600}
8601
8602/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008603 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 */
8605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008606ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008608 if (eap->forceit)
8609 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008610 /* cursor line can be zero on startup */
8611 if (!curwin->w_cursor.lnum)
8612 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008613 coladvance((colnr_T)MAXCOL);
8614 curwin->w_curswant = MAXCOL;
8615 curwin->w_set_curswant = FALSE;
8616 }
8617
Bram Moolenaara40c5002005-01-09 21:16:21 +00008618 /* Ignore the command when already in Insert mode. Inserting an
8619 * expression register that invokes a function can do this. */
8620 if (State & INSERT)
8621 return;
8622
Bram Moolenaar64969662005-12-14 21:59:55 +00008623 if (eap->cmdidx == CMD_startinsert)
8624 restart_edit = 'a';
8625 else if (eap->cmdidx == CMD_startreplace)
8626 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008628 restart_edit = 'V';
8629
8630 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008632 if (eap->cmdidx == CMD_startinsert)
8633 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 curwin->w_curswant = 0; /* avoid MAXCOL */
8635 }
8636}
8637
8638/*
8639 * ":stopinsert"
8640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008642ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644 restart_edit = 0;
8645 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008646 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008649/*
8650 * Execute normal mode command "cmd".
8651 * "remap" can be REMAP_NONE or REMAP_YES.
8652 */
8653 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008654exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008655{
Bram Moolenaar25281632016-01-21 23:32:32 +01008656 /* Stuff the argument into the typeahead buffer. */
8657 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008658 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008659}
Bram Moolenaar25281632016-01-21 23:32:32 +01008660
Bram Moolenaar25281632016-01-21 23:32:32 +01008661/*
8662 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008663 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008664 */
8665 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008666exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008667{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008668 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008669 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008670
Bram Moolenaar905dd902019-04-07 14:21:47 +02008671 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8672 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008673 clear_oparg(&oa);
8674 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008675 while ((!stuff_empty()
8676 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008677 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008678 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008679 {
8680 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008681#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008682 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008683 && oa.op_type == OP_NOP && oa.regname == NUL
8684 && !VIsual_active)
8685 {
8686 /* If terminal_loop() returns OK we got a key that is handled
8687 * in Normal model. With FAIL we first need to position the
8688 * cursor and the screen needs to be redrawn. */
8689 if (terminal_loop(TRUE) == OK)
8690 normal_cmd(&oa, TRUE);
8691 }
8692 else
8693#endif
8694 /* execute a Normal mode cmd */
8695 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008696 }
8697}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008698
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699#ifdef FEAT_FIND_ID
8700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008701ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702{
8703 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8704 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8705 (linenr_T)1, (linenr_T)MAXLNUM);
8706}
8707
Bram Moolenaar4033c552017-09-16 20:54:51 +02008708#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709/*
8710 * ":psearch"
8711 */
8712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008713ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715 g_do_tagpreview = p_pvh;
8716 ex_findpat(eap);
8717 g_do_tagpreview = 0;
8718}
8719#endif
8720
8721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008722ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723{
8724 int whole = TRUE;
8725 long n;
8726 char_u *p;
8727 int action;
8728
8729 switch (cmdnames[eap->cmdidx].cmd_name[2])
8730 {
8731 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8732 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8733 action = ACTION_GOTO;
8734 else
8735 action = ACTION_SHOW;
8736 break;
8737 case 'i': /* ":ilist" and ":dlist" */
8738 action = ACTION_SHOW_ALL;
8739 break;
8740 case 'u': /* ":ijump" and ":djump" */
8741 action = ACTION_GOTO;
8742 break;
8743 default: /* ":isplit" and ":dsplit" */
8744 action = ACTION_SPLIT;
8745 break;
8746 }
8747
8748 n = 1;
8749 if (vim_isdigit(*eap->arg)) /* get count */
8750 {
8751 n = getdigits(&eap->arg);
8752 eap->arg = skipwhite(eap->arg);
8753 }
8754 if (*eap->arg == '/') /* Match regexp, not just whole words */
8755 {
8756 whole = FALSE;
8757 ++eap->arg;
8758 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8759 if (*p)
8760 {
8761 *p++ = NUL;
8762 p = skipwhite(p);
8763
8764 /* Check for trailing illegal characters */
8765 if (!ends_excmd(*p))
8766 eap->errmsg = e_trailing;
8767 else
8768 eap->nextcmd = check_nextcmd(p);
8769 }
8770 }
8771 if (!eap->skip)
8772 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8773 whole, !eap->forceit,
8774 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8775 n, action, eap->line1, eap->line2);
8776}
8777#endif
8778
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779
Bram Moolenaar4033c552017-09-16 20:54:51 +02008780#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781/*
8782 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8783 */
8784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008785ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008787 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8789}
8790
8791/*
8792 * ":pedit"
8793 */
8794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008795ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796{
8797 win_T *curwin_save = curwin;
8798
8799 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00008800 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02008801 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 do_exedit(eap, NULL);
8803 keep_help_flag = FALSE;
8804 if (curwin != curwin_save && win_valid(curwin_save))
8805 {
8806 /* Return cursor to where we were */
8807 validate_cursor();
8808 redraw_later(VALID);
8809 win_enter(curwin_save, TRUE);
8810 }
8811 g_do_tagpreview = 0;
8812}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814
8815/*
8816 * ":stag", ":stselect" and ":stjump".
8817 */
8818 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008819ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820{
8821 postponed_split = -1;
8822 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008823 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8825 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008826 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829/*
8830 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8831 */
8832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008833ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
8835 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8836}
8837
8838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008839ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840{
8841 int cmd;
8842
8843 switch (name[1])
8844 {
8845 case 'j': cmd = DT_JUMP; /* ":tjump" */
8846 break;
8847 case 's': cmd = DT_SELECT; /* ":tselect" */
8848 break;
8849 case 'p': cmd = DT_PREV; /* ":tprevious" */
8850 break;
8851 case 'N': cmd = DT_PREV; /* ":tNext" */
8852 break;
8853 case 'n': cmd = DT_NEXT; /* ":tnext" */
8854 break;
8855 case 'o': cmd = DT_POP; /* ":pop" */
8856 break;
8857 case 'f': /* ":tfirst" */
8858 case 'r': cmd = DT_FIRST; /* ":trewind" */
8859 break;
8860 case 'l': cmd = DT_LAST; /* ":tlast" */
8861 break;
8862 default: /* ":tag" */
8863#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008864 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008866 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 return;
8868 }
8869#endif
8870 cmd = DT_TAG;
8871 break;
8872 }
8873
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008874 if (name[0] == 'l')
8875 {
8876#ifndef FEAT_QUICKFIX
8877 ex_ni(eap);
8878 return;
8879#else
8880 cmd = DT_LTAG;
8881#endif
8882 }
8883
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8885 eap->forceit, TRUE);
8886}
8887
8888/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008889 * Check "str" for starting with a special cmdline variable.
8890 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8891 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8892 */
8893 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008894find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008895{
8896 int len;
8897 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008898 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008899 "%",
8900#define SPEC_PERC 0
8901 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008902#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008903 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008904#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008905 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008906#define SPEC_CCWORD (SPEC_CWORD + 1)
8907 "<cexpr>", /* expr under cursor */
8908#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008909 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008910#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008911 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008912#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008913 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008914#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008915 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008916#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008917 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008918#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008919 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008920#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008921 "<sflnum>", /* script file line number */
8922#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008923#ifdef FEAT_CLIENTSERVER
8924 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008925# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008926#endif
8927 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008928
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008929 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008930 {
8931 len = (int)STRLEN(spec_str[i]);
8932 if (STRNCMP(src, spec_str[i], len) == 0)
8933 {
8934 *usedlen = len;
8935 return i;
8936 }
8937 }
8938 return -1;
8939}
8940
8941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 * Evaluate cmdline variables.
8943 *
8944 * change '%' to curbuf->b_ffname
8945 * '#' to curwin->w_altfile
8946 * '<cword>' to word under the cursor
8947 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008948 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 * '<cfile>' to path name under the cursor
8950 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008951 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 * '<afile>' to file name for autocommand
8953 * '<abuf>' to buffer number for autocommand
8954 * '<amatch>' to matching name for autocommand
8955 *
8956 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8957 * "" for error without a message) and NULL is returned.
8958 * Returns an allocated string if a valid match was found.
8959 * Returns NULL if no match was found. "usedlen" then still contains the
8960 * number of characters to skip.
8961 */
8962 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008963eval_vars(
8964 char_u *src, /* pointer into commandline */
8965 char_u *srcstart, /* beginning of valid memory for src */
8966 int *usedlen, /* characters after src that are used */
8967 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008968 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008969 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00008970 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972 int i;
8973 char_u *s;
8974 char_u *result;
8975 char_u *resultbuf = NULL;
8976 int resultlen;
8977 buf_T *buf;
8978 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8979 int spec_idx;
8980#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02008981 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 int skip_mod = FALSE;
8983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
8986 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008987 if (escaped != NULL)
8988 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989
8990 /*
8991 * Check if there is something to do.
8992 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008993 spec_idx = find_cmdline_var(src, usedlen);
8994 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 {
8996 *usedlen = 1;
8997 return NULL;
8998 }
8999
9000 /*
9001 * Skip when preceded with a backslash "\%" and "\#".
9002 * Note: In "\\%" the % is also not recognized!
9003 */
9004 if (src > srcstart && src[-1] == '\\')
9005 {
9006 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009007 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 return NULL;
9009 }
9010
9011 /*
9012 * word or WORD under cursor
9013 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009014 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9015 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009017 resultlen = find_ident_under_cursor(&result,
9018 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9019 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9020 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 if (resultlen == 0)
9022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009023 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 return NULL;
9025 }
9026 }
9027
9028 /*
9029 * '#': Alternate file name
9030 * '%': Current file name
9031 * File name under the cursor
9032 * File name for autocommand
9033 * and following modifiers
9034 */
9035 else
9036 {
9037 switch (spec_idx)
9038 {
9039 case SPEC_PERC: /* '%': current file */
9040 if (curbuf->b_fname == NULL)
9041 {
9042 result = (char_u *)"";
9043 valid = 0; /* Must have ":p:h" to be valid */
9044 }
9045 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009048#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009049 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009050#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 break;
9053
9054 case SPEC_HASH: /* '#' or "#99": alternate file */
9055 if (src[1] == '#') /* "##": the argument list */
9056 {
9057 result = arg_all();
9058 resultbuf = result;
9059 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009060 if (escaped != NULL)
9061 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062#ifdef FEAT_MODIFY_FNAME
9063 skip_mod = TRUE;
9064#endif
9065 break;
9066 }
9067 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009068 if (*s == '<') /* "#<99" uses v:oldfiles */
9069 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009071 if (s == src + 2 && src[1] == '-')
9072 /* just a minus sign, don't skip over it */
9073 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 *usedlen = (int)(s - src); /* length of what we expand */
9075
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009076 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009078 if (*usedlen < 2)
9079 {
9080 /* Should we give an error message for #<text? */
9081 *usedlen = 1;
9082 return NULL;
9083 }
9084#ifdef FEAT_EVAL
9085 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9086 (long)i);
9087 if (result == NULL)
9088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009089 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009090 return NULL;
9091 }
9092#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009093 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 }
9097 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009098 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009099 if (i == 0 && src[1] == '<' && *usedlen > 1)
9100 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009101 buf = buflist_findnr(i);
9102 if (buf == NULL)
9103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009104 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009105 return NULL;
9106 }
9107 if (lnump != NULL)
9108 *lnump = ECMD_LAST;
9109 if (buf->b_fname == NULL)
9110 {
9111 result = (char_u *)"";
9112 valid = 0; /* Must have ":p:h" to be valid */
9113 }
9114 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009115 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009116 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009117#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009118 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009119#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009120 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 break;
9123
9124#ifdef FEAT_SEARCHPATH
9125 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009126 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 if (result == NULL)
9128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009129 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 return NULL;
9131 }
9132 resultbuf = result; /* remember allocated string */
9133 break;
9134#endif
9135
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 case SPEC_AFILE: /* file name for autocommand */
9137 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009138 if (result != NULL && !autocmd_fname_full)
9139 {
9140 /* Still need to turn the fname into a full path. It is
9141 * postponed to avoid a delay when <afile> is not used. */
9142 autocmd_fname_full = TRUE;
9143 result = FullName_save(autocmd_fname, FALSE);
9144 vim_free(autocmd_fname);
9145 autocmd_fname = result;
9146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 if (result == NULL)
9148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009149 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 return NULL;
9151 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009152 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 break;
9154
9155 case SPEC_ABUF: /* buffer number for autocommand */
9156 if (autocmd_bufnr <= 0)
9157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009158 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 return NULL;
9160 }
9161 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9162 result = strbuf;
9163 break;
9164
9165 case SPEC_AMATCH: /* match name for autocommand */
9166 result = autocmd_match;
9167 if (result == NULL)
9168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009169 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 return NULL;
9171 }
9172 break;
9173
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 case SPEC_SFILE: /* file name for ":so" command */
9175 result = sourcing_name;
9176 if (result == NULL)
9177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009178 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 return NULL;
9180 }
9181 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009182
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009183 case SPEC_SLNUM: /* line in file for ":so" command */
9184 if (sourcing_name == NULL || sourcing_lnum == 0)
9185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009186 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009187 return NULL;
9188 }
9189 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9190 result = strbuf;
9191 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009192
9193#ifdef FEAT_EVAL
9194 case SPEC_SFLNUM: /* line in script file */
9195 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009197 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009198 return NULL;
9199 }
9200 sprintf((char *)strbuf, "%ld",
9201 (long)(current_sctx.sc_lnum + sourcing_lnum));
9202 result = strbuf;
9203 break;
9204#endif
9205
9206#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009208 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9209 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 result = strbuf;
9211 break;
9212#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009213
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009214 default:
9215 result = (char_u *)""; /* avoid gcc warning */
9216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 }
9218
9219 resultlen = (int)STRLEN(result); /* length of new string */
9220 if (src[*usedlen] == '<') /* remove the file name extension */
9221 {
9222 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 resultlen = (int)(s - result);
9225 }
9226#ifdef FEAT_MODIFY_FNAME
9227 else if (!skip_mod)
9228 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009229 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 &resultlen);
9231 if (result == NULL)
9232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009233 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 return NULL;
9235 }
9236 }
9237#endif
9238 }
9239
9240 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9241 {
9242 if (valid != VALID_HEAD + VALID_PATH)
9243 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009244 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009246 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 result = NULL;
9248 }
9249 else
9250 result = vim_strnsave(result, resultlen);
9251 vim_free(resultbuf);
9252 return result;
9253}
9254
9255/*
9256 * Concatenate all files in the argument list, separated by spaces, and return
9257 * it in one allocated string.
9258 * Spaces and backslashes in the file names are escaped with a backslash.
9259 * Returns NULL when out of memory.
9260 */
9261 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009262arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
9264 int len;
9265 int idx;
9266 char_u *retval = NULL;
9267 char_u *p;
9268
9269 /*
9270 * Do this loop two times:
9271 * first time: compute the total length
9272 * second time: concatenate the names
9273 */
9274 for (;;)
9275 {
9276 len = 0;
9277 for (idx = 0; idx < ARGCOUNT; ++idx)
9278 {
9279 p = alist_name(&ARGLIST[idx]);
9280 if (p != NULL)
9281 {
9282 if (len > 0)
9283 {
9284 /* insert a space in between names */
9285 if (retval != NULL)
9286 retval[len] = ' ';
9287 ++len;
9288 }
9289 for ( ; *p != NUL; ++p)
9290 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009291 if (*p == ' '
9292#ifndef BACKSLASH_IN_FILENAME
9293 || *p == '\\'
9294#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009295 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 {
9297 /* insert a backslash */
9298 if (retval != NULL)
9299 retval[len] = '\\';
9300 ++len;
9301 }
9302 if (retval != NULL)
9303 retval[len] = *p;
9304 ++len;
9305 }
9306 }
9307 }
9308
9309 /* second time: break here */
9310 if (retval != NULL)
9311 {
9312 retval[len] = NUL;
9313 break;
9314 }
9315
9316 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009317 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 if (retval == NULL)
9319 break;
9320 }
9321
9322 return retval;
9323}
9324
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325/*
9326 * Expand the <sfile> string in "arg".
9327 *
9328 * Returns an allocated string, or NULL for any error.
9329 */
9330 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009331expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009333 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334 int len;
9335 char_u *result;
9336 char_u *newres;
9337 char_u *repl;
9338 int srclen;
9339 char_u *p;
9340
9341 result = vim_strsave(arg);
9342 if (result == NULL)
9343 return NULL;
9344
9345 for (p = result; *p; )
9346 {
9347 if (STRNCMP(p, "<sfile>", 7) != 0)
9348 ++p;
9349 else
9350 {
9351 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009352 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 if (errormsg != NULL)
9354 {
9355 if (*errormsg)
9356 emsg(errormsg);
9357 vim_free(result);
9358 return NULL;
9359 }
9360 if (repl == NULL) /* no match (cannot happen) */
9361 {
9362 p += srclen;
9363 continue;
9364 }
9365 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9366 newres = alloc(len);
9367 if (newres == NULL)
9368 {
9369 vim_free(repl);
9370 vim_free(result);
9371 return NULL;
9372 }
9373 mch_memmove(newres, result, (size_t)(p - result));
9374 STRCPY(newres + (p - result), repl);
9375 len = (int)STRLEN(newres);
9376 STRCAT(newres, p + srclen);
9377 vim_free(repl);
9378 vim_free(result);
9379 result = newres;
9380 p = newres + len; /* continue after the match */
9381 }
9382 }
9383
9384 return result;
9385}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009388/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009389 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009390 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009393dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 if (fname == NULL)
9396 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009397 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398}
9399#endif
9400
9401/*
9402 * ":behave {mswin,xterm}"
9403 */
9404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009405ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406{
9407 if (STRCMP(eap->arg, "mswin") == 0)
9408 {
9409 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9410 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9411 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9412 set_option_value((char_u *)"keymodel", 0L,
9413 (char_u *)"startsel,stopsel", 0);
9414 }
9415 else if (STRCMP(eap->arg, "xterm") == 0)
9416 {
9417 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9418 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9419 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9420 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9421 }
9422 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009423 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424}
9425
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009426#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9427/*
9428 * Function given to ExpandGeneric() to obtain the possible arguments of the
9429 * ":behave {mswin,xterm}" command.
9430 */
9431 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009432get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009433{
9434 if (idx == 0)
9435 return (char_u *)"mswin";
9436 if (idx == 1)
9437 return (char_u *)"xterm";
9438 return NULL;
9439}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02009440
9441/*
9442 * Function given to ExpandGeneric() to obtain the possible arguments of the
9443 * ":messages {clear}" command.
9444 */
9445 char_u *
9446get_messages_arg(expand_T *xp UNUSED, int idx)
9447{
9448 if (idx == 0)
9449 return (char_u *)"clear";
9450 return NULL;
9451}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009452#endif
9453
Bram Moolenaar113e1072019-01-20 15:30:40 +01009454#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009455 char_u *
9456get_mapclear_arg(expand_T *xp UNUSED, int idx)
9457{
9458 if (idx == 0)
9459 return (char_u *)"<buffer>";
9460 return NULL;
9461}
Bram Moolenaar113e1072019-01-20 15:30:40 +01009462#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464static int filetype_detect = FALSE;
9465static int filetype_plugin = FALSE;
9466static int filetype_indent = FALSE;
9467
9468/*
9469 * ":filetype [plugin] [indent] {on,off,detect}"
9470 * on: Load the filetype.vim file to install autocommands for file types.
9471 * off: Load the ftoff.vim file to remove all autocommands for file types.
9472 * plugin on: load filetype.vim and ftplugin.vim
9473 * plugin off: load ftplugof.vim
9474 * indent on: load filetype.vim and indent.vim
9475 * indent off: load indoff.vim
9476 */
9477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009478ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479{
9480 char_u *arg = eap->arg;
9481 int plugin = FALSE;
9482 int indent = FALSE;
9483
9484 if (*eap->arg == NUL)
9485 {
9486 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009487 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 filetype_detect ? "ON" : "OFF",
9489 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9490 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9491 return;
9492 }
9493
9494 /* Accept "plugin" and "indent" in any order. */
9495 for (;;)
9496 {
9497 if (STRNCMP(arg, "plugin", 6) == 0)
9498 {
9499 plugin = TRUE;
9500 arg = skipwhite(arg + 6);
9501 continue;
9502 }
9503 if (STRNCMP(arg, "indent", 6) == 0)
9504 {
9505 indent = TRUE;
9506 arg = skipwhite(arg + 6);
9507 continue;
9508 }
9509 break;
9510 }
9511 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9512 {
9513 if (*arg == 'o' || !filetype_detect)
9514 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009515 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 filetype_detect = TRUE;
9517 if (plugin)
9518 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009519 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 filetype_plugin = TRUE;
9521 }
9522 if (indent)
9523 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009524 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 filetype_indent = TRUE;
9526 }
9527 }
9528 if (*arg == 'd')
9529 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009530 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009531 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 }
9533 }
9534 else if (STRCMP(arg, "off") == 0)
9535 {
9536 if (plugin || indent)
9537 {
9538 if (plugin)
9539 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009540 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 filetype_plugin = FALSE;
9542 }
9543 if (indent)
9544 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009545 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 filetype_indent = FALSE;
9547 }
9548 }
9549 else
9550 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009551 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 filetype_detect = FALSE;
9553 }
9554 }
9555 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009556 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557}
9558
9559/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009560 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 */
9562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009563ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009566 {
9567 char_u *arg = eap->arg;
9568
9569 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9570 arg += 9;
9571
9572 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9573 if (arg != eap->arg)
9574 did_filetype = FALSE;
9575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009579ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581#ifdef FEAT_DIGRAPHS
9582 if (*eap->arg != NUL)
9583 putdigraph(eap->arg);
9584 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009585 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009587 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588#endif
9589}
9590
9591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009592ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 int flags = 0;
9595
9596 if (eap->cmdidx == CMD_setlocal)
9597 flags = OPT_LOCAL;
9598 else if (eap->cmdidx == CMD_setglobal)
9599 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009600#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601 if (cmdmod.browse && flags == 0)
9602 ex_options(eap);
9603 else
9604#endif
9605 (void)do_set(eap->arg, flags);
9606}
9607
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009608#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9609 void
9610set_no_hlsearch(int flag)
9611{
9612 no_hlsearch = flag;
9613# ifdef FEAT_EVAL
9614 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9615# endif
9616}
9617
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618/*
9619 * ":nohlsearch"
9620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009622ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009624 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009625 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627#endif
9628
9629#ifdef FEAT_CRYPT
9630/*
9631 * ":X": Get crypt key
9632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009634ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009636 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009637 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639#endif
9640
9641#ifdef FEAT_FOLDING
9642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009643ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
9645 if (foldManualAllowed(TRUE))
9646 foldCreate(eap->line1, eap->line2);
9647}
9648
9649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009650ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651{
9652 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9653 eap->forceit, FALSE);
9654}
9655
9656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009657ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658{
9659 linenr_T lnum;
9660
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009661#ifdef FEAT_CLIPBOARD
9662 start_global_changes();
9663#endif
9664
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 /* First set the marks for all lines closed/open. */
9666 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9667 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9668 ml_setmarked(lnum);
9669
9670 /* Execute the command on the marked lines. */
9671 global_exe(eap->arg);
9672 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009673#ifdef FEAT_CLIPBOARD
9674 end_global_changes();
9675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676}
9677#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009678
Bram Moolenaar39665952018-08-15 20:59:48 +02009679#ifdef FEAT_QUICKFIX
9680/*
9681 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9682 * instead of a quickfix command.
9683 */
9684 int
9685is_loclist_cmd(int cmdidx)
9686{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009687 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009688 return FALSE;
9689 return cmdnames[cmdidx].cmd_name[0] == 'l';
9690}
9691#endif
9692
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009693# if defined(FEAT_TIMERS) || defined(PROTO)
9694 int
9695get_pressedreturn(void)
9696{
9697 return ex_pressedreturn;
9698}
9699
9700 void
9701set_pressedreturn(int val)
9702{
9703 ex_pressedreturn = val;
9704}
9705#endif