blob: ef86ae5fc5ffd52a756a8362ad03db1d3c4d0579 [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 **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#ifndef FEAT_QUICKFIX
49# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000050# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# define ex_cc ex_ni
52# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020053# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000054# define ex_cfile ex_ni
55# define qf_list ex_ni
56# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020057# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000059# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020061#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062# define ex_cclose ex_ni
63# define ex_copen ex_ni
64# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020065# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000067#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
68# define ex_cexpr ex_ni
69#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaarb7316892019-05-01 18:08:42 +020071static 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 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
90static void ex_quit(exarg_T *eap);
91static void ex_cquit(exarg_T *eap);
92static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static void ex_close(exarg_T *eap);
94static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
95static void ex_only(exarg_T *eap);
96static void ex_resize(exarg_T *eap);
97static void ex_stag(exarg_T *eap);
98static void ex_tabclose(exarg_T *eap);
99static void ex_tabonly(exarg_T *eap);
100static void ex_tabnext(exarg_T *eap);
101static void ex_tabmove(exarg_T *eap);
102static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200103#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100104static void ex_pclose(exarg_T *eap);
105static void ex_ptag(exarg_T *eap);
106static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#else
108# define ex_pclose ex_ni
109# define ex_ptag ex_ni
110# define ex_pedit ex_ni
111#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100112static void ex_hide(exarg_T *eap);
113static void ex_stop(exarg_T *eap);
114static void ex_exit(exarg_T *eap);
115static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#else
119# define ex_goto ex_ni
120#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100121static void ex_shell(exarg_T *eap);
122static void ex_preserve(exarg_T *eap);
123static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100124static void ex_mode(exarg_T *eap);
125static void ex_wrongmodifier(exarg_T *eap);
126static void ex_find(exarg_T *eap);
127static void ex_open(exarg_T *eap);
128static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129#ifndef FEAT_GUI
130# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100131static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100133#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100134static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#else
136# define ex_tearoff ex_ni
137#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100138#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
139 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#else
142# define ex_popup ex_ni
143#endif
144#ifndef FEAT_GUI_MSWIN
145# define ex_simalt ex_ni
146#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000147#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148# define gui_mch_find_dialog ex_ni
149# define gui_mch_replace_dialog ex_ni
150#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000151#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152# define ex_helpfind ex_ni
153#endif
154#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100155# define ex_cscope ex_ni
156# define ex_scscope ex_ni
157# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#endif
159#ifndef FEAT_SYN_HL
160# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200161# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000162#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200163#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200164# define ex_syntime ex_ni
165#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000166#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000167# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000169# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000170# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000171# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000172#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200173#ifndef FEAT_PERSISTENT_UNDO
174# define ex_rundo ex_ni
175# define ex_wundo ex_ni
176#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200177#ifndef FEAT_LUA
178# define ex_lua ex_script_ni
179# define ex_luado ex_ni
180# define ex_luafile ex_ni
181#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000182#ifndef FEAT_MZSCHEME
183# define ex_mzscheme ex_script_ni
184# define ex_mzfile ex_ni
185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186#ifndef FEAT_PERL
187# define ex_perl ex_script_ni
188# define ex_perldo ex_ni
189#endif
190#ifndef FEAT_PYTHON
191# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200192# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193# define ex_pyfile ex_ni
194#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200195#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200196# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200197# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200198# define ex_py3file ex_ni
199#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100200#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
201# define ex_pyx ex_script_ni
202# define ex_pyxdo ex_ni
203# define ex_pyxfile ex_ni
204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000205#ifndef FEAT_TCL
206# define ex_tcl ex_script_ni
207# define ex_tcldo ex_ni
208# define ex_tclfile ex_ni
209#endif
210#ifndef FEAT_RUBY
211# define ex_ruby ex_script_ni
212# define ex_rubydo ex_ni
213# define ex_rubyfile ex_ni
214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#ifndef FEAT_KEYMAP
216# define ex_loadkeymap ex_ni
217#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100218static void ex_swapname(exarg_T *eap);
219static void ex_syncbind(exarg_T *eap);
220static void ex_read(exarg_T *eap);
221static void ex_pwd(exarg_T *eap);
222static void ex_equal(exarg_T *eap);
223static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000226#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#else
229# define ex_winpos ex_ni
230#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100231static void ex_operators(exarg_T *eap);
232static void ex_put(exarg_T *eap);
233static void ex_copymove(exarg_T *eap);
234static void ex_submagic(exarg_T *eap);
235static void ex_join(exarg_T *eap);
236static void ex_at(exarg_T *eap);
237static void ex_bang(exarg_T *eap);
238static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200239#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100240static void ex_wundo(exarg_T *eap);
241static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200242#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100243static void ex_redo(exarg_T *eap);
244static void ex_later(exarg_T *eap);
245static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100247static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_startinsert(exarg_T *eap);
251static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ex_checkpath(exarg_T *eap);
254static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#else
256# define ex_findpat ex_ni
257# define ex_checkpath ex_ni
258#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200259#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261#else
262# define ex_psearch ex_ni
263#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ex_tag(exarg_T *eap);
265static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
274# define ex_const ex_ni
275# define ex_continue ex_ni
276# define ex_debug ex_ni
277# define ex_debuggreedy ex_ni
278# define ex_delfunction ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# define ex_echo ex_ni
280# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200282# define ex_endfunction ex_ni
283# define ex_endif ex_ni
284# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200286# define ex_eval ex_ni
287# define ex_execute ex_ni
288# define ex_finally ex_ni
289# define ex_finish ex_ni
290# define ex_function ex_ni
291# define ex_if ex_ni
292# define ex_let ex_ni
293# define ex_lockvar ex_ni
294# define ex_oldfiles ex_ni
295# define ex_options ex_ni
296# define ex_packadd ex_ni
297# define ex_packloadall ex_ni
298# define ex_return ex_ni
299# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300# define ex_throw ex_ni
301# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000303# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200306#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307# define ex_loadview ex_ni
308#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200309#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# define ex_viminfo ex_ni
311#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100312static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100313static void ex_filetype(exarg_T *eap);
314static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000316# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317# define ex_diffpatch ex_ni
318# define ex_diffgetput ex_ni
319# define ex_diffsplit ex_ni
320# define ex_diffthis ex_ni
321# define ex_diffupdate ex_ni
322#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100323static void ex_digraphs(exarg_T *eap);
324static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#else
328# define ex_nohlsearch ex_ni
329# define ex_match ex_ni
330#endif
331#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100332static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#else
334# define ex_X ex_ni
335#endif
336#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_fold(exarg_T *eap);
338static void ex_foldopen(exarg_T *eap);
339static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340#else
341# define ex_fold ex_ni
342# define ex_foldopen ex_ni
343# define ex_folddo ex_ni
344#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100345#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346# define ex_language ex_ni
347#endif
348#ifndef FEAT_SIGNS
349# define ex_sign ex_ni
350#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000351#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200352# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000353# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200354# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#ifndef FEAT_JUMPLIST
358# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200359# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360# define ex_changes ex_ni
361#endif
362
Bram Moolenaar05159a02005-02-26 23:04:13 +0000363#ifndef FEAT_PROFILE
364# define ex_profile ex_ni
365#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200366#ifndef FEAT_TERMINAL
367# define ex_terminal ex_ni
368#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200369#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
370# define ex_xrestore ex_ni
371#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200372#if !defined(FEAT_TEXT_PROP)
373# define ex_popupclear ex_ni
374#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376/*
377 * Declare cmdnames[].
378 */
379#define DO_DECLARE_EXCMD
380#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200381#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100382
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383static char_u dollar_command[2] = {'$', 0};
384
385
386#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000387/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388typedef struct
389{
390 char_u *line; /* command line */
391 linenr_T lnum; /* sourcing_lnum of the line */
392} wcmd_T;
393
394/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000395 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 garray_T *lines_gap; /* growarray with line info */
402 int current_line; /* last read line from growarray */
403 int repeating; /* TRUE when looping a second time */
404 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200405 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 void *cookie;
407};
408
Bram Moolenaare96a2492019-06-25 04:12:16 +0200409static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100410static int store_loop_line(garray_T *gap, char_u *line);
411static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000412
413/* Struct to save a few things while debugging. Used in do_cmdline() only. */
414struct dbg_stuff
415{
416 int trylevel;
417 int force_abort;
418 except_T *caught_stack;
419 char_u *vv_exception;
420 char_u *vv_throwpoint;
421 int did_emsg;
422 int got_int;
423 int did_throw;
424 int need_rethrow;
425 int check_cstack;
426 except_T *current_exception;
427};
428
Bram Moolenaared203462004-06-16 11:19:22 +0000429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100430save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000431{
432 dsp->trylevel = trylevel; trylevel = 0;
433 dsp->force_abort = force_abort; force_abort = FALSE;
434 dsp->caught_stack = caught_stack; caught_stack = NULL;
435 dsp->vv_exception = v_exception(NULL);
436 dsp->vv_throwpoint = v_throwpoint(NULL);
437
438 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
439 dsp->did_emsg = did_emsg; did_emsg = FALSE;
440 dsp->got_int = got_int; got_int = FALSE;
441 dsp->did_throw = did_throw; did_throw = FALSE;
442 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
443 dsp->check_cstack = check_cstack; check_cstack = FALSE;
444 dsp->current_exception = current_exception; current_exception = NULL;
445}
446
447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100448restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000449{
450 suppress_errthrow = FALSE;
451 trylevel = dsp->trylevel;
452 force_abort = dsp->force_abort;
453 caught_stack = dsp->caught_stack;
454 (void)v_exception(dsp->vv_exception);
455 (void)v_throwpoint(dsp->vv_throwpoint);
456 did_emsg = dsp->did_emsg;
457 got_int = dsp->got_int;
458 did_throw = dsp->did_throw;
459 need_rethrow = dsp->need_rethrow;
460 check_cstack = dsp->check_cstack;
461 current_exception = dsp->current_exception;
462}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463#endif
464
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465/*
466 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
467 * command is given.
468 */
469 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100470do_exmode(
471 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472{
473 int save_msg_scroll;
474 int prev_msg_row;
475 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100476 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000477
478 if (improved)
479 exmode_active = EXMODE_VIM;
480 else
481 exmode_active = EXMODE_NORMAL;
482 State = NORMAL;
483
484 /* When using ":global /pat/ visual" and then "Q" we return to continue
485 * the :global command. */
486 if (global_busy)
487 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488
489 save_msg_scroll = msg_scroll;
490 ++RedrawingDisabled; /* don't redisplay the window */
491 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492#ifdef FEAT_GUI
493 /* Ignore scrollbar and mouse events in Ex mode */
494 ++hold_gui_events;
495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496
Bram Moolenaar32526b32019-01-19 17:43:09 +0100497 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 while (exmode_active)
499 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000500 /* Check for a ":normal" command and no more characters left. */
501 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
502 {
503 exmode_active = FALSE;
504 break;
505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 msg_scroll = TRUE;
507 need_wait_return = FALSE;
508 ex_pressedreturn = FALSE;
509 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100510 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 prev_msg_row = msg_row;
512 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 if (improved)
514 {
515 cmdline_row = msg_row;
516 do_cmdline(NULL, getexline, NULL, 0);
517 }
518 else
519 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
520 lines_left = Rows - 1;
521
Bram Moolenaardf177f62005-02-22 08:39:57 +0000522 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100523 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100526 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 if (ex_pressedreturn)
530 {
531 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000532 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 msg_row = prev_msg_row;
534 if (prev_msg_row == Rows - 1)
535 msg_row--;
536 }
537 msg_col = 0;
538 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
539 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
543 {
544 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100545 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100547 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 }
550
551#ifdef FEAT_GUI
552 --hold_gui_events;
553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 --RedrawingDisabled;
555 --no_wait_return;
556 update_screen(CLEAR);
557 need_wait_return = FALSE;
558 msg_scroll = save_msg_scroll;
559}
560
561/*
562 * Execute a simple command line. Used for translated commands like "*".
563 */
564 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100565do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
567 return do_cmdline(cmd, NULL, NULL,
568 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
569}
570
571/*
572 * do_cmdline(): execute one Ex command line
573 *
574 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100575 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 * 2. Split up in parts separated with '|'.
577 *
578 * This function can be called recursively!
579 *
580 * flags:
581 * DOCMD_VERBOSE - The command will be included in the error message.
582 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100583 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * DOCMD_KEYTYPED - Don't reset KeyTyped.
585 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
586 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
587 *
588 * return FAIL if cmdline could not be executed, OK otherwise
589 */
590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100591do_cmdline(
592 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200593 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100594 void *cookie, /* argument for fgetline() */
595 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596{
597 char_u *next_cmdline; /* next cmd to execute */
598 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100599 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 static int recursive = 0; /* recursive depth */
601 int msg_didout_before_start = 0;
602 int count = 0; /* line number count */
603 int did_inc = FALSE; /* incremented RedrawingDisabled */
604 int retval = OK;
605#ifdef FEAT_EVAL
606 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000607 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 int current_line = 0; /* active line in lines_ga */
609 char_u *fname = NULL; /* function or script name */
610 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
611 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000612 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 int initial_trylevel;
614 struct msglist **saved_msg_list = NULL;
615 struct msglist *private_msg_list;
616
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100617 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200618 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000620 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000622 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100624# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625# define cmd_cookie cookie
626#endif
627 static int call_depth = 0; /* recursiveness */
628
629#ifdef FEAT_EVAL
630 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
631 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000632 * This ensures that the do_errthrow() call in do_one_cmd() does not
633 * combine the messages stored by an earlier invocation of do_one_cmd()
634 * with the command name of the later one. This would happen when
635 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 saved_msg_list = msg_list;
637 msg_list = &private_msg_list;
638 private_msg_list = NULL;
639#endif
640
641 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100642 * here. The value of 200 allows nested function calls, ":source", etc.
643 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100644 if (call_depth >= 200
645#ifdef FEAT_EVAL
646 && call_depth >= p_mfd
647#endif
648 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100650 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#ifdef FEAT_EVAL
652 /* When converting to an exception, we do not include the command name
653 * since this is not an error of the specific command. */
654 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
655 msg_list = saved_msg_list;
656#endif
657 return FAIL;
658 }
659 ++call_depth;
660
661#ifdef FEAT_EVAL
662 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000663 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 cstack.cs_trylevel = 0;
665 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000666 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
668
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100669 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670
671 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100672 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000673 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 ++ex_nesting_level;
675
676 /* Get the function or script name and the address where the next breakpoint
677 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000678 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 fname = func_name(real_cookie);
681 breakpoint = func_breakpoint(real_cookie);
682 dbg_tick = func_dbg_tick(real_cookie);
683 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100684 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 {
686 fname = sourcing_name;
687 breakpoint = source_breakpoint(real_cookie);
688 dbg_tick = source_dbg_tick(real_cookie);
689 }
690
691 /*
692 * Initialize "force_abort" and "suppress_errthrow" at the top level.
693 */
694 if (!recursive)
695 {
696 force_abort = FALSE;
697 suppress_errthrow = FALSE;
698 }
699
700 /*
701 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000702 * exception handling (used when debugging). Otherwise clear it to avoid
703 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000705 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000706 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000707 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200708 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
710 initial_trylevel = trylevel;
711
712 /*
713 * "did_throw" will be set to TRUE when an exception is being thrown.
714 */
715 did_throw = FALSE;
716#endif
717 /*
718 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000719 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 * If force_abort is set, we cancel everything.
721 */
722 did_emsg = FALSE;
723
724 /*
725 * KeyTyped is only set when calling vgetc(). Reset it here when not
726 * calling vgetc() (sourced command lines).
727 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100728 if (!(flags & DOCMD_KEYTYPED)
729 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 KeyTyped = FALSE;
731
732 /*
733 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000734 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 * - for multiple commands on one line, separated with '|'
736 * - when repeating until there are no more lines (for ":source")
737 */
738 next_cmdline = cmdline;
739 do
740 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000741#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100742 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000743#endif
744
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000745 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 if (next_cmdline == NULL
747#ifdef FEAT_EVAL
748 && !force_abort
749 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000750 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751#endif
752 )
753 did_emsg = FALSE;
754
755 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000756 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100757 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * 3. If a line is given: Make a copy, so we can mess with it.
759 */
760
761#ifdef FEAT_EVAL
762 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000763 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 {
765 /* Each '|' separated command is stored separately in lines_ga, to
766 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100767 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768
769 /* Check if a function has returned or, unless it has an unclosed
770 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000774 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 func_line_end(real_cookie);
776# endif
777 if (func_has_ended(real_cookie))
778 {
779 retval = FAIL;
780 break;
781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000784 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000786 script_line_end();
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100790 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
792 retval = FAIL;
793 break;
794 }
795
796 /* If breakpoints have been added/deleted need to check for it. */
797 if (breakpoint != NULL && dbg_tick != NULL
798 && *dbg_tick != debug_tick)
799 {
800 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100801 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 fname, sourcing_lnum);
803 *dbg_tick = debug_tick;
804 }
805
806 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
807 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
808
809 /* Did we encounter a breakpoint? */
810 if (breakpoint != NULL && *breakpoint != 0
811 && *breakpoint <= sourcing_lnum)
812 {
813 dbg_breakpoint(fname, sourcing_lnum);
814 /* Find next breakpoint. */
815 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100816 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 fname, sourcing_lnum);
818 *dbg_tick = debug_tick;
819 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000821 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822 {
823 if (getline_is_func)
824 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000826 script_line_start();
827 }
828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 }
830
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000831 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000833 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100834 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 * below, so that it stores lines in or reads them from
836 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000837 * while/for loop. */
838 cmd_getline = get_loop_line;
839 cmd_cookie = (void *)&cmd_loop_cookie;
840 cmd_loop_cookie.lines_gap = &lines_ga;
841 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100842 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000843 cmd_loop_cookie.cookie = cookie;
844 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 }
846 else
847 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100848 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 cmd_cookie = cookie;
850 }
851#endif
852
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100853 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 if (next_cmdline == NULL)
855 {
856 /*
857 * Need to set msg_didout for the first line after an ":if",
858 * otherwise the ":if" will be overwritten.
859 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100862 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863#ifdef FEAT_EVAL
864 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
865#else
866 0
867#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200868 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 {
870 /* Don't call wait_return for aborted command line. The NULL
871 * returned for the end of a sourced file or executed function
872 * doesn't do this. */
873 if (KeyTyped && !(flags & DOCMD_REPEAT))
874 need_wait_return = FALSE;
875 retval = FAIL;
876 break;
877 }
878 used_getline = TRUE;
879
880 /*
881 * Keep the first typed line. Clear it when more lines are typed.
882 */
883 if (flags & DOCMD_KEEPLINE)
884 {
885 vim_free(repeat_cmdline);
886 if (count == 0)
887 repeat_cmdline = vim_strsave(next_cmdline);
888 else
889 repeat_cmdline = NULL;
890 }
891 }
892
893 /* 3. Make a copy of the command so we can mess with it. */
894 else if (cmdline_copy == NULL)
895 {
896 next_cmdline = vim_strsave(next_cmdline);
897 if (next_cmdline == NULL)
898 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100899 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 retval = FAIL;
901 break;
902 }
903 }
904 cmdline_copy = next_cmdline;
905
906#ifdef FEAT_EVAL
907 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000908 * Save the current line when inside a ":while" or ":for", and when
909 * the command looks like a ":while" or ":for", because we may need it
910 * later. When there is a '|' and another command, it is stored
911 * separately, because we need to be able to jump back to it from an
912 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000914 if (current_line == lines_ga.ga_len
915 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000917 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 {
919 retval = FAIL;
920 break;
921 }
922 }
923 did_endif = FALSE;
924#endif
925
926 if (count++ == 0)
927 {
928 /*
929 * All output from the commands is put below each other, without
930 * waiting for a return. Don't do this when executing commands
931 * from a script or when being called recursive (e.g. for ":e
932 * +command file").
933 */
934 if (!(flags & DOCMD_NOWAIT) && !recursive)
935 {
936 msg_didout_before_start = msg_didout;
937 msg_didany = FALSE; /* no output yet */
938 msg_start();
939 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200940 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 ++RedrawingDisabled;
942 did_inc = TRUE;
943 }
944 }
945
946 if (p_verbose >= 15 && sourcing_name != NULL)
947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000949 verbose_enter_scroll();
950
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000953 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100954 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000955
956 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 --no_wait_return;
958 }
959
960 /*
961 * 2. Execute one '|' separated command.
962 * do_one_cmd() will return NULL if there is no trailing '|'.
963 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
964 */
965 ++recursive;
966 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
967#ifdef FEAT_EVAL
968 &cstack,
969#endif
970 cmd_getline, cmd_cookie);
971 --recursive;
972
973#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000974 if (cmd_cookie == (void *)&cmd_loop_cookie)
975 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000977 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#endif
979
980 if (next_cmdline == NULL)
981 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100982 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 /*
985 * If the command was typed, remember it for the ':' register.
986 * Do this AFTER executing the command to make :@: work.
987 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100988 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 && new_last_cmdline != NULL)
990 {
991 vim_free(last_cmdline);
992 last_cmdline = new_last_cmdline;
993 new_last_cmdline = NULL;
994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 else
997 {
998 /* need to copy the command after the '|' to cmdline_copy, for the
999 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001000 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 next_cmdline = cmdline_copy;
1002 }
1003
1004
1005#ifdef FEAT_EVAL
1006 /* reset did_emsg for a function that is not aborted by an error */
1007 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001008 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 && !func_has_abort(real_cookie))
1010 did_emsg = FALSE;
1011
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001012 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 {
1014 ++current_line;
1015
1016 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001017 * An ":endwhile", ":endfor" and ":continue" is handled here.
1018 * If we were executing commands, jump back to the ":while" or
1019 * ":for".
1020 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001022 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001024 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001026 /* Jump back to the matching ":while" or ":for". Be careful
1027 * not to use a cs_line[] from an entry that isn't a ":while"
1028 * or ":for": It would make "current_line" invalid and can
1029 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if (!did_emsg && !got_int && !did_throw
1031 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 && (cstack.cs_flags[cstack.cs_idx]
1033 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 && cstack.cs_line[cstack.cs_idx] >= 0
1035 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1036 {
1037 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 /* remember we jumped there */
1039 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 line_breakcheck(); /* check if CTRL-C typed */
1041
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 /* Check for the next breakpoint at or after the ":while"
1043 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (breakpoint != NULL)
1045 {
1046 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001047 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 fname,
1049 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1050 *dbg_tick = debug_tick;
1051 }
1052 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001057 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1058 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060 }
1061
1062 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001067 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1069 }
1070 }
1071
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001072 /* Check for the next breakpoint after a watchexpression */
1073 if (breakpoint != NULL && has_watchexpr())
1074 {
1075 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1076 *dbg_tick = debug_tick;
1077 }
1078
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 /*
1080 * When not inside any ":while" loop, clear remembered lines.
1081 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
1084 if (lines_ga.ga_len > 0)
1085 {
1086 sourcing_lnum =
1087 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1088 free_cmdlines(&lines_ga);
1089 }
1090 current_line = 0;
1091 }
1092
1093 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1095 * being restored at the ":endtry". Reset them here and set the
1096 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1097 * This includes the case where a missing ":endif", ":endwhile" or
1098 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 cstack.cs_lflags &= ~CSL_HAD_FINA;
1103 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1104 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 did_throw ? (void *)current_exception : NULL);
1106 did_emsg = got_int = did_throw = FALSE;
1107 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1108 }
1109
1110 /* Update global "trylevel" for recursive calls to do_cmdline() from
1111 * within this loop. */
1112 trylevel = initial_trylevel + cstack.cs_trylevel;
1113
1114 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001115 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 * files) is aborted because of an error, an interrupt, or an uncaught
1117 * exception, cancel everything. If it is left normally, reset
1118 * force_abort to get the non-EH compatible abortion behavior for
1119 * the rest of the script.
1120 */
1121 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1122 force_abort = FALSE;
1123
1124 /* Convert an interrupt to an exception if appropriate. */
1125 (void)do_intthrow(&cstack);
1126#endif /* FEAT_EVAL */
1127
1128 }
1129 /*
1130 * Continue executing command lines when:
1131 * - no CTRL-C typed, no aborting error, no exception thrown or try
1132 * conditionals need to be checked for executing finally clauses or
1133 * catching an interrupt exception
1134 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001135 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 * looping for ":source" command or function call.
1137 */
1138 while (!((got_int
1139#ifdef FEAT_EVAL
1140 || (did_emsg && force_abort) || did_throw
1141#endif
1142 )
1143#ifdef FEAT_EVAL
1144 && cstack.cs_trylevel == 0
1145#endif
1146 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001147 && !(did_emsg
1148#ifdef FEAT_EVAL
1149 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001150 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001151 * the :endtry to be missed. */
1152 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1153#endif
1154 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001155 && (getline_equal(fgetline, cookie, getexmodeline)
1156 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 && (next_cmdline != NULL
1158#ifdef FEAT_EVAL
1159 || cstack.cs_idx >= 0
1160#endif
1161 || (flags & DOCMD_REPEAT)));
1162
1163 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001164 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165#ifdef FEAT_EVAL
1166 free_cmdlines(&lines_ga);
1167 ga_clear(&lines_ga);
1168
1169 if (cstack.cs_idx >= 0)
1170 {
1171 /*
1172 * If a sourced file or executed function ran to its end, report the
1173 * unclosed conditional.
1174 */
1175 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001176 && ((getline_equal(fgetline, cookie, getsourceline)
1177 && !source_finished(fgetline, cookie))
1178 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 && !func_has_ended(real_cookie))))
1180 {
1181 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001182 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001184 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001186 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001188 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
1190
1191 /*
1192 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1193 * ":endtry" in a sourced file or executed function. If the try
1194 * conditional is in its finally clause, ignore anything pending.
1195 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001196 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 */
1198 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001199 {
1200 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1201
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001202 if (idx >= 0)
1203 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001204 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1205 &cstack.cs_looplevel);
1206 }
1207 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 trylevel = initial_trylevel;
1209 }
1210
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1212 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001214 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 ? (char_u *)"endfunction" : (char_u *)NULL);
1216
1217 if (trylevel == 0)
1218 {
1219 /*
1220 * When an exception is being thrown out of the outermost try
1221 * conditional, discard the uncaught exception, disable the conversion
1222 * of interrupts or errors to exceptions, and ensure that no more
1223 * commands are executed.
1224 */
1225 if (did_throw)
1226 {
1227 void *p = NULL;
1228 char_u *saved_sourcing_name;
1229 int saved_sourcing_lnum;
1230 struct msglist *messages = NULL, *next;
1231
1232 /*
1233 * If the uncaught exception is a user exception, report it as an
1234 * error. If it is an error exception, display the saved error
1235 * message now. For an interrupt exception, do nothing; the
1236 * interrupt message is given elsewhere.
1237 */
1238 switch (current_exception->type)
1239 {
1240 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001241 vim_snprintf((char *)IObuff, IOSIZE,
1242 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 current_exception->value);
1244 p = vim_strsave(IObuff);
1245 break;
1246 case ET_ERROR:
1247 messages = current_exception->messages;
1248 current_exception->messages = NULL;
1249 break;
1250 case ET_INTERRUPT:
1251 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 }
1253
1254 saved_sourcing_name = sourcing_name;
1255 saved_sourcing_lnum = sourcing_lnum;
1256 sourcing_name = current_exception->throw_name;
1257 sourcing_lnum = current_exception->throw_lnum;
1258 current_exception->throw_name = NULL;
1259
1260 discard_current_exception(); /* uses IObuff if 'verbose' */
1261 suppress_errthrow = TRUE;
1262 force_abort = TRUE;
1263
1264 if (messages != NULL)
1265 {
1266 do
1267 {
1268 next = messages->next;
1269 emsg(messages->msg);
1270 vim_free(messages->msg);
1271 vim_free(messages);
1272 messages = next;
1273 }
1274 while (messages != NULL);
1275 }
1276 else if (p != NULL)
1277 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001278 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 vim_free(p);
1280 }
1281 vim_free(sourcing_name);
1282 sourcing_name = saved_sourcing_name;
1283 sourcing_lnum = saved_sourcing_lnum;
1284 }
1285
1286 /*
1287 * On an interrupt or an aborting error not converted to an exception,
1288 * disable the conversion of errors to exceptions. (Interrupts are not
1289 * converted any more, here.) This enables also the interrupt message
1290 * when force_abort is set and did_emsg unset in case of an interrupt
1291 * from a finally clause after an error.
1292 */
1293 else if (got_int || (did_emsg && force_abort))
1294 suppress_errthrow = TRUE;
1295 }
1296
1297 /*
1298 * The current cstack will be freed when do_cmdline() returns. An uncaught
1299 * exception will have to be rethrown in the previous cstack. If a function
1300 * has just returned or a script file was just finished and the previous
1301 * cstack belongs to the same function or, respectively, script file, it
1302 * will have to be checked for finally clauses to be executed due to the
1303 * ":return" or ":finish". This is done in do_one_cmd().
1304 */
1305 if (did_throw)
1306 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001307 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001309 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 && ex_nesting_level > func_level(real_cookie) + 1))
1311 {
1312 if (!did_throw)
1313 check_cstack = TRUE;
1314 }
1315 else
1316 {
1317 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001318 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 --ex_nesting_level;
1320 /*
1321 * Go to debug mode when returning from a function in which we are
1322 * single-stepping.
1323 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 if ((getline_equal(fgetline, cookie, getsourceline)
1325 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 ? (char_u *)_("End of sourced file")
1329 : (char_u *)_("End of function"));
1330 }
1331
1332 /*
1333 * Restore the exception environment (done after returning from the
1334 * debugger).
1335 */
1336 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001337 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 msg_list = saved_msg_list;
1340#endif /* FEAT_EVAL */
1341
1342 /*
1343 * If there was too much output to fit on the command line, ask the user to
1344 * hit return before redrawing the screen. With the ":global" command we do
1345 * this only once after the command is finished.
1346 */
1347 if (did_inc)
1348 {
1349 --RedrawingDisabled;
1350 --no_wait_return;
1351 msg_scroll = FALSE;
1352
1353 /*
1354 * When just finished an ":if"-":else" which was typed, no need to
1355 * wait for hit-return. Also for an error situation.
1356 */
1357 if (retval == FAIL
1358#ifdef FEAT_EVAL
1359 || (did_endif && KeyTyped && !did_emsg)
1360#endif
1361 )
1362 {
1363 need_wait_return = FALSE;
1364 msg_didany = FALSE; /* don't wait when restarting edit */
1365 }
1366 else if (need_wait_return)
1367 {
1368 /*
1369 * The msg_start() above clears msg_didout. The wait_return we do
1370 * here should not overwrite the command that may be shown before
1371 * doing that.
1372 */
1373 msg_didout |= msg_didout_before_start;
1374 wait_return(FALSE);
1375 }
1376 }
1377
Bram Moolenaar2a942252012-11-28 23:03:07 +01001378#ifdef FEAT_EVAL
1379 did_endif = FALSE; /* in case do_cmdline used recursively */
1380#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 /*
1382 * Reset if_level, in case a sourced script file contains more ":if" than
1383 * ":endif" (could be ":if x | foo | endif").
1384 */
1385 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001386#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001387
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 --call_depth;
1389 return retval;
1390}
1391
1392#ifdef FEAT_EVAL
1393/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001394 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 */
1396 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001397get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001399 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 wcmd_T *wp;
1401 char_u *line;
1402
1403 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1404 {
1405 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001406 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001408 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001410 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001412 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001413 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 ++cp->current_line;
1415
1416 return line;
1417 }
1418
1419 KeyTyped = FALSE;
1420 ++cp->current_line;
1421 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1422 sourcing_lnum = wp->lnum;
1423 return vim_strsave(wp->line);
1424}
1425
1426/*
1427 * Store a line in "gap" so that a ":while" loop can execute it again.
1428 */
1429 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001430store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431{
1432 if (ga_grow(gap, 1) == FAIL)
1433 return FAIL;
1434 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1435 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1436 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 return OK;
1438}
1439
1440/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001441 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 */
1443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001444free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 while (gap->ga_len > 0)
1447 {
1448 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1449 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451}
1452#endif
1453
1454/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001455 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1456 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001459getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001460 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001461 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001462 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001465 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001466 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467
Bram Moolenaar89d40322006-08-29 15:30:07 +00001468 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001469 * function that's originally used to obtain the lines. This may be
1470 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001471 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001472 cp = (struct loop_cookie *)cookie;
1473 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 {
1475 gp = cp->getline;
1476 cp = cp->cookie;
1477 }
1478 return gp == func;
1479#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481#endif
1482}
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001485 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 * getline function. Otherwise return "cookie".
1487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001489getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001490 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001491 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
Bram Moolenaar13505972019-01-24 15:04:48 +01001493#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001494 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
Bram Moolenaar89d40322006-08-29 15:30:07 +00001497 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001498 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001500 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 cp = (struct loop_cookie *)cookie;
1502 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
1504 gp = cp->getline;
1505 cp = cp->cookie;
1506 }
1507 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001508#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001511}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001513
1514/*
1515 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1516 * ":bwipeout", etc.
1517 * Returns the buffer number.
1518 */
1519 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001520compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001521{
1522 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001523 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001524 int count = offset;
1525
1526 buf = firstbuf;
1527 while (buf->b_next != NULL && buf->b_fnum < lnum)
1528 buf = buf->b_next;
1529 while (count != 0)
1530 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001531 count += (offset < 0) ? 1 : -1;
1532 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1533 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001534 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001535 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001536 if (addr_type == ADDR_LOADED_BUFFERS)
1537 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001538 while (buf->b_ml.ml_mfp == NULL)
1539 {
1540 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1541 if (nextbuf == NULL)
1542 break;
1543 buf = nextbuf;
1544 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001545 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001546 /* we might have gone too far, last buffer is not loadedd */
1547 if (addr_type == ADDR_LOADED_BUFFERS)
1548 while (buf->b_ml.ml_mfp == NULL)
1549 {
1550 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1551 if (nextbuf == NULL)
1552 break;
1553 buf = nextbuf;
1554 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001555 return buf->b_fnum;
1556}
1557
Bram Moolenaarb7316892019-05-01 18:08:42 +02001558/*
1559 * Return the window number of "win".
1560 * When "win" is NULL return the number of windows.
1561 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001562 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001563current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001564{
1565 win_T *wp;
1566 int nr = 0;
1567
Bram Moolenaar29323592016-07-24 22:04:11 +02001568 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001569 {
1570 ++nr;
1571 if (wp == win)
1572 break;
1573 }
1574 return nr;
1575}
1576
1577 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001578current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001579{
1580 tabpage_T *tp;
1581 int nr = 0;
1582
Bram Moolenaar29323592016-07-24 22:04:11 +02001583 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001584 {
1585 ++nr;
1586 if (tp == tab)
1587 break;
1588 }
1589 return nr;
1590}
1591
1592# define CURRENT_WIN_NR current_win_nr(curwin)
1593# define LAST_WIN_NR current_win_nr(NULL)
1594# define CURRENT_TAB_NR current_tab_nr(curtab)
1595# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597/*
1598 * Execute one Ex command.
1599 *
1600 * If 'sourcing' is TRUE, the command will be included in the error message.
1601 *
1602 * 1. skip comment lines and leading space
1603 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001604 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001606 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607 * 6. parse arguments
1608 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001610 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 *
1612 * This function may be called recursively!
1613 */
1614#if (_MSC_VER == 1200)
1615/*
Bram Moolenaared203462004-06-16 11:19:22 +00001616 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001618 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619#endif
1620 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001621do_one_cmd(
1622 char_u **cmdlinep,
1623 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001625 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001627 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001628 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629{
1630 char_u *p;
1631 linenr_T lnum;
1632 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001633 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001634 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001636 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001638 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001640 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
1642 vim_memset(&ea, 0, sizeof(ea));
1643 ea.line1 = 1;
1644 ea.line2 = 1;
1645#ifdef FEAT_EVAL
1646 ++ex_nesting_level;
1647#endif
1648
Bram Moolenaar21691f82012-12-05 19:13:18 +01001649 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (quitmore
1651#ifdef FEAT_EVAL
1652 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001653 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001654#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001655 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001656 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 --quitmore;
1658
1659 /*
1660 * Reset browse, confirm, etc.. They are restored when returning, for
1661 * recursive calls.
1662 */
1663 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001665 /* "#!anything" is handled like a comment. */
1666 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1667 goto doend;
1668
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001669/*
1670 * 1. Skip comment lines and leading white space and colons.
1671 * 2. Handle command modifiers.
1672 */
1673 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001675 ea.cmdlinep = cmdlinep;
1676 ea.getline = fgetline;
1677 ea.cookie = cookie;
1678#ifdef FEAT_EVAL
1679 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001681 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001682 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001684 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686#ifdef FEAT_EVAL
1687 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1688 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1689#else
1690 ea.skip = (if_level > 0);
1691#endif
1692
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001694 * 3. Skip over the range to find the command. Let "p" point to after it.
1695 *
1696 * We need the command to know what kind of range it uses.
1697 */
1698 cmd = ea.cmd;
1699 ea.cmd = skip_range(ea.cmd, NULL);
1700 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1701 ea.cmd = skipwhite(ea.cmd + 1);
1702 p = find_command(&ea, NULL);
1703
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001704#ifdef FEAT_EVAL
1705# ifdef FEAT_PROFILE
1706 // Count this line for profiling if skip is TRUE.
1707 if (do_profiling == PROF_YES
1708 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1709 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1710 {
1711 int skip = did_emsg || got_int || did_throw;
1712
1713 if (ea.cmdidx == CMD_catch)
1714 skip = !skip && !(cstack->cs_idx >= 0
1715 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1716 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1717 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1718 skip = skip || !(cstack->cs_idx >= 0
1719 && !(cstack->cs_flags[cstack->cs_idx]
1720 & (CSF_ACTIVE | CSF_TRUE)));
1721 else if (ea.cmdidx == CMD_finally)
1722 skip = FALSE;
1723 else if (ea.cmdidx != CMD_endif
1724 && ea.cmdidx != CMD_endfor
1725 && ea.cmdidx != CMD_endtry
1726 && ea.cmdidx != CMD_endwhile)
1727 skip = ea.skip;
1728
1729 if (!skip)
1730 {
1731 if (getline_equal(fgetline, cookie, get_func_line))
1732 func_line_exec(getline_cookie(fgetline, cookie));
1733 else if (getline_equal(fgetline, cookie, getsourceline))
1734 script_line_exec();
1735 }
1736 }
1737# endif
1738
1739 /* May go to debug mode. If this happens and the ">quit" debug command is
1740 * used, throw an interrupt exception and skip the next command. */
1741 dbg_check_breakpoint(&ea);
1742 if (!ea.skip && got_int)
1743 {
1744 ea.skip = TRUE;
1745 (void)do_intthrow(cstack);
1746 }
1747#endif
1748
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001749/*
1750 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 *
1752 * where 'addr' is:
1753 *
1754 * % (entire file)
1755 * $ [+-NUM]
1756 * 'x [+-NUM] (where x denotes a currently defined mark)
1757 * . [+-NUM]
1758 * [+-NUM]..
1759 * NUM
1760 *
1761 * The ea.cmd pointer is updated to point to the first character following the
1762 * range spec. If an initial address is found, but no second, the upper bound
1763 * is equal to the lower.
1764 */
1765
Bram Moolenaar25190db2019-05-04 15:05:28 +02001766 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001767 if (!IS_USER_CMDIDX(ea.cmdidx))
1768 {
1769 if (ea.cmdidx != CMD_SIZE)
1770 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1771 else
1772 ea.addr_type = ADDR_LINES;
1773
Bram Moolenaar25190db2019-05-04 15:05:28 +02001774 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001775 if (ea.cmdidx == CMD_wincmd && p != NULL)
1776 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001777#ifdef FEAT_QUICKFIX
1778 // :.cc in quickfix window uses line number
1779 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1780 ea.addr_type = ADDR_OTHER;
1781#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001782 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001783
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001784 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001785 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001786 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001789 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 */
1791
1792 /*
1793 * Skip ':' and any white space
1794 */
1795 ea.cmd = skipwhite(ea.cmd);
1796 while (*ea.cmd == ':')
1797 ea.cmd = skipwhite(ea.cmd + 1);
1798
1799 /*
1800 * If we got a line, but no command, then go to the line.
1801 * If we find a '|' or '\n' we set ea.nextcmd.
1802 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 if (*ea.cmd == NUL || *ea.cmd == '"'
1804 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 {
1806 /*
1807 * strange vi behaviour:
1808 * ":3" jumps to line 3
1809 * ":3|..." prints line 3
1810 * ":|" prints current line
1811 */
1812 if (ea.skip) /* skip this if inside :if */
1813 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001814 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
1816 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001817 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if ((errormsg = invalid_range(&ea)) == NULL)
1819 {
1820 correct_range(&ea);
1821 ex_print(&ea);
1822 }
1823 }
1824 else if (ea.addr_count != 0)
1825 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001826 if (ea.line2 > curbuf->b_ml.ml_line_count)
1827 {
1828 /* With '-' in 'cpoptions' a line number past the file is an
1829 * error, otherwise put it at the end of the file. */
1830 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1831 ea.line2 = -1;
1832 else
1833 ea.line2 = curbuf->b_ml.ml_line_count;
1834 }
1835
1836 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 else
1839 {
1840 if (ea.line2 == 0)
1841 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 else
1843 curwin->w_cursor.lnum = ea.line2;
1844 beginline(BL_SOL | BL_FIX);
1845 }
1846 }
1847 goto doend;
1848 }
1849
Bram Moolenaard5005162014-08-22 23:05:54 +02001850 /* If this looks like an undefined user command and there are CmdUndefined
1851 * autocommands defined, trigger the matching autocommands. */
1852 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1853 && ASCII_ISUPPER(*ea.cmd)
1854 && has_cmdundefined())
1855 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001856 int ret;
1857
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001858 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001859 while (ASCII_ISALNUM(*p))
1860 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001861 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001862 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1863 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001864 /* If the autocommands did something and didn't cause an error, try
1865 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001866 p = (ret
1867#ifdef FEAT_EVAL
1868 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001869#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001870 ) ? find_command(&ea, NULL) : ea.cmd;
1871 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001872
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (p == NULL)
1874 {
1875 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001876 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 goto doend;
1878 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001879 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001880 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1881 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 {
1883 errormsg = uc_fun_cmd();
1884 goto doend;
1885 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (ea.cmdidx == CMD_SIZE)
1888 {
1889 if (!ea.skip)
1890 {
1891 STRCPY(IObuff, _("E492: Not an editor command"));
1892 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001893 {
1894 /* If the modifier was parsed OK the error must be in the
1895 * following command */
1896 if (after_modifier != NULL)
1897 append_command(after_modifier);
1898 else
1899 append_command(*cmdlinep);
1900 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001901 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001902 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 goto doend;
1905 }
1906
Bram Moolenaar958636c2014-10-21 20:01:58 +02001907 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1908 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001909#ifdef HAVE_EX_SCRIPT_NI
1910 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1911#endif
1912 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914#ifndef FEAT_EVAL
1915 /*
1916 * When the expression evaluation is disabled, recognize the ":if" and
1917 * ":endif" commands and ignore everything in between it.
1918 */
1919 if (ea.cmdidx == CMD_if)
1920 ++if_level;
1921 if (if_level)
1922 {
1923 if (ea.cmdidx == CMD_endif)
1924 --if_level;
1925 goto doend;
1926 }
1927
1928#endif
1929
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001930 /* forced commands */
1931 if (*p == '!' && ea.cmdidx != CMD_substitute
1932 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 {
1934 ++p;
1935 ea.forceit = TRUE;
1936 }
1937 else
1938 ea.forceit = FALSE;
1939
1940/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001941 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001943 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001944 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
1946 if (!ea.skip)
1947 {
1948#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001949 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001951 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001952 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 goto doend;
1954 }
1955#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001956 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001957 {
1958 errormsg = _("E981: Command not allowed in rvim");
1959 goto doend;
1960 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001961 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
1963 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001964 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 goto doend;
1966 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001967
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001968 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001969 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001971 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001972 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 goto doend;
1974 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001975
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001976 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001977 * Do allow ":checktime" (it is postponed).
1978 * Do allow ":edit" (check for an argument later).
1979 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001980 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001981 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001982 && ea.cmdidx != CMD_edit
1983 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001984 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001985 && curbuf_locked())
1986 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001988 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
1990 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001991 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 goto doend;
1993 }
1994 }
1995
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001996 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001998 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 goto doend;
2000 }
2001
2002 /*
2003 * Don't complain about the range if it is not used
2004 * (could happen if line_count is accidentally set to 0).
2005 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002006 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
2008 /*
2009 * If the range is backwards, ask for confirmation and, if given, swap
2010 * ea.line1 & ea.line2 so it's forwards again.
2011 * When global command is busy, don't ask, will fail below.
2012 */
2013 if (!global_busy && ea.line1 > ea.line2)
2014 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002015 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002017 if (sourcing || exmode_active)
2018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002019 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002020 goto doend;
2021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 if (ask_yesno((char_u *)
2023 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002024 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 }
2026 lnum = ea.line1;
2027 ea.line1 = ea.line2;
2028 ea.line2 = lnum;
2029 }
2030 if ((errormsg = invalid_range(&ea)) != NULL)
2031 goto doend;
2032 }
2033
Bram Moolenaarb7316892019-05-01 18:08:42 +02002034 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2035 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 ea.line2 = 1;
2037
2038 correct_range(&ea);
2039
2040#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002041 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002042 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
2044 /* Put the first line at the start of a closed fold, put the last line
2045 * at the end of a closed fold. */
2046 (void)hasFolding(ea.line1, &ea.line1, NULL);
2047 (void)hasFolding(ea.line2, NULL, &ea.line2);
2048 }
2049#endif
2050
2051#ifdef FEAT_QUICKFIX
2052 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002053 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 * option here, so things like % get expanded.
2055 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002056 p = replace_makeprg(&ea, p, cmdlinep);
2057 if (p == NULL)
2058 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059#endif
2060
2061 /*
2062 * Skip to start of argument.
2063 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2064 */
2065 if (ea.cmdidx == CMD_bang)
2066 ea.arg = p;
2067 else
2068 ea.arg = skipwhite(p);
2069
Bram Moolenaar379fb762018-08-30 15:58:28 +02002070 // ":file" cannot be run with an argument when "curbuf_lock" is set
2071 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2072 goto doend;
2073
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 /*
2075 * Check for "++opt=val" argument.
2076 * Must be first, allow ":w ++enc=utf8 !cmd"
2077 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002078 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2080 if (getargopt(&ea) == FAIL && !ni)
2081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002082 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 goto doend;
2084 }
2085
2086 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2087 {
2088 if (*ea.arg == '>') /* append */
2089 {
2090 if (*++ea.arg != '>') /* typed wrong */
2091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002092 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 goto doend;
2094 }
2095 ea.arg = skipwhite(ea.arg + 1);
2096 ea.append = TRUE;
2097 }
2098 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2099 {
2100 ++ea.arg;
2101 ea.usefilter = TRUE;
2102 }
2103 }
2104
2105 if (ea.cmdidx == CMD_read)
2106 {
2107 if (ea.forceit)
2108 {
2109 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2110 ea.forceit = FALSE;
2111 }
2112 else if (*ea.arg == '!') /* :r !filter */
2113 {
2114 ++ea.arg;
2115 ea.usefilter = TRUE;
2116 }
2117 }
2118
2119 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2120 {
2121 ea.amount = 1;
2122 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2123 {
2124 ++ea.arg;
2125 ++ea.amount;
2126 }
2127 ea.arg = skipwhite(ea.arg);
2128 }
2129
2130 /*
2131 * Check for "+command" argument, before checking for next command.
2132 * Don't do this for ":read !cmd" and ":write !cmd".
2133 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002134 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2136
2137 /*
2138 * Check for '|' to separate commands and '"' to start comments.
2139 * Don't do this for ":read !cmd" and ":write !cmd".
2140 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002141 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 separate_nextcmd(&ea);
2143
2144 /*
2145 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002146 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2147 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002149 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002150 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002151 || ea.cmdidx == CMD_global
2152 || ea.cmdidx == CMD_vglobal
2153 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
2155 for (p = ea.arg; *p; ++p)
2156 {
2157 /* Remove one backslash before a newline, so that it's possible to
2158 * pass a newline to the shell and also a newline that is preceded
2159 * with a backslash. This makes it impossible to end a shell
2160 * command in a backslash, but that doesn't appear useful.
2161 * Halving the number of backslashes is incompatible with previous
2162 * versions. */
2163 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002164 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 else if (*p == '\n')
2166 {
2167 ea.nextcmd = p + 1;
2168 *p = NUL;
2169 break;
2170 }
2171 }
2172 }
2173
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002174 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002176 buf_T *buf;
2177
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002179 switch (ea.addr_type)
2180 {
2181 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002182 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002183 ea.line2 = curbuf->b_ml.ml_line_count;
2184 break;
2185 case ADDR_LOADED_BUFFERS:
2186 buf = firstbuf;
2187 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2188 buf = buf->b_next;
2189 ea.line1 = buf->b_fnum;
2190 buf = lastbuf;
2191 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2192 buf = buf->b_prev;
2193 ea.line2 = buf->b_fnum;
2194 break;
2195 case ADDR_BUFFERS:
2196 ea.line1 = firstbuf->b_fnum;
2197 ea.line2 = lastbuf->b_fnum;
2198 break;
2199 case ADDR_WINDOWS:
2200 ea.line2 = LAST_WIN_NR;
2201 break;
2202 case ADDR_TABS:
2203 ea.line2 = LAST_TAB_NR;
2204 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002205 case ADDR_TABS_RELATIVE:
2206 ea.line2 = 1;
2207 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002208 case ADDR_ARGUMENTS:
2209 if (ARGCOUNT == 0)
2210 ea.line1 = ea.line2 = 0;
2211 else
2212 ea.line2 = ARGCOUNT;
2213 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002214 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002215#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002216 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002217 if (ea.line2 == 0)
2218 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002219#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002220 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002221 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002222 case ADDR_UNSIGNED:
2223 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002224 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002225 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228
2229 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002230 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002232 /* Do not allow register = for user commands */
2233 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002234 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002236#ifndef FEAT_CLIPBOARD
2237 /* check these explicitly for a more specific error message */
2238 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002240 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002241 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002244 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2245 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002246 {
2247 ea.regname = *ea.arg++;
2248#ifdef FEAT_EVAL
2249 /* for '=' register: accept the rest of the line as an expression */
2250 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2251 {
2252 set_expr_line(vim_strsave(ea.arg));
2253 ea.arg += STRLEN(ea.arg);
2254 }
2255#endif
2256 ea.arg = skipwhite(ea.arg);
2257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
2259
2260 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002261 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 * count, it's a buffer name.
2263 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002264 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2265 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002266 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 {
2268 n = getdigits(&ea.arg);
2269 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002272 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 goto doend;
2274 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002275 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
2277 ea.line2 = n;
2278 if (ea.addr_count == 0)
2279 ea.addr_count = 1;
2280 }
2281 else
2282 {
2283 ea.line1 = ea.line2;
2284 ea.line2 += n - 1;
2285 ++ea.addr_count;
2286 /*
2287 * Be vi compatible: no error message for out of range.
2288 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002289 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 ea.line2 = curbuf->b_ml.ml_line_count;
2291 }
2292 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002293
2294 /*
2295 * Check for flags: 'l', 'p' and '#'.
2296 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002297 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002298 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002299 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2300 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002302 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002303 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 goto doend;
2305 }
2306
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002307 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 goto doend;
2311 }
2312
2313#ifdef FEAT_EVAL
2314 /*
2315 * Skip the command when it's not going to be executed.
2316 * The commands like :if, :endif, etc. always need to be executed.
2317 * Also make an exception for commands that handle a trailing command
2318 * themselves.
2319 */
2320 if (ea.skip)
2321 {
2322 switch (ea.cmdidx)
2323 {
2324 /* commands that need evaluation */
2325 case CMD_while:
2326 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002327 case CMD_for:
2328 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 case CMD_if:
2330 case CMD_elseif:
2331 case CMD_else:
2332 case CMD_endif:
2333 case CMD_try:
2334 case CMD_catch:
2335 case CMD_finally:
2336 case CMD_endtry:
2337 case CMD_function:
2338 break;
2339
2340 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002341 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 * the list at ":help :bar". */
2343 case CMD_aboveleft:
2344 case CMD_and:
2345 case CMD_belowright:
2346 case CMD_botright:
2347 case CMD_browse:
2348 case CMD_call:
2349 case CMD_confirm:
2350 case CMD_delfunction:
2351 case CMD_djump:
2352 case CMD_dlist:
2353 case CMD_dsearch:
2354 case CMD_dsplit:
2355 case CMD_echo:
2356 case CMD_echoerr:
2357 case CMD_echomsg:
2358 case CMD_echon:
2359 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002360 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 case CMD_help:
2362 case CMD_hide:
2363 case CMD_ijump:
2364 case CMD_ilist:
2365 case CMD_isearch:
2366 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002367 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 case CMD_keepjumps:
2369 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002370 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 case CMD_leftabove:
2372 case CMD_let:
2373 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002374 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002376 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002377 case CMD_noautocmd:
2378 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 case CMD_perl:
2380 case CMD_psearch:
2381 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002382 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002383 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 case CMD_return:
2385 case CMD_rightbelow:
2386 case CMD_ruby:
2387 case CMD_silent:
2388 case CMD_smagic:
2389 case CMD_snomagic:
2390 case CMD_substitute:
2391 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002392 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 case CMD_tcl:
2394 case CMD_throw:
2395 case CMD_tilde:
2396 case CMD_topleft:
2397 case CMD_unlet:
2398 case CMD_verbose:
2399 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002400 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 break;
2402
2403 default: goto doend;
2404 }
2405 }
2406#endif
2407
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002408 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2411 goto doend;
2412 }
2413
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 /*
2415 * Accept buffer name. Cannot be used at the same time with a buffer
2416 * number. Don't do this for a user command.
2417 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002418 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002419 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
2421 /*
2422 * :bdelete, :bwipeout and :bunload take several arguments, separated
2423 * by spaces: find next space (skipping over escaped characters).
2424 * The others take one argument: ignore trailing spaces.
2425 */
2426 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2427 || ea.cmdidx == CMD_bunload)
2428 p = skiptowhite_esc(ea.arg);
2429 else
2430 {
2431 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002432 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 --p;
2434 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002435 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002436 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 if (ea.line2 < 0) /* failed */
2438 goto doend;
2439 ea.addr_count = 1;
2440 ea.arg = skipwhite(p);
2441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
Bram Moolenaar2be57332018-02-13 18:05:18 +01002443 /* The :try command saves the emsg_silent flag, reset it here when
2444 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002445 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002446 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002447 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002448 if (emsg_silent < 0)
2449 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002450 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002451 }
2452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002454 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar958636c2014-10-21 20:01:58 +02002457 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
2459 /*
2460 * Execute a user-defined command.
2461 */
2462 do_ucmd(&ea);
2463 }
2464 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002467 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
2469 ea.errmsg = NULL;
2470 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2471 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002472 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 }
2474
2475#ifdef FEAT_EVAL
2476 /*
2477 * If the command just executed called do_cmdline(), any throw or ":return"
2478 * or ":finish" encountered there must also check the cstack of the still
2479 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2480 * exception, or reanimate a returned function or finished script file and
2481 * return or finish it again.
2482 */
2483 if (need_rethrow)
2484 do_throw(cstack);
2485 else if (check_cstack)
2486 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002487 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 && current_func_returned())
2491 do_return(&ea, TRUE, FALSE, NULL);
2492 }
2493 need_rethrow = check_cstack = FALSE;
2494#endif
2495
2496doend:
2497 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002498 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002500 curwin->w_cursor.col = 0;
2501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2504 {
2505 if (sourcing)
2506 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002507 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002510 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002512 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514 emsg(errormsg);
2515 }
2516#ifdef FEAT_EVAL
2517 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002518 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2519 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520#endif
2521
Bram Moolenaareffed932018-08-14 13:38:17 +02002522 if (ea.verbose_save >= 0)
2523 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002524
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002525 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002527 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaareffed932018-08-14 13:38:17 +02002529 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 /* messages could be enabled for a serious error, need to check if the
2532 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002533 if (!did_emsg || msg_silent > ea.save_msg_silent)
2534 msg_silent = ea.save_msg_silent;
2535 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (emsg_silent < 0)
2537 emsg_silent = 0;
2538 /* Restore msg_scroll, it's set by file I/O commands, even when no
2539 * message is actually displayed. */
2540 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002541
2542 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2543 * somewhere in the line. Put it back in the first column. */
2544 if (redirecting())
2545 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 }
2547
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002548#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002549 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002550 --sandbox;
2551#endif
2552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2554 ea.nextcmd = NULL;
2555
2556#ifdef FEAT_EVAL
2557 --ex_nesting_level;
2558#endif
2559
2560 return ea.nextcmd;
2561}
2562#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002563 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564#endif
2565
2566/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002567 * Parse and skip over command modifiers:
2568 * - update eap->cmd
2569 * - store flags in "cmdmod".
2570 * - Set ex_pressedreturn for an empty command line.
2571 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002572 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002573 * - set p_verbose for ":verbose"
2574 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002575 * When "skip_only" is TRUE the global variables are not changed, except for
2576 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002577 * Return FAIL when the command is not to be executed.
2578 * May set "errormsg" to an error message.
2579 */
2580 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002581parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002582{
2583 char_u *p;
2584
2585 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2586 eap->verbose_save = -1;
2587 eap->save_msg_silent = -1;
2588
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002589 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002590 for (;;)
2591 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002592 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2593 ++eap->cmd;
2594
2595 /* in ex mode, an empty line works like :+ */
2596 if (*eap->cmd == NUL && exmode_active
2597 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2598 || getline_equal(eap->getline, eap->cookie, getexline))
2599 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2600 {
2601 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002602 if (!skip_only)
2603 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 }
2605
2606 /* ignore comment and empty lines */
2607 if (*eap->cmd == '"')
2608 return FAIL;
2609 if (*eap->cmd == NUL)
2610 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002611 if (!skip_only)
2612 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002613 return FAIL;
2614 }
2615
Bram Moolenaareffed932018-08-14 13:38:17 +02002616 p = skip_range(eap->cmd, NULL);
2617 switch (*p)
2618 {
2619 /* When adding an entry, also modify cmd_exists(). */
2620 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2621 break;
2622 cmdmod.split |= WSP_ABOVE;
2623 continue;
2624
2625 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2626 {
2627 cmdmod.split |= WSP_BELOW;
2628 continue;
2629 }
2630 if (checkforcmd(&eap->cmd, "browse", 3))
2631 {
2632#ifdef FEAT_BROWSE_CMD
2633 cmdmod.browse = TRUE;
2634#endif
2635 continue;
2636 }
2637 if (!checkforcmd(&eap->cmd, "botright", 2))
2638 break;
2639 cmdmod.split |= WSP_BOT;
2640 continue;
2641
2642 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2643 break;
2644#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2645 cmdmod.confirm = TRUE;
2646#endif
2647 continue;
2648
2649 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2650 {
2651 cmdmod.keepmarks = TRUE;
2652 continue;
2653 }
2654 if (checkforcmd(&eap->cmd, "keepalt", 5))
2655 {
2656 cmdmod.keepalt = TRUE;
2657 continue;
2658 }
2659 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2660 {
2661 cmdmod.keeppatterns = TRUE;
2662 continue;
2663 }
2664 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2665 break;
2666 cmdmod.keepjumps = TRUE;
2667 continue;
2668
2669 case 'f': /* only accept ":filter {pat} cmd" */
2670 {
2671 char_u *reg_pat;
2672
2673 if (!checkforcmd(&p, "filter", 4)
2674 || *p == NUL || ends_excmd(*p))
2675 break;
2676 if (*p == '!')
2677 {
2678 cmdmod.filter_force = TRUE;
2679 p = skipwhite(p + 1);
2680 if (*p == NUL || ends_excmd(*p))
2681 break;
2682 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002683 if (skip_only)
2684 p = skip_vimgrep_pat(p, NULL, NULL);
2685 else
2686 // NOTE: This puts a NUL after the pattern.
2687 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002688 if (p == NULL || *p == NUL)
2689 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002690 if (!skip_only)
2691 {
2692 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002693 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002694 if (cmdmod.filter_regmatch.regprog == NULL)
2695 break;
2696 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002697 eap->cmd = p;
2698 continue;
2699 }
2700
2701 /* ":hide" and ":hide | cmd" are not modifiers */
2702 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2703 || *p == NUL || ends_excmd(*p))
2704 break;
2705 eap->cmd = p;
2706 cmdmod.hide = TRUE;
2707 continue;
2708
2709 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2710 {
2711 cmdmod.lockmarks = TRUE;
2712 continue;
2713 }
2714
2715 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2716 break;
2717 cmdmod.split |= WSP_ABOVE;
2718 continue;
2719
2720 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2721 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002722 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002723 {
2724 /* Set 'eventignore' to "all". Restore the
2725 * existing option value later. */
2726 cmdmod.save_ei = vim_strsave(p_ei);
2727 set_string_option_direct((char_u *)"ei", -1,
2728 (char_u *)"all", OPT_FREE, SID_NONE);
2729 }
2730 continue;
2731 }
2732 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2733 break;
2734 cmdmod.noswapfile = TRUE;
2735 continue;
2736
2737 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2738 break;
2739 cmdmod.split |= WSP_BELOW;
2740 continue;
2741
2742 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2743 {
2744#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002745 if (!skip_only)
2746 {
2747 if (!eap->did_sandbox)
2748 ++sandbox;
2749 eap->did_sandbox = TRUE;
2750 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002751#endif
2752 continue;
2753 }
2754 if (!checkforcmd(&eap->cmd, "silent", 3))
2755 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002756 if (!skip_only)
2757 {
2758 if (eap->save_msg_silent == -1)
2759 eap->save_msg_silent = msg_silent;
2760 ++msg_silent;
2761 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002762 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2763 {
2764 /* ":silent!", but not "silent !cmd" */
2765 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002766 if (!skip_only)
2767 {
2768 ++emsg_silent;
2769 ++eap->did_esilent;
2770 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002771 }
2772 continue;
2773
2774 case 't': if (checkforcmd(&p, "tab", 3))
2775 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002776 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002778 long tabnr = get_address(eap, &eap->cmd,
2779 ADDR_TABS, eap->skip,
2780 skip_only, FALSE, 1);
2781 if (tabnr == MAXLNUM)
2782 cmdmod.tab = tabpage_index(curtab) + 1;
2783 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002785 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002787 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002788 return FAIL;
2789 }
2790 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 }
2793 eap->cmd = p;
2794 continue;
2795 }
2796 if (!checkforcmd(&eap->cmd, "topleft", 2))
2797 break;
2798 cmdmod.split |= WSP_TOP;
2799 continue;
2800
2801 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2802 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002803 if (!skip_only)
2804 {
2805 if (eap->save_msg_silent == -1)
2806 eap->save_msg_silent = msg_silent;
2807 msg_silent = 0;
2808 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 continue;
2810
2811 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2812 {
2813 cmdmod.split |= WSP_VERT;
2814 continue;
2815 }
2816 if (!checkforcmd(&p, "verbose", 4))
2817 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002818 if (!skip_only)
2819 {
2820 if (eap->verbose_save < 0)
2821 eap->verbose_save = p_verbose;
2822 if (vim_isdigit(*eap->cmd))
2823 p_verbose = atoi((char *)eap->cmd);
2824 else
2825 p_verbose = 1;
2826 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002827 eap->cmd = p;
2828 continue;
2829 }
2830 break;
2831 }
2832
2833 return OK;
2834}
2835
2836/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002837 * Free contents of "cmdmod".
2838 */
2839 static void
2840free_cmdmod(void)
2841{
2842 if (cmdmod.save_ei != NULL)
2843 {
2844 /* Restore 'eventignore' to the value before ":noautocmd". */
2845 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2846 OPT_FREE, SID_NONE);
2847 free_string_option(cmdmod.save_ei);
2848 }
2849
2850 if (cmdmod.filter_regmatch.regprog != NULL)
2851 vim_regfree(cmdmod.filter_regmatch.regprog);
2852}
2853
2854/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002855 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002856 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002857 * Return FAIL and set "errormsg" or return OK.
2858 */
2859 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002860parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002861{
2862 int address_count = 1;
2863 linenr_T lnum;
2864
2865 // Repeat for all ',' or ';' separated addresses.
2866 for (;;)
2867 {
2868 eap->line1 = eap->line2;
2869 switch (eap->addr_type)
2870 {
2871 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002872 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002873 // default is current line number
2874 eap->line2 = curwin->w_cursor.lnum;
2875 break;
2876 case ADDR_WINDOWS:
2877 eap->line2 = CURRENT_WIN_NR;
2878 break;
2879 case ADDR_ARGUMENTS:
2880 eap->line2 = curwin->w_arg_idx + 1;
2881 if (eap->line2 > ARGCOUNT)
2882 eap->line2 = ARGCOUNT;
2883 break;
2884 case ADDR_LOADED_BUFFERS:
2885 case ADDR_BUFFERS:
2886 eap->line2 = curbuf->b_fnum;
2887 break;
2888 case ADDR_TABS:
2889 eap->line2 = CURRENT_TAB_NR;
2890 break;
2891 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002892 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002893 eap->line2 = 1;
2894 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002895 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002896#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002897 eap->line2 = qf_get_cur_idx(eap);
2898#endif
2899 break;
2900 case ADDR_QUICKFIX_VALID:
2901#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002902 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002903#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002904 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002905 case ADDR_NONE:
2906 // Will give an error later if a range is found.
2907 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002908 }
2909 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002910 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002911 eap->addr_count == 0, address_count++);
2912 if (eap->cmd == NULL) // error detected
2913 return FAIL;
2914 if (lnum == MAXLNUM)
2915 {
2916 if (*eap->cmd == '%') // '%' - all lines
2917 {
2918 ++eap->cmd;
2919 switch (eap->addr_type)
2920 {
2921 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002922 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002923 eap->line1 = 1;
2924 eap->line2 = curbuf->b_ml.ml_line_count;
2925 break;
2926 case ADDR_LOADED_BUFFERS:
2927 {
2928 buf_T *buf = firstbuf;
2929
2930 while (buf->b_next != NULL
2931 && buf->b_ml.ml_mfp == NULL)
2932 buf = buf->b_next;
2933 eap->line1 = buf->b_fnum;
2934 buf = lastbuf;
2935 while (buf->b_prev != NULL
2936 && buf->b_ml.ml_mfp == NULL)
2937 buf = buf->b_prev;
2938 eap->line2 = buf->b_fnum;
2939 break;
2940 }
2941 case ADDR_BUFFERS:
2942 eap->line1 = firstbuf->b_fnum;
2943 eap->line2 = lastbuf->b_fnum;
2944 break;
2945 case ADDR_WINDOWS:
2946 case ADDR_TABS:
2947 if (IS_USER_CMDIDX(eap->cmdidx))
2948 {
2949 eap->line1 = 1;
2950 eap->line2 = eap->addr_type == ADDR_WINDOWS
2951 ? LAST_WIN_NR : LAST_TAB_NR;
2952 }
2953 else
2954 {
2955 // there is no Vim command which uses '%' and
2956 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002957 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002958 return FAIL;
2959 }
2960 break;
2961 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002962 case ADDR_UNSIGNED:
2963 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002964 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002965 return FAIL;
2966 case ADDR_ARGUMENTS:
2967 if (ARGCOUNT == 0)
2968 eap->line1 = eap->line2 = 0;
2969 else
2970 {
2971 eap->line1 = 1;
2972 eap->line2 = ARGCOUNT;
2973 }
2974 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002975 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002976#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002977 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002978 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002979 if (eap->line2 == 0)
2980 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002981#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002982 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002983 case ADDR_NONE:
2984 // Will give an error later if a range is found.
2985 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002986 }
2987 ++eap->addr_count;
2988 }
2989 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2990 {
2991 pos_T *fp;
2992
2993 // '*' - visual area
2994 if (eap->addr_type != ADDR_LINES)
2995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002996 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002997 return FAIL;
2998 }
2999
3000 ++eap->cmd;
3001 if (!eap->skip)
3002 {
3003 fp = getmark('<', FALSE);
3004 if (check_mark(fp) == FAIL)
3005 return FAIL;
3006 eap->line1 = fp->lnum;
3007 fp = getmark('>', FALSE);
3008 if (check_mark(fp) == FAIL)
3009 return FAIL;
3010 eap->line2 = fp->lnum;
3011 ++eap->addr_count;
3012 }
3013 }
3014 }
3015 else
3016 eap->line2 = lnum;
3017 eap->addr_count++;
3018
3019 if (*eap->cmd == ';')
3020 {
3021 if (!eap->skip)
3022 {
3023 curwin->w_cursor.lnum = eap->line2;
3024 // don't leave the cursor on an illegal line or column
3025 check_cursor();
3026 }
3027 }
3028 else if (*eap->cmd != ',')
3029 break;
3030 ++eap->cmd;
3031 }
3032
3033 // One address given: set start and end lines.
3034 if (eap->addr_count == 1)
3035 {
3036 eap->line1 = eap->line2;
3037 // ... but only implicit: really no address given
3038 if (lnum == MAXLNUM)
3039 eap->addr_count = 0;
3040 }
3041 return OK;
3042}
3043
3044/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003045 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 * If there is a match advance "pp" to the argument and return TRUE.
3047 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003048 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003049checkforcmd(
3050 char_u **pp, /* start of command */
3051 char *cmd, /* name of command */
3052 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053{
3054 int i;
3055
3056 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003057 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 break;
3059 if (i >= len && !isalpha((*pp)[i]))
3060 {
3061 *pp = skipwhite(*pp + i);
3062 return TRUE;
3063 }
3064 return FALSE;
3065}
3066
3067/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003068 * Append "cmd" to the error message in IObuff.
3069 * Takes care of limiting the length and handling 0xa0, which would be
3070 * invisible otherwise.
3071 */
3072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003073append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003074{
3075 char_u *s = cmd;
3076 char_u *d;
3077
3078 STRCAT(IObuff, ": ");
3079 d = IObuff + STRLEN(IObuff);
3080 while (*s != NUL && d - IObuff < IOSIZE - 7)
3081 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003082 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003083 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003084 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003085 STRCPY(d, "<a0>");
3086 d += 4;
3087 }
3088 else
3089 MB_COPY_CHAR(s, d);
3090 }
3091 *d = NUL;
3092}
3093
3094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003096 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003097 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003098 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 * Returns NULL for an ambiguous user command.
3100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003102find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103{
3104 int len;
3105 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003106 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107
3108 /*
3109 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003110 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * - the 'k' command can directly be followed by any character.
3112 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003113 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003115 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 */
3117 p = eap->cmd;
3118 if (*p == 'k')
3119 {
3120 eap->cmdidx = CMD_k;
3121 ++p;
3122 }
3123 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003124 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3125 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 || p[1] == 'g'
3127 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3128 || p[1] == 'I'
3129 || (p[1] == 'r' && p[2] != 'e')))
3130 {
3131 eap->cmdidx = CMD_substitute;
3132 ++p;
3133 }
3134 else
3135 {
3136 while (ASCII_ISALPHA(*p))
3137 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003138 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003139 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003140 while (ASCII_ISALNUM(*p))
3141 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 /* check for non-alpha command */
3144 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3145 ++p;
3146 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003147 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3148 {
3149 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3150 * :delete with the 'l' flag. Same for 'p'. */
3151 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003152 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003153 break;
3154 if (i == len - 1)
3155 {
3156 --len;
3157 if (p[-1] == 'l')
3158 eap->flags |= EXFLAG_LIST;
3159 else
3160 eap->flags |= EXFLAG_PRINT;
3161 }
3162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003164 if (ASCII_ISLOWER(eap->cmd[0]))
3165 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003166 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003167 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003168
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003169 if (command_count != (int)CMD_SIZE)
3170 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003171 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003172 getout(1);
3173 }
3174
3175 /* Use a precomputed index for fast look-up in cmdnames[]
3176 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003177 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3178 if (ASCII_ISLOWER(c2))
3179 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003182 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
3184 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3185 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3186 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3187 (size_t)len) == 0)
3188 {
3189#ifdef FEAT_EVAL
3190 if (full != NULL
3191 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3192 *full = TRUE;
3193#endif
3194 break;
3195 }
3196
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003197 // Look for a user defined command as a last resort. Let ":Print" be
3198 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003199 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3200 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003202 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 while (ASCII_ISALNUM(*p))
3204 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003205 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003207 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 eap->cmdidx = CMD_SIZE;
3209 }
3210
3211 return p;
3212}
3213
3214#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003215static struct cmdmod
3216{
3217 char *name;
3218 int minlen;
3219 int has_count; /* :123verbose :3tab */
3220} cmdmods[] = {
3221 {"aboveleft", 3, FALSE},
3222 {"belowright", 3, FALSE},
3223 {"botright", 2, FALSE},
3224 {"browse", 3, FALSE},
3225 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003226 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003227 {"hide", 3, FALSE},
3228 {"keepalt", 5, FALSE},
3229 {"keepjumps", 5, FALSE},
3230 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003231 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003232 {"leftabove", 5, FALSE},
3233 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003234 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003235 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003236 {"rightbelow", 6, FALSE},
3237 {"sandbox", 3, FALSE},
3238 {"silent", 3, FALSE},
3239 {"tab", 3, TRUE},
3240 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003241 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003242 {"verbose", 4, TRUE},
3243 {"vertical", 4, FALSE},
3244};
3245
3246/*
3247 * Return length of a command modifier (including optional count).
3248 * Return zero when it's not a modifier.
3249 */
3250 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003251modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003252{
3253 int i, j;
3254 char_u *p = cmd;
3255
3256 if (VIM_ISDIGIT(*cmd))
3257 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003258 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003259 {
3260 for (j = 0; p[j] != NUL; ++j)
3261 if (p[j] != cmdmods[i].name[j])
3262 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003263 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003264 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003265 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003266 }
3267 return 0;
3268}
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270/*
3271 * Return > 0 if an Ex command "name" exists.
3272 * Return 2 if there is an exact match.
3273 * Return 3 if there is an ambiguous match.
3274 */
3275 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003276cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 exarg_T ea;
3279 int full = FALSE;
3280 int i;
3281 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003282 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003285 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 for (j = 0; name[j] != NUL; ++j)
3288 if (name[j] != cmdmods[i].name[j])
3289 break;
3290 if (name[j] == NUL && j >= cmdmods[i].minlen)
3291 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3292 }
3293
Bram Moolenaara9587612006-05-04 21:47:50 +00003294 /* Check built-in commands and user defined commands.
3295 * For ":2match" and ":3match" we need to skip the number. */
3296 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003298 p = find_command(&ea, &full);
3299 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003301 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3302 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003303 if (*skipwhite(p) != NUL)
3304 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3306}
3307#endif
3308
Bram Moolenaard0190392019-08-23 21:17:35 +02003309 cmdidx_T
3310excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311{
Bram Moolenaard0190392019-08-23 21:17:35 +02003312 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313
Bram Moolenaard0190392019-08-23 21:17:35 +02003314 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3315 idx = (cmdidx_T)((int)idx + 1))
3316 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 break;
3318
Bram Moolenaard0190392019-08-23 21:17:35 +02003319 return idx;
3320}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
Bram Moolenaard0190392019-08-23 21:17:35 +02003322 long
3323excmd_get_argt(cmdidx_T idx)
3324{
3325 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326}
3327
3328/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003329 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 *
3331 * Backslashed delimiters after / or ? will be skipped, and commands will
3332 * not be expanded between /'s and ?'s or after "'".
3333 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003334 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 * Returns the "cmd" pointer advanced to beyond the range.
3336 */
3337 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003338skip_range(
3339 char_u *cmd,
3340 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003342 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003344 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003346 if (*cmd == '\\')
3347 {
3348 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3349 ++cmd;
3350 else
3351 break;
3352 }
3353 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
3355 if (*++cmd == NUL && ctx != NULL)
3356 *ctx = EXPAND_NOTHING;
3357 }
3358 else if (*cmd == '/' || *cmd == '?')
3359 {
3360 delim = *cmd++;
3361 while (*cmd != NUL && *cmd != delim)
3362 if (*cmd++ == '\\' && *cmd != NUL)
3363 ++cmd;
3364 if (*cmd == NUL && ctx != NULL)
3365 *ctx = EXPAND_NOTHING;
3366 }
3367 if (*cmd != NUL)
3368 ++cmd;
3369 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003370
3371 /* Skip ":" and white space. */
3372 while (*cmd == ':')
3373 cmd = skipwhite(cmd + 1);
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 return cmd;
3376}
3377
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003378 static void
3379addr_error(cmd_addr_T addr_type)
3380{
3381 if (addr_type == ADDR_NONE)
3382 emsg(_(e_norange));
3383 else
3384 emsg(_(e_invrange));
3385}
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003388 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 *
3390 * Set ptr to the next character after the part that was interpreted.
3391 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003392 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 *
3394 * Return MAXLNUM when no Ex address was found.
3395 */
3396 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003397get_address(
3398 exarg_T *eap UNUSED,
3399 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02003400 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003401 int skip, // only skip the address, don't use it
3402 int silent, // no errors or side effects
3403 int to_other_file, // flag: may jump to other file
3404 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405{
Bram Moolenaarb7316892019-05-01 18:08:42 +02003406 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 int c;
3408 int i;
3409 long n;
3410 char_u *cmd;
3411 pos_T pos;
3412 pos_T *fp;
3413 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003414 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 cmd = skipwhite(*ptr);
3417 lnum = MAXLNUM;
3418 do
3419 {
3420 switch (*cmd)
3421 {
3422 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003423 ++cmd;
3424 switch (addr_type)
3425 {
3426 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003427 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 lnum = curwin->w_cursor.lnum;
3429 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003430 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003431 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003432 break;
3433 case ADDR_ARGUMENTS:
3434 lnum = curwin->w_arg_idx + 1;
3435 break;
3436 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003437 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003438 lnum = curbuf->b_fnum;
3439 break;
3440 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003441 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003442 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003443 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003444 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003445 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003446 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003447 cmd = NULL;
3448 goto error;
3449 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003450 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003451#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003452 lnum = qf_get_cur_idx(eap);
3453#endif
3454 break;
3455 case ADDR_QUICKFIX_VALID:
3456#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003457 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003458#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003459 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003460 }
3461 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462
3463 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003464 ++cmd;
3465 switch (addr_type)
3466 {
3467 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003468 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 lnum = curbuf->b_ml.ml_line_count;
3470 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003471 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003472 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003473 break;
3474 case ADDR_ARGUMENTS:
3475 lnum = ARGCOUNT;
3476 break;
3477 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003478 buf = lastbuf;
3479 while (buf->b_ml.ml_mfp == NULL)
3480 {
3481 if (buf->b_prev == NULL)
3482 break;
3483 buf = buf->b_prev;
3484 }
3485 lnum = buf->b_fnum;
3486 break;
3487 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003488 lnum = lastbuf->b_fnum;
3489 break;
3490 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003491 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003492 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003493 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003494 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003495 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003496 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003497 cmd = NULL;
3498 goto error;
3499 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003500 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003501#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003502 lnum = qf_get_size(eap);
3503 if (lnum == 0)
3504 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003505#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003506 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003507 case ADDR_QUICKFIX_VALID:
3508#ifdef FEAT_QUICKFIX
3509 lnum = qf_get_valid_size(eap);
3510 if (lnum == 0)
3511 lnum = 1;
3512#endif
3513 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003514 }
3515 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
3517 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003518 if (*++cmd == NUL)
3519 {
3520 cmd = NULL;
3521 goto error;
3522 }
3523 if (addr_type != ADDR_LINES)
3524 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003525 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003526 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003527 goto error;
3528 }
3529 if (skip)
3530 ++cmd;
3531 else
3532 {
3533 /* Only accept a mark in another file when it is
3534 * used by itself: ":'M". */
3535 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3536 ++cmd;
3537 if (fp == (pos_T *)-1)
3538 /* Jumped to another file. */
3539 lnum = curwin->w_cursor.lnum;
3540 else
3541 {
3542 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
3544 cmd = NULL;
3545 goto error;
3546 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003547 lnum = fp->lnum;
3548 }
3549 }
3550 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 case '/':
3553 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003554 c = *cmd++;
3555 if (addr_type != ADDR_LINES)
3556 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003557 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003558 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003559 goto error;
3560 }
3561 if (skip) /* skip "/pat/" */
3562 {
3563 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3564 if (*cmd == c)
3565 ++cmd;
3566 }
3567 else
3568 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003569 int flags;
3570
3571 pos = curwin->w_cursor; // save curwin->w_cursor
3572
3573 // When '/' or '?' follows another address, start from
3574 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003575 if (lnum != MAXLNUM)
3576 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003577
3578 // Start a forward search at the end of the line (unless
3579 // before the first line).
3580 // Start a backward search at the start of the line.
3581 // This makes sure we never match in the current
3582 // line, and can match anywhere in the
3583 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003584 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003585 curwin->w_cursor.col = MAXCOL;
3586 else
3587 curwin->w_cursor.col = 0;
3588 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003589 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
3590 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003591 {
3592 curwin->w_cursor = pos;
3593 cmd = NULL;
3594 goto error;
3595 }
3596 lnum = curwin->w_cursor.lnum;
3597 curwin->w_cursor = pos;
3598 /* adjust command string pointer */
3599 cmd += searchcmdlen;
3600 }
3601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003604 ++cmd;
3605 if (addr_type != ADDR_LINES)
3606 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003607 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003608 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003609 goto error;
3610 }
3611 if (*cmd == '&')
3612 i = RE_SUBST;
3613 else if (*cmd == '?' || *cmd == '/')
3614 i = RE_SEARCH;
3615 else
3616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003617 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003618 cmd = NULL;
3619 goto error;
3620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003622 if (!skip)
3623 {
3624 /*
3625 * When search follows another address, start from
3626 * there.
3627 */
3628 if (lnum != MAXLNUM)
3629 pos.lnum = lnum;
3630 else
3631 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003633 /*
3634 * Start the search just like for the above
3635 * do_search().
3636 */
3637 if (*cmd != '?')
3638 pos.col = MAXCOL;
3639 else
3640 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003641 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003642 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003643 *cmd == '?' ? BACKWARD : FORWARD,
3644 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003645 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003646 lnum = pos.lnum;
3647 else
3648 {
3649 cmd = NULL;
3650 goto error;
3651 }
3652 }
3653 ++cmd;
3654 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003657 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3658 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 }
3660
3661 for (;;)
3662 {
3663 cmd = skipwhite(cmd);
3664 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3665 break;
3666
3667 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003668 {
3669 switch (addr_type)
3670 {
3671 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003672 case ADDR_OTHER:
3673 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003674 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003675 break;
3676 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003677 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003678 break;
3679 case ADDR_ARGUMENTS:
3680 lnum = curwin->w_arg_idx + 1;
3681 break;
3682 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003683 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003684 lnum = curbuf->b_fnum;
3685 break;
3686 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003687 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003688 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003689 case ADDR_TABS_RELATIVE:
3690 lnum = 1;
3691 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003692 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003693#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003694 lnum = qf_get_cur_idx(eap);
3695#endif
3696 break;
3697 case ADDR_QUICKFIX_VALID:
3698#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003699 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003700#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003701 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003702 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003703 case ADDR_UNSIGNED:
3704 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003705 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003706 }
3707 }
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 if (VIM_ISDIGIT(*cmd))
3710 i = '+'; /* "number" is same as "+number" */
3711 else
3712 i = *cmd++;
3713 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3714 n = 1;
3715 else
3716 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003717
3718 if (addr_type == ADDR_TABS_RELATIVE)
3719 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003720 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003721 cmd = NULL;
3722 goto error;
3723 }
3724 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003725 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003726 lnum = compute_buffer_local_count(
3727 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003729 {
3730#ifdef FEAT_FOLDING
3731 /* Relative line addressing, need to adjust for folded lines
3732 * now, but only do it after the first address. */
3733 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3734 && address_count >= 2)
3735 (void)hasFolding(lnum, NULL, &lnum);
3736#endif
3737 if (i == '-')
3738 lnum -= n;
3739 else
3740 lnum += n;
3741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743 } while (*cmd == '/' || *cmd == '?');
3744
3745error:
3746 *ptr = cmd;
3747 return lnum;
3748}
3749
3750/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003751 * Get flags from an Ex command argument.
3752 */
3753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003754get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003755{
3756 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3757 {
3758 if (*eap->arg == 'l')
3759 eap->flags |= EXFLAG_LIST;
3760 else if (*eap->arg == 'p')
3761 eap->flags |= EXFLAG_PRINT;
3762 else
3763 eap->flags |= EXFLAG_NR;
3764 eap->arg = skipwhite(eap->arg + 1);
3765 }
3766}
3767
3768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 * Function called for command which is Not Implemented. NI!
3770 */
3771 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003772ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
3774 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003775 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776}
3777
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003778#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779/*
3780 * Function called for script command which is Not Implemented. NI!
3781 * Skips over ":perl <<EOF" constructs.
3782 */
3783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003784ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786 if (!eap->skip)
3787 ex_ni(eap);
3788 else
3789 vim_free(script_get(eap, eap->arg));
3790}
3791#endif
3792
3793/*
3794 * Check range in Ex command for validity.
3795 * Return NULL when valid, error message when invalid.
3796 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003797 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003798invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003800 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003801
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if ( eap->line1 < 0
3803 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003804 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003805 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003806
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003807 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003808 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003809 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003810 {
3811 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003812 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003813#ifdef FEAT_DIFF
3814 + (eap->cmdidx == CMD_diffget)
3815#endif
3816 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003817 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003818 break;
3819 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003820 /* add 1 if ARGCOUNT is 0 */
3821 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003822 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003823 break;
3824 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003825 // Only a boundary check, not whether the buffers actually
3826 // exist.
3827 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003828 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003829 break;
3830 case ADDR_LOADED_BUFFERS:
3831 buf = firstbuf;
3832 while (buf->b_ml.ml_mfp == NULL)
3833 {
3834 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003835 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003836 buf = buf->b_next;
3837 }
3838 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003839 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003840 buf = lastbuf;
3841 while (buf->b_ml.ml_mfp == NULL)
3842 {
3843 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003844 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003845 buf = buf->b_prev;
3846 }
3847 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003848 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003849 break;
3850 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003851 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003852 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003853 break;
3854 case ADDR_TABS:
3855 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003856 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003857 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003858 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003859 case ADDR_OTHER:
3860 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003861 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003862 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003863#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003864 // No error for value that is too big, will use the last entry.
3865 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003866 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003867#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003868 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003869 case ADDR_QUICKFIX_VALID:
3870#ifdef FEAT_QUICKFIX
3871 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3872 || eap->line2 < 0)
3873 return _(e_invrange);
3874#endif
3875 break;
3876 case ADDR_UNSIGNED:
3877 if (eap->line2 < 0)
3878 return _(e_invrange);
3879 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003880 case ADDR_NONE:
3881 // Will give an error elsewhere.
3882 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003883 }
3884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return NULL;
3886}
3887
3888/*
3889 * Correct the range for zero line number, if required.
3890 */
3891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003892correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003894 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 {
3896 if (eap->line1 == 0)
3897 eap->line1 = 1;
3898 if (eap->line2 == 0)
3899 eap->line2 = 1;
3900 }
3901}
3902
Bram Moolenaar748bf032005-02-02 23:04:36 +00003903#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003904/*
3905 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3906 * pattern. Otherwise return eap->arg.
3907 */
3908 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003909skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003910{
3911 char_u *p = eap->arg;
3912
Bram Moolenaara37420f2006-02-04 22:37:47 +00003913 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
3914 || eap->cmdidx == CMD_vimgrepadd
3915 || eap->cmdidx == CMD_lvimgrepadd
3916 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00003917 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003918 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003919 if (p == NULL)
3920 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003921 }
3922 return p;
3923}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003924
3925/*
3926 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
3927 * in the command line, so that things like % get expanded.
3928 */
3929 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003930replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003931{
3932 char_u *new_cmdline;
3933 char_u *program;
3934 char_u *pos;
3935 char_u *ptr;
3936 int len;
3937 int i;
3938
3939 /*
3940 * Don't do it when ":vimgrep" is used for ":grep".
3941 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00003942 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
3943 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3944 || eap->cmdidx == CMD_grepadd
3945 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003946 && !grep_internal(eap->cmdidx))
3947 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00003948 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3949 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003950 {
3951 if (*curbuf->b_p_gp == NUL)
3952 program = p_gp;
3953 else
3954 program = curbuf->b_p_gp;
3955 }
3956 else
3957 {
3958 if (*curbuf->b_p_mp == NUL)
3959 program = p_mp;
3960 else
3961 program = curbuf->b_p_mp;
3962 }
3963
3964 p = skipwhite(p);
3965
3966 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3967 {
3968 /* replace $* by given arguments */
3969 i = 1;
3970 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
3971 ++i;
3972 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003973 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003974 if (new_cmdline == NULL)
3975 return NULL; /* out of memory */
3976 ptr = new_cmdline;
3977 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3978 {
3979 i = (int)(pos - program);
3980 STRNCPY(ptr, program, i);
3981 STRCPY(ptr += i, p);
3982 ptr += len;
3983 program = pos + 2;
3984 }
3985 STRCPY(ptr, program);
3986 }
3987 else
3988 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02003989 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003990 if (new_cmdline == NULL)
3991 return NULL; /* out of memory */
3992 STRCPY(new_cmdline, program);
3993 STRCAT(new_cmdline, " ");
3994 STRCAT(new_cmdline, p);
3995 }
3996 msg_make(p);
3997
3998 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
3999 vim_free(*cmdlinep);
4000 *cmdlinep = new_cmdline;
4001 p = new_cmdline;
4002 }
4003 return p;
4004}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004005#endif
4006
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007/*
4008 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004009 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 * Return FAIL for failure, OK otherwise.
4011 */
4012 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004013expand_filename(
4014 exarg_T *eap,
4015 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004016 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
4018 int has_wildcards; /* need to expand wildcards */
4019 char_u *repl;
4020 int srclen;
4021 char_u *p;
4022 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004023 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
Bram Moolenaar748bf032005-02-02 23:04:36 +00004025#ifdef FEAT_QUICKFIX
4026 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4027 p = skip_grep_pat(eap);
4028#else
4029 p = eap->arg;
4030#endif
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 /*
4033 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4034 * the file name contains a wildcard it should not cause expanding.
4035 * (it will be expanded anyway if there is a wildcard before replacing).
4036 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004037 has_wildcards = mch_has_wildcard(p);
4038 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004040#ifdef FEAT_EVAL
4041 /* Skip over `=expr`, wildcards in it are not expanded. */
4042 if (p[0] == '`' && p[1] == '=')
4043 {
4044 p += 2;
4045 (void)skip_expr(&p);
4046 if (*p == '`')
4047 ++p;
4048 continue;
4049 }
4050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 /*
4052 * Quick check if this cannot be the start of a special string.
4053 * Also removes backslash before '%', '#' and '<'.
4054 */
4055 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4056 {
4057 ++p;
4058 continue;
4059 }
4060
4061 /*
4062 * Try to find a match at this position.
4063 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004064 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4065 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (*errormsgp != NULL) /* error detected */
4067 return FAIL;
4068 if (repl == NULL) /* no match found */
4069 {
4070 p += srclen;
4071 continue;
4072 }
4073
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004074 /* Wildcards won't be expanded below, the replacement is taken
4075 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4076 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4077 {
4078 char_u *l = repl;
4079
4080 repl = expand_env_save(repl);
4081 vim_free(l);
4082 }
4083
Bram Moolenaar63b92542007-03-27 14:57:09 +00004084 /* Need to escape white space et al. with a backslash.
4085 * Don't do this for:
4086 * - replacement that already has been escaped: "##"
4087 * - shell commands (may have to use quotes instead).
4088 * - non-unix systems when there is a single argument (spaces don't
4089 * separate arguments then).
4090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004092 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 && eap->cmdidx != CMD_grep
4095 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004096 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004097 && eap->cmdidx != CMD_lgrep
4098 && eap->cmdidx != CMD_lgrepadd
4099 && eap->cmdidx != CMD_lmake
4100 && eap->cmdidx != CMD_make
4101 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004103 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#endif
4105 )
4106 {
4107 char_u *l;
4108#ifdef BACKSLASH_IN_FILENAME
4109 /* Don't escape a backslash here, because rem_backslash() doesn't
4110 * remove it later. */
4111 static char_u *nobslash = (char_u *)" \t\"|";
4112# define ESCAPE_CHARS nobslash
4113#else
4114# define ESCAPE_CHARS escape_chars
4115#endif
4116
4117 for (l = repl; *l; ++l)
4118 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4119 {
4120 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4121 if (l != NULL)
4122 {
4123 vim_free(repl);
4124 repl = l;
4125 }
4126 break;
4127 }
4128 }
4129
4130 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004131 if ((eap->usefilter || eap->cmdidx == CMD_bang
4132 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004133 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
4135 char_u *l;
4136
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004137 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if (l != NULL)
4139 {
4140 vim_free(repl);
4141 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 }
4144
4145 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4146 vim_free(repl);
4147 if (p == NULL)
4148 return FAIL;
4149 }
4150
4151 /*
4152 * One file argument: Expand wildcards.
4153 * Don't do this with ":r !command" or ":w !command".
4154 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004155 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
4157 /*
4158 * May do this twice:
4159 * 1. Replace environment variables.
4160 * 2. Replace any other wildcards, remove backslashes.
4161 */
4162 for (n = 1; n <= 2; ++n)
4163 {
4164 if (n == 2)
4165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 /*
4167 * Halve the number of backslashes (this is Vi compatible).
4168 * For Unix and OS/2, when wildcards are expanded, this is
4169 * done by ExpandOne() below.
4170 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004171#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 if (!has_wildcards)
4173#endif
4174 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 }
4176
4177 if (has_wildcards)
4178 {
4179 if (n == 1)
4180 {
4181 /*
4182 * First loop: May expand environment variables. This
4183 * can be done much faster with expand_env() than with
4184 * something else (e.g., calling a shell).
4185 * After expanding environment variables, check again
4186 * if there are still wildcards present.
4187 */
4188 if (vim_strchr(eap->arg, '$') != NULL
4189 || vim_strchr(eap->arg, '~') != NULL)
4190 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004191 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004192 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 has_wildcards = mch_has_wildcard(NameBuff);
4194 p = NameBuff;
4195 }
4196 else
4197 p = NULL;
4198 }
4199 else /* n == 2 */
4200 {
4201 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004202 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204 ExpandInit(&xpc);
4205 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004206 if (p_wic)
4207 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004209 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 if (p == NULL)
4211 return FAIL;
4212 }
4213 if (p != NULL)
4214 {
4215 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4216 p, cmdlinep);
4217 if (n == 2) /* p came from ExpandOne() */
4218 vim_free(p);
4219 }
4220 }
4221 }
4222 }
4223 return OK;
4224}
4225
4226/*
4227 * Replace part of the command line, keeping eap->cmd, eap->arg and
4228 * eap->nextcmd correct.
4229 * "src" points to the part that is to be replaced, of length "srclen".
4230 * "repl" is the replacement string.
4231 * Returns a pointer to the character after the replaced string.
4232 * Returns NULL for failure.
4233 */
4234 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004235repl_cmdline(
4236 exarg_T *eap,
4237 char_u *src,
4238 int srclen,
4239 char_u *repl,
4240 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241{
4242 int len;
4243 int i;
4244 char_u *new_cmdline;
4245
4246 /*
4247 * The new command line is build in new_cmdline[].
4248 * First allocate it.
4249 * Careful: a "+cmd" argument may have been NUL terminated.
4250 */
4251 len = (int)STRLEN(repl);
4252 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004253 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02004255 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 return NULL; /* out of memory! */
4257
4258 /*
4259 * Copy the stuff before the expanded part.
4260 * Copy the expanded stuff.
4261 * Copy what came after the expanded part.
4262 * Copy the next commands, if there are any.
4263 */
4264 i = (int)(src - *cmdlinep); /* length of part before match */
4265 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 mch_memmove(new_cmdline + i, repl, (size_t)len);
4268 i += len; /* remember the end of the string */
4269 STRCPY(new_cmdline + i, src + srclen);
4270 src = new_cmdline + i; /* remember where to continue */
4271
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004272 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
4274 i = (int)STRLEN(new_cmdline) + 1;
4275 STRCPY(new_cmdline + i, eap->nextcmd);
4276 eap->nextcmd = new_cmdline + i;
4277 }
4278 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4279 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4280 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4281 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4282 vim_free(*cmdlinep);
4283 *cmdlinep = new_cmdline;
4284
4285 return src;
4286}
4287
4288/*
4289 * Check for '|' to separate commands and '"' to start comments.
4290 */
4291 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004292separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293{
4294 char_u *p;
4295
Bram Moolenaar86b68352004-12-27 21:59:20 +00004296#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004297 p = skip_grep_pat(eap);
4298#else
4299 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004300#endif
4301
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004302 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
4304 if (*p == Ctrl_V)
4305 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004306 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 ++p; /* skip CTRL-V and next char */
4308 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004309 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004310 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (*p == NUL) /* stop at NUL after CTRL-V */
4312 break;
4313 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004314
4315#ifdef FEAT_EVAL
4316 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004317 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004318 {
4319 p += 2;
4320 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004321 }
4322#endif
4323
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 /* Check for '"': start of comment or '|': next command */
4325 /* :@" and :*" do not start a comment!
4326 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004327 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4329 || p != eap->arg)
4330 && (eap->cmdidx != CMD_redir
4331 || p != eap->arg + 1 || p[-1] != '@'))
4332 || *p == '|' || *p == '\n')
4333 {
4334 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004335 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 * AND 'b' is present in 'cpoptions'.
4337 */
4338 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004339 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004341 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 --p;
4343 }
4344 else
4345 {
4346 eap->nextcmd = check_nextcmd(p);
4347 *p = NUL;
4348 break;
4349 }
4350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004352
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004353 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 del_trailing_spaces(eap->arg);
4355}
4356
4357/*
4358 * get + command from ex argument
4359 */
4360 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004361getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362{
4363 char_u *arg = *argp;
4364 char_u *command = NULL;
4365
4366 if (*arg == '+') /* +[command] */
4367 {
4368 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004369 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 command = dollar_command;
4371 else
4372 {
4373 command = arg;
4374 arg = skip_cmd_arg(command, TRUE);
4375 if (*arg != NUL)
4376 *arg++ = NUL; /* terminate command with NUL */
4377 }
4378
4379 arg = skipwhite(arg); /* skip over spaces */
4380 *argp = arg;
4381 }
4382 return command;
4383}
4384
4385/*
4386 * Find end of "+command" argument. Skip over "\ " and "\\".
4387 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004388 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004389skip_cmd_arg(
4390 char_u *p,
4391 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392{
4393 while (*p && !vim_isspace(*p))
4394 {
4395 if (*p == '\\' && p[1] != NUL)
4396 {
4397 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004398 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 else
4400 ++p;
4401 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004402 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 return p;
4405}
4406
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004407 int
4408get_bad_opt(char_u *p, exarg_T *eap)
4409{
4410 if (STRICMP(p, "keep") == 0)
4411 eap->bad_char = BAD_KEEP;
4412 else if (STRICMP(p, "drop") == 0)
4413 eap->bad_char = BAD_DROP;
4414 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4415 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004416 else
4417 return FAIL;
4418 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004419}
4420
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421/*
4422 * Get "++opt=arg" argument.
4423 * Return FAIL or OK.
4424 */
4425 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004426getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427{
4428 char_u *arg = eap->arg + 2;
4429 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004430 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
4433 /* ":edit ++[no]bin[ary] file" */
4434 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4435 {
4436 if (*arg == 'n')
4437 {
4438 arg += 2;
4439 eap->force_bin = FORCE_NOBIN;
4440 }
4441 else
4442 eap->force_bin = FORCE_BIN;
4443 if (!checkforcmd(&arg, "binary", 3))
4444 return FAIL;
4445 eap->arg = skipwhite(arg);
4446 return OK;
4447 }
4448
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004449 /* ":read ++edit file" */
4450 if (STRNCMP(arg, "edit", 4) == 0)
4451 {
4452 eap->read_edit = TRUE;
4453 eap->arg = skipwhite(arg + 4);
4454 return OK;
4455 }
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 if (STRNCMP(arg, "ff", 2) == 0)
4458 {
4459 arg += 2;
4460 pp = &eap->force_ff;
4461 }
4462 else if (STRNCMP(arg, "fileformat", 10) == 0)
4463 {
4464 arg += 10;
4465 pp = &eap->force_ff;
4466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 else if (STRNCMP(arg, "enc", 3) == 0)
4468 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004469 if (STRNCMP(arg, "encoding", 8) == 0)
4470 arg += 8;
4471 else
4472 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 pp = &eap->force_enc;
4474 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004475 else if (STRNCMP(arg, "bad", 3) == 0)
4476 {
4477 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004478 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 if (pp == NULL || *arg != '=')
4482 return FAIL;
4483
4484 ++arg;
4485 *pp = (int)(arg - eap->cmd);
4486 arg = skip_cmd_arg(arg, FALSE);
4487 eap->arg = skipwhite(arg);
4488 *arg = NUL;
4489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if (pp == &eap->force_ff)
4491 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4493 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004494 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004496 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
4498 /* Make 'fileencoding' lower case. */
4499 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4500 *p = TOLOWER_ASC(*p);
4501 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004502 else
4503 {
4504 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4505 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004506 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004507 return FAIL;
4508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 return OK;
4511}
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004514ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515{
4516 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004517 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 * directory for security reasons.
4519 */
4520 if (secure)
4521 {
4522 secure = 2;
4523 eap->errmsg = e_curdir;
4524 }
4525 else if (eap->cmdidx == CMD_autocmd)
4526 do_autocmd(eap->arg, eap->forceit);
4527 else
4528 do_augroup(eap->arg, eap->forceit);
4529}
4530
4531/*
4532 * ":doautocmd": Apply the automatic commands to the current buffer.
4533 */
4534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004535ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004537 char_u *arg = eap->arg;
4538 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004539 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004540
Bram Moolenaar1610d052016-06-09 22:53:01 +02004541 (void)do_doautocmd(arg, TRUE, &did_aucmd);
4542 /* Only when there is no <nomodeline>. */
4543 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004544 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547/*
4548 * :[N]bunload[!] [N] [bufname] unload buffer
4549 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4550 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4551 */
4552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004553ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004555 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004556 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 eap->errmsg = do_bufdel(
4558 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4559 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4560 : DOBUF_UNLOAD, eap->arg,
4561 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4562}
4563
4564/*
4565 * :[N]buffer [N] to buffer N
4566 * :[N]sbuffer [N] to buffer N
4567 */
4568 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004569ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004571 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004572 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (*eap->arg)
4574 eap->errmsg = e_trailing;
4575 else
4576 {
4577 if (eap->addr_count == 0) /* default is current buffer */
4578 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4579 else
4580 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004581 if (eap->do_ecmd_cmd != NULL)
4582 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
4584}
4585
4586/*
4587 * :[N]bmodified [N] to next mod. buffer
4588 * :[N]sbmodified [N] to next mod. buffer
4589 */
4590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004591ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
4593 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004594 if (eap->do_ecmd_cmd != NULL)
4595 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596}
4597
4598/*
4599 * :[N]bnext [N] to next buffer
4600 * :[N]sbnext [N] split and to next buffer
4601 */
4602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004606 if (eap->do_ecmd_cmd != NULL)
4607 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608}
4609
4610/*
4611 * :[N]bNext [N] to previous buffer
4612 * :[N]bprevious [N] to previous buffer
4613 * :[N]sbNext [N] split and to previous buffer
4614 * :[N]sbprevious [N] split and to previous buffer
4615 */
4616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004617ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618{
4619 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004620 if (eap->do_ecmd_cmd != NULL)
4621 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622}
4623
4624/*
4625 * :brewind to first buffer
4626 * :bfirst to first buffer
4627 * :sbrewind split and to first buffer
4628 * :sbfirst split and to first buffer
4629 */
4630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004634 if (eap->do_ecmd_cmd != NULL)
4635 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636}
4637
4638/*
4639 * :blast to last buffer
4640 * :sblast split and to last buffer
4641 */
4642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004646 if (eap->do_ecmd_cmd != NULL)
4647 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
4650 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004651ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
4653 return (c == NUL || c == '|' || c == '"' || c == '\n');
4654}
4655
4656#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4657 || defined(PROTO)
4658/*
4659 * Return the next command, after the first '|' or '\n'.
4660 * Return NULL if not found.
4661 */
4662 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004663find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 while (*p != '|' && *p != '\n')
4666 {
4667 if (*p == NUL)
4668 return NULL;
4669 ++p;
4670 }
4671 return (p + 1);
4672}
4673#endif
4674
4675/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004676 * Check if *p is a separator between Ex commands, skipping over white space.
4677 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 */
4679 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004680check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004682 char_u *s = skipwhite(p);
4683
4684 if (*s == '|' || *s == '\n')
4685 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 else
4687 return NULL;
4688}
4689
4690/*
4691 * - if there are more files to edit
4692 * - and this is the last window
4693 * - and forceit not used
4694 * - and not repeated twice on a row
4695 * return FAIL and give error message if 'message' TRUE
4696 * return OK otherwise
4697 */
4698 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004699check_more(
4700 int message, /* when FALSE check only, no messages */
4701 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
4703 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4704
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004705 if (!forceit && only_one_window()
4706 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
4708 if (message)
4709 {
4710#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4711 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4712 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004713 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004715 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4716 NGETTEXT("%d more file to edit. Quit anyway?",
4717 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4719 return OK;
4720 return FAIL;
4721 }
4722#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004723 semsg(NGETTEXT("E173: %d more file to edit",
4724 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 quitmore = 2; /* next try to quit is allowed */
4726 }
4727 return FAIL;
4728 }
4729 return OK;
4730}
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732/*
4733 * Function given to ExpandGeneric() to obtain the list of command names.
4734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004736get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
4738 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 return cmdnames[idx].cmd_name;
4741}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004744ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
Bram Moolenaare6850792010-05-14 15:28:44 +02004746 if (*eap->arg == NUL)
4747 {
4748#ifdef FEAT_EVAL
4749 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4750 char_u *p = NULL;
4751
4752 if (expr != NULL)
4753 {
4754 ++emsg_off;
4755 p = eval_to_string(expr, NULL, FALSE);
4756 --emsg_off;
4757 vim_free(expr);
4758 }
4759 if (p != NULL)
4760 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004761 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004762 vim_free(p);
4763 }
4764 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004765 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004766#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004767 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004768#endif
4769 }
4770 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004771 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004772
4773#ifdef FEAT_VTP
4774 else if (has_vtp_working())
4775 {
4776 // background color change requires clear + redraw
4777 update_screen(CLEAR);
4778 redrawcmd();
4779 }
4780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781}
4782
4783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004784ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
4786 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004787 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 do_highlight(eap->arg, eap->forceit, FALSE);
4789}
4790
4791
4792/*
4793 * Call this function if we thought we were going to exit, but we won't
4794 * (because of an error). May need to restore the terminal mode.
4795 */
4796 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004797not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
4799 exiting = FALSE;
4800 settmode(TMODE_RAW);
4801}
4802
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004803 static int
4804before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4805{
4806 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4807
4808 /* Bail out when autocommands closed the window.
4809 * Refuse to quit when the buffer in the last window is being closed (can
4810 * only happen in autocommands). */
4811 if (!win_valid(wp)
4812 || curbuf_locked()
4813 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4814 return TRUE;
4815
4816 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4817 {
4818 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
4819 /* Refuse to quit when locked or when the buffer in the last window is
4820 * being closed (can only happen in autocommands). */
4821 if (curbuf_locked()
4822 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4823 return TRUE;
4824 }
4825
4826 return FALSE;
4827}
4828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004830 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004831 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 */
4833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004834ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004836 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#ifdef FEAT_CMDWIN
4839 if (cmdwin_type != 0)
4840 {
4841 cmdwin_result = Ctrl_C;
4842 return;
4843 }
4844#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004845 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004846 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004847 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004848 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004849 return;
4850 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004851 if (eap->addr_count > 0)
4852 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004853 int wnr = eap->line2;
4854
4855 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4856 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004857 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004858 }
4859 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004860 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004861
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004862 /* Refuse to quit when locked. */
4863 if (curbuf_locked())
4864 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004865
4866 /* Trigger QuitPre and maybe ExitPre */
4867 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004868 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870#ifdef FEAT_NETBEANS_INTG
4871 netbeansForcedQuit = eap->forceit;
4872#endif
4873
4874 /*
4875 * If there are more files or windows we won't exit.
4876 */
4877 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4878 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004879 if ((!buf_hide(wp->w_buffer)
4880 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004881 | (eap->forceit ? CCGD_FORCEIT : 0)
4882 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004884 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
4886 not_exiting();
4887 }
4888 else
4889 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02004890 /* quit last window
4891 * Note: only_one_window() returns true, even so a help window is
4892 * still open. In that case only quit, if no address has been
4893 * specified. Example:
4894 * :h|wincmd w|1q - don't quit
4895 * :h|wincmd w|q - quit
4896 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004897 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02004899 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004900#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004904 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906}
4907
4908/*
4909 * ":cquit".
4910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004912ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
4914 getout(1); /* this does not always pass on the exit code to the Manx
4915 compiler. why? */
4916}
4917
4918/*
4919 * ":qall": try to quit all windows
4920 */
4921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004922ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924# ifdef FEAT_CMDWIN
4925 if (cmdwin_type != 0)
4926 {
4927 if (eap->forceit)
4928 cmdwin_result = K_XF1; /* ex_window() takes care of this */
4929 else
4930 cmdwin_result = K_XF2;
4931 return;
4932 }
4933# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004934
4935 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004936 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004937 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004938 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004939 return;
4940 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004941
4942 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004943 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004944
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01004946 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 getout(0);
4948 not_exiting();
4949}
4950
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951/*
4952 * ":close": close current window, unless it is the last one
4953 */
4954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004955ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004957 win_T *win;
4958 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004959#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004961 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004963#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004964 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004965 {
4966 if (eap->addr_count == 0)
4967 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004968 else
4969 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004970 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004971 {
4972 winnr++;
4973 if (winnr == eap->line2)
4974 break;
4975 }
4976 if (win == NULL)
4977 win = lastwin;
4978 ex_win_close(eap->forceit, win, NULL);
4979 }
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981}
4982
Bram Moolenaar4033c552017-09-16 20:54:51 +02004983#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004984/*
4985 * ":pclose": Close any preview window.
4986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004988ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004989{
4990 win_T *win;
4991
Bram Moolenaar79648732019-07-18 21:43:07 +02004992 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02004993 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004994 if (win->w_p_pvw)
4995 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004996 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02004997 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004998 }
Bram Moolenaar79648732019-07-18 21:43:07 +02004999# ifdef FEAT_TEXT_PROP
5000 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005001 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005002# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005003}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005004#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005005
Bram Moolenaarf740b292006-02-16 22:11:02 +00005006/*
5007 * Close window "win" and take care of handling closing the last window for a
5008 * modified buffer.
5009 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005011ex_win_close(
5012 int forceit,
5013 win_T *win,
5014 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015{
5016 int need_hide;
5017 buf_T *buf = win->w_buffer;
5018
5019 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005020 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005022#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if ((p_confirm || cmdmod.confirm) && p_write)
5024 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005025 bufref_T bufref;
5026
5027 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005029 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 return;
5031 need_hide = FALSE;
5032 }
5033 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005036 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 return;
5038 }
5039 }
5040
Bram Moolenaar4033c552017-09-16 20:54:51 +02005041#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005043#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005044
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005046 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005047 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005048 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005049 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050}
5051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005053 * Handle the argument for a tabpage related ex command.
5054 * Returns a tabpage number.
5055 * When an error is encountered then eap->errmsg is set.
5056 */
5057 static int
5058get_tabpage_arg(exarg_T *eap)
5059{
5060 int tab_number;
5061 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5062
5063 if (eap->arg && *eap->arg != NUL)
5064 {
5065 char_u *p = eap->arg;
5066 char_u *p_save;
5067 int relative = 0; /* argument +N/-N means: go to N places to the
5068 * right/left relative to the current position. */
5069
5070 if (*p == '-')
5071 {
5072 relative = -1;
5073 p++;
5074 }
5075 else if (*p == '+')
5076 {
5077 relative = 1;
5078 p++;
5079 }
5080
5081 p_save = p;
5082 tab_number = getdigits(&p);
5083
5084 if (relative == 0)
5085 {
5086 if (STRCMP(p, "$") == 0)
5087 tab_number = LAST_TAB_NR;
5088 else if (p == p_save || *p_save == '-' || *p != NUL
5089 || tab_number > LAST_TAB_NR)
5090 {
5091 /* No numbers as argument. */
5092 eap->errmsg = e_invarg;
5093 goto theend;
5094 }
5095 }
5096 else
5097 {
5098 if (*p_save == NUL)
5099 tab_number = 1;
5100 else if (p == p_save || *p_save == '-' || *p != NUL
5101 || tab_number == 0)
5102 {
5103 /* No numbers as argument. */
5104 eap->errmsg = e_invarg;
5105 goto theend;
5106 }
5107 tab_number = tab_number * relative + tabpage_index(curtab);
5108 if (!unaccept_arg0 && relative == -1)
5109 --tab_number;
5110 }
5111 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5112 eap->errmsg = e_invarg;
5113 }
5114 else if (eap->addr_count > 0)
5115 {
5116 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005117 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005118 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005119 tab_number = 0;
5120 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005121 else
5122 {
5123 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005124 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005125 {
5126 --tab_number;
5127 if (tab_number < unaccept_arg0)
5128 eap->errmsg = e_invarg;
5129 }
5130 }
5131 }
5132 else
5133 {
5134 switch (eap->cmdidx)
5135 {
5136 case CMD_tabnext:
5137 tab_number = tabpage_index(curtab) + 1;
5138 if (tab_number > LAST_TAB_NR)
5139 tab_number = 1;
5140 break;
5141 case CMD_tabmove:
5142 tab_number = LAST_TAB_NR;
5143 break;
5144 default:
5145 tab_number = tabpage_index(curtab);
5146 }
5147 }
5148
5149theend:
5150 return tab_number;
5151}
5152
5153/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005154 * ":tabclose": close current tab page, unless it is the last one.
5155 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
5157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005158ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005160 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005161 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005162
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005163# ifdef FEAT_CMDWIN
5164 if (cmdwin_type != 0)
5165 cmdwin_result = K_IGNORE;
5166 else
5167# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005168 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005169 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005172 tab_number = get_tabpage_arg(eap);
5173 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005174 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005175 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005176 if (tp == NULL)
5177 {
5178 beep_flush();
5179 return;
5180 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005181 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005182 {
5183 tabpage_close_other(tp, eap->forceit);
5184 return;
5185 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005186 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005187 tabpage_close(eap->forceit);
5188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005190}
5191
5192/*
5193 * ":tabonly": close all tab pages except the current one
5194 */
5195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005196ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005197{
5198 tabpage_T *tp;
5199 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005200 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005201
5202# ifdef FEAT_CMDWIN
5203 if (cmdwin_type != 0)
5204 cmdwin_result = K_IGNORE;
5205 else
5206# endif
5207 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005208 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005209 else
5210 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005211 tab_number = get_tabpage_arg(eap);
5212 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005213 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005214 goto_tabpage(tab_number);
5215 /* Repeat this up to a 1000 times, because autocommands may
5216 * mess up the lists. */
5217 for (done = 0; done < 1000; ++done)
5218 {
5219 FOR_ALL_TABPAGES(tp)
5220 if (tp->tp_topframe != topframe)
5221 {
5222 tabpage_close_other(tp, eap->forceit);
5223 /* if we failed to close it quit */
5224 if (valid_tabpage(tp))
5225 done = 1000;
5226 /* start over, "tp" is now invalid */
5227 break;
5228 }
5229 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005230 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005231 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005232 }
5233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005237 * Close the current tab page.
5238 */
5239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005240tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005241{
5242 /* First close all the windows but the current one. If that worked then
5243 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01005244 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005245 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005246 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005247 ex_win_close(forceit, curwin, NULL);
5248# ifdef FEAT_GUI
5249 need_mouse_correct = TRUE;
5250# endif
5251}
5252
5253/*
5254 * Close tab page "tp", which is not the current tab page.
5255 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005256 * Also takes care of the tab pages line disappearing when closing the
5257 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005258 */
5259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005261{
5262 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005263 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005264 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005265
5266 /* Limit to 1000 windows, autocommands may add a window while we close
5267 * one. OK, so I'm paranoid... */
5268 while (++done < 1000)
5269 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005270 wp = tp->tp_firstwin;
5271 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005272
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005273 /* Autocommands may delete the tab page under our fingers and we may
5274 * fail to close a window with a modified buffer. */
5275 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005276 break;
5277 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005278
Bram Moolenaar29323592016-07-24 22:04:11 +02005279 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005280
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005281 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005282 if (h != tabline_height())
5283 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005284}
5285
5286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 * ":only".
5288 */
5289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005290ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005292 win_T *wp;
5293 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294# ifdef FEAT_GUI
5295 need_mouse_correct = TRUE;
5296# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005297 if (eap->addr_count > 0)
5298 {
5299 wnr = eap->line2;
5300 for (wp = firstwin; --wnr > 0; )
5301 {
5302 if (wp->w_next == NULL)
5303 break;
5304 else
5305 wp = wp->w_next;
5306 }
5307 win_goto(wp);
5308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 close_others(TRUE, eap->forceit);
5310}
5311
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005313ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005315 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01005316 if (!eap->skip)
5317 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005318#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005319 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005320#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005321 if (eap->addr_count == 0)
5322 win_close(curwin, FALSE); /* don't free buffer */
5323 else
5324 {
5325 int winnr = 0;
5326 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005327
Bram Moolenaar2256c992016-11-15 21:17:07 +01005328 FOR_ALL_WINDOWS(win)
5329 {
5330 winnr++;
5331 if (winnr == eap->line2)
5332 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005333 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005334 if (win == NULL)
5335 win = lastwin;
5336 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 }
5339}
5340
5341/*
5342 * ":stop" and ":suspend": Suspend Vim.
5343 */
5344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 /*
5348 * Disallow suspending for "rvim".
5349 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005350 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
5352 if (!eap->forceit)
5353 autowrite_all();
5354 windgoto((int)Rows - 1, 0);
5355 out_char('\n');
5356 out_flush();
5357 stoptermcap();
5358 out_flush(); /* needed for SUN to restore xterm buffer */
5359#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02005360 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361#endif
5362 ui_suspend(); /* call machine specific function */
5363#ifdef FEAT_TITLE
5364 maketitle();
5365 resettitle(); /* force updating the title */
5366#endif
5367 starttermcap();
5368 scroll_start(); /* scroll screen before redrawing */
5369 redraw_later_clear();
5370 shell_resized(); /* may have resized window */
5371 }
5372}
5373
5374/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005375 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 */
5377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
5380#ifdef FEAT_CMDWIN
5381 if (cmdwin_type != 0)
5382 {
5383 cmdwin_result = Ctrl_C;
5384 return;
5385 }
5386#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005387 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005388 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005389 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005390 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005391 return;
5392 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005393
5394 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005395 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
5397 /*
5398 * if more files or windows we won't exit
5399 */
5400 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5401 exiting = TRUE;
5402 if ( ((eap->cmdidx == CMD_wq
5403 || curbufIsChanged())
5404 && do_write(eap) == FAIL)
5405 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005406 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
5408 not_exiting();
5409 }
5410 else
5411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005414 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415# ifdef FEAT_GUI
5416 need_mouse_correct = TRUE;
5417# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02005418 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005419 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 }
5421}
5422
5423/*
5424 * ":print", ":list", ":number".
5425 */
5426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005427ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005429 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005430 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005431 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005433 for ( ;!got_int; ui_breakcheck())
5434 {
5435 print_line(eap->line1,
5436 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5437 || (eap->flags & EXFLAG_NR)),
5438 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5439 if (++eap->line1 > eap->line2)
5440 break;
5441 out_flush(); /* show one line at a time */
5442 }
5443 setpcmark();
5444 /* put cursor at last line */
5445 curwin->w_cursor.lnum = eap->line2;
5446 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 }
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450}
5451
5452#ifdef FEAT_BYTEOFF
5453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 goto_byte(eap->line2);
5457}
5458#endif
5459
5460/*
5461 * ":shell".
5462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005464ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465{
5466 do_shell(NULL, 0);
5467}
5468
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005469#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005471static int drop_busy = FALSE;
5472static int drop_filec;
5473static char_u **drop_filev = NULL;
5474static int drop_split;
5475static void (*drop_callback)(void *);
5476static void *drop_cookie;
5477
5478 static void
5479handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480{
5481 exarg_T ea;
5482 int save_msg_scroll = msg_scroll;
5483
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005484 // Setting the argument list may cause screen updates and being called
5485 // recursively. Avoid that by setting drop_busy.
5486 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
5488 /* Check whether the current buffer is changed. If so, we will need
5489 * to split the current window or data could be lost.
5490 * We don't need to check if the 'hidden' option is set, as in this
5491 * case the buffer won't be lost.
5492 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005493 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 {
5495 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005496 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 --emsg_off;
5498 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005499 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 if (win_split(0, 0) == FAIL)
5502 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005503 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504
5505 /* When splitting the window, create a new alist. Otherwise the
5506 * existing one is overwritten. */
5507 alist_unlink(curwin->w_alist);
5508 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
5510
5511 /*
5512 * Set up the new argument list.
5513 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005514 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
5516 /*
5517 * Move to the first file.
5518 */
5519 /* Fake up a minimal "next" command for do_argfile() */
5520 vim_memset(&ea, 0, sizeof(ea));
5521 ea.cmd = (char_u *)"next";
5522 do_argfile(&ea, 0);
5523
5524 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
5525 * mode that is not needed here. */
5526 need_start_insertmode = FALSE;
5527
5528 /* Restore msg_scroll, otherwise a following command may cause scrolling
5529 * unexpectedly. The screen will be redrawn by the caller, thus
5530 * msg_scroll being set by displaying a message is irrelevant. */
5531 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005532
5533 if (drop_callback != NULL)
5534 drop_callback(drop_cookie);
5535
5536 drop_filev = NULL;
5537 drop_busy = FALSE;
5538}
5539
5540/*
5541 * Handle a file drop. The code is here because a drop is *nearly* like an
5542 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005543 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005544 * code is very similar to :args and hence needs access to a lot of the static
5545 * functions in this file.
5546 *
5547 * The "filev" list must have been allocated using alloc(), as should each item
5548 * in the list. This function takes over responsibility for freeing the "filev"
5549 * list.
5550 */
5551 void
5552handle_drop(
5553 int filec, // the number of files dropped
5554 char_u **filev, // the list of files dropped
5555 int split, // force splitting the window
5556 void (*callback)(void *), // to be called after setting the argument
5557 // list
5558 void *cookie) // argument for "callback" (allocated)
5559{
5560 // Cannot handle recursive drops, finish the pending one.
5561 if (drop_busy)
5562 {
5563 FreeWild(filec, filev);
5564 vim_free(cookie);
5565 return;
5566 }
5567
5568 // When calling handle_drop() more than once in a row we only use the last
5569 // one.
5570 if (drop_filev != NULL)
5571 {
5572 FreeWild(drop_filec, drop_filev);
5573 vim_free(drop_cookie);
5574 }
5575
5576 drop_filec = filec;
5577 drop_filev = filev;
5578 drop_split = split;
5579 drop_callback = callback;
5580 drop_cookie = cookie;
5581
5582 // Postpone this when:
5583 // - editing the command line
5584 // - not possible to change the current buffer
5585 // - updating the screen
5586 // As it may change buffers and window structures that are in use and cause
5587 // freed memory to be used.
5588 if (text_locked() || curbuf_locked() || updating_screen)
5589 return;
5590
5591 handle_drop_internal();
5592}
5593
5594/*
5595 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5596 * reset: Handle a postponed drop.
5597 */
5598 void
5599handle_any_postponed_drop(void)
5600{
5601 if (!drop_busy && drop_filev != NULL
5602 && !text_locked() && !curbuf_locked() && !updating_screen)
5603 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605#endif
5606
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 * ":preserve".
5609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005613 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 ml_preserve(curbuf, TRUE);
5615}
5616
5617/*
5618 * ":recover".
5619 */
5620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005621ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622{
5623 /* Set recoverymode right away to avoid the ATTENTION prompt. */
5624 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005625 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5626 | CCGD_MULTWIN
5627 | (eap->forceit ? CCGD_FORCEIT : 0)
5628 | CCGD_EXCMD)
5629
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 && (*eap->arg == NUL
5631 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005632 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 recoverymode = FALSE;
5634}
5635
5636/*
5637 * Command modifier used in a wrong way.
5638 */
5639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005640ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 eap->errmsg = e_invcmd;
5643}
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645/*
5646 * :sview [+command] file split window with new file, read-only
5647 * :split [[+command] file] split window with current or new file
5648 * :vsplit [[+command] file] split window vertically with current or new file
5649 * :new [[+command] file] split window with no or new file
5650 * :vnew [[+command] file] split vertically window with no or new file
5651 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005652 *
5653 * :tabedit open new Tab page with empty window
5654 * :tabedit [+command] file open new Tab page and edit "file"
5655 * :tabnew [[+command] file] just like :tabedit
5656 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 */
5658 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005661 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005662#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005664#endif
5665#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005667#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005668 int use_tab = eap->cmdidx == CMD_tabedit
5669 || eap->cmdidx == CMD_tabfind
5670 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005672 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005673 return;
5674
Bram Moolenaar4033c552017-09-16 20:54:51 +02005675#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678
Bram Moolenaar4033c552017-09-16 20:54:51 +02005679#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005681 * quickfix windows. But it's OK when doing ":tab split". */
5682 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {
5684 if (eap->cmdidx == CMD_split)
5685 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (eap->cmdidx == CMD_vsplit)
5687 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
Bram Moolenaar4033c552017-09-16 20:54:51 +02005691#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005692 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
5694 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5695 FNAME_MESS, TRUE, curbuf->b_ffname);
5696 if (fname == NULL)
5697 goto theend;
5698 eap->arg = fname;
5699 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005700# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005701 else
5702# endif
5703#endif
5704#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 && eap->cmdidx != CMD_new)
5708 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005709 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005710# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005711 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005712# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005713 au_has_group((char_u *)"FileExplorer"))
5714 {
5715 /* No browsing supported but we do have the file explorer:
5716 * Edit the directory. */
5717 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5718 eap->arg = (char_u *)".";
5719 }
5720 else
5721 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005722 fname = do_browse(0, (char_u *)(use_tab
5723 ? _("Edit File in new tab page")
5724 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005726 if (fname == NULL)
5727 goto theend;
5728 eap->arg = fname;
5729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005734 /*
5735 * Either open new tab page or split the window.
5736 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005737 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005738 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005739 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5740 : eap->addr_count == 0 ? 0
5741 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005742 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005743 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005744
5745 /* set the alternate buffer for the window we came from */
5746 if (curwin != old_curwin
5747 && win_valid(old_curwin)
5748 && old_curwin->w_buffer != curbuf
5749 && !cmdmod.keepalt)
5750 old_curwin->w_alt_fnum = curbuf->b_fnum;
5751 }
5752 }
5753 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 /* Reset 'scrollbind' when editing another file, but keep it when
5757 * doing ":split" without arguments. */
5758 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005759# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005763 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 else
5765 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 do_exedit(eap, old_curwin);
5767 }
5768
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005769# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005773# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774theend:
5775 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005778
5779/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005780 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005781 */
5782 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005783tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005784{
5785 exarg_T ea;
5786
5787 vim_memset(&ea, 0, sizeof(ea));
5788 ea.cmdidx = CMD_tabnew;
5789 ea.cmd = (char_u *)"tabn";
5790 ea.arg = (char_u *)"";
5791 ex_splitview(&ea);
5792}
5793
5794/*
5795 * :tabnext command
5796 */
5797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005798ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005799{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005800 int tab_number;
5801
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005802 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005803 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005804 switch (eap->cmdidx)
5805 {
5806 case CMD_tabfirst:
5807 case CMD_tabrewind:
5808 goto_tabpage(1);
5809 break;
5810 case CMD_tablast:
5811 goto_tabpage(9999);
5812 break;
5813 case CMD_tabprevious:
5814 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005815 if (eap->arg && *eap->arg != NUL)
5816 {
5817 char_u *p = eap->arg;
5818 char_u *p_save = p;
5819
5820 tab_number = getdigits(&p);
5821 if (p == p_save || *p_save == '-' || *p != NUL
5822 || tab_number == 0)
5823 {
5824 /* No numbers as argument. */
5825 eap->errmsg = e_invarg;
5826 return;
5827 }
5828 }
5829 else
5830 {
5831 if (eap->addr_count == 0)
5832 tab_number = 1;
5833 else
5834 {
5835 tab_number = eap->line2;
5836 if (tab_number < 1)
5837 {
5838 eap->errmsg = e_invrange;
5839 return;
5840 }
5841 }
5842 }
5843 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005844 break;
5845 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005846 tab_number = get_tabpage_arg(eap);
5847 if (eap->errmsg == NULL)
5848 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005849 break;
5850 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005851}
5852
5853/*
5854 * :tabmove command
5855 */
5856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005857ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005858{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005859 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005860
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005861 tab_number = get_tabpage_arg(eap);
5862 if (eap->errmsg == NULL)
5863 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005864}
5865
5866/*
5867 * :tabs command: List tabs and their contents.
5868 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005870ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005871{
5872 tabpage_T *tp;
5873 win_T *wp;
5874 int tabcount = 1;
5875
5876 msg_start();
5877 msg_scroll = TRUE;
5878 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5879 {
5880 msg_putchar('\n');
5881 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005882 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005883 out_flush(); /* output one line at a time */
5884 ui_breakcheck();
5885
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005886 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005887 wp = firstwin;
5888 else
5889 wp = tp->tp_firstwin;
5890 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5891 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005892 msg_putchar('\n');
5893 msg_putchar(wp == curwin ? '>' : ' ');
5894 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005895 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5896 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005897 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005898 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005899 else
5900 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
5901 IObuff, IOSIZE, TRUE);
5902 msg_outtrans(IObuff);
5903 out_flush(); /* output one line at a time */
5904 ui_breakcheck();
5905 }
5906 }
5907}
5908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909/*
5910 * ":mode": Set screen mode.
5911 * If no argument given, just get the screen size and redraw.
5912 */
5913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005914ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
5916 if (*eap->arg == NUL)
5917 shell_resized();
5918 else
5919 mch_screenmode(eap->arg);
5920}
5921
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922/*
5923 * ":resize".
5924 * set, increment or decrement current window height
5925 */
5926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005927ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928{
5929 int n;
5930 win_T *wp = curwin;
5931
5932 if (eap->addr_count > 0)
5933 {
5934 n = eap->line2;
5935 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
5936 ;
5937 }
5938
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005939# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 if (cmdmod.split & WSP_VERT)
5944 {
5945 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02005946 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
5948 n = 9999;
5949 win_setwidth_win((int)n, wp);
5950 }
5951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 {
5953 if (*eap->arg == '-' || *eap->arg == '+')
5954 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02005955 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 n = 9999;
5957 win_setheight_win((int)n, wp);
5958 }
5959}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
5961/*
5962 * ":find [+command] <file>" command.
5963 */
5964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967#ifdef FEAT_SEARCHPATH
5968 char_u *fname;
5969 int count;
5970
5971 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
5972 TRUE, curbuf->b_ffname);
5973 if (eap->addr_count > 0)
5974 {
5975 /* Repeat finding the file "count" times. This matters when it
5976 * appears several times in the path. */
5977 count = eap->line2;
5978 while (fname != NULL && --count > 0)
5979 {
5980 vim_free(fname);
5981 fname = find_file_in_path(NULL, 0, FNAME_MESS,
5982 FALSE, curbuf->b_ffname);
5983 }
5984 }
5985
5986 if (fname != NULL)
5987 {
5988 eap->arg = fname;
5989#endif
5990 do_exedit(eap, NULL);
5991#ifdef FEAT_SEARCHPATH
5992 vim_free(fname);
5993 }
5994#endif
5995}
5996
5997/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00005998 * ":open" simulation: for now just work like ":visual".
5999 */
6000 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006001ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006002{
6003 regmatch_T regmatch;
6004 char_u *p;
6005
6006 curwin->w_cursor.lnum = eap->line2;
6007 beginline(BL_SOL | BL_FIX);
6008 if (*eap->arg == '/')
6009 {
6010 /* ":open /pattern/": put cursor in column found with pattern */
6011 ++eap->arg;
6012 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6013 *p = NUL;
6014 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6015 if (regmatch.regprog != NULL)
6016 {
6017 regmatch.rm_ic = p_ic;
6018 p = ml_get_curline();
6019 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006020 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006021 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006022 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006023 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006024 }
6025 /* Move to the NUL, ignore any other arguments. */
6026 eap->arg += STRLEN(eap->arg);
6027 }
6028 check_cursor();
6029
6030 eap->cmdidx = CMD_visual;
6031 do_exedit(eap, NULL);
6032}
6033
6034/*
6035 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 */
6037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006038ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 do_exedit(eap, NULL);
6041}
6042
6043/*
6044 * ":edit <file>" command and alikes.
6045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006047do_exedit(
6048 exarg_T *eap,
6049 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006053 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006055 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006056 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 /*
6058 * ":vi" command ends Ex mode.
6059 */
6060 if (exmode_active && (eap->cmdidx == CMD_visual
6061 || eap->cmdidx == CMD_view))
6062 {
6063 exmode_active = FALSE;
6064 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006065 {
6066 /* Special case: ":global/pat/visual\NLvi-commands" */
6067 if (global_busy)
6068 {
6069 int rd = RedrawingDisabled;
6070 int nwr = no_wait_return;
6071 int ms = msg_scroll;
6072#ifdef FEAT_GUI
6073 int he = hold_gui_events;
6074#endif
6075
6076 if (eap->nextcmd != NULL)
6077 {
6078 stuffReadbuff(eap->nextcmd);
6079 eap->nextcmd = NULL;
6080 }
6081
6082 if (exmode_was != EXMODE_VIM)
6083 settmode(TMODE_RAW);
6084 RedrawingDisabled = 0;
6085 no_wait_return = 0;
6086 need_wait_return = FALSE;
6087 msg_scroll = 0;
6088#ifdef FEAT_GUI
6089 hold_gui_events = 0;
6090#endif
6091 must_redraw = CLEAR;
6092
6093 main_loop(FALSE, TRUE);
6094
6095 RedrawingDisabled = rd;
6096 no_wait_return = nwr;
6097 msg_scroll = ms;
6098#ifdef FEAT_GUI
6099 hold_gui_events = he;
6100#endif
6101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 }
6105
6106 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006107 || eap->cmdidx == CMD_tabnew
6108 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006109 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006111 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 setpcmark();
6113 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006114 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6115 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006117 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 || *eap->arg != NUL
6119#ifdef FEAT_BROWSE
6120 || cmdmod.browse
6121#endif
6122 )
6123 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006124 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
6125 * can bring us here, others are stopped earlier. */
6126 if (*eap->arg != NUL && curbuf_locked())
6127 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 n = readonlymode;
6130 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6131 readonlymode = TRUE;
6132 else if (eap->cmdidx == CMD_enew)
6133 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6134 empty buffer */
6135 setpcmark();
6136 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6137 NULL, eap,
6138 /* ":edit" goes to first line if Vi compatible */
6139 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6140 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6141 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006142 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01006144 /* after a split we can use an existing buffer */
6145 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006147 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
6149 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (old_curwin != NULL)
6151 {
6152 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006153 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006155#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006156 cleanup_T cs;
6157
6158 /* Reset the error/interrupt/exception state here so that
6159 * aborting() returns FALSE when closing a window. */
6160 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006161#endif
6162#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006164#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006165 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006166
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006167#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006168 /* Restore the error/interrupt/exception state if not
6169 * discarded by a new aborting error, interrupt, or
6170 * uncaught exception. */
6171 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 }
6174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 }
6176 else if (readonlymode && curbuf->b_nwindows == 1)
6177 {
6178 /* When editing an already visited buffer, 'readonly' won't be set
6179 * but the previous value is kept. With ":view" and ":sview" we
6180 * want the file to be readonly, except when another window is
6181 * editing the same buffer. */
6182 curbuf->b_p_ro = TRUE;
6183 }
6184 readonlymode = n;
6185 }
6186 else
6187 {
6188 if (eap->do_ecmd_cmd != NULL)
6189 do_cmdline_cmd(eap->do_ecmd_cmd);
6190#ifdef FEAT_TITLE
6191 n = curwin->w_arg_idx_invalid;
6192#endif
6193 check_arg_idx(curwin);
6194#ifdef FEAT_TITLE
6195 if (n != curwin->w_arg_idx_invalid)
6196 maketitle();
6197#endif
6198 }
6199
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 /*
6201 * if ":split file" worked, set alternate file name in old window to new
6202 * file
6203 */
6204 if (old_curwin != NULL
6205 && *eap->arg != NUL
6206 && curwin != old_curwin
6207 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006208 && old_curwin->w_buffer != curbuf
6209 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211
6212 ex_no_reprint = TRUE;
6213}
6214
6215#ifndef FEAT_GUI
6216/*
6217 * ":gui" and ":gvim" when there is no GUI.
6218 */
6219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006220ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
6222 eap->errmsg = e_nogvim;
6223}
6224#endif
6225
Bram Moolenaar4f974752019-02-17 17:44:42 +01006226#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006228ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 gui_make_tearoff(eap->arg);
6231}
6232#endif
6233
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006234#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6235 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006237ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006239# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6240 if (gui.in_use)
6241 gui_make_popup(eap->arg, eap->forceit);
6242# ifdef FEAT_TERM_POPUP_MENU
6243 else
6244# endif
6245# endif
6246# ifdef FEAT_TERM_POPUP_MENU
6247 pum_make_popup(eap->arg, eap->forceit);
6248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249}
6250#endif
6251
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006253ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006256 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006258 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259}
6260
6261/*
6262 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6263 * offset.
6264 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006267ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006270 win_T *save_curwin = curwin;
6271 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 long topline;
6273 long y;
6274 linenr_T old_linenr = curwin->w_cursor.lnum;
6275
6276 setpcmark();
6277
6278 /*
6279 * determine max topline
6280 */
6281 if (curwin->w_p_scb)
6282 {
6283 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006284 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 {
6286 if (wp->w_p_scb && wp->w_buffer)
6287 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006288 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (topline > y)
6290 topline = y;
6291 }
6292 }
6293 if (topline < 1)
6294 topline = 1;
6295 }
6296 else
6297 {
6298 topline = 1;
6299 }
6300
6301
6302 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006303 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006305 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
6307 if (curwin->w_p_scb)
6308 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006309 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 y = topline - curwin->w_topline;
6311 if (y > 0)
6312 scrollup(y, TRUE);
6313 else
6314 scrolldown(-y, TRUE);
6315 curwin->w_scbind_pos = topline;
6316 redraw_later(VALID);
6317 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
6320 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006321 curwin = save_curwin;
6322 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 if (curwin->w_p_scb)
6324 {
6325 did_syncbind = TRUE;
6326 checkpcmark();
6327 if (old_linenr != curwin->w_cursor.lnum)
6328 {
6329 char_u ctrl_o[2];
6330
6331 ctrl_o[0] = Ctrl_O;
6332 ctrl_o[1] = 0;
6333 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6334 }
6335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336}
6337
6338
6339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006340ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006342 int i;
6343 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6344 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
6346 if (eap->usefilter) /* :r!cmd */
6347 do_bang(1, eap, FALSE, FALSE, TRUE);
6348 else
6349 {
6350 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6351 return;
6352
6353#ifdef FEAT_BROWSE
6354 if (cmdmod.browse)
6355 {
6356 char_u *browseFile;
6357
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006358 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 NULL, NULL, NULL, curbuf);
6360 if (browseFile != NULL)
6361 {
6362 i = readfile(browseFile, NULL,
6363 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6364 vim_free(browseFile);
6365 }
6366 else
6367 i = OK;
6368 }
6369 else
6370#endif
6371 if (*eap->arg == NUL)
6372 {
6373 if (check_fname() == FAIL) /* check for no file name */
6374 return;
6375 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6376 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6377 }
6378 else
6379 {
6380 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6381 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6382 i = readfile(eap->arg, NULL,
6383 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6384
6385 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006386 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006388#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (!aborting())
6390#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006391 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 }
6393 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006394 {
6395 if (empty && exmode_active)
6396 {
6397 /* Delete the empty line that remains. Historically ex does
6398 * this but vi doesn't. */
6399 if (eap->line2 == 0)
6400 lnum = curbuf->b_ml.ml_line_count;
6401 else
6402 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006403 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006404 {
6405 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006406 if (curwin->w_cursor.lnum > 1
6407 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006408 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006409 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006410 }
6411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
6415}
6416
Bram Moolenaarea408852005-06-25 22:49:46 +00006417static char_u *prev_dir = NULL;
6418
6419#if defined(EXITFREE) || defined(PROTO)
6420 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006421free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006422{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006423 VIM_CLEAR(prev_dir);
6424 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006425}
6426#endif
6427
Bram Moolenaarf4258302013-06-02 18:20:17 +02006428/*
6429 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006430 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6431 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006432 */
6433 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006434post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006435{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006436 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006437 // Clear tab local directory for both :cd and :tcd
6438 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006439 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006440 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006441 {
6442 /* If still in global directory, need to remember current
6443 * directory as global directory. */
6444 if (globaldir == NULL && prev_dir != NULL)
6445 globaldir = vim_strsave(prev_dir);
6446 /* Remember this local directory for the window. */
6447 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006448 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006449 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006450 curtab->tp_localdir = vim_strsave(NameBuff);
6451 else
6452 curwin->w_localdir = vim_strsave(NameBuff);
6453 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006454 }
6455 else
6456 {
6457 /* We are now in the global directory, no need to remember its
6458 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01006459 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006460 }
6461
6462 shorten_fnames(TRUE);
6463}
6464
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006465/*
6466 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6467 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6468 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6469 * Otherwise changes the global directory.
6470 * Returns TRUE if the directory is successfully changed.
6471 */
6472 int
6473changedir_func(
6474 char_u *new_dir,
6475 int forceit,
6476 cdscope_T scope)
6477{
6478 char_u *tofree;
6479 int dir_differs;
6480 int retval = FALSE;
6481
6482 if (allbuf_locked())
6483 return FALSE;
6484
6485 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6486 {
6487 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6488 return FALSE;
6489 }
6490
6491 // ":cd -": Change to previous directory
6492 if (STRCMP(new_dir, "-") == 0)
6493 {
6494 if (prev_dir == NULL)
6495 {
6496 emsg(_("E186: No previous directory"));
6497 return FALSE;
6498 }
6499 new_dir = prev_dir;
6500 }
6501
6502 // Save current directory for next ":cd -"
6503 tofree = prev_dir;
6504 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6505 prev_dir = vim_strsave(NameBuff);
6506 else
6507 prev_dir = NULL;
6508
6509#if defined(UNIX) || defined(VMS)
6510 // for UNIX ":cd" means: go to home directory
6511 if (*new_dir == NUL)
6512 {
6513 // use NameBuff for home directory name
6514# ifdef VMS
6515 char_u *p;
6516
6517 p = mch_getenv((char_u *)"SYS$LOGIN");
6518 if (p == NULL || *p == NUL) // empty is the same as not set
6519 NameBuff[0] = NUL;
6520 else
6521 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6522# else
6523 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6524# endif
6525 new_dir = NameBuff;
6526 }
6527#endif
6528 dir_differs = new_dir == NULL || prev_dir == NULL
6529 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6530 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6531 emsg(_(e_failed));
6532 else
6533 {
6534 char_u *acmd_fname;
6535
6536 post_chdir(scope);
6537
6538 if (dir_differs)
6539 {
6540 if (scope == CDSCOPE_WINDOW)
6541 acmd_fname = (char_u *)"window";
6542 else if (scope == CDSCOPE_TABPAGE)
6543 acmd_fname = (char_u *)"tabpage";
6544 else
6545 acmd_fname = (char_u *)"global";
6546 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6547 curbuf);
6548 }
6549 retval = TRUE;
6550 }
6551 vim_free(tofree);
6552
6553 return retval;
6554}
Bram Moolenaarea408852005-06-25 22:49:46 +00006555
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006557 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006559 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006560ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006562 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563
6564 new_dir = eap->arg;
6565#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006566 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 if (*new_dir == NUL)
6568 ex_pwd(NULL);
6569 else
6570#endif
6571 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006572 cdscope_T scope = CDSCOPE_GLOBAL;
6573
6574 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6575 scope = CDSCOPE_WINDOW;
6576 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6577 scope = CDSCOPE_TABPAGE;
6578
6579 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006580 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006581 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006582 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 ex_pwd(eap);
6584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 }
6586}
6587
6588/*
6589 * ":pwd".
6590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006592ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593{
6594 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6595 {
6596#ifdef BACKSLASH_IN_FILENAME
6597 slash_adjust(NameBuff);
6598#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006599 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 }
6601 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006602 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603}
6604
6605/*
6606 * ":=".
6607 */
6608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006609ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006611 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006612 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613}
6614
6615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006616ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006618 int n;
6619 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620
6621 if (cursor_valid())
6622 {
6623 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6624 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006625 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006627
6628 len = eap->line2;
6629 switch (*eap->arg)
6630 {
6631 case 'm': break;
6632 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006633 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006634 }
6635 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636}
6637
6638/*
6639 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6640 */
6641 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006642do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
Bram Moolenaar52953192019-08-16 10:27:13 +02006644 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006645 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006646# ifdef ELAPSED_FUNC
6647 elapsed_T start_tv;
6648
6649 // Remember at what time we started, so that we know how much longer we
6650 // should wait after waiting for a bit.
6651 ELAPSED_INIT(start_tv);
6652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006655 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006656 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006658 wait_now = msec - done > 1000L ? 1000L : msec - done;
6659#ifdef FEAT_TIMERS
6660 {
6661 long due_time = check_due_timer();
6662
6663 if (due_time > 0 && due_time < wait_now)
6664 wait_now = due_time;
6665 }
6666#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006667#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006668 if (has_any_channel() && wait_now > 20L)
6669 wait_now = 20L;
6670#endif
6671#ifdef FEAT_SOUND
6672 if (has_any_sound_callback() && wait_now > 20L)
6673 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006674#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006675 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006676
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006677#ifdef FEAT_JOB_CHANNEL
6678 if (has_any_channel())
6679 ui_breakcheck_force(TRUE);
6680 else
6681#endif
6682 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006683#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006684 // Process the netbeans and clientserver messages that may have been
6685 // received in the call to ui_breakcheck() when the GUI is in use. This
6686 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006687 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006688#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006689
6690# ifdef ELAPSED_FUNC
6691 // actual time passed
6692 done = ELAPSED_FUNC(start_tv);
6693# else
6694 // guestimate time passed (will actually be more)
6695 done += wait_now;
6696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006698
6699 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6700 // input buffer, otherwise a following call to input() fails.
6701 if (got_int)
6702 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703}
6704
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705/*
6706 * ":winsize" command (obsolete).
6707 */
6708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006709ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 int w, h;
6712 char_u *arg = eap->arg;
6713 char_u *p;
6714
6715 w = getdigits(&arg);
6716 arg = skipwhite(arg);
6717 p = arg;
6718 h = getdigits(&arg);
6719 if (*p != NUL && *arg == NUL)
6720 set_shellsize(w, h, TRUE);
6721 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006722 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723}
6724
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006726ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727{
6728 int xchar = NUL;
6729 char_u *p;
6730
6731 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6732 {
6733 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
6734 if (eap->arg[1] == NUL)
6735 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006736 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 return;
6738 }
6739 xchar = eap->arg[1];
6740 p = eap->arg + 2;
6741 }
6742 else
6743 p = eap->arg + 1;
6744
6745 eap->nextcmd = check_nextcmd(p);
6746 p = skipwhite(p);
6747 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006748 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006749 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
6751 /* Pass flags on for ":vertical wincmd ]". */
6752 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006753 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6755 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006756 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 }
6758}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
Bram Moolenaar843ee412004-06-30 16:16:41 +00006760#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761/*
6762 * ":winpos".
6763 */
6764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006765ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766{
6767 int x, y;
6768 char_u *arg = eap->arg;
6769 char_u *p;
6770
6771 if (*arg == NUL)
6772 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006773# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006774# ifdef VIMDLL
6775 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6776 mch_get_winpos(&x, &y) != FAIL)
6777# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006779# else
6780 if (mch_get_winpos(&x, &y) != FAIL)
6781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
6783 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006784 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
6786 else
6787# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006788 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 }
6790 else
6791 {
6792 x = getdigits(&arg);
6793 arg = skipwhite(arg);
6794 p = arg;
6795 y = getdigits(&arg);
6796 if (*p == NUL || *arg != NUL)
6797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006798 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 return;
6800 }
6801# ifdef FEAT_GUI
6802 if (gui.in_use)
6803 gui_mch_set_winpos(x, y);
6804 else if (gui.starting)
6805 {
6806 /* Remember the coordinates for when the window is opened. */
6807 gui_win_x = x;
6808 gui_win_y = y;
6809 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006810# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 else
6812# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006813# endif
6814# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006815 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816# endif
6817# ifdef HAVE_TGETENT
6818 if (*T_CWP)
6819 term_set_winpos(x, y);
6820# endif
6821 }
6822}
6823#endif
6824
6825/*
6826 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6827 */
6828 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006829ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830{
6831 oparg_T oa;
6832
6833 clear_oparg(&oa);
6834 oa.regname = eap->regname;
6835 oa.start.lnum = eap->line1;
6836 oa.end.lnum = eap->line2;
6837 oa.line_count = eap->line2 - eap->line1 + 1;
6838 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
6841 {
6842 setpcmark();
6843 curwin->w_cursor.lnum = eap->line1;
6844 beginline(BL_SOL | BL_FIX);
6845 }
6846
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006847 if (VIsual_active)
6848 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006849
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 switch (eap->cmdidx)
6851 {
6852 case CMD_delete:
6853 oa.op_type = OP_DELETE;
6854 op_delete(&oa);
6855 break;
6856
6857 case CMD_yank:
6858 oa.op_type = OP_YANK;
6859 (void)op_yank(&oa, FALSE, TRUE);
6860 break;
6861
6862 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02006863 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006865 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6866#else
6867 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006869 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 oa.op_type = OP_RSHIFT;
6871 else
6872 oa.op_type = OP_LSHIFT;
6873 op_shift(&oa, FALSE, eap->amount);
6874 break;
6875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006877 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878}
6879
6880/*
6881 * ":put".
6882 */
6883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006884ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 /* ":0put" works like ":1put!". */
6887 if (eap->line2 == 0)
6888 {
6889 eap->line2 = 1;
6890 eap->forceit = TRUE;
6891 }
6892 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006893 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6894 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895}
6896
6897/*
6898 * Handle ":copy" and ":move".
6899 */
6900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006901ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902{
6903 long n;
6904
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02006905 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 if (eap->arg == NULL) /* error detected */
6907 {
6908 eap->nextcmd = NULL;
6909 return;
6910 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00006911 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913 /*
6914 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
6915 */
6916 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
6917 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02006918 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 return;
6920 }
6921
6922 if (eap->cmdidx == CMD_move)
6923 {
6924 if (do_move(eap->line1, eap->line2, n) == FAIL)
6925 return;
6926 }
6927 else
6928 ex_copy(eap->line1, eap->line2, n);
6929 u_clearline();
6930 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006931 ex_may_print(eap);
6932}
6933
6934/*
6935 * Print the current line if flags were given to the Ex command.
6936 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02006937 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006938ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006939{
6940 if (eap->flags != 0)
6941 {
6942 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
6943 (eap->flags & EXFLAG_LIST));
6944 ex_no_reprint = TRUE;
6945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946}
6947
6948/*
6949 * ":smagic" and ":snomagic".
6950 */
6951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006952ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
6954 int magic_save = p_magic;
6955
6956 p_magic = (eap->cmdidx == CMD_smagic);
6957 do_sub(eap);
6958 p_magic = magic_save;
6959}
6960
6961/*
6962 * ":join".
6963 */
6964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006965ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 curwin->w_cursor.lnum = eap->line1;
6968 if (eap->line1 == eap->line2)
6969 {
6970 if (eap->addr_count >= 2) /* :2,2join does nothing */
6971 return;
6972 if (eap->line2 == curbuf->b_ml.ml_line_count)
6973 {
6974 beep_flush();
6975 return;
6976 }
6977 ++eap->line2;
6978 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02006979 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006981 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982}
6983
6984/*
6985 * ":[addr]@r" or ":[addr]*r": execute register
6986 */
6987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006988ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989{
6990 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00006991 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
6993 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02006994 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996#ifdef USE_ON_FLY_SCROLL
6997 dont_scroll = TRUE; /* disallow scrolling here */
6998#endif
6999
7000 /* get the register name. No name means to use the previous one */
7001 c = *eap->arg;
7002 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7003 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007004 /* Put the register in the typeahead buffer with the "silent" flag. */
7005 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7006 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 else
7011 {
7012 int save_efr = exec_from_reg;
7013
7014 exec_from_reg = TRUE;
7015
7016 /*
7017 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007018 * Continue until the stuff buffer is empty and all added characters
7019 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007021 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7023
7024 exec_from_reg = save_efr;
7025 }
7026}
7027
7028/*
7029 * ":!".
7030 */
7031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007032ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7035}
7036
7037/*
7038 * ":undo".
7039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007041ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
Bram Moolenaard3667a22006-03-16 21:35:52 +00007043 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02007044 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007045 else
7046 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047}
7048
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007049#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007051ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007052{
7053 char_u hash[UNDO_HASH_SIZE];
7054
7055 u_compute_hash(hash);
7056 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7057}
7058
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007060ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007061{
7062 char_u hash[UNDO_HASH_SIZE];
7063
7064 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007065 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007066}
7067#endif
7068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069/*
7070 * ":redo".
7071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007073ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074{
7075 u_redo(1);
7076}
7077
7078/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007079 * ":earlier" and ":later".
7080 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007082ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007083{
7084 long count = 0;
7085 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007086 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007087 char_u *p = eap->arg;
7088
7089 if (*p == NUL)
7090 count = 1;
7091 else if (isdigit(*p))
7092 {
7093 count = getdigits(&p);
7094 switch (*p)
7095 {
7096 case 's': ++p; sec = TRUE; break;
7097 case 'm': ++p; sec = TRUE; count *= 60; break;
7098 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007099 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7100 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007101 }
7102 }
7103
7104 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007105 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007106 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007107 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7108 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007109}
7110
7111/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 * ":redir": start/stop redirection.
7113 */
7114 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007115ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
7117 char *mode;
7118 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007119 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
Bram Moolenaarba768492016-07-08 15:32:54 +02007121#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007122 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007124 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007125 return;
7126 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007127#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007128
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 if (STRICMP(eap->arg, "END") == 0)
7130 close_redir();
7131 else
7132 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007133 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007135 ++arg;
7136 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007138 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 mode = "a";
7140 }
7141 else
7142 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007143 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144
7145 close_redir();
7146
7147 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007148 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (fname == NULL)
7150 return;
7151#ifdef FEAT_BROWSE
7152 if (cmdmod.browse)
7153 {
7154 char_u *browseFile;
7155
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007156 browseFile = do_browse(BROWSE_SAVE,
7157 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007158 fname, NULL, NULL,
7159 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (browseFile == NULL)
7161 return; /* operation cancelled */
7162 vim_free(fname);
7163 fname = browseFile;
7164 eap->forceit = TRUE; /* since dialog already asked */
7165 }
7166#endif
7167
7168 redir_fd = open_exfile(fname, eap->forceit, mode);
7169 vim_free(fname);
7170 }
7171#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007172 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 {
7174 /* redirect to a register a-z (resp. A-Z for appending) */
7175 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007176 ++arg;
7177 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007179 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007180 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007182 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007184 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007185 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00007186 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007187 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00007189 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007190 if (*arg == '>')
7191 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007192 /* Make register empty when not using @A-@Z and the
7193 * command is valid. */
7194 if (*arg == NUL && !isupper(redir_reg))
7195 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007197 }
7198 if (*arg != NUL)
7199 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007200 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007201 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007202 }
7203 }
7204 else if (*arg == '=' && arg[1] == '>')
7205 {
7206 int append;
7207
7208 /* redirect to a variable */
7209 close_redir();
7210 arg += 2;
7211
7212 if (*arg == '>')
7213 {
7214 ++arg;
7215 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
7217 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007218 append = FALSE;
7219
7220 if (var_redir_start(skipwhite(arg), append) == OK)
7221 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223#endif
7224
7225 /* TODO: redirect to a buffer */
7226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007228 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007230
7231 /* Make sure redirection is not off. Can happen for cmdline completion
7232 * that indirectly invokes a command to catch its output. */
7233 if (redir_fd != NULL
7234#ifdef FEAT_EVAL
7235 || redir_reg || redir_vname
7236#endif
7237 )
7238 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239}
7240
7241/*
7242 * ":redraw": force redraw
7243 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007244 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007245ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
7247 int r = RedrawingDisabled;
7248 int p = p_lz;
7249
7250 RedrawingDisabled = 0;
7251 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007252 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007254 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255#ifdef FEAT_TITLE
7256 if (need_maketitle)
7257 maketitle();
7258#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007259#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7260# ifdef VIMDLL
7261 if (!gui.in_use)
7262# endif
7263 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 RedrawingDisabled = r;
7266 p_lz = p;
7267
7268 /* Reset msg_didout, so that a message that's there is overwritten. */
7269 msg_didout = FALSE;
7270 msg_col = 0;
7271
Bram Moolenaar56667a52013-07-17 11:54:28 +02007272 /* No need to wait after an intentional redraw. */
7273 need_wait_return = FALSE;
7274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 out_flush();
7276}
7277
7278/*
7279 * ":redrawstatus": force redraw of status line(s)
7280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007282ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 int r = RedrawingDisabled;
7285 int p = p_lz;
7286
7287 RedrawingDisabled = 0;
7288 p_lz = FALSE;
7289 if (eap->forceit)
7290 status_redraw_all();
7291 else
7292 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007293 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 RedrawingDisabled = r;
7295 p_lz = p;
7296 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297}
7298
Bram Moolenaare12bab32019-01-08 22:02:56 +01007299/*
7300 * ":redrawtabline": force redraw of the tabline
7301 */
7302 static void
7303ex_redrawtabline(exarg_T *eap UNUSED)
7304{
7305 int r = RedrawingDisabled;
7306 int p = p_lz;
7307
7308 RedrawingDisabled = 0;
7309 p_lz = FALSE;
7310
7311 draw_tabline();
7312
7313 RedrawingDisabled = r;
7314 p_lz = p;
7315 out_flush();
7316}
7317
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007319close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320{
7321 if (redir_fd != NULL)
7322 {
7323 fclose(redir_fd);
7324 redir_fd = NULL;
7325 }
7326#ifdef FEAT_EVAL
7327 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007328 if (redir_vname)
7329 {
7330 var_redir_stop();
7331 redir_vname = 0;
7332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333#endif
7334}
7335
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007336#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007337 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007338vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007339{
7340 if (vim_mkdir(name, prot) != 0)
7341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007342 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007343 return FAIL;
7344 }
7345 return OK;
7346}
7347#endif
7348
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349/*
7350 * Open a file for writing for an Ex command, with some checks.
7351 * Return file descriptor, or NULL on failure.
7352 */
7353 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007354open_exfile(
7355 char_u *fname,
7356 int forceit,
7357 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
7359 FILE *fd;
7360
7361#ifdef UNIX
7362 /* with Unix it is possible to open a directory */
7363 if (mch_isdir(fname))
7364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007365 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 return NULL;
7367 }
7368#endif
7369 if (!forceit && *mode != 'a' && vim_fexists(fname))
7370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007371 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 return NULL;
7373 }
7374
7375 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007376 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
7378 return fd;
7379}
7380
7381/*
7382 * ":mark" and ":k".
7383 */
7384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007385ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386{
7387 pos_T pos;
7388
7389 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007390 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007392 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 else
7394 {
7395 pos = curwin->w_cursor; /* save curwin->w_cursor */
7396 curwin->w_cursor.lnum = eap->line2;
7397 beginline(BL_WHITE | BL_FIX);
7398 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007399 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 curwin->w_cursor = pos; /* restore curwin->w_cursor */
7401 }
7402}
7403
7404/*
7405 * Update w_topline, w_leftcol and the cursor position.
7406 */
7407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007408update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410 check_cursor(); /* put cursor on valid line */
7411 update_topline();
7412 if (!curwin->w_p_wrap)
7413 validate_cursor();
7414 update_curswant();
7415}
7416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007418 * Save the current State and go to Normal mode.
7419 * Return TRUE if the typeahead could be saved.
7420 */
7421 int
7422save_current_state(save_state_T *sst)
7423{
7424 sst->save_msg_scroll = msg_scroll;
7425 sst->save_restart_edit = restart_edit;
7426 sst->save_msg_didout = msg_didout;
7427 sst->save_State = State;
7428 sst->save_insertmode = p_im;
7429 sst->save_finish_op = finish_op;
7430 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007431 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007432
7433 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
7434 restart_edit = 0; /* don't go to Insert mode */
7435 p_im = FALSE; /* don't use 'insertmode' */
7436
7437 /*
7438 * Save the current typeahead. This is required to allow using ":normal"
7439 * from an event handler and makes sure we don't hang when the argument
7440 * ends with half a command.
7441 */
7442 save_typeahead(&sst->tabuf);
7443 return sst->tabuf.typebuf_valid;
7444}
7445
7446 void
7447restore_current_state(save_state_T *sst)
7448{
7449 /* Restore the previous typeahead. */
7450 restore_typeahead(&sst->tabuf);
7451
7452 msg_scroll = sst->save_msg_scroll;
7453 restart_edit = sst->save_restart_edit;
7454 p_im = sst->save_insertmode;
7455 finish_op = sst->save_finish_op;
7456 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007457 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007458 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
7459
7460 /* Restore the state (needed when called from a function executed for
7461 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
7462 State = sst->save_State;
7463#ifdef CURSOR_SHAPE
7464 ui_cursor_shape(); /* may show different cursor shape */
7465#endif
7466}
7467
7468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 * ":normal[!] {commands}": Execute normal mode commands.
7470 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007471 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007472ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007474 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 char_u *arg = NULL;
7476 int l;
7477 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007479 if (ex_normal_lock > 0)
7480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007481 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007482 return;
7483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 if (ex_normal_busy >= p_mmd)
7485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007486 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 return;
7488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 /*
7491 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7492 * this for the K_SPECIAL leading byte, otherwise special keys will not
7493 * work.
7494 */
7495 if (has_mbyte)
7496 {
7497 int len = 0;
7498
7499 /* Count the number of characters to be escaped. */
7500 for (p = eap->arg; *p != NUL; ++p)
7501 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007502#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 if (*p == CSI) /* leadbyte CSI */
7504 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007505#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007506 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01007508#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 )
7512 len += 2;
7513 }
7514 if (len > 0)
7515 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007516 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 if (arg != NULL)
7518 {
7519 len = 0;
7520 for (p = eap->arg; *p != NUL; ++p)
7521 {
7522 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007523#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 if (*p == CSI)
7525 {
7526 arg[len++] = KS_EXTRA;
7527 arg[len++] = (int)KE_CSI;
7528 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007529#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007530 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 {
7532 arg[len++] = *++p;
7533 if (*p == K_SPECIAL)
7534 {
7535 arg[len++] = KS_SPECIAL;
7536 arg[len++] = KE_FILLER;
7537 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007538#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 else if (*p == CSI)
7540 {
7541 arg[len++] = KS_EXTRA;
7542 arg[len++] = (int)KE_CSI;
7543 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 }
7546 arg[len] = NUL;
7547 }
7548 }
7549 }
7550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007552 ++ex_normal_busy;
7553 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 {
7555 /*
7556 * Repeat the :normal command for each line in the range. When no
7557 * range given, execute it just once, without positioning the cursor
7558 * first.
7559 */
7560 do
7561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 if (eap->addr_count != 0)
7563 {
7564 curwin->w_cursor.lnum = eap->line1++;
7565 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007566 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568
Bram Moolenaar13505972019-01-24 15:04:48 +01007569 exec_normal_cmd(arg != NULL
7570 ? arg
7571 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 }
7573 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7574 }
7575
7576 /* Might not return to the main loop when in an event handler. */
7577 update_topline_cursor();
7578
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007579 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007581#ifdef FEAT_MOUSE
7582 setmouse();
7583#endif
7584#ifdef CURSOR_SHAPE
7585 ui_cursor_shape(); /* may show different cursor shape */
7586#endif
7587
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589}
7590
7591/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007592 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 */
7594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007595ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007597 if (eap->forceit)
7598 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007599 /* cursor line can be zero on startup */
7600 if (!curwin->w_cursor.lnum)
7601 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00007602 coladvance((colnr_T)MAXCOL);
7603 curwin->w_curswant = MAXCOL;
7604 curwin->w_set_curswant = FALSE;
7605 }
7606
Bram Moolenaara40c5002005-01-09 21:16:21 +00007607 /* Ignore the command when already in Insert mode. Inserting an
7608 * expression register that invokes a function can do this. */
7609 if (State & INSERT)
7610 return;
7611
Bram Moolenaar64969662005-12-14 21:59:55 +00007612 if (eap->cmdidx == CMD_startinsert)
7613 restart_edit = 'a';
7614 else if (eap->cmdidx == CMD_startreplace)
7615 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007617 restart_edit = 'V';
7618
7619 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007621 if (eap->cmdidx == CMD_startinsert)
7622 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 curwin->w_curswant = 0; /* avoid MAXCOL */
7624 }
7625}
7626
7627/*
7628 * ":stopinsert"
7629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007631ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
7633 restart_edit = 0;
7634 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007635 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007638/*
7639 * Execute normal mode command "cmd".
7640 * "remap" can be REMAP_NONE or REMAP_YES.
7641 */
7642 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007643exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007644{
Bram Moolenaar25281632016-01-21 23:32:32 +01007645 /* Stuff the argument into the typeahead buffer. */
7646 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007647 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007648}
Bram Moolenaar25281632016-01-21 23:32:32 +01007649
Bram Moolenaar25281632016-01-21 23:32:32 +01007650/*
7651 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007652 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007653 */
7654 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007655exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007656{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007657 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007658 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007659
Bram Moolenaar905dd902019-04-07 14:21:47 +02007660 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7661 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007662 clear_oparg(&oa);
7663 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007664 while ((!stuff_empty()
7665 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007666 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007667 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007668 {
7669 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007670#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007671 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007672 && oa.op_type == OP_NOP && oa.regname == NUL
7673 && !VIsual_active)
7674 {
7675 /* If terminal_loop() returns OK we got a key that is handled
7676 * in Normal model. With FAIL we first need to position the
7677 * cursor and the screen needs to be redrawn. */
7678 if (terminal_loop(TRUE) == OK)
7679 normal_cmd(&oa, TRUE);
7680 }
7681 else
7682#endif
7683 /* execute a Normal mode cmd */
7684 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007685 }
7686}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007687
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688#ifdef FEAT_FIND_ID
7689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007690ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
7692 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7693 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7694 (linenr_T)1, (linenr_T)MAXLNUM);
7695}
7696
Bram Moolenaar4033c552017-09-16 20:54:51 +02007697#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698/*
7699 * ":psearch"
7700 */
7701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007702ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703{
7704 g_do_tagpreview = p_pvh;
7705 ex_findpat(eap);
7706 g_do_tagpreview = 0;
7707}
7708#endif
7709
7710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 int whole = TRUE;
7714 long n;
7715 char_u *p;
7716 int action;
7717
7718 switch (cmdnames[eap->cmdidx].cmd_name[2])
7719 {
7720 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
7721 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7722 action = ACTION_GOTO;
7723 else
7724 action = ACTION_SHOW;
7725 break;
7726 case 'i': /* ":ilist" and ":dlist" */
7727 action = ACTION_SHOW_ALL;
7728 break;
7729 case 'u': /* ":ijump" and ":djump" */
7730 action = ACTION_GOTO;
7731 break;
7732 default: /* ":isplit" and ":dsplit" */
7733 action = ACTION_SPLIT;
7734 break;
7735 }
7736
7737 n = 1;
7738 if (vim_isdigit(*eap->arg)) /* get count */
7739 {
7740 n = getdigits(&eap->arg);
7741 eap->arg = skipwhite(eap->arg);
7742 }
7743 if (*eap->arg == '/') /* Match regexp, not just whole words */
7744 {
7745 whole = FALSE;
7746 ++eap->arg;
7747 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7748 if (*p)
7749 {
7750 *p++ = NUL;
7751 p = skipwhite(p);
7752
7753 /* Check for trailing illegal characters */
7754 if (!ends_excmd(*p))
7755 eap->errmsg = e_trailing;
7756 else
7757 eap->nextcmd = check_nextcmd(p);
7758 }
7759 }
7760 if (!eap->skip)
7761 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7762 whole, !eap->forceit,
7763 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7764 n, action, eap->line1, eap->line2);
7765}
7766#endif
7767
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
Bram Moolenaar4033c552017-09-16 20:54:51 +02007769#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770/*
7771 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7772 */
7773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007774ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01007776 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7778}
7779
7780/*
7781 * ":pedit"
7782 */
7783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 win_T *curwin_save = curwin;
7787
Bram Moolenaar026587b2019-08-17 15:08:00 +02007788 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007790 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007791
Bram Moolenaar026587b2019-08-17 15:08:00 +02007792 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007794
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 if (curwin != curwin_save && win_valid(curwin_save))
7796 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007797 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 validate_cursor();
7799 redraw_later(VALID);
7800 win_enter(curwin_save, TRUE);
7801 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007802# ifdef FEAT_TEXT_PROP
7803 else if (WIN_IS_POPUP(curwin))
7804 {
7805 // can't keep focus in popup window
7806 win_enter(firstwin, TRUE);
7807 }
7808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 g_do_tagpreview = 0;
7810}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812
7813/*
7814 * ":stag", ":stselect" and ":stjump".
7815 */
7816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007817ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 postponed_split = -1;
7820 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007821 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7823 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007824 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826
7827/*
7828 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7829 */
7830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007831ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
7833 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7834}
7835
7836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007837ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 int cmd;
7840
7841 switch (name[1])
7842 {
7843 case 'j': cmd = DT_JUMP; /* ":tjump" */
7844 break;
7845 case 's': cmd = DT_SELECT; /* ":tselect" */
7846 break;
7847 case 'p': cmd = DT_PREV; /* ":tprevious" */
7848 break;
7849 case 'N': cmd = DT_PREV; /* ":tNext" */
7850 break;
7851 case 'n': cmd = DT_NEXT; /* ":tnext" */
7852 break;
7853 case 'o': cmd = DT_POP; /* ":pop" */
7854 break;
7855 case 'f': /* ":tfirst" */
7856 case 'r': cmd = DT_FIRST; /* ":trewind" */
7857 break;
7858 case 'l': cmd = DT_LAST; /* ":tlast" */
7859 break;
7860 default: /* ":tag" */
7861#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007862 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007864 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 return;
7866 }
7867#endif
7868 cmd = DT_TAG;
7869 break;
7870 }
7871
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007872 if (name[0] == 'l')
7873 {
7874#ifndef FEAT_QUICKFIX
7875 ex_ni(eap);
7876 return;
7877#else
7878 cmd = DT_LTAG;
7879#endif
7880 }
7881
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7883 eap->forceit, TRUE);
7884}
7885
7886/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007887 * Check "str" for starting with a special cmdline variable.
7888 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7889 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7890 */
7891 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007892find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007893{
7894 int len;
7895 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007896 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007897 "%",
7898#define SPEC_PERC 0
7899 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02007900#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007901 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007902#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007903 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007904#define SPEC_CCWORD (SPEC_CWORD + 1)
7905 "<cexpr>", /* expr under cursor */
7906#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007907 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007908#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007909 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007910#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007911 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007912#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007913 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007914#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007915 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007916#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007917 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007918#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007919 "<sflnum>", /* script file line number */
7920#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007921#ifdef FEAT_CLIENTSERVER
7922 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007923# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007924#endif
7925 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007926
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007927 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007928 {
7929 len = (int)STRLEN(spec_str[i]);
7930 if (STRNCMP(src, spec_str[i], len) == 0)
7931 {
7932 *usedlen = len;
7933 return i;
7934 }
7935 }
7936 return -1;
7937}
7938
7939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 * Evaluate cmdline variables.
7941 *
7942 * change '%' to curbuf->b_ffname
7943 * '#' to curwin->w_altfile
7944 * '<cword>' to word under the cursor
7945 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02007946 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 * '<cfile>' to path name under the cursor
7948 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007949 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 * '<afile>' to file name for autocommand
7951 * '<abuf>' to buffer number for autocommand
7952 * '<amatch>' to matching name for autocommand
7953 *
7954 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
7955 * "" for error without a message) and NULL is returned.
7956 * Returns an allocated string if a valid match was found.
7957 * Returns NULL if no match was found. "usedlen" then still contains the
7958 * number of characters to skip.
7959 */
7960 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007961eval_vars(
7962 char_u *src, /* pointer into commandline */
7963 char_u *srcstart, /* beginning of valid memory for src */
7964 int *usedlen, /* characters after src that are used */
7965 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007966 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007967 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00007968 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 int i;
7971 char_u *s;
7972 char_u *result;
7973 char_u *resultbuf = NULL;
7974 int resultlen;
7975 buf_T *buf;
7976 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
7977 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02007978 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981
7982 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00007983 if (escaped != NULL)
7984 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
7986 /*
7987 * Check if there is something to do.
7988 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007989 spec_idx = find_cmdline_var(src, usedlen);
7990 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 {
7992 *usedlen = 1;
7993 return NULL;
7994 }
7995
7996 /*
7997 * Skip when preceded with a backslash "\%" and "\#".
7998 * Note: In "\\%" the % is also not recognized!
7999 */
8000 if (src > srcstart && src[-1] == '\\')
8001 {
8002 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00008003 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 return NULL;
8005 }
8006
8007 /*
8008 * word or WORD under cursor
8009 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008010 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8011 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008013 resultlen = find_ident_under_cursor(&result,
8014 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8015 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8016 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 if (resultlen == 0)
8018 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008019 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 return NULL;
8021 }
8022 }
8023
8024 /*
8025 * '#': Alternate file name
8026 * '%': Current file name
8027 * File name under the cursor
8028 * File name for autocommand
8029 * and following modifiers
8030 */
8031 else
8032 {
8033 switch (spec_idx)
8034 {
8035 case SPEC_PERC: /* '%': current file */
8036 if (curbuf->b_fname == NULL)
8037 {
8038 result = (char_u *)"";
8039 valid = 0; /* Must have ":p:h" to be valid */
8040 }
8041 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008044 tilde_file = STRCMP(result, "~") == 0;
8045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 break;
8047
8048 case SPEC_HASH: /* '#' or "#99": alternate file */
8049 if (src[1] == '#') /* "##": the argument list */
8050 {
8051 result = arg_all();
8052 resultbuf = result;
8053 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008054 if (escaped != NULL)
8055 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 break;
8058 }
8059 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008060 if (*s == '<') /* "#<99" uses v:oldfiles */
8061 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008063 if (s == src + 2 && src[1] == '-')
8064 /* just a minus sign, don't skip over it */
8065 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 *usedlen = (int)(s - src); /* length of what we expand */
8067
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008068 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008070 if (*usedlen < 2)
8071 {
8072 /* Should we give an error message for #<text? */
8073 *usedlen = 1;
8074 return NULL;
8075 }
8076#ifdef FEAT_EVAL
8077 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8078 (long)i);
8079 if (result == NULL)
8080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008081 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008082 return NULL;
8083 }
8084#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008085 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 }
8089 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008090 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008091 if (i == 0 && src[1] == '<' && *usedlen > 1)
8092 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008093 buf = buflist_findnr(i);
8094 if (buf == NULL)
8095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008096 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008097 return NULL;
8098 }
8099 if (lnump != NULL)
8100 *lnump = ECMD_LAST;
8101 if (buf->b_fname == NULL)
8102 {
8103 result = (char_u *)"";
8104 valid = 0; /* Must have ":p:h" to be valid */
8105 }
8106 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008107 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008108 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008109 tilde_file = STRCMP(result, "~") == 0;
8110 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 break;
8113
8114#ifdef FEAT_SEARCHPATH
8115 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008116 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 if (result == NULL)
8118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008119 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 return NULL;
8121 }
8122 resultbuf = result; /* remember allocated string */
8123 break;
8124#endif
8125
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 case SPEC_AFILE: /* file name for autocommand */
8127 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008128 if (result != NULL && !autocmd_fname_full)
8129 {
8130 /* Still need to turn the fname into a full path. It is
8131 * postponed to avoid a delay when <afile> is not used. */
8132 autocmd_fname_full = TRUE;
8133 result = FullName_save(autocmd_fname, FALSE);
8134 vim_free(autocmd_fname);
8135 autocmd_fname = result;
8136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (result == NULL)
8138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008139 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 return NULL;
8141 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008142 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 break;
8144
8145 case SPEC_ABUF: /* buffer number for autocommand */
8146 if (autocmd_bufnr <= 0)
8147 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008148 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 return NULL;
8150 }
8151 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8152 result = strbuf;
8153 break;
8154
8155 case SPEC_AMATCH: /* match name for autocommand */
8156 result = autocmd_match;
8157 if (result == NULL)
8158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008159 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 return NULL;
8161 }
8162 break;
8163
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 case SPEC_SFILE: /* file name for ":so" command */
8165 result = sourcing_name;
8166 if (result == NULL)
8167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008168 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 return NULL;
8170 }
8171 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008172
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008173 case SPEC_SLNUM: /* line in file for ":so" command */
8174 if (sourcing_name == NULL || sourcing_lnum == 0)
8175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008176 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008177 return NULL;
8178 }
8179 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
8180 result = strbuf;
8181 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008182
8183#ifdef FEAT_EVAL
8184 case SPEC_SFLNUM: /* line in script file */
8185 if (current_sctx.sc_lnum + sourcing_lnum == 0)
8186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008187 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008188 return NULL;
8189 }
8190 sprintf((char *)strbuf, "%ld",
8191 (long)(current_sctx.sc_lnum + sourcing_lnum));
8192 result = strbuf;
8193 break;
8194#endif
8195
8196#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008198 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8199 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 result = strbuf;
8201 break;
8202#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008203
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008204 default:
8205 result = (char_u *)""; /* avoid gcc warning */
8206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 }
8208
8209 resultlen = (int)STRLEN(result); /* length of new string */
8210 if (src[*usedlen] == '<') /* remove the file name extension */
8211 {
8212 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 resultlen = (int)(s - result);
8215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 else if (!skip_mod)
8217 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008218 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 &resultlen);
8220 if (result == NULL)
8221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008222 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 return NULL;
8224 }
8225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227
8228 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8229 {
8230 if (valid != VALID_HEAD + VALID_PATH)
8231 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008232 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008234 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 result = NULL;
8236 }
8237 else
8238 result = vim_strnsave(result, resultlen);
8239 vim_free(resultbuf);
8240 return result;
8241}
8242
8243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 * Expand the <sfile> string in "arg".
8245 *
8246 * Returns an allocated string, or NULL for any error.
8247 */
8248 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008249expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008251 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 int len;
8253 char_u *result;
8254 char_u *newres;
8255 char_u *repl;
8256 int srclen;
8257 char_u *p;
8258
8259 result = vim_strsave(arg);
8260 if (result == NULL)
8261 return NULL;
8262
8263 for (p = result; *p; )
8264 {
8265 if (STRNCMP(p, "<sfile>", 7) != 0)
8266 ++p;
8267 else
8268 {
8269 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00008270 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 if (errormsg != NULL)
8272 {
8273 if (*errormsg)
8274 emsg(errormsg);
8275 vim_free(result);
8276 return NULL;
8277 }
8278 if (repl == NULL) /* no match (cannot happen) */
8279 {
8280 p += srclen;
8281 continue;
8282 }
8283 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8284 newres = alloc(len);
8285 if (newres == NULL)
8286 {
8287 vim_free(repl);
8288 vim_free(result);
8289 return NULL;
8290 }
8291 mch_memmove(newres, result, (size_t)(p - result));
8292 STRCPY(newres + (p - result), repl);
8293 len = (int)STRLEN(newres);
8294 STRCAT(newres, p + srclen);
8295 vim_free(repl);
8296 vim_free(result);
8297 result = newres;
8298 p = newres + len; /* continue after the match */
8299 }
8300 }
8301
8302 return result;
8303}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008306/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008307 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008308 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008311dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 if (fname == NULL)
8314 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008315 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316}
8317#endif
8318
8319/*
8320 * ":behave {mswin,xterm}"
8321 */
8322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008323ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324{
8325 if (STRCMP(eap->arg, "mswin") == 0)
8326 {
8327 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8328 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8329 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8330 set_option_value((char_u *)"keymodel", 0L,
8331 (char_u *)"startsel,stopsel", 0);
8332 }
8333 else if (STRCMP(eap->arg, "xterm") == 0)
8334 {
8335 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8336 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8337 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8338 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8339 }
8340 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008341 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342}
8343
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344static int filetype_detect = FALSE;
8345static int filetype_plugin = FALSE;
8346static int filetype_indent = FALSE;
8347
8348/*
8349 * ":filetype [plugin] [indent] {on,off,detect}"
8350 * on: Load the filetype.vim file to install autocommands for file types.
8351 * off: Load the ftoff.vim file to remove all autocommands for file types.
8352 * plugin on: load filetype.vim and ftplugin.vim
8353 * plugin off: load ftplugof.vim
8354 * indent on: load filetype.vim and indent.vim
8355 * indent off: load indoff.vim
8356 */
8357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008358ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359{
8360 char_u *arg = eap->arg;
8361 int plugin = FALSE;
8362 int indent = FALSE;
8363
8364 if (*eap->arg == NUL)
8365 {
8366 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008367 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368 filetype_detect ? "ON" : "OFF",
8369 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8370 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8371 return;
8372 }
8373
8374 /* Accept "plugin" and "indent" in any order. */
8375 for (;;)
8376 {
8377 if (STRNCMP(arg, "plugin", 6) == 0)
8378 {
8379 plugin = TRUE;
8380 arg = skipwhite(arg + 6);
8381 continue;
8382 }
8383 if (STRNCMP(arg, "indent", 6) == 0)
8384 {
8385 indent = TRUE;
8386 arg = skipwhite(arg + 6);
8387 continue;
8388 }
8389 break;
8390 }
8391 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8392 {
8393 if (*arg == 'o' || !filetype_detect)
8394 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008395 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 filetype_detect = TRUE;
8397 if (plugin)
8398 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008399 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 filetype_plugin = TRUE;
8401 }
8402 if (indent)
8403 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008404 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 filetype_indent = TRUE;
8406 }
8407 }
8408 if (*arg == 'd')
8409 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008410 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008411 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 }
8413 }
8414 else if (STRCMP(arg, "off") == 0)
8415 {
8416 if (plugin || indent)
8417 {
8418 if (plugin)
8419 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008420 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 filetype_plugin = FALSE;
8422 }
8423 if (indent)
8424 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008425 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 filetype_indent = FALSE;
8427 }
8428 }
8429 else
8430 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008431 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 filetype_detect = FALSE;
8433 }
8434 }
8435 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008436 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437}
8438
8439/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008440 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 */
8442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008443ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444{
8445 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008446 {
8447 char_u *arg = eap->arg;
8448
8449 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8450 arg += 9;
8451
8452 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8453 if (arg != eap->arg)
8454 did_filetype = FALSE;
8455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008459ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461#ifdef FEAT_DIGRAPHS
8462 if (*eap->arg != NUL)
8463 putdigraph(eap->arg);
8464 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008465 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008467 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#endif
8469}
8470
8471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008472ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473{
8474 int flags = 0;
8475
8476 if (eap->cmdidx == CMD_setlocal)
8477 flags = OPT_LOCAL;
8478 else if (eap->cmdidx == CMD_setglobal)
8479 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008480#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (cmdmod.browse && flags == 0)
8482 ex_options(eap);
8483 else
8484#endif
8485 (void)do_set(eap->arg, flags);
8486}
8487
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008488#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8489 void
8490set_no_hlsearch(int flag)
8491{
8492 no_hlsearch = flag;
8493# ifdef FEAT_EVAL
8494 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8495# endif
8496}
8497
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498/*
8499 * ":nohlsearch"
8500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008502ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008504 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008505 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507#endif
8508
8509#ifdef FEAT_CRYPT
8510/*
8511 * ":X": Get crypt key
8512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008514ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008516 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008517 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518}
8519#endif
8520
8521#ifdef FEAT_FOLDING
8522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008523ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524{
8525 if (foldManualAllowed(TRUE))
8526 foldCreate(eap->line1, eap->line2);
8527}
8528
8529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008530ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8533 eap->forceit, FALSE);
8534}
8535
8536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008537ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 linenr_T lnum;
8540
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008541#ifdef FEAT_CLIPBOARD
8542 start_global_changes();
8543#endif
8544
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 /* First set the marks for all lines closed/open. */
8546 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8547 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8548 ml_setmarked(lnum);
8549
8550 /* Execute the command on the marked lines. */
8551 global_exe(eap->arg);
8552 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008553#ifdef FEAT_CLIPBOARD
8554 end_global_changes();
8555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556}
8557#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008558
Bram Moolenaar39665952018-08-15 20:59:48 +02008559#ifdef FEAT_QUICKFIX
8560/*
8561 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8562 * instead of a quickfix command.
8563 */
8564 int
8565is_loclist_cmd(int cmdidx)
8566{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008567 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008568 return FALSE;
8569 return cmdnames[cmdidx].cmd_name[0] == 'l';
8570}
8571#endif
8572
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008573# if defined(FEAT_TIMERS) || defined(PROTO)
8574 int
8575get_pressedreturn(void)
8576{
8577 return ex_pressedreturn;
8578}
8579
8580 void
8581set_pressedreturn(int val)
8582{
8583 ex_pressedreturn = val;
8584}
8585#endif