blob: 4dd7ba23d8134c8a9d208f29bfc87c2a93802287 [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/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200562 * Print the executed command for when 'verbose' is set.
563 * When "lnum" is 0 only print the command.
564 */
565 static void
566msg_verbose_cmd(linenr_T lnum, char_u *cmd)
567{
568 ++no_wait_return;
569 verbose_enter_scroll();
570
571 if (lnum == 0)
572 smsg(_("Executing: %s"), cmd);
573 else
574 smsg(_("line %ld: %s"), (long)lnum, cmd);
575 if (msg_silent == 0)
576 msg_puts("\n"); // don't overwrite this
577
578 verbose_leave_scroll();
579 --no_wait_return;
580}
581
582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 * Execute a simple command line. Used for translated commands like "*".
584 */
585 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100586do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 return do_cmdline(cmd, NULL, NULL,
589 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
590}
591
592/*
593 * do_cmdline(): execute one Ex command line
594 *
595 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100596 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 * 2. Split up in parts separated with '|'.
598 *
599 * This function can be called recursively!
600 *
601 * flags:
602 * DOCMD_VERBOSE - The command will be included in the error message.
603 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100604 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 * DOCMD_KEYTYPED - Don't reset KeyTyped.
606 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
607 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
608 *
609 * return FAIL if cmdline could not be executed, OK otherwise
610 */
611 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100612do_cmdline(
613 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200614 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100615 void *cookie, /* argument for fgetline() */
616 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 char_u *next_cmdline; /* next cmd to execute */
619 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100620 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 static int recursive = 0; /* recursive depth */
622 int msg_didout_before_start = 0;
623 int count = 0; /* line number count */
624 int did_inc = FALSE; /* incremented RedrawingDisabled */
625 int retval = OK;
626#ifdef FEAT_EVAL
627 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000628 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 int current_line = 0; /* active line in lines_ga */
630 char_u *fname = NULL; /* function or script name */
631 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
632 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000633 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 int initial_trylevel;
635 struct msglist **saved_msg_list = NULL;
636 struct msglist *private_msg_list;
637
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100638 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200639 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000641 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000643 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100645# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646# define cmd_cookie cookie
647#endif
648 static int call_depth = 0; /* recursiveness */
649
650#ifdef FEAT_EVAL
651 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
652 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000653 * This ensures that the do_errthrow() call in do_one_cmd() does not
654 * combine the messages stored by an earlier invocation of do_one_cmd()
655 * with the command name of the later one. This would happen when
656 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 saved_msg_list = msg_list;
658 msg_list = &private_msg_list;
659 private_msg_list = NULL;
660#endif
661
662 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100663 * here. The value of 200 allows nested function calls, ":source", etc.
664 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100665 if (call_depth >= 200
666#ifdef FEAT_EVAL
667 && call_depth >= p_mfd
668#endif
669 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100671 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_EVAL
673 /* When converting to an exception, we do not include the command name
674 * since this is not an error of the specific command. */
675 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
676 msg_list = saved_msg_list;
677#endif
678 return FAIL;
679 }
680 ++call_depth;
681
682#ifdef FEAT_EVAL
683 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000684 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 cstack.cs_trylevel = 0;
686 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
689
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100690 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000694 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ++ex_nesting_level;
696
697 /* Get the function or script name and the address where the next breakpoint
698 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 fname = func_name(real_cookie);
702 breakpoint = func_breakpoint(real_cookie);
703 dbg_tick = func_dbg_tick(real_cookie);
704 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = sourcing_name;
708 breakpoint = source_breakpoint(real_cookie);
709 dbg_tick = source_dbg_tick(real_cookie);
710 }
711
712 /*
713 * Initialize "force_abort" and "suppress_errthrow" at the top level.
714 */
715 if (!recursive)
716 {
717 force_abort = FALSE;
718 suppress_errthrow = FALSE;
719 }
720
721 /*
722 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000723 * exception handling (used when debugging). Otherwise clear it to avoid
724 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000726 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000727 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200729 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
731 initial_trylevel = trylevel;
732
733 /*
734 * "did_throw" will be set to TRUE when an exception is being thrown.
735 */
736 did_throw = FALSE;
737#endif
738 /*
739 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000740 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * If force_abort is set, we cancel everything.
742 */
743 did_emsg = FALSE;
744
745 /*
746 * KeyTyped is only set when calling vgetc(). Reset it here when not
747 * calling vgetc() (sourced command lines).
748 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100749 if (!(flags & DOCMD_KEYTYPED)
750 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 KeyTyped = FALSE;
752
753 /*
754 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 * - for multiple commands on one line, separated with '|'
757 * - when repeating until there are no more lines (for ":source")
758 */
759 next_cmdline = cmdline;
760 do
761 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000762#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000764#endif
765
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000766 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (next_cmdline == NULL
768#ifdef FEAT_EVAL
769 && !force_abort
770 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 )
774 did_emsg = FALSE;
775
776 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000777 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 * 3. If a line is given: Make a copy, so we can mess with it.
780 */
781
782#ifdef FEAT_EVAL
783 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
786 /* Each '|' separated command is stored separately in lines_ga, to
787 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100788 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /* Check if a function has returned or, unless it has an unclosed
791 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000795 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 func_line_end(real_cookie);
797# endif
798 if (func_has_ended(real_cookie))
799 {
800 retval = FAIL;
801 break;
802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000805 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100806 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 script_line_end();
808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {
813 retval = FAIL;
814 break;
815 }
816
817 /* If breakpoints have been added/deleted need to check for it. */
818 if (breakpoint != NULL && dbg_tick != NULL
819 && *dbg_tick != debug_tick)
820 {
821 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 fname, sourcing_lnum);
824 *dbg_tick = debug_tick;
825 }
826
827 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
828 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
829
830 /* Did we encounter a breakpoint? */
831 if (breakpoint != NULL && *breakpoint != 0
832 && *breakpoint <= sourcing_lnum)
833 {
834 dbg_breakpoint(fname, sourcing_lnum);
835 /* Find next breakpoint. */
836 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 fname, sourcing_lnum);
839 *dbg_tick = debug_tick;
840 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000842 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843 {
844 if (getline_is_func)
845 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 script_line_start();
848 }
849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000852 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000854 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 * below, so that it stores lines in or reads them from
857 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000858 * while/for loop. */
859 cmd_getline = get_loop_line;
860 cmd_cookie = (void *)&cmd_loop_cookie;
861 cmd_loop_cookie.lines_gap = &lines_ga;
862 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 cmd_loop_cookie.cookie = cookie;
865 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 else
868 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 cmd_cookie = cookie;
871 }
872#endif
873
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100874 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200889 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
891 /* Don't call wait_return for aborted command line. The NULL
892 * returned for the end of a sourced file or executed function
893 * doesn't do this. */
894 if (KeyTyped && !(flags & DOCMD_REPEAT))
895 need_wait_return = FALSE;
896 retval = FAIL;
897 break;
898 }
899 used_getline = TRUE;
900
901 /*
902 * Keep the first typed line. Clear it when more lines are typed.
903 */
904 if (flags & DOCMD_KEEPLINE)
905 {
906 vim_free(repeat_cmdline);
907 if (count == 0)
908 repeat_cmdline = vim_strsave(next_cmdline);
909 else
910 repeat_cmdline = NULL;
911 }
912 }
913
914 /* 3. Make a copy of the command so we can mess with it. */
915 else if (cmdline_copy == NULL)
916 {
917 next_cmdline = vim_strsave(next_cmdline);
918 if (next_cmdline == NULL)
919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100920 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 retval = FAIL;
922 break;
923 }
924 }
925 cmdline_copy = next_cmdline;
926
927#ifdef FEAT_EVAL
928 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000929 * Save the current line when inside a ":while" or ":for", and when
930 * the command looks like a ":while" or ":for", because we may need it
931 * later. When there is a '|' and another command, it is stored
932 * separately, because we need to be able to jump back to it from an
933 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 if (current_line == lines_ga.ga_len
936 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 retval = FAIL;
941 break;
942 }
943 }
944 did_endif = FALSE;
945#endif
946
947 if (count++ == 0)
948 {
949 /*
950 * All output from the commands is put below each other, without
951 * waiting for a return. Don't do this when executing commands
952 * from a script or when being called recursive (e.g. for ":e
953 * +command file").
954 */
955 if (!(flags & DOCMD_NOWAIT) && !recursive)
956 {
957 msg_didout_before_start = msg_didout;
958 msg_didany = FALSE; /* no output yet */
959 msg_start();
960 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200961 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ++RedrawingDisabled;
963 did_inc = TRUE;
964 }
965 }
966
967 if (p_verbose >= 15 && sourcing_name != NULL)
Bram Moolenaar4facea32019-10-12 20:17:40 +0200968 msg_verbose_cmd(sourcing_lnum, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970 /*
971 * 2. Execute one '|' separated command.
972 * do_one_cmd() will return NULL if there is no trailing '|'.
973 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
974 */
975 ++recursive;
976 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
977#ifdef FEAT_EVAL
978 &cstack,
979#endif
980 cmd_getline, cmd_cookie);
981 --recursive;
982
983#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 if (cmd_cookie == (void *)&cmd_loop_cookie)
985 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#endif
989
990 if (next_cmdline == NULL)
991 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100992 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 /*
995 * If the command was typed, remember it for the ':' register.
996 * Do this AFTER executing the command to make :@: work.
997 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 && new_last_cmdline != NULL)
1000 {
1001 vim_free(last_cmdline);
1002 last_cmdline = new_last_cmdline;
1003 new_last_cmdline = NULL;
1004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 else
1007 {
1008 /* need to copy the command after the '|' to cmdline_copy, for the
1009 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001010 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 next_cmdline = cmdline_copy;
1012 }
1013
1014
1015#ifdef FEAT_EVAL
1016 /* reset did_emsg for a function that is not aborted by an error */
1017 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 && !func_has_abort(real_cookie))
1020 did_emsg = FALSE;
1021
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001022 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
1024 ++current_line;
1025
1026 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 * An ":endwhile", ":endfor" and ":continue" is handled here.
1028 * If we were executing commands, jump back to the ":while" or
1029 * ":for".
1030 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 /* Jump back to the matching ":while" or ":for". Be careful
1037 * not to use a cs_line[] from an entry that isn't a ":while"
1038 * or ":for": It would make "current_line" invalid and can
1039 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (!did_emsg && !got_int && !did_throw
1041 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 && (cstack.cs_flags[cstack.cs_idx]
1043 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 && cstack.cs_line[cstack.cs_idx] >= 0
1045 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1046 {
1047 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 /* remember we jumped there */
1049 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 line_breakcheck(); /* check if CTRL-C typed */
1051
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 /* Check for the next breakpoint at or after the ":while"
1053 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (breakpoint != NULL)
1055 {
1056 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001057 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 fname,
1059 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1060 *dbg_tick = debug_tick;
1061 }
1062 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001067 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1068 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
1070 }
1071
1072 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1079 }
1080 }
1081
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001082 /* Check for the next breakpoint after a watchexpression */
1083 if (breakpoint != NULL && has_watchexpr())
1084 {
1085 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1086 *dbg_tick = debug_tick;
1087 }
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 /*
1090 * When not inside any ":while" loop, clear remembered lines.
1091 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
1094 if (lines_ga.ga_len > 0)
1095 {
1096 sourcing_lnum =
1097 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1098 free_cmdlines(&lines_ga);
1099 }
1100 current_line = 0;
1101 }
1102
1103 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1105 * being restored at the ":endtry". Reset them here and set the
1106 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1107 * This includes the case where a missing ":endif", ":endwhile" or
1108 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 cstack.cs_lflags &= ~CSL_HAD_FINA;
1113 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1114 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 did_throw ? (void *)current_exception : NULL);
1116 did_emsg = got_int = did_throw = FALSE;
1117 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1118 }
1119
1120 /* Update global "trylevel" for recursive calls to do_cmdline() from
1121 * within this loop. */
1122 trylevel = initial_trylevel + cstack.cs_trylevel;
1123
1124 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001125 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 * files) is aborted because of an error, an interrupt, or an uncaught
1127 * exception, cancel everything. If it is left normally, reset
1128 * force_abort to get the non-EH compatible abortion behavior for
1129 * the rest of the script.
1130 */
1131 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1132 force_abort = FALSE;
1133
1134 /* Convert an interrupt to an exception if appropriate. */
1135 (void)do_intthrow(&cstack);
1136#endif /* FEAT_EVAL */
1137
1138 }
1139 /*
1140 * Continue executing command lines when:
1141 * - no CTRL-C typed, no aborting error, no exception thrown or try
1142 * conditionals need to be checked for executing finally clauses or
1143 * catching an interrupt exception
1144 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001145 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * looping for ":source" command or function call.
1147 */
1148 while (!((got_int
1149#ifdef FEAT_EVAL
1150 || (did_emsg && force_abort) || did_throw
1151#endif
1152 )
1153#ifdef FEAT_EVAL
1154 && cstack.cs_trylevel == 0
1155#endif
1156 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001157 && !(did_emsg
1158#ifdef FEAT_EVAL
1159 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001160 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001161 * the :endtry to be missed. */
1162 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1163#endif
1164 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001165 && (getline_equal(fgetline, cookie, getexmodeline)
1166 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && (next_cmdline != NULL
1168#ifdef FEAT_EVAL
1169 || cstack.cs_idx >= 0
1170#endif
1171 || (flags & DOCMD_REPEAT)));
1172
1173 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001174 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#ifdef FEAT_EVAL
1176 free_cmdlines(&lines_ga);
1177 ga_clear(&lines_ga);
1178
1179 if (cstack.cs_idx >= 0)
1180 {
1181 /*
1182 * If a sourced file or executed function ran to its end, report the
1183 * unclosed conditional.
1184 */
1185 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001186 && ((getline_equal(fgetline, cookie, getsourceline)
1187 && !source_finished(fgetline, cookie))
1188 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 && !func_has_ended(real_cookie))))
1190 {
1191 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
1200
1201 /*
1202 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1203 * ":endtry" in a sourced file or executed function. If the try
1204 * conditional is in its finally clause, ignore anything pending.
1205 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001206 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 */
1208 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001209 {
1210 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1211
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001212 if (idx >= 0)
1213 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1215 &cstack.cs_looplevel);
1216 }
1217 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 trylevel = initial_trylevel;
1219 }
1220
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1222 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001224 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 ? (char_u *)"endfunction" : (char_u *)NULL);
1226
1227 if (trylevel == 0)
1228 {
1229 /*
1230 * When an exception is being thrown out of the outermost try
1231 * conditional, discard the uncaught exception, disable the conversion
1232 * of interrupts or errors to exceptions, and ensure that no more
1233 * commands are executed.
1234 */
1235 if (did_throw)
1236 {
1237 void *p = NULL;
1238 char_u *saved_sourcing_name;
1239 int saved_sourcing_lnum;
1240 struct msglist *messages = NULL, *next;
1241
1242 /*
1243 * If the uncaught exception is a user exception, report it as an
1244 * error. If it is an error exception, display the saved error
1245 * message now. For an interrupt exception, do nothing; the
1246 * interrupt message is given elsewhere.
1247 */
1248 switch (current_exception->type)
1249 {
1250 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001251 vim_snprintf((char *)IObuff, IOSIZE,
1252 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 current_exception->value);
1254 p = vim_strsave(IObuff);
1255 break;
1256 case ET_ERROR:
1257 messages = current_exception->messages;
1258 current_exception->messages = NULL;
1259 break;
1260 case ET_INTERRUPT:
1261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263
1264 saved_sourcing_name = sourcing_name;
1265 saved_sourcing_lnum = sourcing_lnum;
1266 sourcing_name = current_exception->throw_name;
1267 sourcing_lnum = current_exception->throw_lnum;
1268 current_exception->throw_name = NULL;
1269
1270 discard_current_exception(); /* uses IObuff if 'verbose' */
1271 suppress_errthrow = TRUE;
1272 force_abort = TRUE;
1273
1274 if (messages != NULL)
1275 {
1276 do
1277 {
1278 next = messages->next;
1279 emsg(messages->msg);
1280 vim_free(messages->msg);
1281 vim_free(messages);
1282 messages = next;
1283 }
1284 while (messages != NULL);
1285 }
1286 else if (p != NULL)
1287 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001288 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 vim_free(p);
1290 }
1291 vim_free(sourcing_name);
1292 sourcing_name = saved_sourcing_name;
1293 sourcing_lnum = saved_sourcing_lnum;
1294 }
1295
1296 /*
1297 * On an interrupt or an aborting error not converted to an exception,
1298 * disable the conversion of errors to exceptions. (Interrupts are not
1299 * converted any more, here.) This enables also the interrupt message
1300 * when force_abort is set and did_emsg unset in case of an interrupt
1301 * from a finally clause after an error.
1302 */
1303 else if (got_int || (did_emsg && force_abort))
1304 suppress_errthrow = TRUE;
1305 }
1306
1307 /*
1308 * The current cstack will be freed when do_cmdline() returns. An uncaught
1309 * exception will have to be rethrown in the previous cstack. If a function
1310 * has just returned or a script file was just finished and the previous
1311 * cstack belongs to the same function or, respectively, script file, it
1312 * will have to be checked for finally clauses to be executed due to the
1313 * ":return" or ":finish". This is done in do_one_cmd().
1314 */
1315 if (did_throw)
1316 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001317 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001319 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 && ex_nesting_level > func_level(real_cookie) + 1))
1321 {
1322 if (!did_throw)
1323 check_cstack = TRUE;
1324 }
1325 else
1326 {
1327 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 --ex_nesting_level;
1330 /*
1331 * Go to debug mode when returning from a function in which we are
1332 * single-stepping.
1333 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001334 if ((getline_equal(fgetline, cookie, getsourceline)
1335 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 ? (char_u *)_("End of sourced file")
1339 : (char_u *)_("End of function"));
1340 }
1341
1342 /*
1343 * Restore the exception environment (done after returning from the
1344 * debugger).
1345 */
1346 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001347 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349 msg_list = saved_msg_list;
1350#endif /* FEAT_EVAL */
1351
1352 /*
1353 * If there was too much output to fit on the command line, ask the user to
1354 * hit return before redrawing the screen. With the ":global" command we do
1355 * this only once after the command is finished.
1356 */
1357 if (did_inc)
1358 {
1359 --RedrawingDisabled;
1360 --no_wait_return;
1361 msg_scroll = FALSE;
1362
1363 /*
1364 * When just finished an ":if"-":else" which was typed, no need to
1365 * wait for hit-return. Also for an error situation.
1366 */
1367 if (retval == FAIL
1368#ifdef FEAT_EVAL
1369 || (did_endif && KeyTyped && !did_emsg)
1370#endif
1371 )
1372 {
1373 need_wait_return = FALSE;
1374 msg_didany = FALSE; /* don't wait when restarting edit */
1375 }
1376 else if (need_wait_return)
1377 {
1378 /*
1379 * The msg_start() above clears msg_didout. The wait_return we do
1380 * here should not overwrite the command that may be shown before
1381 * doing that.
1382 */
1383 msg_didout |= msg_didout_before_start;
1384 wait_return(FALSE);
1385 }
1386 }
1387
Bram Moolenaar2a942252012-11-28 23:03:07 +01001388#ifdef FEAT_EVAL
1389 did_endif = FALSE; /* in case do_cmdline used recursively */
1390#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 /*
1392 * Reset if_level, in case a sourced script file contains more ":if" than
1393 * ":endif" (could be ":if x | foo | endif").
1394 */
1395 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001396#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001397
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 --call_depth;
1399 return retval;
1400}
1401
1402#ifdef FEAT_EVAL
1403/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001404 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 */
1406 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001407get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001409 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 wcmd_T *wp;
1411 char_u *line;
1412
1413 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1414 {
1415 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001416 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001418 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001420 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001422 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001423 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 ++cp->current_line;
1425
1426 return line;
1427 }
1428
1429 KeyTyped = FALSE;
1430 ++cp->current_line;
1431 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1432 sourcing_lnum = wp->lnum;
1433 return vim_strsave(wp->line);
1434}
1435
1436/*
1437 * Store a line in "gap" so that a ":while" loop can execute it again.
1438 */
1439 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001440store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
1442 if (ga_grow(gap, 1) == FAIL)
1443 return FAIL;
1444 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1445 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1446 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 return OK;
1448}
1449
1450/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001451 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 */
1453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001454free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
1456 while (gap->ga_len > 0)
1457 {
1458 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1459 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461}
1462#endif
1463
1464/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001465 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1466 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001469getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001470 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001471 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001472 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
1474#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001475 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001476 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
Bram Moolenaar89d40322006-08-29 15:30:07 +00001478 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001479 * function that's originally used to obtain the lines. This may be
1480 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001481 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001482 cp = (struct loop_cookie *)cookie;
1483 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 {
1485 gp = cp->getline;
1486 cp = cp->cookie;
1487 }
1488 return gp == func;
1489#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001490 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491#endif
1492}
1493
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001495 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 * getline function. Otherwise return "cookie".
1497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001499getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001500 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001501 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502{
Bram Moolenaar13505972019-01-24 15:04:48 +01001503#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001504 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001505 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001508 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001510 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001511 cp = (struct loop_cookie *)cookie;
1512 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 {
1514 gp = cp->getline;
1515 cp = cp->cookie;
1516 }
1517 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001518#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001521}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001523
1524/*
1525 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1526 * ":bwipeout", etc.
1527 * Returns the buffer number.
1528 */
1529 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001530compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001531{
1532 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001533 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001534 int count = offset;
1535
1536 buf = firstbuf;
1537 while (buf->b_next != NULL && buf->b_fnum < lnum)
1538 buf = buf->b_next;
1539 while (count != 0)
1540 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001541 count += (offset < 0) ? 1 : -1;
1542 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1543 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001544 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001545 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001546 if (addr_type == ADDR_LOADED_BUFFERS)
1547 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001548 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 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001556 /* we might have gone too far, last buffer is not loadedd */
1557 if (addr_type == ADDR_LOADED_BUFFERS)
1558 while (buf->b_ml.ml_mfp == NULL)
1559 {
1560 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1561 if (nextbuf == NULL)
1562 break;
1563 buf = nextbuf;
1564 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001565 return buf->b_fnum;
1566}
1567
Bram Moolenaarb7316892019-05-01 18:08:42 +02001568/*
1569 * Return the window number of "win".
1570 * When "win" is NULL return the number of windows.
1571 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001572 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001573current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001574{
1575 win_T *wp;
1576 int nr = 0;
1577
Bram Moolenaar29323592016-07-24 22:04:11 +02001578 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001579 {
1580 ++nr;
1581 if (wp == win)
1582 break;
1583 }
1584 return nr;
1585}
1586
1587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001588current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001589{
1590 tabpage_T *tp;
1591 int nr = 0;
1592
Bram Moolenaar29323592016-07-24 22:04:11 +02001593 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001594 {
1595 ++nr;
1596 if (tp == tab)
1597 break;
1598 }
1599 return nr;
1600}
1601
1602# define CURRENT_WIN_NR current_win_nr(curwin)
1603# define LAST_WIN_NR current_win_nr(NULL)
1604# define CURRENT_TAB_NR current_tab_nr(curtab)
1605# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607/*
1608 * Execute one Ex command.
1609 *
1610 * If 'sourcing' is TRUE, the command will be included in the error message.
1611 *
1612 * 1. skip comment lines and leading space
1613 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001614 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001615 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001616 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617 * 6. parse arguments
1618 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 *
1622 * This function may be called recursively!
1623 */
1624#if (_MSC_VER == 1200)
1625/*
Bram Moolenaared203462004-06-16 11:19:22 +00001626 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001628 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629#endif
1630 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001631do_one_cmd(
1632 char_u **cmdlinep,
1633 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001635 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001637 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001638 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639{
1640 char_u *p;
1641 linenr_T lnum;
1642 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001643 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001644 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001646 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001648 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001650 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
1652 vim_memset(&ea, 0, sizeof(ea));
1653 ea.line1 = 1;
1654 ea.line2 = 1;
1655#ifdef FEAT_EVAL
1656 ++ex_nesting_level;
1657#endif
1658
Bram Moolenaar21691f82012-12-05 19:13:18 +01001659 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (quitmore
1661#ifdef FEAT_EVAL
1662 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001663 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001664#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001665 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 --quitmore;
1668
1669 /*
1670 * Reset browse, confirm, etc.. They are restored when returning, for
1671 * recursive calls.
1672 */
1673 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001675 /* "#!anything" is handled like a comment. */
1676 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1677 goto doend;
1678
Bram Moolenaar4facea32019-10-12 20:17:40 +02001679 if (p_verbose >= 16)
1680 msg_verbose_cmd(0, *cmdlinep);
1681
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001682/*
1683 * 1. Skip comment lines and leading white space and colons.
1684 * 2. Handle command modifiers.
1685 */
1686 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001688 ea.cmdlinep = cmdlinep;
1689 ea.getline = fgetline;
1690 ea.cookie = cookie;
1691#ifdef FEAT_EVAL
1692 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001694 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001695 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001697 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699#ifdef FEAT_EVAL
1700 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1701 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1702#else
1703 ea.skip = (if_level > 0);
1704#endif
1705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001707 * 3. Skip over the range to find the command. Let "p" point to after it.
1708 *
1709 * We need the command to know what kind of range it uses.
1710 */
1711 cmd = ea.cmd;
1712 ea.cmd = skip_range(ea.cmd, NULL);
1713 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1714 ea.cmd = skipwhite(ea.cmd + 1);
1715 p = find_command(&ea, NULL);
1716
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001717#ifdef FEAT_EVAL
1718# ifdef FEAT_PROFILE
1719 // Count this line for profiling if skip is TRUE.
1720 if (do_profiling == PROF_YES
1721 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1722 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1723 {
1724 int skip = did_emsg || got_int || did_throw;
1725
1726 if (ea.cmdidx == CMD_catch)
1727 skip = !skip && !(cstack->cs_idx >= 0
1728 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1729 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1730 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1731 skip = skip || !(cstack->cs_idx >= 0
1732 && !(cstack->cs_flags[cstack->cs_idx]
1733 & (CSF_ACTIVE | CSF_TRUE)));
1734 else if (ea.cmdidx == CMD_finally)
1735 skip = FALSE;
1736 else if (ea.cmdidx != CMD_endif
1737 && ea.cmdidx != CMD_endfor
1738 && ea.cmdidx != CMD_endtry
1739 && ea.cmdidx != CMD_endwhile)
1740 skip = ea.skip;
1741
1742 if (!skip)
1743 {
1744 if (getline_equal(fgetline, cookie, get_func_line))
1745 func_line_exec(getline_cookie(fgetline, cookie));
1746 else if (getline_equal(fgetline, cookie, getsourceline))
1747 script_line_exec();
1748 }
1749 }
1750# endif
1751
1752 /* May go to debug mode. If this happens and the ">quit" debug command is
1753 * used, throw an interrupt exception and skip the next command. */
1754 dbg_check_breakpoint(&ea);
1755 if (!ea.skip && got_int)
1756 {
1757 ea.skip = TRUE;
1758 (void)do_intthrow(cstack);
1759 }
1760#endif
1761
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001762/*
1763 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 *
1765 * where 'addr' is:
1766 *
1767 * % (entire file)
1768 * $ [+-NUM]
1769 * 'x [+-NUM] (where x denotes a currently defined mark)
1770 * . [+-NUM]
1771 * [+-NUM]..
1772 * NUM
1773 *
1774 * The ea.cmd pointer is updated to point to the first character following the
1775 * range spec. If an initial address is found, but no second, the upper bound
1776 * is equal to the lower.
1777 */
1778
Bram Moolenaar25190db2019-05-04 15:05:28 +02001779 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001780 if (!IS_USER_CMDIDX(ea.cmdidx))
1781 {
1782 if (ea.cmdidx != CMD_SIZE)
1783 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1784 else
1785 ea.addr_type = ADDR_LINES;
1786
Bram Moolenaar25190db2019-05-04 15:05:28 +02001787 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001788 if (ea.cmdidx == CMD_wincmd && p != NULL)
1789 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001790#ifdef FEAT_QUICKFIX
1791 // :.cc in quickfix window uses line number
1792 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1793 ea.addr_type = ADDR_OTHER;
1794#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001795 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001796
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001797 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001798 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001799 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001802 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 */
1804
1805 /*
1806 * Skip ':' and any white space
1807 */
1808 ea.cmd = skipwhite(ea.cmd);
1809 while (*ea.cmd == ':')
1810 ea.cmd = skipwhite(ea.cmd + 1);
1811
1812 /*
1813 * If we got a line, but no command, then go to the line.
1814 * If we find a '|' or '\n' we set ea.nextcmd.
1815 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001816 if (*ea.cmd == NUL || *ea.cmd == '"'
1817 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
1819 /*
1820 * strange vi behaviour:
1821 * ":3" jumps to line 3
1822 * ":3|..." prints line 3
1823 * ":|" prints current line
1824 */
1825 if (ea.skip) /* skip this if inside :if */
1826 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001827 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001830 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 if ((errormsg = invalid_range(&ea)) == NULL)
1832 {
1833 correct_range(&ea);
1834 ex_print(&ea);
1835 }
1836 }
1837 else if (ea.addr_count != 0)
1838 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001839 if (ea.line2 > curbuf->b_ml.ml_line_count)
1840 {
1841 /* With '-' in 'cpoptions' a line number past the file is an
1842 * error, otherwise put it at the end of the file. */
1843 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1844 ea.line2 = -1;
1845 else
1846 ea.line2 = curbuf->b_ml.ml_line_count;
1847 }
1848
1849 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001850 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 else
1852 {
1853 if (ea.line2 == 0)
1854 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
1856 curwin->w_cursor.lnum = ea.line2;
1857 beginline(BL_SOL | BL_FIX);
1858 }
1859 }
1860 goto doend;
1861 }
1862
Bram Moolenaard5005162014-08-22 23:05:54 +02001863 /* If this looks like an undefined user command and there are CmdUndefined
1864 * autocommands defined, trigger the matching autocommands. */
1865 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1866 && ASCII_ISUPPER(*ea.cmd)
1867 && has_cmdundefined())
1868 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001869 int ret;
1870
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001871 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001872 while (ASCII_ISALNUM(*p))
1873 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001874 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001875 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1876 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001877 /* If the autocommands did something and didn't cause an error, try
1878 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001879 p = (ret
1880#ifdef FEAT_EVAL
1881 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001882#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001883 ) ? find_command(&ea, NULL) : ea.cmd;
1884 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (p == NULL)
1887 {
1888 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001889 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 goto doend;
1891 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001892 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001893 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1894 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 errormsg = uc_fun_cmd();
1897 goto doend;
1898 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (ea.cmdidx == CMD_SIZE)
1901 {
1902 if (!ea.skip)
1903 {
1904 STRCPY(IObuff, _("E492: Not an editor command"));
1905 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001906 {
1907 /* If the modifier was parsed OK the error must be in the
1908 * following command */
1909 if (after_modifier != NULL)
1910 append_command(after_modifier);
1911 else
1912 append_command(*cmdlinep);
1913 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001915 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917 goto doend;
1918 }
1919
Bram Moolenaar958636c2014-10-21 20:01:58 +02001920 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1921 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001922#ifdef HAVE_EX_SCRIPT_NI
1923 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1924#endif
1925 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
1927#ifndef FEAT_EVAL
1928 /*
1929 * When the expression evaluation is disabled, recognize the ":if" and
1930 * ":endif" commands and ignore everything in between it.
1931 */
1932 if (ea.cmdidx == CMD_if)
1933 ++if_level;
1934 if (if_level)
1935 {
1936 if (ea.cmdidx == CMD_endif)
1937 --if_level;
1938 goto doend;
1939 }
1940
1941#endif
1942
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001943 /* forced commands */
1944 if (*p == '!' && ea.cmdidx != CMD_substitute
1945 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 ++p;
1948 ea.forceit = TRUE;
1949 }
1950 else
1951 ea.forceit = FALSE;
1952
1953/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001954 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001956 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001957 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
1959 if (!ea.skip)
1960 {
1961#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001962 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001964 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001965 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 goto doend;
1967 }
1968#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001969 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001970 {
1971 errormsg = _("E981: Command not allowed in rvim");
1972 goto doend;
1973 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001974 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 {
1976 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001977 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 goto doend;
1979 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001980
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001981 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001982 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001984 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001985 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 goto doend;
1987 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001988
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001989 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001990 * Do allow ":checktime" (it is postponed).
1991 * Do allow ":edit" (check for an argument later).
1992 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001993 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001994 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001995 && ea.cmdidx != CMD_edit
1996 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001997 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001998 && curbuf_locked())
1999 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002001 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 {
2003 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002004 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 goto doend;
2006 }
2007 }
2008
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002009 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto doend;
2013 }
2014
2015 /*
2016 * Don't complain about the range if it is not used
2017 * (could happen if line_count is accidentally set to 0).
2018 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002019 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 {
2021 /*
2022 * If the range is backwards, ask for confirmation and, if given, swap
2023 * ea.line1 & ea.line2 so it's forwards again.
2024 * When global command is busy, don't ask, will fail below.
2025 */
2026 if (!global_busy && ea.line1 > ea.line2)
2027 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002028 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002030 if (sourcing || exmode_active)
2031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002033 goto doend;
2034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (ask_yesno((char_u *)
2036 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 lnum = ea.line1;
2040 ea.line1 = ea.line2;
2041 ea.line2 = lnum;
2042 }
2043 if ((errormsg = invalid_range(&ea)) != NULL)
2044 goto doend;
2045 }
2046
Bram Moolenaarb7316892019-05-01 18:08:42 +02002047 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2048 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 ea.line2 = 1;
2050
2051 correct_range(&ea);
2052
2053#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002054 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002055 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 /* Put the first line at the start of a closed fold, put the last line
2058 * at the end of a closed fold. */
2059 (void)hasFolding(ea.line1, &ea.line1, NULL);
2060 (void)hasFolding(ea.line2, NULL, &ea.line2);
2061 }
2062#endif
2063
2064#ifdef FEAT_QUICKFIX
2065 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002066 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 * option here, so things like % get expanded.
2068 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002069 p = replace_makeprg(&ea, p, cmdlinep);
2070 if (p == NULL)
2071 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072#endif
2073
2074 /*
2075 * Skip to start of argument.
2076 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2077 */
2078 if (ea.cmdidx == CMD_bang)
2079 ea.arg = p;
2080 else
2081 ea.arg = skipwhite(p);
2082
Bram Moolenaar379fb762018-08-30 15:58:28 +02002083 // ":file" cannot be run with an argument when "curbuf_lock" is set
2084 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2085 goto doend;
2086
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 /*
2088 * Check for "++opt=val" argument.
2089 * Must be first, allow ":w ++enc=utf8 !cmd"
2090 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002091 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2093 if (getargopt(&ea) == FAIL && !ni)
2094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002095 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 goto doend;
2097 }
2098
2099 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2100 {
2101 if (*ea.arg == '>') /* append */
2102 {
2103 if (*++ea.arg != '>') /* typed wrong */
2104 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002105 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 goto doend;
2107 }
2108 ea.arg = skipwhite(ea.arg + 1);
2109 ea.append = TRUE;
2110 }
2111 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2112 {
2113 ++ea.arg;
2114 ea.usefilter = TRUE;
2115 }
2116 }
2117
2118 if (ea.cmdidx == CMD_read)
2119 {
2120 if (ea.forceit)
2121 {
2122 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2123 ea.forceit = FALSE;
2124 }
2125 else if (*ea.arg == '!') /* :r !filter */
2126 {
2127 ++ea.arg;
2128 ea.usefilter = TRUE;
2129 }
2130 }
2131
2132 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2133 {
2134 ea.amount = 1;
2135 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2136 {
2137 ++ea.arg;
2138 ++ea.amount;
2139 }
2140 ea.arg = skipwhite(ea.arg);
2141 }
2142
2143 /*
2144 * Check for "+command" argument, before checking for next command.
2145 * Don't do this for ":read !cmd" and ":write !cmd".
2146 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002147 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2149
2150 /*
2151 * Check for '|' to separate commands and '"' to start comments.
2152 * Don't do this for ":read !cmd" and ":write !cmd".
2153 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002154 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 separate_nextcmd(&ea);
2156
2157 /*
2158 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002159 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2160 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002162 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002163 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002164 || ea.cmdidx == CMD_global
2165 || ea.cmdidx == CMD_vglobal
2166 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
2168 for (p = ea.arg; *p; ++p)
2169 {
2170 /* Remove one backslash before a newline, so that it's possible to
2171 * pass a newline to the shell and also a newline that is preceded
2172 * with a backslash. This makes it impossible to end a shell
2173 * command in a backslash, but that doesn't appear useful.
2174 * Halving the number of backslashes is incompatible with previous
2175 * versions. */
2176 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002177 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 else if (*p == '\n')
2179 {
2180 ea.nextcmd = p + 1;
2181 *p = NUL;
2182 break;
2183 }
2184 }
2185 }
2186
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002187 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002189 buf_T *buf;
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002192 switch (ea.addr_type)
2193 {
2194 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002195 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002196 ea.line2 = curbuf->b_ml.ml_line_count;
2197 break;
2198 case ADDR_LOADED_BUFFERS:
2199 buf = firstbuf;
2200 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2201 buf = buf->b_next;
2202 ea.line1 = buf->b_fnum;
2203 buf = lastbuf;
2204 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2205 buf = buf->b_prev;
2206 ea.line2 = buf->b_fnum;
2207 break;
2208 case ADDR_BUFFERS:
2209 ea.line1 = firstbuf->b_fnum;
2210 ea.line2 = lastbuf->b_fnum;
2211 break;
2212 case ADDR_WINDOWS:
2213 ea.line2 = LAST_WIN_NR;
2214 break;
2215 case ADDR_TABS:
2216 ea.line2 = LAST_TAB_NR;
2217 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002218 case ADDR_TABS_RELATIVE:
2219 ea.line2 = 1;
2220 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002221 case ADDR_ARGUMENTS:
2222 if (ARGCOUNT == 0)
2223 ea.line1 = ea.line2 = 0;
2224 else
2225 ea.line2 = ARGCOUNT;
2226 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002227 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002228#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002229 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002230 if (ea.line2 == 0)
2231 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002232#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002233 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002234 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002235 case ADDR_UNSIGNED:
2236 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002237 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002238 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 }
2241
2242 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002243 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002245 /* Do not allow register = for user commands */
2246 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002247 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002249#ifndef FEAT_CLIPBOARD
2250 /* check these explicitly for a more specific error message */
2251 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002253 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002254 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
2256#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002257 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2258 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002259 {
2260 ea.regname = *ea.arg++;
2261#ifdef FEAT_EVAL
2262 /* for '=' register: accept the rest of the line as an expression */
2263 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2264 {
2265 set_expr_line(vim_strsave(ea.arg));
2266 ea.arg += STRLEN(ea.arg);
2267 }
2268#endif
2269 ea.arg = skipwhite(ea.arg);
2270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272
2273 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002274 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 * count, it's a buffer name.
2276 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002277 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2278 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002279 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
2281 n = getdigits(&ea.arg);
2282 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002283 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002285 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 goto doend;
2287 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002288 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
2290 ea.line2 = n;
2291 if (ea.addr_count == 0)
2292 ea.addr_count = 1;
2293 }
2294 else
2295 {
2296 ea.line1 = ea.line2;
2297 ea.line2 += n - 1;
2298 ++ea.addr_count;
2299 /*
2300 * Be vi compatible: no error message for out of range.
2301 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002302 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 ea.line2 = curbuf->b_ml.ml_line_count;
2304 }
2305 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002306
2307 /*
2308 * Check for flags: 'l', 'p' and '#'.
2309 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002310 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2313 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002315 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002316 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 goto doend;
2318 }
2319
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002322 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 goto doend;
2324 }
2325
2326#ifdef FEAT_EVAL
2327 /*
2328 * Skip the command when it's not going to be executed.
2329 * The commands like :if, :endif, etc. always need to be executed.
2330 * Also make an exception for commands that handle a trailing command
2331 * themselves.
2332 */
2333 if (ea.skip)
2334 {
2335 switch (ea.cmdidx)
2336 {
2337 /* commands that need evaluation */
2338 case CMD_while:
2339 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002340 case CMD_for:
2341 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 case CMD_if:
2343 case CMD_elseif:
2344 case CMD_else:
2345 case CMD_endif:
2346 case CMD_try:
2347 case CMD_catch:
2348 case CMD_finally:
2349 case CMD_endtry:
2350 case CMD_function:
2351 break;
2352
2353 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002354 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 * the list at ":help :bar". */
2356 case CMD_aboveleft:
2357 case CMD_and:
2358 case CMD_belowright:
2359 case CMD_botright:
2360 case CMD_browse:
2361 case CMD_call:
2362 case CMD_confirm:
2363 case CMD_delfunction:
2364 case CMD_djump:
2365 case CMD_dlist:
2366 case CMD_dsearch:
2367 case CMD_dsplit:
2368 case CMD_echo:
2369 case CMD_echoerr:
2370 case CMD_echomsg:
2371 case CMD_echon:
2372 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002373 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 case CMD_help:
2375 case CMD_hide:
2376 case CMD_ijump:
2377 case CMD_ilist:
2378 case CMD_isearch:
2379 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002380 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 case CMD_keepjumps:
2382 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002383 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 case CMD_leftabove:
2385 case CMD_let:
2386 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002387 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002389 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002390 case CMD_noautocmd:
2391 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 case CMD_perl:
2393 case CMD_psearch:
2394 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002395 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002396 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 case CMD_return:
2398 case CMD_rightbelow:
2399 case CMD_ruby:
2400 case CMD_silent:
2401 case CMD_smagic:
2402 case CMD_snomagic:
2403 case CMD_substitute:
2404 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002405 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_tcl:
2407 case CMD_throw:
2408 case CMD_tilde:
2409 case CMD_topleft:
2410 case CMD_unlet:
2411 case CMD_verbose:
2412 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002413 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 break;
2415
2416 default: goto doend;
2417 }
2418 }
2419#endif
2420
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002421 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
2423 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2424 goto doend;
2425 }
2426
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /*
2428 * Accept buffer name. Cannot be used at the same time with a buffer
2429 * number. Don't do this for a user command.
2430 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002431 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002432 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 /*
2435 * :bdelete, :bwipeout and :bunload take several arguments, separated
2436 * by spaces: find next space (skipping over escaped characters).
2437 * The others take one argument: ignore trailing spaces.
2438 */
2439 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2440 || ea.cmdidx == CMD_bunload)
2441 p = skiptowhite_esc(ea.arg);
2442 else
2443 {
2444 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002445 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 --p;
2447 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002448 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002449 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 if (ea.line2 < 0) /* failed */
2451 goto doend;
2452 ea.addr_count = 1;
2453 ea.arg = skipwhite(p);
2454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
Bram Moolenaar2be57332018-02-13 18:05:18 +01002456 /* The :try command saves the emsg_silent flag, reset it here when
2457 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002458 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002459 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002460 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002461 if (emsg_silent < 0)
2462 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002463 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002464 }
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002467 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar958636c2014-10-21 20:01:58 +02002470 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 /*
2473 * Execute a user-defined command.
2474 */
2475 do_ucmd(&ea);
2476 }
2477 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002480 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
2482 ea.errmsg = NULL;
2483 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2484 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002485 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487
2488#ifdef FEAT_EVAL
2489 /*
2490 * If the command just executed called do_cmdline(), any throw or ":return"
2491 * or ":finish" encountered there must also check the cstack of the still
2492 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2493 * exception, or reanimate a returned function or finished script file and
2494 * return or finish it again.
2495 */
2496 if (need_rethrow)
2497 do_throw(cstack);
2498 else if (check_cstack)
2499 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002500 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002502 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 && current_func_returned())
2504 do_return(&ea, TRUE, FALSE, NULL);
2505 }
2506 need_rethrow = check_cstack = FALSE;
2507#endif
2508
2509doend:
2510 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002513 curwin->w_cursor.col = 0;
2514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2517 {
2518 if (sourcing)
2519 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002520 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002523 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002525 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
2527 emsg(errormsg);
2528 }
2529#ifdef FEAT_EVAL
2530 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002531 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2532 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#endif
2534
Bram Moolenaareffed932018-08-14 13:38:17 +02002535 if (ea.verbose_save >= 0)
2536 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002537
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002538 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002540 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaareffed932018-08-14 13:38:17 +02002542 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
2544 /* messages could be enabled for a serious error, need to check if the
2545 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002546 if (!did_emsg || msg_silent > ea.save_msg_silent)
2547 msg_silent = ea.save_msg_silent;
2548 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (emsg_silent < 0)
2550 emsg_silent = 0;
2551 /* Restore msg_scroll, it's set by file I/O commands, even when no
2552 * message is actually displayed. */
2553 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002554
2555 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2556 * somewhere in the line. Put it back in the first column. */
2557 if (redirecting())
2558 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002561#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002562 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002563 --sandbox;
2564#endif
2565
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2567 ea.nextcmd = NULL;
2568
2569#ifdef FEAT_EVAL
2570 --ex_nesting_level;
2571#endif
2572
2573 return ea.nextcmd;
2574}
2575#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002576 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577#endif
2578
2579/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002580 * Parse and skip over command modifiers:
2581 * - update eap->cmd
2582 * - store flags in "cmdmod".
2583 * - Set ex_pressedreturn for an empty command line.
2584 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002585 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002586 * - set p_verbose for ":verbose"
2587 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002588 * When "skip_only" is TRUE the global variables are not changed, except for
2589 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002590 * Return FAIL when the command is not to be executed.
2591 * May set "errormsg" to an error message.
2592 */
2593 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002594parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002595{
2596 char_u *p;
2597
2598 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2599 eap->verbose_save = -1;
2600 eap->save_msg_silent = -1;
2601
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002602 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 for (;;)
2604 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002605 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2606 ++eap->cmd;
2607
2608 /* in ex mode, an empty line works like :+ */
2609 if (*eap->cmd == NUL && exmode_active
2610 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2611 || getline_equal(eap->getline, eap->cookie, getexline))
2612 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2613 {
2614 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002615 if (!skip_only)
2616 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002617 }
2618
2619 /* ignore comment and empty lines */
2620 if (*eap->cmd == '"')
2621 return FAIL;
2622 if (*eap->cmd == NUL)
2623 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002624 if (!skip_only)
2625 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002626 return FAIL;
2627 }
2628
Bram Moolenaareffed932018-08-14 13:38:17 +02002629 p = skip_range(eap->cmd, NULL);
2630 switch (*p)
2631 {
2632 /* When adding an entry, also modify cmd_exists(). */
2633 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2634 break;
2635 cmdmod.split |= WSP_ABOVE;
2636 continue;
2637
2638 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2639 {
2640 cmdmod.split |= WSP_BELOW;
2641 continue;
2642 }
2643 if (checkforcmd(&eap->cmd, "browse", 3))
2644 {
2645#ifdef FEAT_BROWSE_CMD
2646 cmdmod.browse = TRUE;
2647#endif
2648 continue;
2649 }
2650 if (!checkforcmd(&eap->cmd, "botright", 2))
2651 break;
2652 cmdmod.split |= WSP_BOT;
2653 continue;
2654
2655 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2656 break;
2657#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2658 cmdmod.confirm = TRUE;
2659#endif
2660 continue;
2661
2662 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2663 {
2664 cmdmod.keepmarks = TRUE;
2665 continue;
2666 }
2667 if (checkforcmd(&eap->cmd, "keepalt", 5))
2668 {
2669 cmdmod.keepalt = TRUE;
2670 continue;
2671 }
2672 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2673 {
2674 cmdmod.keeppatterns = TRUE;
2675 continue;
2676 }
2677 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2678 break;
2679 cmdmod.keepjumps = TRUE;
2680 continue;
2681
2682 case 'f': /* only accept ":filter {pat} cmd" */
2683 {
2684 char_u *reg_pat;
2685
2686 if (!checkforcmd(&p, "filter", 4)
2687 || *p == NUL || ends_excmd(*p))
2688 break;
2689 if (*p == '!')
2690 {
2691 cmdmod.filter_force = TRUE;
2692 p = skipwhite(p + 1);
2693 if (*p == NUL || ends_excmd(*p))
2694 break;
2695 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002696 if (skip_only)
2697 p = skip_vimgrep_pat(p, NULL, NULL);
2698 else
2699 // NOTE: This puts a NUL after the pattern.
2700 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002701 if (p == NULL || *p == NUL)
2702 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002703 if (!skip_only)
2704 {
2705 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002706 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002707 if (cmdmod.filter_regmatch.regprog == NULL)
2708 break;
2709 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002710 eap->cmd = p;
2711 continue;
2712 }
2713
2714 /* ":hide" and ":hide | cmd" are not modifiers */
2715 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2716 || *p == NUL || ends_excmd(*p))
2717 break;
2718 eap->cmd = p;
2719 cmdmod.hide = TRUE;
2720 continue;
2721
2722 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2723 {
2724 cmdmod.lockmarks = TRUE;
2725 continue;
2726 }
2727
2728 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2729 break;
2730 cmdmod.split |= WSP_ABOVE;
2731 continue;
2732
2733 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2734 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002735 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002736 {
2737 /* Set 'eventignore' to "all". Restore the
2738 * existing option value later. */
2739 cmdmod.save_ei = vim_strsave(p_ei);
2740 set_string_option_direct((char_u *)"ei", -1,
2741 (char_u *)"all", OPT_FREE, SID_NONE);
2742 }
2743 continue;
2744 }
2745 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2746 break;
2747 cmdmod.noswapfile = TRUE;
2748 continue;
2749
2750 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2751 break;
2752 cmdmod.split |= WSP_BELOW;
2753 continue;
2754
2755 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2756 {
2757#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002758 if (!skip_only)
2759 {
2760 if (!eap->did_sandbox)
2761 ++sandbox;
2762 eap->did_sandbox = TRUE;
2763 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002764#endif
2765 continue;
2766 }
2767 if (!checkforcmd(&eap->cmd, "silent", 3))
2768 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002769 if (!skip_only)
2770 {
2771 if (eap->save_msg_silent == -1)
2772 eap->save_msg_silent = msg_silent;
2773 ++msg_silent;
2774 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2776 {
2777 /* ":silent!", but not "silent !cmd" */
2778 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002779 if (!skip_only)
2780 {
2781 ++emsg_silent;
2782 ++eap->did_esilent;
2783 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 }
2785 continue;
2786
2787 case 't': if (checkforcmd(&p, "tab", 3))
2788 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002789 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002791 long tabnr = get_address(eap, &eap->cmd,
2792 ADDR_TABS, eap->skip,
2793 skip_only, FALSE, 1);
2794 if (tabnr == MAXLNUM)
2795 cmdmod.tab = tabpage_index(curtab) + 1;
2796 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002798 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002800 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002801 return FAIL;
2802 }
2803 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 }
2806 eap->cmd = p;
2807 continue;
2808 }
2809 if (!checkforcmd(&eap->cmd, "topleft", 2))
2810 break;
2811 cmdmod.split |= WSP_TOP;
2812 continue;
2813
2814 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2815 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002816 if (!skip_only)
2817 {
2818 if (eap->save_msg_silent == -1)
2819 eap->save_msg_silent = msg_silent;
2820 msg_silent = 0;
2821 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 continue;
2823
2824 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2825 {
2826 cmdmod.split |= WSP_VERT;
2827 continue;
2828 }
2829 if (!checkforcmd(&p, "verbose", 4))
2830 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (!skip_only)
2832 {
2833 if (eap->verbose_save < 0)
2834 eap->verbose_save = p_verbose;
2835 if (vim_isdigit(*eap->cmd))
2836 p_verbose = atoi((char *)eap->cmd);
2837 else
2838 p_verbose = 1;
2839 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 eap->cmd = p;
2841 continue;
2842 }
2843 break;
2844 }
2845
2846 return OK;
2847}
2848
2849/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002850 * Free contents of "cmdmod".
2851 */
2852 static void
2853free_cmdmod(void)
2854{
2855 if (cmdmod.save_ei != NULL)
2856 {
2857 /* Restore 'eventignore' to the value before ":noautocmd". */
2858 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2859 OPT_FREE, SID_NONE);
2860 free_string_option(cmdmod.save_ei);
2861 }
2862
2863 if (cmdmod.filter_regmatch.regprog != NULL)
2864 vim_regfree(cmdmod.filter_regmatch.regprog);
2865}
2866
2867/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002868 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002869 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002870 * Return FAIL and set "errormsg" or return OK.
2871 */
2872 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002873parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002874{
2875 int address_count = 1;
2876 linenr_T lnum;
2877
2878 // Repeat for all ',' or ';' separated addresses.
2879 for (;;)
2880 {
2881 eap->line1 = eap->line2;
2882 switch (eap->addr_type)
2883 {
2884 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002885 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002886 // default is current line number
2887 eap->line2 = curwin->w_cursor.lnum;
2888 break;
2889 case ADDR_WINDOWS:
2890 eap->line2 = CURRENT_WIN_NR;
2891 break;
2892 case ADDR_ARGUMENTS:
2893 eap->line2 = curwin->w_arg_idx + 1;
2894 if (eap->line2 > ARGCOUNT)
2895 eap->line2 = ARGCOUNT;
2896 break;
2897 case ADDR_LOADED_BUFFERS:
2898 case ADDR_BUFFERS:
2899 eap->line2 = curbuf->b_fnum;
2900 break;
2901 case ADDR_TABS:
2902 eap->line2 = CURRENT_TAB_NR;
2903 break;
2904 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002905 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002906 eap->line2 = 1;
2907 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002908 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002909#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002910 eap->line2 = qf_get_cur_idx(eap);
2911#endif
2912 break;
2913 case ADDR_QUICKFIX_VALID:
2914#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002916#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002917 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002918 case ADDR_NONE:
2919 // Will give an error later if a range is found.
2920 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002921 }
2922 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002923 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002924 eap->addr_count == 0, address_count++);
2925 if (eap->cmd == NULL) // error detected
2926 return FAIL;
2927 if (lnum == MAXLNUM)
2928 {
2929 if (*eap->cmd == '%') // '%' - all lines
2930 {
2931 ++eap->cmd;
2932 switch (eap->addr_type)
2933 {
2934 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002935 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002936 eap->line1 = 1;
2937 eap->line2 = curbuf->b_ml.ml_line_count;
2938 break;
2939 case ADDR_LOADED_BUFFERS:
2940 {
2941 buf_T *buf = firstbuf;
2942
2943 while (buf->b_next != NULL
2944 && buf->b_ml.ml_mfp == NULL)
2945 buf = buf->b_next;
2946 eap->line1 = buf->b_fnum;
2947 buf = lastbuf;
2948 while (buf->b_prev != NULL
2949 && buf->b_ml.ml_mfp == NULL)
2950 buf = buf->b_prev;
2951 eap->line2 = buf->b_fnum;
2952 break;
2953 }
2954 case ADDR_BUFFERS:
2955 eap->line1 = firstbuf->b_fnum;
2956 eap->line2 = lastbuf->b_fnum;
2957 break;
2958 case ADDR_WINDOWS:
2959 case ADDR_TABS:
2960 if (IS_USER_CMDIDX(eap->cmdidx))
2961 {
2962 eap->line1 = 1;
2963 eap->line2 = eap->addr_type == ADDR_WINDOWS
2964 ? LAST_WIN_NR : LAST_TAB_NR;
2965 }
2966 else
2967 {
2968 // there is no Vim command which uses '%' and
2969 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002970 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002971 return FAIL;
2972 }
2973 break;
2974 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002975 case ADDR_UNSIGNED:
2976 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002977 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002978 return FAIL;
2979 case ADDR_ARGUMENTS:
2980 if (ARGCOUNT == 0)
2981 eap->line1 = eap->line2 = 0;
2982 else
2983 {
2984 eap->line1 = 1;
2985 eap->line2 = ARGCOUNT;
2986 }
2987 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002988 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002989#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002990 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002991 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002992 if (eap->line2 == 0)
2993 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002994#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002995 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002996 case ADDR_NONE:
2997 // Will give an error later if a range is found.
2998 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002999 }
3000 ++eap->addr_count;
3001 }
3002 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3003 {
3004 pos_T *fp;
3005
3006 // '*' - visual area
3007 if (eap->addr_type != ADDR_LINES)
3008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003009 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003010 return FAIL;
3011 }
3012
3013 ++eap->cmd;
3014 if (!eap->skip)
3015 {
3016 fp = getmark('<', FALSE);
3017 if (check_mark(fp) == FAIL)
3018 return FAIL;
3019 eap->line1 = fp->lnum;
3020 fp = getmark('>', FALSE);
3021 if (check_mark(fp) == FAIL)
3022 return FAIL;
3023 eap->line2 = fp->lnum;
3024 ++eap->addr_count;
3025 }
3026 }
3027 }
3028 else
3029 eap->line2 = lnum;
3030 eap->addr_count++;
3031
3032 if (*eap->cmd == ';')
3033 {
3034 if (!eap->skip)
3035 {
3036 curwin->w_cursor.lnum = eap->line2;
3037 // don't leave the cursor on an illegal line or column
3038 check_cursor();
3039 }
3040 }
3041 else if (*eap->cmd != ',')
3042 break;
3043 ++eap->cmd;
3044 }
3045
3046 // One address given: set start and end lines.
3047 if (eap->addr_count == 1)
3048 {
3049 eap->line1 = eap->line2;
3050 // ... but only implicit: really no address given
3051 if (lnum == MAXLNUM)
3052 eap->addr_count = 0;
3053 }
3054 return OK;
3055}
3056
3057/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003058 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 * If there is a match advance "pp" to the argument and return TRUE.
3060 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003061 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003062checkforcmd(
3063 char_u **pp, /* start of command */
3064 char *cmd, /* name of command */
3065 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 int i;
3068
3069 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003070 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 break;
3072 if (i >= len && !isalpha((*pp)[i]))
3073 {
3074 *pp = skipwhite(*pp + i);
3075 return TRUE;
3076 }
3077 return FALSE;
3078}
3079
3080/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003081 * Append "cmd" to the error message in IObuff.
3082 * Takes care of limiting the length and handling 0xa0, which would be
3083 * invisible otherwise.
3084 */
3085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003086append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003087{
3088 char_u *s = cmd;
3089 char_u *d;
3090
3091 STRCAT(IObuff, ": ");
3092 d = IObuff + STRLEN(IObuff);
3093 while (*s != NUL && d - IObuff < IOSIZE - 7)
3094 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003095 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003096 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003097 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003098 STRCPY(d, "<a0>");
3099 d += 4;
3100 }
3101 else
3102 MB_COPY_CHAR(s, d);
3103 }
3104 *d = NUL;
3105}
3106
3107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003109 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003110 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003111 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 * Returns NULL for an ambiguous user command.
3113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003115find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 int len;
3118 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003119 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 /*
3122 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003123 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 * - the 'k' command can directly be followed by any character.
3125 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003126 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003128 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 */
3130 p = eap->cmd;
3131 if (*p == 'k')
3132 {
3133 eap->cmdidx = CMD_k;
3134 ++p;
3135 }
3136 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003137 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3138 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 || p[1] == 'g'
3140 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3141 || p[1] == 'I'
3142 || (p[1] == 'r' && p[2] != 'e')))
3143 {
3144 eap->cmdidx = CMD_substitute;
3145 ++p;
3146 }
3147 else
3148 {
3149 while (ASCII_ISALPHA(*p))
3150 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003151 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003152 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003153 while (ASCII_ISALNUM(*p))
3154 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003155
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 /* check for non-alpha command */
3157 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3158 ++p;
3159 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003160 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3161 {
3162 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3163 * :delete with the 'l' flag. Same for 'p'. */
3164 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003165 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003166 break;
3167 if (i == len - 1)
3168 {
3169 --len;
3170 if (p[-1] == 'l')
3171 eap->flags |= EXFLAG_LIST;
3172 else
3173 eap->flags |= EXFLAG_PRINT;
3174 }
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003177 if (ASCII_ISLOWER(eap->cmd[0]))
3178 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003179 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003180 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003181
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003182 if (command_count != (int)CMD_SIZE)
3183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003184 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003185 getout(1);
3186 }
3187
3188 /* Use a precomputed index for fast look-up in cmdnames[]
3189 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003190 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3191 if (ASCII_ISLOWER(c2))
3192 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003195 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3198 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3199 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3200 (size_t)len) == 0)
3201 {
3202#ifdef FEAT_EVAL
3203 if (full != NULL
3204 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3205 *full = TRUE;
3206#endif
3207 break;
3208 }
3209
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003210 // Look for a user defined command as a last resort. Let ":Print" be
3211 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003212 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3213 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003215 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 while (ASCII_ISALNUM(*p))
3217 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003218 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 eap->cmdidx = CMD_SIZE;
3222 }
3223
3224 return p;
3225}
3226
3227#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003228static struct cmdmod
3229{
3230 char *name;
3231 int minlen;
3232 int has_count; /* :123verbose :3tab */
3233} cmdmods[] = {
3234 {"aboveleft", 3, FALSE},
3235 {"belowright", 3, FALSE},
3236 {"botright", 2, FALSE},
3237 {"browse", 3, FALSE},
3238 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003239 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003240 {"hide", 3, FALSE},
3241 {"keepalt", 5, FALSE},
3242 {"keepjumps", 5, FALSE},
3243 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003244 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003245 {"leftabove", 5, FALSE},
3246 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003247 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003248 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003249 {"rightbelow", 6, FALSE},
3250 {"sandbox", 3, FALSE},
3251 {"silent", 3, FALSE},
3252 {"tab", 3, TRUE},
3253 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003254 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003255 {"verbose", 4, TRUE},
3256 {"vertical", 4, FALSE},
3257};
3258
3259/*
3260 * Return length of a command modifier (including optional count).
3261 * Return zero when it's not a modifier.
3262 */
3263 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003264modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003265{
3266 int i, j;
3267 char_u *p = cmd;
3268
3269 if (VIM_ISDIGIT(*cmd))
3270 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003271 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003272 {
3273 for (j = 0; p[j] != NUL; ++j)
3274 if (p[j] != cmdmods[i].name[j])
3275 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003276 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003277 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003278 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003279 }
3280 return 0;
3281}
3282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283/*
3284 * Return > 0 if an Ex command "name" exists.
3285 * Return 2 if there is an exact match.
3286 * Return 3 if there is an ambiguous match.
3287 */
3288 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003289cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 exarg_T ea;
3292 int full = FALSE;
3293 int i;
3294 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003295 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
3297 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003298 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 for (j = 0; name[j] != NUL; ++j)
3301 if (name[j] != cmdmods[i].name[j])
3302 break;
3303 if (name[j] == NUL && j >= cmdmods[i].minlen)
3304 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3305 }
3306
Bram Moolenaara9587612006-05-04 21:47:50 +00003307 /* Check built-in commands and user defined commands.
3308 * For ":2match" and ":3match" we need to skip the number. */
3309 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003311 p = find_command(&ea, &full);
3312 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003314 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3315 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003316 if (*skipwhite(p) != NUL)
3317 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3319}
3320#endif
3321
Bram Moolenaard0190392019-08-23 21:17:35 +02003322 cmdidx_T
3323excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
Bram Moolenaard0190392019-08-23 21:17:35 +02003325 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaard0190392019-08-23 21:17:35 +02003327 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3328 idx = (cmdidx_T)((int)idx + 1))
3329 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 break;
3331
Bram Moolenaard0190392019-08-23 21:17:35 +02003332 return idx;
3333}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
Bram Moolenaard0190392019-08-23 21:17:35 +02003335 long
3336excmd_get_argt(cmdidx_T idx)
3337{
3338 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339}
3340
3341/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003342 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 *
3344 * Backslashed delimiters after / or ? will be skipped, and commands will
3345 * not be expanded between /'s and ?'s or after "'".
3346 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003347 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 * Returns the "cmd" pointer advanced to beyond the range.
3349 */
3350 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003351skip_range(
3352 char_u *cmd,
3353 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003355 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003357 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003359 if (*cmd == '\\')
3360 {
3361 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3362 ++cmd;
3363 else
3364 break;
3365 }
3366 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 if (*++cmd == NUL && ctx != NULL)
3369 *ctx = EXPAND_NOTHING;
3370 }
3371 else if (*cmd == '/' || *cmd == '?')
3372 {
3373 delim = *cmd++;
3374 while (*cmd != NUL && *cmd != delim)
3375 if (*cmd++ == '\\' && *cmd != NUL)
3376 ++cmd;
3377 if (*cmd == NUL && ctx != NULL)
3378 *ctx = EXPAND_NOTHING;
3379 }
3380 if (*cmd != NUL)
3381 ++cmd;
3382 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003383
3384 /* Skip ":" and white space. */
3385 while (*cmd == ':')
3386 cmd = skipwhite(cmd + 1);
3387
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return cmd;
3389}
3390
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003391 static void
3392addr_error(cmd_addr_T addr_type)
3393{
3394 if (addr_type == ADDR_NONE)
3395 emsg(_(e_norange));
3396 else
3397 emsg(_(e_invrange));
3398}
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003401 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 *
3403 * Set ptr to the next character after the part that was interpreted.
3404 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003405 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 *
3407 * Return MAXLNUM when no Ex address was found.
3408 */
3409 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003410get_address(
3411 exarg_T *eap UNUSED,
3412 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02003413 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003414 int skip, // only skip the address, don't use it
3415 int silent, // no errors or side effects
3416 int to_other_file, // flag: may jump to other file
3417 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
Bram Moolenaarb7316892019-05-01 18:08:42 +02003419 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 int c;
3421 int i;
3422 long n;
3423 char_u *cmd;
3424 pos_T pos;
3425 pos_T *fp;
3426 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003427 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
3429 cmd = skipwhite(*ptr);
3430 lnum = MAXLNUM;
3431 do
3432 {
3433 switch (*cmd)
3434 {
3435 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003436 ++cmd;
3437 switch (addr_type)
3438 {
3439 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003440 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 lnum = curwin->w_cursor.lnum;
3442 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003443 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003444 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003445 break;
3446 case ADDR_ARGUMENTS:
3447 lnum = curwin->w_arg_idx + 1;
3448 break;
3449 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003450 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003451 lnum = curbuf->b_fnum;
3452 break;
3453 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003454 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003455 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003456 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003457 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003458 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003459 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003460 cmd = NULL;
3461 goto error;
3462 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003463 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003464#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003465 lnum = qf_get_cur_idx(eap);
3466#endif
3467 break;
3468 case ADDR_QUICKFIX_VALID:
3469#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003470 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003471#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003472 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003473 }
3474 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003477 ++cmd;
3478 switch (addr_type)
3479 {
3480 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003481 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 lnum = curbuf->b_ml.ml_line_count;
3483 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003484 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003485 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003486 break;
3487 case ADDR_ARGUMENTS:
3488 lnum = ARGCOUNT;
3489 break;
3490 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003491 buf = lastbuf;
3492 while (buf->b_ml.ml_mfp == NULL)
3493 {
3494 if (buf->b_prev == NULL)
3495 break;
3496 buf = buf->b_prev;
3497 }
3498 lnum = buf->b_fnum;
3499 break;
3500 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003501 lnum = lastbuf->b_fnum;
3502 break;
3503 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003504 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003505 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003506 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003507 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003508 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003509 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003510 cmd = NULL;
3511 goto error;
3512 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003513 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003514#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003515 lnum = qf_get_size(eap);
3516 if (lnum == 0)
3517 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003518#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003519 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003520 case ADDR_QUICKFIX_VALID:
3521#ifdef FEAT_QUICKFIX
3522 lnum = qf_get_valid_size(eap);
3523 if (lnum == 0)
3524 lnum = 1;
3525#endif
3526 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003527 }
3528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003531 if (*++cmd == NUL)
3532 {
3533 cmd = NULL;
3534 goto error;
3535 }
3536 if (addr_type != ADDR_LINES)
3537 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003538 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003539 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003540 goto error;
3541 }
3542 if (skip)
3543 ++cmd;
3544 else
3545 {
3546 /* Only accept a mark in another file when it is
3547 * used by itself: ":'M". */
3548 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3549 ++cmd;
3550 if (fp == (pos_T *)-1)
3551 /* Jumped to another file. */
3552 lnum = curwin->w_cursor.lnum;
3553 else
3554 {
3555 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
3557 cmd = NULL;
3558 goto error;
3559 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003560 lnum = fp->lnum;
3561 }
3562 }
3563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 case '/':
3566 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003567 c = *cmd++;
3568 if (addr_type != ADDR_LINES)
3569 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003570 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003571 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003572 goto error;
3573 }
3574 if (skip) /* skip "/pat/" */
3575 {
3576 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3577 if (*cmd == c)
3578 ++cmd;
3579 }
3580 else
3581 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003582 int flags;
3583
3584 pos = curwin->w_cursor; // save curwin->w_cursor
3585
3586 // When '/' or '?' follows another address, start from
3587 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003588 if (lnum != MAXLNUM)
3589 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003590
3591 // Start a forward search at the end of the line (unless
3592 // before the first line).
3593 // Start a backward search at the start of the line.
3594 // This makes sure we never match in the current
3595 // line, and can match anywhere in the
3596 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003597 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003598 curwin->w_cursor.col = MAXCOL;
3599 else
3600 curwin->w_cursor.col = 0;
3601 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003602 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003603 if (!do_search(NULL, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003604 {
3605 curwin->w_cursor = pos;
3606 cmd = NULL;
3607 goto error;
3608 }
3609 lnum = curwin->w_cursor.lnum;
3610 curwin->w_cursor = pos;
3611 /* adjust command string pointer */
3612 cmd += searchcmdlen;
3613 }
3614 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003617 ++cmd;
3618 if (addr_type != ADDR_LINES)
3619 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003620 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003621 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003622 goto error;
3623 }
3624 if (*cmd == '&')
3625 i = RE_SUBST;
3626 else if (*cmd == '?' || *cmd == '/')
3627 i = RE_SEARCH;
3628 else
3629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003630 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003631 cmd = NULL;
3632 goto error;
3633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003635 if (!skip)
3636 {
3637 /*
3638 * When search follows another address, start from
3639 * there.
3640 */
3641 if (lnum != MAXLNUM)
3642 pos.lnum = lnum;
3643 else
3644 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003646 /*
3647 * Start the search just like for the above
3648 * do_search().
3649 */
3650 if (*cmd != '?')
3651 pos.col = MAXCOL;
3652 else
3653 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003654 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003655 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003656 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003657 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003658 lnum = pos.lnum;
3659 else
3660 {
3661 cmd = NULL;
3662 goto error;
3663 }
3664 }
3665 ++cmd;
3666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003669 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3670 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
3672
3673 for (;;)
3674 {
3675 cmd = skipwhite(cmd);
3676 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3677 break;
3678
3679 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003680 {
3681 switch (addr_type)
3682 {
3683 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003684 case ADDR_OTHER:
3685 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003686 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003687 break;
3688 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003689 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003690 break;
3691 case ADDR_ARGUMENTS:
3692 lnum = curwin->w_arg_idx + 1;
3693 break;
3694 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003695 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003696 lnum = curbuf->b_fnum;
3697 break;
3698 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003699 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003700 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003701 case ADDR_TABS_RELATIVE:
3702 lnum = 1;
3703 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003704 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003705#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003706 lnum = qf_get_cur_idx(eap);
3707#endif
3708 break;
3709 case ADDR_QUICKFIX_VALID:
3710#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003711 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003712#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003713 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003714 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003715 case ADDR_UNSIGNED:
3716 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003717 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003718 }
3719 }
3720
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 if (VIM_ISDIGIT(*cmd))
3722 i = '+'; /* "number" is same as "+number" */
3723 else
3724 i = *cmd++;
3725 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3726 n = 1;
3727 else
3728 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003729
3730 if (addr_type == ADDR_TABS_RELATIVE)
3731 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003732 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003733 cmd = NULL;
3734 goto error;
3735 }
3736 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003737 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003738 lnum = compute_buffer_local_count(
3739 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003741 {
3742#ifdef FEAT_FOLDING
3743 /* Relative line addressing, need to adjust for folded lines
3744 * now, but only do it after the first address. */
3745 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3746 && address_count >= 2)
3747 (void)hasFolding(lnum, NULL, &lnum);
3748#endif
3749 if (i == '-')
3750 lnum -= n;
3751 else
3752 lnum += n;
3753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 } while (*cmd == '/' || *cmd == '?');
3756
3757error:
3758 *ptr = cmd;
3759 return lnum;
3760}
3761
3762/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003763 * Get flags from an Ex command argument.
3764 */
3765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003766get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003767{
3768 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3769 {
3770 if (*eap->arg == 'l')
3771 eap->flags |= EXFLAG_LIST;
3772 else if (*eap->arg == 'p')
3773 eap->flags |= EXFLAG_PRINT;
3774 else
3775 eap->flags |= EXFLAG_NR;
3776 eap->arg = skipwhite(eap->arg + 1);
3777 }
3778}
3779
3780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 * Function called for command which is Not Implemented. NI!
3782 */
3783 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003784ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785{
3786 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003787 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788}
3789
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003790#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791/*
3792 * Function called for script command which is Not Implemented. NI!
3793 * Skips over ":perl <<EOF" constructs.
3794 */
3795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003796ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
3798 if (!eap->skip)
3799 ex_ni(eap);
3800 else
3801 vim_free(script_get(eap, eap->arg));
3802}
3803#endif
3804
3805/*
3806 * Check range in Ex command for validity.
3807 * Return NULL when valid, error message when invalid.
3808 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003809 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003810invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003812 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if ( eap->line1 < 0
3815 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003816 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003817 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003818
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003819 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003820 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003821 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003822 {
3823 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003824 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003825#ifdef FEAT_DIFF
3826 + (eap->cmdidx == CMD_diffget)
3827#endif
3828 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003829 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003830 break;
3831 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003832 /* add 1 if ARGCOUNT is 0 */
3833 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003834 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003835 break;
3836 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003837 // Only a boundary check, not whether the buffers actually
3838 // exist.
3839 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003840 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003841 break;
3842 case ADDR_LOADED_BUFFERS:
3843 buf = firstbuf;
3844 while (buf->b_ml.ml_mfp == NULL)
3845 {
3846 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003847 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003848 buf = buf->b_next;
3849 }
3850 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003851 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003852 buf = lastbuf;
3853 while (buf->b_ml.ml_mfp == NULL)
3854 {
3855 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003856 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003857 buf = buf->b_prev;
3858 }
3859 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003860 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003861 break;
3862 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003863 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003864 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003865 break;
3866 case ADDR_TABS:
3867 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003868 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003869 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003870 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003871 case ADDR_OTHER:
3872 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003873 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003874 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003875#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003876 // No error for value that is too big, will use the last entry.
3877 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003878 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003879#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003880 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003881 case ADDR_QUICKFIX_VALID:
3882#ifdef FEAT_QUICKFIX
3883 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3884 || eap->line2 < 0)
3885 return _(e_invrange);
3886#endif
3887 break;
3888 case ADDR_UNSIGNED:
3889 if (eap->line2 < 0)
3890 return _(e_invrange);
3891 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003892 case ADDR_NONE:
3893 // Will give an error elsewhere.
3894 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003895 }
3896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 return NULL;
3898}
3899
3900/*
3901 * Correct the range for zero line number, if required.
3902 */
3903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003904correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003906 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
3908 if (eap->line1 == 0)
3909 eap->line1 = 1;
3910 if (eap->line2 == 0)
3911 eap->line2 = 1;
3912 }
3913}
3914
Bram Moolenaar748bf032005-02-02 23:04:36 +00003915#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003916/*
3917 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3918 * pattern. Otherwise return eap->arg.
3919 */
3920 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003921skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003922{
3923 char_u *p = eap->arg;
3924
Bram Moolenaara37420f2006-02-04 22:37:47 +00003925 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
3926 || eap->cmdidx == CMD_vimgrepadd
3927 || eap->cmdidx == CMD_lvimgrepadd
3928 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00003929 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003930 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003931 if (p == NULL)
3932 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003933 }
3934 return p;
3935}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003936
3937/*
3938 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
3939 * in the command line, so that things like % get expanded.
3940 */
3941 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003942replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003943{
3944 char_u *new_cmdline;
3945 char_u *program;
3946 char_u *pos;
3947 char_u *ptr;
3948 int len;
3949 int i;
3950
3951 /*
3952 * Don't do it when ":vimgrep" is used for ":grep".
3953 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00003954 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
3955 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3956 || eap->cmdidx == CMD_grepadd
3957 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003958 && !grep_internal(eap->cmdidx))
3959 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00003960 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3961 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003962 {
3963 if (*curbuf->b_p_gp == NUL)
3964 program = p_gp;
3965 else
3966 program = curbuf->b_p_gp;
3967 }
3968 else
3969 {
3970 if (*curbuf->b_p_mp == NUL)
3971 program = p_mp;
3972 else
3973 program = curbuf->b_p_mp;
3974 }
3975
3976 p = skipwhite(p);
3977
3978 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3979 {
3980 /* replace $* by given arguments */
3981 i = 1;
3982 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
3983 ++i;
3984 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003985 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003986 if (new_cmdline == NULL)
3987 return NULL; /* out of memory */
3988 ptr = new_cmdline;
3989 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3990 {
3991 i = (int)(pos - program);
3992 STRNCPY(ptr, program, i);
3993 STRCPY(ptr += i, p);
3994 ptr += len;
3995 program = pos + 2;
3996 }
3997 STRCPY(ptr, program);
3998 }
3999 else
4000 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004001 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004002 if (new_cmdline == NULL)
4003 return NULL; /* out of memory */
4004 STRCPY(new_cmdline, program);
4005 STRCAT(new_cmdline, " ");
4006 STRCAT(new_cmdline, p);
4007 }
4008 msg_make(p);
4009
4010 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4011 vim_free(*cmdlinep);
4012 *cmdlinep = new_cmdline;
4013 p = new_cmdline;
4014 }
4015 return p;
4016}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004017#endif
4018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019/*
4020 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004021 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 * Return FAIL for failure, OK otherwise.
4023 */
4024 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004025expand_filename(
4026 exarg_T *eap,
4027 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004028 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029{
4030 int has_wildcards; /* need to expand wildcards */
4031 char_u *repl;
4032 int srclen;
4033 char_u *p;
4034 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004035 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036
Bram Moolenaar748bf032005-02-02 23:04:36 +00004037#ifdef FEAT_QUICKFIX
4038 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4039 p = skip_grep_pat(eap);
4040#else
4041 p = eap->arg;
4042#endif
4043
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 /*
4045 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4046 * the file name contains a wildcard it should not cause expanding.
4047 * (it will be expanded anyway if there is a wildcard before replacing).
4048 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004049 has_wildcards = mch_has_wildcard(p);
4050 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004052#ifdef FEAT_EVAL
4053 /* Skip over `=expr`, wildcards in it are not expanded. */
4054 if (p[0] == '`' && p[1] == '=')
4055 {
4056 p += 2;
4057 (void)skip_expr(&p);
4058 if (*p == '`')
4059 ++p;
4060 continue;
4061 }
4062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 /*
4064 * Quick check if this cannot be the start of a special string.
4065 * Also removes backslash before '%', '#' and '<'.
4066 */
4067 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4068 {
4069 ++p;
4070 continue;
4071 }
4072
4073 /*
4074 * Try to find a match at this position.
4075 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004076 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4077 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 if (*errormsgp != NULL) /* error detected */
4079 return FAIL;
4080 if (repl == NULL) /* no match found */
4081 {
4082 p += srclen;
4083 continue;
4084 }
4085
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004086 /* Wildcards won't be expanded below, the replacement is taken
4087 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4088 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4089 {
4090 char_u *l = repl;
4091
4092 repl = expand_env_save(repl);
4093 vim_free(l);
4094 }
4095
Bram Moolenaar63b92542007-03-27 14:57:09 +00004096 /* Need to escape white space et al. with a backslash.
4097 * Don't do this for:
4098 * - replacement that already has been escaped: "##"
4099 * - shell commands (may have to use quotes instead).
4100 * - non-unix systems when there is a single argument (spaces don't
4101 * separate arguments then).
4102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004104 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 && eap->cmdidx != CMD_grep
4107 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004108 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004109 && eap->cmdidx != CMD_lgrep
4110 && eap->cmdidx != CMD_lgrepadd
4111 && eap->cmdidx != CMD_lmake
4112 && eap->cmdidx != CMD_make
4113 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004115 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116#endif
4117 )
4118 {
4119 char_u *l;
4120#ifdef BACKSLASH_IN_FILENAME
4121 /* Don't escape a backslash here, because rem_backslash() doesn't
4122 * remove it later. */
4123 static char_u *nobslash = (char_u *)" \t\"|";
4124# define ESCAPE_CHARS nobslash
4125#else
4126# define ESCAPE_CHARS escape_chars
4127#endif
4128
4129 for (l = repl; *l; ++l)
4130 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4131 {
4132 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4133 if (l != NULL)
4134 {
4135 vim_free(repl);
4136 repl = l;
4137 }
4138 break;
4139 }
4140 }
4141
4142 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004143 if ((eap->usefilter || eap->cmdidx == CMD_bang
4144 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004145 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 {
4147 char_u *l;
4148
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004149 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 if (l != NULL)
4151 {
4152 vim_free(repl);
4153 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
4155 }
4156
4157 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4158 vim_free(repl);
4159 if (p == NULL)
4160 return FAIL;
4161 }
4162
4163 /*
4164 * One file argument: Expand wildcards.
4165 * Don't do this with ":r !command" or ":w !command".
4166 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004167 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {
4169 /*
4170 * May do this twice:
4171 * 1. Replace environment variables.
4172 * 2. Replace any other wildcards, remove backslashes.
4173 */
4174 for (n = 1; n <= 2; ++n)
4175 {
4176 if (n == 2)
4177 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 /*
4179 * Halve the number of backslashes (this is Vi compatible).
4180 * For Unix and OS/2, when wildcards are expanded, this is
4181 * done by ExpandOne() below.
4182 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004183#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 if (!has_wildcards)
4185#endif
4186 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 }
4188
4189 if (has_wildcards)
4190 {
4191 if (n == 1)
4192 {
4193 /*
4194 * First loop: May expand environment variables. This
4195 * can be done much faster with expand_env() than with
4196 * something else (e.g., calling a shell).
4197 * After expanding environment variables, check again
4198 * if there are still wildcards present.
4199 */
4200 if (vim_strchr(eap->arg, '$') != NULL
4201 || vim_strchr(eap->arg, '~') != NULL)
4202 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004203 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004204 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 has_wildcards = mch_has_wildcard(NameBuff);
4206 p = NameBuff;
4207 }
4208 else
4209 p = NULL;
4210 }
4211 else /* n == 2 */
4212 {
4213 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004214 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 ExpandInit(&xpc);
4217 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004218 if (p_wic)
4219 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004221 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 if (p == NULL)
4223 return FAIL;
4224 }
4225 if (p != NULL)
4226 {
4227 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4228 p, cmdlinep);
4229 if (n == 2) /* p came from ExpandOne() */
4230 vim_free(p);
4231 }
4232 }
4233 }
4234 }
4235 return OK;
4236}
4237
4238/*
4239 * Replace part of the command line, keeping eap->cmd, eap->arg and
4240 * eap->nextcmd correct.
4241 * "src" points to the part that is to be replaced, of length "srclen".
4242 * "repl" is the replacement string.
4243 * Returns a pointer to the character after the replaced string.
4244 * Returns NULL for failure.
4245 */
4246 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004247repl_cmdline(
4248 exarg_T *eap,
4249 char_u *src,
4250 int srclen,
4251 char_u *repl,
4252 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 int len;
4255 int i;
4256 char_u *new_cmdline;
4257
4258 /*
4259 * The new command line is build in new_cmdline[].
4260 * First allocate it.
4261 * Careful: a "+cmd" argument may have been NUL terminated.
4262 */
4263 len = (int)STRLEN(repl);
4264 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004265 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02004267 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 return NULL; /* out of memory! */
4269
4270 /*
4271 * Copy the stuff before the expanded part.
4272 * Copy the expanded stuff.
4273 * Copy what came after the expanded part.
4274 * Copy the next commands, if there are any.
4275 */
4276 i = (int)(src - *cmdlinep); /* length of part before match */
4277 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 mch_memmove(new_cmdline + i, repl, (size_t)len);
4280 i += len; /* remember the end of the string */
4281 STRCPY(new_cmdline + i, src + srclen);
4282 src = new_cmdline + i; /* remember where to continue */
4283
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004284 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
4286 i = (int)STRLEN(new_cmdline) + 1;
4287 STRCPY(new_cmdline + i, eap->nextcmd);
4288 eap->nextcmd = new_cmdline + i;
4289 }
4290 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4291 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4292 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4293 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4294 vim_free(*cmdlinep);
4295 *cmdlinep = new_cmdline;
4296
4297 return src;
4298}
4299
4300/*
4301 * Check for '|' to separate commands and '"' to start comments.
4302 */
4303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004304separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
4306 char_u *p;
4307
Bram Moolenaar86b68352004-12-27 21:59:20 +00004308#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004309 p = skip_grep_pat(eap);
4310#else
4311 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004312#endif
4313
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004314 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 if (*p == Ctrl_V)
4317 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004318 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 ++p; /* skip CTRL-V and next char */
4320 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004321 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004322 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (*p == NUL) /* stop at NUL after CTRL-V */
4324 break;
4325 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004326
4327#ifdef FEAT_EVAL
4328 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004329 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004330 {
4331 p += 2;
4332 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004333 }
4334#endif
4335
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 /* Check for '"': start of comment or '|': next command */
4337 /* :@" and :*" do not start a comment!
4338 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004339 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4341 || p != eap->arg)
4342 && (eap->cmdidx != CMD_redir
4343 || p != eap->arg + 1 || p[-1] != '@'))
4344 || *p == '|' || *p == '\n')
4345 {
4346 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004347 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 * AND 'b' is present in 'cpoptions'.
4349 */
4350 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004351 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004353 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 --p;
4355 }
4356 else
4357 {
4358 eap->nextcmd = check_nextcmd(p);
4359 *p = NUL;
4360 break;
4361 }
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004364
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004365 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 del_trailing_spaces(eap->arg);
4367}
4368
4369/*
4370 * get + command from ex argument
4371 */
4372 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004373getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
4375 char_u *arg = *argp;
4376 char_u *command = NULL;
4377
4378 if (*arg == '+') /* +[command] */
4379 {
4380 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004381 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 command = dollar_command;
4383 else
4384 {
4385 command = arg;
4386 arg = skip_cmd_arg(command, TRUE);
4387 if (*arg != NUL)
4388 *arg++ = NUL; /* terminate command with NUL */
4389 }
4390
4391 arg = skipwhite(arg); /* skip over spaces */
4392 *argp = arg;
4393 }
4394 return command;
4395}
4396
4397/*
4398 * Find end of "+command" argument. Skip over "\ " and "\\".
4399 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004400 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004401skip_cmd_arg(
4402 char_u *p,
4403 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
4405 while (*p && !vim_isspace(*p))
4406 {
4407 if (*p == '\\' && p[1] != NUL)
4408 {
4409 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004410 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 else
4412 ++p;
4413 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004414 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 return p;
4417}
4418
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004419 int
4420get_bad_opt(char_u *p, exarg_T *eap)
4421{
4422 if (STRICMP(p, "keep") == 0)
4423 eap->bad_char = BAD_KEEP;
4424 else if (STRICMP(p, "drop") == 0)
4425 eap->bad_char = BAD_DROP;
4426 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4427 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004428 else
4429 return FAIL;
4430 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004431}
4432
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433/*
4434 * Get "++opt=arg" argument.
4435 * Return FAIL or OK.
4436 */
4437 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004438getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
4440 char_u *arg = eap->arg + 2;
4441 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004442 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 /* ":edit ++[no]bin[ary] file" */
4446 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4447 {
4448 if (*arg == 'n')
4449 {
4450 arg += 2;
4451 eap->force_bin = FORCE_NOBIN;
4452 }
4453 else
4454 eap->force_bin = FORCE_BIN;
4455 if (!checkforcmd(&arg, "binary", 3))
4456 return FAIL;
4457 eap->arg = skipwhite(arg);
4458 return OK;
4459 }
4460
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004461 /* ":read ++edit file" */
4462 if (STRNCMP(arg, "edit", 4) == 0)
4463 {
4464 eap->read_edit = TRUE;
4465 eap->arg = skipwhite(arg + 4);
4466 return OK;
4467 }
4468
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (STRNCMP(arg, "ff", 2) == 0)
4470 {
4471 arg += 2;
4472 pp = &eap->force_ff;
4473 }
4474 else if (STRNCMP(arg, "fileformat", 10) == 0)
4475 {
4476 arg += 10;
4477 pp = &eap->force_ff;
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 else if (STRNCMP(arg, "enc", 3) == 0)
4480 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004481 if (STRNCMP(arg, "encoding", 8) == 0)
4482 arg += 8;
4483 else
4484 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 pp = &eap->force_enc;
4486 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004487 else if (STRNCMP(arg, "bad", 3) == 0)
4488 {
4489 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004490 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 if (pp == NULL || *arg != '=')
4494 return FAIL;
4495
4496 ++arg;
4497 *pp = (int)(arg - eap->cmd);
4498 arg = skip_cmd_arg(arg, FALSE);
4499 eap->arg = skipwhite(arg);
4500 *arg = NUL;
4501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (pp == &eap->force_ff)
4503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4505 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004506 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004508 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
4510 /* Make 'fileencoding' lower case. */
4511 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4512 *p = TOLOWER_ASC(*p);
4513 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004514 else
4515 {
4516 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4517 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004518 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004519 return FAIL;
4520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 return OK;
4523}
4524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004526ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
4528 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004529 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 * directory for security reasons.
4531 */
4532 if (secure)
4533 {
4534 secure = 2;
4535 eap->errmsg = e_curdir;
4536 }
4537 else if (eap->cmdidx == CMD_autocmd)
4538 do_autocmd(eap->arg, eap->forceit);
4539 else
4540 do_augroup(eap->arg, eap->forceit);
4541}
4542
4543/*
4544 * ":doautocmd": Apply the automatic commands to the current buffer.
4545 */
4546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004547ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004549 char_u *arg = eap->arg;
4550 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004551 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004552
Bram Moolenaar1610d052016-06-09 22:53:01 +02004553 (void)do_doautocmd(arg, TRUE, &did_aucmd);
4554 /* Only when there is no <nomodeline>. */
4555 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004556 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559/*
4560 * :[N]bunload[!] [N] [bufname] unload buffer
4561 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4562 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4563 */
4564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004565ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004567 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004568 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 eap->errmsg = do_bufdel(
4570 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4571 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4572 : DOBUF_UNLOAD, eap->arg,
4573 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4574}
4575
4576/*
4577 * :[N]buffer [N] to buffer N
4578 * :[N]sbuffer [N] to buffer N
4579 */
4580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004581ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004583 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004584 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (*eap->arg)
4586 eap->errmsg = e_trailing;
4587 else
4588 {
4589 if (eap->addr_count == 0) /* default is current buffer */
4590 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4591 else
4592 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004593 if (eap->do_ecmd_cmd != NULL)
4594 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596}
4597
4598/*
4599 * :[N]bmodified [N] to next mod. buffer
4600 * :[N]sbmodified [N] to next mod. buffer
4601 */
4602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 goto_buffer(eap, DOBUF_MOD, 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 next buffer
4612 * :[N]sbnext [N] split and to next buffer
4613 */
4614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004615ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
4617 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004618 if (eap->do_ecmd_cmd != NULL)
4619 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620}
4621
4622/*
4623 * :[N]bNext [N] to previous buffer
4624 * :[N]bprevious [N] to previous buffer
4625 * :[N]sbNext [N] split and to previous buffer
4626 * :[N]sbprevious [N] split and to previous buffer
4627 */
4628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004629ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004632 if (eap->do_ecmd_cmd != NULL)
4633 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634}
4635
4636/*
4637 * :brewind to first buffer
4638 * :bfirst to first buffer
4639 * :sbrewind split and to first buffer
4640 * :sbfirst split and to first buffer
4641 */
4642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 goto_buffer(eap, DOBUF_FIRST, FORWARD, 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}
4649
4650/*
4651 * :blast to last buffer
4652 * :sblast split and to last buffer
4653 */
4654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004655ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
4657 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004658 if (eap->do_ecmd_cmd != NULL)
4659 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004663ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 return (c == NUL || c == '|' || c == '"' || c == '\n');
4666}
4667
4668#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4669 || defined(PROTO)
4670/*
4671 * Return the next command, after the first '|' or '\n'.
4672 * Return NULL if not found.
4673 */
4674 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004675find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 while (*p != '|' && *p != '\n')
4678 {
4679 if (*p == NUL)
4680 return NULL;
4681 ++p;
4682 }
4683 return (p + 1);
4684}
4685#endif
4686
4687/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004688 * Check if *p is a separator between Ex commands, skipping over white space.
4689 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 */
4691 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004694 char_u *s = skipwhite(p);
4695
4696 if (*s == '|' || *s == '\n')
4697 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 else
4699 return NULL;
4700}
4701
4702/*
4703 * - if there are more files to edit
4704 * - and this is the last window
4705 * - and forceit not used
4706 * - and not repeated twice on a row
4707 * return FAIL and give error message if 'message' TRUE
4708 * return OK otherwise
4709 */
4710 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004711check_more(
4712 int message, /* when FALSE check only, no messages */
4713 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4716
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004717 if (!forceit && only_one_window()
4718 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
4720 if (message)
4721 {
4722#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4723 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4724 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004725 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004727 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4728 NGETTEXT("%d more file to edit. Quit anyway?",
4729 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4731 return OK;
4732 return FAIL;
4733 }
4734#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004735 semsg(NGETTEXT("E173: %d more file to edit",
4736 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 quitmore = 2; /* next try to quit is allowed */
4738 }
4739 return FAIL;
4740 }
4741 return OK;
4742}
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744/*
4745 * Function given to ExpandGeneric() to obtain the list of command names.
4746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004748get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 return cmdnames[idx].cmd_name;
4753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
Bram Moolenaare6850792010-05-14 15:28:44 +02004758 if (*eap->arg == NUL)
4759 {
4760#ifdef FEAT_EVAL
4761 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4762 char_u *p = NULL;
4763
4764 if (expr != NULL)
4765 {
4766 ++emsg_off;
4767 p = eval_to_string(expr, NULL, FALSE);
4768 --emsg_off;
4769 vim_free(expr);
4770 }
4771 if (p != NULL)
4772 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004773 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004774 vim_free(p);
4775 }
4776 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004777 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004778#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004779 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004780#endif
4781 }
4782 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004783 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004784
4785#ifdef FEAT_VTP
4786 else if (has_vtp_working())
4787 {
4788 // background color change requires clear + redraw
4789 update_screen(CLEAR);
4790 redrawcmd();
4791 }
4792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793}
4794
4795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004796ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004799 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 do_highlight(eap->arg, eap->forceit, FALSE);
4801}
4802
4803
4804/*
4805 * Call this function if we thought we were going to exit, but we won't
4806 * (because of an error). May need to restore the terminal mode.
4807 */
4808 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004809not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 exiting = FALSE;
4812 settmode(TMODE_RAW);
4813}
4814
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004815 static int
4816before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4817{
4818 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4819
4820 /* Bail out when autocommands closed the window.
4821 * Refuse to quit when the buffer in the last window is being closed (can
4822 * only happen in autocommands). */
4823 if (!win_valid(wp)
4824 || curbuf_locked()
4825 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4826 return TRUE;
4827
4828 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4829 {
4830 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
4831 /* Refuse to quit when locked or when the buffer in the last window is
4832 * being closed (can only happen in autocommands). */
4833 if (curbuf_locked()
4834 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4835 return TRUE;
4836 }
4837
4838 return FALSE;
4839}
4840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004842 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004843 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 */
4845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004846ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004848 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850#ifdef FEAT_CMDWIN
4851 if (cmdwin_type != 0)
4852 {
4853 cmdwin_result = Ctrl_C;
4854 return;
4855 }
4856#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004857 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004858 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004859 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004860 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004861 return;
4862 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004863 if (eap->addr_count > 0)
4864 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004865 int wnr = eap->line2;
4866
4867 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4868 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004869 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004870 }
4871 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004872 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004873
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004874 /* Refuse to quit when locked. */
4875 if (curbuf_locked())
4876 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004877
4878 /* Trigger QuitPre and maybe ExitPre */
4879 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882#ifdef FEAT_NETBEANS_INTG
4883 netbeansForcedQuit = eap->forceit;
4884#endif
4885
4886 /*
4887 * If there are more files or windows we won't exit.
4888 */
4889 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4890 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004891 if ((!buf_hide(wp->w_buffer)
4892 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004893 | (eap->forceit ? CCGD_FORCEIT : 0)
4894 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004896 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
4898 not_exiting();
4899 }
4900 else
4901 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02004902 /* quit last window
4903 * Note: only_one_window() returns true, even so a help window is
4904 * still open. In that case only quit, if no address has been
4905 * specified. Example:
4906 * :h|wincmd w|1q - don't quit
4907 * :h|wincmd w|q - quit
4908 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004909 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02004911 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004912#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004916 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918}
4919
4920/*
4921 * ":cquit".
4922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004924ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
4926 getout(1); /* this does not always pass on the exit code to the Manx
4927 compiler. why? */
4928}
4929
4930/*
4931 * ":qall": try to quit all windows
4932 */
4933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004934ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936# ifdef FEAT_CMDWIN
4937 if (cmdwin_type != 0)
4938 {
4939 if (eap->forceit)
4940 cmdwin_result = K_XF1; /* ex_window() takes care of this */
4941 else
4942 cmdwin_result = K_XF2;
4943 return;
4944 }
4945# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004946
4947 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004948 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004949 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004950 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004951 return;
4952 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004953
4954 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004955 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01004958 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 getout(0);
4960 not_exiting();
4961}
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * ":close": close current window, unless it is the last one
4965 */
4966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004967ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004969 win_T *win;
4970 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004971#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004973 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004975#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004976 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004977 {
4978 if (eap->addr_count == 0)
4979 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004980 else
4981 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004982 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004983 {
4984 winnr++;
4985 if (winnr == eap->line2)
4986 break;
4987 }
4988 if (win == NULL)
4989 win = lastwin;
4990 ex_win_close(eap->forceit, win, NULL);
4991 }
4992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993}
4994
Bram Moolenaar4033c552017-09-16 20:54:51 +02004995#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004996/*
4997 * ":pclose": Close any preview window.
4998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005000ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005001{
5002 win_T *win;
5003
Bram Moolenaar79648732019-07-18 21:43:07 +02005004 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005005 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005006 if (win->w_p_pvw)
5007 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005008 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005009 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005010 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005011# ifdef FEAT_TEXT_PROP
5012 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005013 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005014# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005015}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005016#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005017
Bram Moolenaarf740b292006-02-16 22:11:02 +00005018/*
5019 * Close window "win" and take care of handling closing the last window for a
5020 * modified buffer.
5021 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005023ex_win_close(
5024 int forceit,
5025 win_T *win,
5026 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028 int need_hide;
5029 buf_T *buf = win->w_buffer;
5030
5031 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005032 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005034#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if ((p_confirm || cmdmod.confirm) && p_write)
5036 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005037 bufref_T bufref;
5038
5039 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005041 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 return;
5043 need_hide = FALSE;
5044 }
5045 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005048 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 return;
5050 }
5051 }
5052
Bram Moolenaar4033c552017-09-16 20:54:51 +02005053#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005055#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005058 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005059 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005060 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005061 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062}
5063
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005065 * Handle the argument for a tabpage related ex command.
5066 * Returns a tabpage number.
5067 * When an error is encountered then eap->errmsg is set.
5068 */
5069 static int
5070get_tabpage_arg(exarg_T *eap)
5071{
5072 int tab_number;
5073 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5074
5075 if (eap->arg && *eap->arg != NUL)
5076 {
5077 char_u *p = eap->arg;
5078 char_u *p_save;
5079 int relative = 0; /* argument +N/-N means: go to N places to the
5080 * right/left relative to the current position. */
5081
5082 if (*p == '-')
5083 {
5084 relative = -1;
5085 p++;
5086 }
5087 else if (*p == '+')
5088 {
5089 relative = 1;
5090 p++;
5091 }
5092
5093 p_save = p;
5094 tab_number = getdigits(&p);
5095
5096 if (relative == 0)
5097 {
5098 if (STRCMP(p, "$") == 0)
5099 tab_number = LAST_TAB_NR;
5100 else if (p == p_save || *p_save == '-' || *p != NUL
5101 || tab_number > LAST_TAB_NR)
5102 {
5103 /* No numbers as argument. */
5104 eap->errmsg = e_invarg;
5105 goto theend;
5106 }
5107 }
5108 else
5109 {
5110 if (*p_save == NUL)
5111 tab_number = 1;
5112 else if (p == p_save || *p_save == '-' || *p != NUL
5113 || tab_number == 0)
5114 {
5115 /* No numbers as argument. */
5116 eap->errmsg = e_invarg;
5117 goto theend;
5118 }
5119 tab_number = tab_number * relative + tabpage_index(curtab);
5120 if (!unaccept_arg0 && relative == -1)
5121 --tab_number;
5122 }
5123 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5124 eap->errmsg = e_invarg;
5125 }
5126 else if (eap->addr_count > 0)
5127 {
5128 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005129 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005130 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005131 tab_number = 0;
5132 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005133 else
5134 {
5135 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005136 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005137 {
5138 --tab_number;
5139 if (tab_number < unaccept_arg0)
5140 eap->errmsg = e_invarg;
5141 }
5142 }
5143 }
5144 else
5145 {
5146 switch (eap->cmdidx)
5147 {
5148 case CMD_tabnext:
5149 tab_number = tabpage_index(curtab) + 1;
5150 if (tab_number > LAST_TAB_NR)
5151 tab_number = 1;
5152 break;
5153 case CMD_tabmove:
5154 tab_number = LAST_TAB_NR;
5155 break;
5156 default:
5157 tab_number = tabpage_index(curtab);
5158 }
5159 }
5160
5161theend:
5162 return tab_number;
5163}
5164
5165/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005166 * ":tabclose": close current tab page, unless it is the last one.
5167 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 */
5169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005170ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005172 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005173 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005174
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005175# ifdef FEAT_CMDWIN
5176 if (cmdwin_type != 0)
5177 cmdwin_result = K_IGNORE;
5178 else
5179# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005180 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005181 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005184 tab_number = get_tabpage_arg(eap);
5185 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005186 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005187 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005188 if (tp == NULL)
5189 {
5190 beep_flush();
5191 return;
5192 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005193 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005194 {
5195 tabpage_close_other(tp, eap->forceit);
5196 return;
5197 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005198 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005199 tabpage_close(eap->forceit);
5200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005202}
5203
5204/*
5205 * ":tabonly": close all tab pages except the current one
5206 */
5207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005208ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005209{
5210 tabpage_T *tp;
5211 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005212 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005213
5214# ifdef FEAT_CMDWIN
5215 if (cmdwin_type != 0)
5216 cmdwin_result = K_IGNORE;
5217 else
5218# endif
5219 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005220 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005221 else
5222 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005223 tab_number = get_tabpage_arg(eap);
5224 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005225 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005226 goto_tabpage(tab_number);
5227 /* Repeat this up to a 1000 times, because autocommands may
5228 * mess up the lists. */
5229 for (done = 0; done < 1000; ++done)
5230 {
5231 FOR_ALL_TABPAGES(tp)
5232 if (tp->tp_topframe != topframe)
5233 {
5234 tabpage_close_other(tp, eap->forceit);
5235 /* if we failed to close it quit */
5236 if (valid_tabpage(tp))
5237 done = 1000;
5238 /* start over, "tp" is now invalid */
5239 break;
5240 }
5241 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005242 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005243 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005244 }
5245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005249 * Close the current tab page.
5250 */
5251 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005252tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005253{
5254 /* First close all the windows but the current one. If that worked then
5255 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01005256 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005257 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005258 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005259 ex_win_close(forceit, curwin, NULL);
5260# ifdef FEAT_GUI
5261 need_mouse_correct = TRUE;
5262# endif
5263}
5264
5265/*
5266 * Close tab page "tp", which is not the current tab page.
5267 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005268 * Also takes care of the tab pages line disappearing when closing the
5269 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005270 */
5271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005272tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005273{
5274 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005275 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005276 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005277
5278 /* Limit to 1000 windows, autocommands may add a window while we close
5279 * one. OK, so I'm paranoid... */
5280 while (++done < 1000)
5281 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005282 wp = tp->tp_firstwin;
5283 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005284
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005285 /* Autocommands may delete the tab page under our fingers and we may
5286 * fail to close a window with a modified buffer. */
5287 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005288 break;
5289 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005290
Bram Moolenaar29323592016-07-24 22:04:11 +02005291 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005292
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005293 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005294 if (h != tabline_height())
5295 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005296}
5297
5298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * ":only".
5300 */
5301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005302ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005304 win_T *wp;
5305 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306# ifdef FEAT_GUI
5307 need_mouse_correct = TRUE;
5308# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005309 if (eap->addr_count > 0)
5310 {
5311 wnr = eap->line2;
5312 for (wp = firstwin; --wnr > 0; )
5313 {
5314 if (wp->w_next == NULL)
5315 break;
5316 else
5317 wp = wp->w_next;
5318 }
5319 win_goto(wp);
5320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 close_others(TRUE, eap->forceit);
5322}
5323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005325ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005327 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01005328 if (!eap->skip)
5329 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005330#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005331 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005332#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005333 if (eap->addr_count == 0)
5334 win_close(curwin, FALSE); /* don't free buffer */
5335 else
5336 {
5337 int winnr = 0;
5338 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005339
Bram Moolenaar2256c992016-11-15 21:17:07 +01005340 FOR_ALL_WINDOWS(win)
5341 {
5342 winnr++;
5343 if (winnr == eap->line2)
5344 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005345 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005346 if (win == NULL)
5347 win = lastwin;
5348 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
5351}
5352
5353/*
5354 * ":stop" and ":suspend": Suspend Vim.
5355 */
5356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005357ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359 /*
5360 * Disallow suspending for "rvim".
5361 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005362 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
5364 if (!eap->forceit)
5365 autowrite_all();
5366 windgoto((int)Rows - 1, 0);
5367 out_char('\n');
5368 out_flush();
5369 stoptermcap();
5370 out_flush(); /* needed for SUN to restore xterm buffer */
5371#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02005372 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
5374 ui_suspend(); /* call machine specific function */
5375#ifdef FEAT_TITLE
5376 maketitle();
5377 resettitle(); /* force updating the title */
5378#endif
5379 starttermcap();
5380 scroll_start(); /* scroll screen before redrawing */
5381 redraw_later_clear();
5382 shell_resized(); /* may have resized window */
5383 }
5384}
5385
5386/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005387 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 */
5389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005390ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392#ifdef FEAT_CMDWIN
5393 if (cmdwin_type != 0)
5394 {
5395 cmdwin_result = Ctrl_C;
5396 return;
5397 }
5398#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005399 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005400 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005401 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005402 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005403 return;
5404 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005405
5406 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005407 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
5409 /*
5410 * if more files or windows we won't exit
5411 */
5412 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5413 exiting = TRUE;
5414 if ( ((eap->cmdidx == CMD_wq
5415 || curbufIsChanged())
5416 && do_write(eap) == FAIL)
5417 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005418 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
5420 not_exiting();
5421 }
5422 else
5423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005426 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427# ifdef FEAT_GUI
5428 need_mouse_correct = TRUE;
5429# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02005430 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005431 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
5433}
5434
5435/*
5436 * ":print", ":list", ":number".
5437 */
5438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005439ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005441 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005442 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005445 for ( ;!got_int; ui_breakcheck())
5446 {
5447 print_line(eap->line1,
5448 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5449 || (eap->flags & EXFLAG_NR)),
5450 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5451 if (++eap->line1 > eap->line2)
5452 break;
5453 out_flush(); /* show one line at a time */
5454 }
5455 setpcmark();
5456 /* put cursor at last line */
5457 curwin->w_cursor.lnum = eap->line2;
5458 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462}
5463
5464#ifdef FEAT_BYTEOFF
5465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005466ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 goto_byte(eap->line2);
5469}
5470#endif
5471
5472/*
5473 * ":shell".
5474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005476ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 do_shell(NULL, 0);
5479}
5480
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005481#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005483static int drop_busy = FALSE;
5484static int drop_filec;
5485static char_u **drop_filev = NULL;
5486static int drop_split;
5487static void (*drop_callback)(void *);
5488static void *drop_cookie;
5489
5490 static void
5491handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 exarg_T ea;
5494 int save_msg_scroll = msg_scroll;
5495
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005496 // Setting the argument list may cause screen updates and being called
5497 // recursively. Avoid that by setting drop_busy.
5498 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
5500 /* Check whether the current buffer is changed. If so, we will need
5501 * to split the current window or data could be lost.
5502 * We don't need to check if the 'hidden' option is set, as in this
5503 * case the buffer won't be lost.
5504 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005505 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005508 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 --emsg_off;
5510 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005511 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 if (win_split(0, 0) == FAIL)
5514 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005515 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 /* When splitting the window, create a new alist. Otherwise the
5518 * existing one is overwritten. */
5519 alist_unlink(curwin->w_alist);
5520 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 }
5522
5523 /*
5524 * Set up the new argument list.
5525 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005526 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528 /*
5529 * Move to the first file.
5530 */
5531 /* Fake up a minimal "next" command for do_argfile() */
5532 vim_memset(&ea, 0, sizeof(ea));
5533 ea.cmd = (char_u *)"next";
5534 do_argfile(&ea, 0);
5535
5536 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
5537 * mode that is not needed here. */
5538 need_start_insertmode = FALSE;
5539
5540 /* Restore msg_scroll, otherwise a following command may cause scrolling
5541 * unexpectedly. The screen will be redrawn by the caller, thus
5542 * msg_scroll being set by displaying a message is irrelevant. */
5543 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005544
5545 if (drop_callback != NULL)
5546 drop_callback(drop_cookie);
5547
5548 drop_filev = NULL;
5549 drop_busy = FALSE;
5550}
5551
5552/*
5553 * Handle a file drop. The code is here because a drop is *nearly* like an
5554 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005555 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005556 * code is very similar to :args and hence needs access to a lot of the static
5557 * functions in this file.
5558 *
5559 * The "filev" list must have been allocated using alloc(), as should each item
5560 * in the list. This function takes over responsibility for freeing the "filev"
5561 * list.
5562 */
5563 void
5564handle_drop(
5565 int filec, // the number of files dropped
5566 char_u **filev, // the list of files dropped
5567 int split, // force splitting the window
5568 void (*callback)(void *), // to be called after setting the argument
5569 // list
5570 void *cookie) // argument for "callback" (allocated)
5571{
5572 // Cannot handle recursive drops, finish the pending one.
5573 if (drop_busy)
5574 {
5575 FreeWild(filec, filev);
5576 vim_free(cookie);
5577 return;
5578 }
5579
5580 // When calling handle_drop() more than once in a row we only use the last
5581 // one.
5582 if (drop_filev != NULL)
5583 {
5584 FreeWild(drop_filec, drop_filev);
5585 vim_free(drop_cookie);
5586 }
5587
5588 drop_filec = filec;
5589 drop_filev = filev;
5590 drop_split = split;
5591 drop_callback = callback;
5592 drop_cookie = cookie;
5593
5594 // Postpone this when:
5595 // - editing the command line
5596 // - not possible to change the current buffer
5597 // - updating the screen
5598 // As it may change buffers and window structures that are in use and cause
5599 // freed memory to be used.
5600 if (text_locked() || curbuf_locked() || updating_screen)
5601 return;
5602
5603 handle_drop_internal();
5604}
5605
5606/*
5607 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5608 * reset: Handle a postponed drop.
5609 */
5610 void
5611handle_any_postponed_drop(void)
5612{
5613 if (!drop_busy && drop_filev != NULL
5614 && !text_locked() && !curbuf_locked() && !updating_screen)
5615 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616}
5617#endif
5618
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 * ":preserve".
5621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005625 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 ml_preserve(curbuf, TRUE);
5627}
5628
5629/*
5630 * ":recover".
5631 */
5632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005633ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 /* Set recoverymode right away to avoid the ATTENTION prompt. */
5636 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005637 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5638 | CCGD_MULTWIN
5639 | (eap->forceit ? CCGD_FORCEIT : 0)
5640 | CCGD_EXCMD)
5641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 && (*eap->arg == NUL
5643 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005644 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 recoverymode = FALSE;
5646}
5647
5648/*
5649 * Command modifier used in a wrong way.
5650 */
5651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 eap->errmsg = e_invcmd;
5655}
5656
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657/*
5658 * :sview [+command] file split window with new file, read-only
5659 * :split [[+command] file] split window with current or new file
5660 * :vsplit [[+command] file] split window vertically with current or new file
5661 * :new [[+command] file] split window with no or new file
5662 * :vnew [[+command] file] split vertically window with no or new file
5663 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005664 *
5665 * :tabedit open new Tab page with empty window
5666 * :tabedit [+command] file open new Tab page and edit "file"
5667 * :tabnew [[+command] file] just like :tabedit
5668 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 */
5670 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005673 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005674#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005676#endif
5677#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005679#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005680 int use_tab = eap->cmdidx == CMD_tabedit
5681 || eap->cmdidx == CMD_tabfind
5682 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005684 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005685 return;
5686
Bram Moolenaar4033c552017-09-16 20:54:51 +02005687#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 need_mouse_correct = TRUE;
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_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005693 * quickfix windows. But it's OK when doing ":tab split". */
5694 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {
5696 if (eap->cmdidx == CMD_split)
5697 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 if (eap->cmdidx == CMD_vsplit)
5699 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
Bram Moolenaar4033c552017-09-16 20:54:51 +02005703#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005704 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
5706 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5707 FNAME_MESS, TRUE, curbuf->b_ffname);
5708 if (fname == NULL)
5709 goto theend;
5710 eap->arg = fname;
5711 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005712# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005713 else
5714# endif
5715#endif
5716#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 && eap->cmdidx != CMD_new)
5720 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005721 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005722# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005723 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005724# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005725 au_has_group((char_u *)"FileExplorer"))
5726 {
5727 /* No browsing supported but we do have the file explorer:
5728 * Edit the directory. */
5729 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5730 eap->arg = (char_u *)".";
5731 }
5732 else
5733 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005734 fname = do_browse(0, (char_u *)(use_tab
5735 ? _("Edit File in new tab page")
5736 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005738 if (fname == NULL)
5739 goto theend;
5740 eap->arg = fname;
5741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005746 /*
5747 * Either open new tab page or split the window.
5748 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005749 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005750 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005751 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5752 : eap->addr_count == 0 ? 0
5753 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005754 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005755 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005756
5757 /* set the alternate buffer for the window we came from */
5758 if (curwin != old_curwin
5759 && win_valid(old_curwin)
5760 && old_curwin->w_buffer != curbuf
5761 && !cmdmod.keepalt)
5762 old_curwin->w_alt_fnum = curbuf->b_fnum;
5763 }
5764 }
5765 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 /* Reset 'scrollbind' when editing another file, but keep it when
5769 * doing ":split" without arguments. */
5770 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005771# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005775 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 else
5777 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 do_exedit(eap, old_curwin);
5779 }
5780
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005781# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005785# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786theend:
5787 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005790
5791/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005792 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005793 */
5794 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005795tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005796{
5797 exarg_T ea;
5798
5799 vim_memset(&ea, 0, sizeof(ea));
5800 ea.cmdidx = CMD_tabnew;
5801 ea.cmd = (char_u *)"tabn";
5802 ea.arg = (char_u *)"";
5803 ex_splitview(&ea);
5804}
5805
5806/*
5807 * :tabnext command
5808 */
5809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005810ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005811{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005812 int tab_number;
5813
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005814 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005815 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005816 switch (eap->cmdidx)
5817 {
5818 case CMD_tabfirst:
5819 case CMD_tabrewind:
5820 goto_tabpage(1);
5821 break;
5822 case CMD_tablast:
5823 goto_tabpage(9999);
5824 break;
5825 case CMD_tabprevious:
5826 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005827 if (eap->arg && *eap->arg != NUL)
5828 {
5829 char_u *p = eap->arg;
5830 char_u *p_save = p;
5831
5832 tab_number = getdigits(&p);
5833 if (p == p_save || *p_save == '-' || *p != NUL
5834 || tab_number == 0)
5835 {
5836 /* No numbers as argument. */
5837 eap->errmsg = e_invarg;
5838 return;
5839 }
5840 }
5841 else
5842 {
5843 if (eap->addr_count == 0)
5844 tab_number = 1;
5845 else
5846 {
5847 tab_number = eap->line2;
5848 if (tab_number < 1)
5849 {
5850 eap->errmsg = e_invrange;
5851 return;
5852 }
5853 }
5854 }
5855 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005856 break;
5857 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005858 tab_number = get_tabpage_arg(eap);
5859 if (eap->errmsg == NULL)
5860 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005861 break;
5862 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005863}
5864
5865/*
5866 * :tabmove command
5867 */
5868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005869ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005870{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005871 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005872
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005873 tab_number = get_tabpage_arg(eap);
5874 if (eap->errmsg == NULL)
5875 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005876}
5877
5878/*
5879 * :tabs command: List tabs and their contents.
5880 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005882ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005883{
5884 tabpage_T *tp;
5885 win_T *wp;
5886 int tabcount = 1;
5887
5888 msg_start();
5889 msg_scroll = TRUE;
5890 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5891 {
5892 msg_putchar('\n');
5893 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005894 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895 out_flush(); /* output one line at a time */
5896 ui_breakcheck();
5897
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005898 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005899 wp = firstwin;
5900 else
5901 wp = tp->tp_firstwin;
5902 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5903 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005904 msg_putchar('\n');
5905 msg_putchar(wp == curwin ? '>' : ' ');
5906 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005907 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5908 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005909 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005910 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005911 else
5912 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
5913 IObuff, IOSIZE, TRUE);
5914 msg_outtrans(IObuff);
5915 out_flush(); /* output one line at a time */
5916 ui_breakcheck();
5917 }
5918 }
5919}
5920
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921/*
5922 * ":mode": Set screen mode.
5923 * If no argument given, just get the screen size and redraw.
5924 */
5925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005926ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927{
5928 if (*eap->arg == NUL)
5929 shell_resized();
5930 else
5931 mch_screenmode(eap->arg);
5932}
5933
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934/*
5935 * ":resize".
5936 * set, increment or decrement current window height
5937 */
5938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005939ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941 int n;
5942 win_T *wp = curwin;
5943
5944 if (eap->addr_count > 0)
5945 {
5946 n = eap->line2;
5947 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
5948 ;
5949 }
5950
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005951# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 if (cmdmod.split & WSP_VERT)
5956 {
5957 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02005958 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
5960 n = 9999;
5961 win_setwidth_win((int)n, wp);
5962 }
5963 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 {
5965 if (*eap->arg == '-' || *eap->arg == '+')
5966 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02005967 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 n = 9999;
5969 win_setheight_win((int)n, wp);
5970 }
5971}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972
5973/*
5974 * ":find [+command] <file>" command.
5975 */
5976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005977ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978{
5979#ifdef FEAT_SEARCHPATH
5980 char_u *fname;
5981 int count;
5982
5983 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
5984 TRUE, curbuf->b_ffname);
5985 if (eap->addr_count > 0)
5986 {
5987 /* Repeat finding the file "count" times. This matters when it
5988 * appears several times in the path. */
5989 count = eap->line2;
5990 while (fname != NULL && --count > 0)
5991 {
5992 vim_free(fname);
5993 fname = find_file_in_path(NULL, 0, FNAME_MESS,
5994 FALSE, curbuf->b_ffname);
5995 }
5996 }
5997
5998 if (fname != NULL)
5999 {
6000 eap->arg = fname;
6001#endif
6002 do_exedit(eap, NULL);
6003#ifdef FEAT_SEARCHPATH
6004 vim_free(fname);
6005 }
6006#endif
6007}
6008
6009/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006010 * ":open" simulation: for now just work like ":visual".
6011 */
6012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006013ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006014{
6015 regmatch_T regmatch;
6016 char_u *p;
6017
6018 curwin->w_cursor.lnum = eap->line2;
6019 beginline(BL_SOL | BL_FIX);
6020 if (*eap->arg == '/')
6021 {
6022 /* ":open /pattern/": put cursor in column found with pattern */
6023 ++eap->arg;
6024 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6025 *p = NUL;
6026 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6027 if (regmatch.regprog != NULL)
6028 {
6029 regmatch.rm_ic = p_ic;
6030 p = ml_get_curline();
6031 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006032 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006033 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006034 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006035 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006036 }
6037 /* Move to the NUL, ignore any other arguments. */
6038 eap->arg += STRLEN(eap->arg);
6039 }
6040 check_cursor();
6041
6042 eap->cmdidx = CMD_visual;
6043 do_exedit(eap, NULL);
6044}
6045
6046/*
6047 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 */
6049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006050ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 do_exedit(eap, NULL);
6053}
6054
6055/*
6056 * ":edit <file>" command and alikes.
6057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006059do_exedit(
6060 exarg_T *eap,
6061 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
6063 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006065 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006067 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006068 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 /*
6070 * ":vi" command ends Ex mode.
6071 */
6072 if (exmode_active && (eap->cmdidx == CMD_visual
6073 || eap->cmdidx == CMD_view))
6074 {
6075 exmode_active = FALSE;
6076 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006077 {
6078 /* Special case: ":global/pat/visual\NLvi-commands" */
6079 if (global_busy)
6080 {
6081 int rd = RedrawingDisabled;
6082 int nwr = no_wait_return;
6083 int ms = msg_scroll;
6084#ifdef FEAT_GUI
6085 int he = hold_gui_events;
6086#endif
6087
6088 if (eap->nextcmd != NULL)
6089 {
6090 stuffReadbuff(eap->nextcmd);
6091 eap->nextcmd = NULL;
6092 }
6093
6094 if (exmode_was != EXMODE_VIM)
6095 settmode(TMODE_RAW);
6096 RedrawingDisabled = 0;
6097 no_wait_return = 0;
6098 need_wait_return = FALSE;
6099 msg_scroll = 0;
6100#ifdef FEAT_GUI
6101 hold_gui_events = 0;
6102#endif
6103 must_redraw = CLEAR;
6104
6105 main_loop(FALSE, TRUE);
6106
6107 RedrawingDisabled = rd;
6108 no_wait_return = nwr;
6109 msg_scroll = ms;
6110#ifdef FEAT_GUI
6111 hold_gui_events = he;
6112#endif
6113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
6117
6118 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006119 || eap->cmdidx == CMD_tabnew
6120 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006121 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006123 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 setpcmark();
6125 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006126 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6127 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006129 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 || *eap->arg != NUL
6131#ifdef FEAT_BROWSE
6132 || cmdmod.browse
6133#endif
6134 )
6135 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006136 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
6137 * can bring us here, others are stopped earlier. */
6138 if (*eap->arg != NUL && curbuf_locked())
6139 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006140
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 n = readonlymode;
6142 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6143 readonlymode = TRUE;
6144 else if (eap->cmdidx == CMD_enew)
6145 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6146 empty buffer */
6147 setpcmark();
6148 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6149 NULL, eap,
6150 /* ":edit" goes to first line if Vi compatible */
6151 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6152 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6153 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006154 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01006156 /* after a split we can use an existing buffer */
6157 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006159 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 {
6161 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 if (old_curwin != NULL)
6163 {
6164 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006165 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006167#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006168 cleanup_T cs;
6169
6170 /* Reset the error/interrupt/exception state here so that
6171 * aborting() returns FALSE when closing a window. */
6172 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006173#endif
6174#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006176#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006177 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006178
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006179#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006180 /* Restore the error/interrupt/exception state if not
6181 * discarded by a new aborting error, interrupt, or
6182 * uncaught exception. */
6183 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 }
6186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
6188 else if (readonlymode && curbuf->b_nwindows == 1)
6189 {
6190 /* When editing an already visited buffer, 'readonly' won't be set
6191 * but the previous value is kept. With ":view" and ":sview" we
6192 * want the file to be readonly, except when another window is
6193 * editing the same buffer. */
6194 curbuf->b_p_ro = TRUE;
6195 }
6196 readonlymode = n;
6197 }
6198 else
6199 {
6200 if (eap->do_ecmd_cmd != NULL)
6201 do_cmdline_cmd(eap->do_ecmd_cmd);
6202#ifdef FEAT_TITLE
6203 n = curwin->w_arg_idx_invalid;
6204#endif
6205 check_arg_idx(curwin);
6206#ifdef FEAT_TITLE
6207 if (n != curwin->w_arg_idx_invalid)
6208 maketitle();
6209#endif
6210 }
6211
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 /*
6213 * if ":split file" worked, set alternate file name in old window to new
6214 * file
6215 */
6216 if (old_curwin != NULL
6217 && *eap->arg != NUL
6218 && curwin != old_curwin
6219 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006220 && old_curwin->w_buffer != curbuf
6221 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
6224 ex_no_reprint = TRUE;
6225}
6226
6227#ifndef FEAT_GUI
6228/*
6229 * ":gui" and ":gvim" when there is no GUI.
6230 */
6231 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006232ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233{
6234 eap->errmsg = e_nogvim;
6235}
6236#endif
6237
Bram Moolenaar4f974752019-02-17 17:44:42 +01006238#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006240ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 gui_make_tearoff(eap->arg);
6243}
6244#endif
6245
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006246#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6247 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006249ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006251# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6252 if (gui.in_use)
6253 gui_make_popup(eap->arg, eap->forceit);
6254# ifdef FEAT_TERM_POPUP_MENU
6255 else
6256# endif
6257# endif
6258# ifdef FEAT_TERM_POPUP_MENU
6259 pum_make_popup(eap->arg, eap->forceit);
6260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261}
6262#endif
6263
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006265ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
6267 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006268 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006270 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271}
6272
6273/*
6274 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6275 * offset.
6276 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006279ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006282 win_T *save_curwin = curwin;
6283 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 long topline;
6285 long y;
6286 linenr_T old_linenr = curwin->w_cursor.lnum;
6287
6288 setpcmark();
6289
6290 /*
6291 * determine max topline
6292 */
6293 if (curwin->w_p_scb)
6294 {
6295 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006296 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 {
6298 if (wp->w_p_scb && wp->w_buffer)
6299 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006300 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 if (topline > y)
6302 topline = y;
6303 }
6304 }
6305 if (topline < 1)
6306 topline = 1;
6307 }
6308 else
6309 {
6310 topline = 1;
6311 }
6312
6313
6314 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006315 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006317 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 {
6319 if (curwin->w_p_scb)
6320 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006321 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 y = topline - curwin->w_topline;
6323 if (y > 0)
6324 scrollup(y, TRUE);
6325 else
6326 scrolldown(-y, TRUE);
6327 curwin->w_scbind_pos = topline;
6328 redraw_later(VALID);
6329 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
6332 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006333 curwin = save_curwin;
6334 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 if (curwin->w_p_scb)
6336 {
6337 did_syncbind = TRUE;
6338 checkpcmark();
6339 if (old_linenr != curwin->w_cursor.lnum)
6340 {
6341 char_u ctrl_o[2];
6342
6343 ctrl_o[0] = Ctrl_O;
6344 ctrl_o[1] = 0;
6345 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6346 }
6347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350
6351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006352ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006354 int i;
6355 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6356 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357
6358 if (eap->usefilter) /* :r!cmd */
6359 do_bang(1, eap, FALSE, FALSE, TRUE);
6360 else
6361 {
6362 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6363 return;
6364
6365#ifdef FEAT_BROWSE
6366 if (cmdmod.browse)
6367 {
6368 char_u *browseFile;
6369
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006370 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 NULL, NULL, NULL, curbuf);
6372 if (browseFile != NULL)
6373 {
6374 i = readfile(browseFile, NULL,
6375 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6376 vim_free(browseFile);
6377 }
6378 else
6379 i = OK;
6380 }
6381 else
6382#endif
6383 if (*eap->arg == NUL)
6384 {
6385 if (check_fname() == FAIL) /* check for no file name */
6386 return;
6387 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6388 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6389 }
6390 else
6391 {
6392 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6393 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6394 i = readfile(eap->arg, NULL,
6395 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6396
6397 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006398 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006400#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 if (!aborting())
6402#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006403 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 }
6405 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006406 {
6407 if (empty && exmode_active)
6408 {
6409 /* Delete the empty line that remains. Historically ex does
6410 * this but vi doesn't. */
6411 if (eap->line2 == 0)
6412 lnum = curbuf->b_ml.ml_line_count;
6413 else
6414 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006415 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006416 {
6417 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006418 if (curwin->w_cursor.lnum > 1
6419 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006420 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006421 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006422 }
6423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 }
6427}
6428
Bram Moolenaarea408852005-06-25 22:49:46 +00006429static char_u *prev_dir = NULL;
6430
6431#if defined(EXITFREE) || defined(PROTO)
6432 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006433free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006434{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006435 VIM_CLEAR(prev_dir);
6436 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006437}
6438#endif
6439
Bram Moolenaarf4258302013-06-02 18:20:17 +02006440/*
6441 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006442 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6443 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006444 */
6445 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006446post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006447{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006448 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006449 // Clear tab local directory for both :cd and :tcd
6450 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006451 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006452 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006453 {
6454 /* If still in global directory, need to remember current
6455 * directory as global directory. */
6456 if (globaldir == NULL && prev_dir != NULL)
6457 globaldir = vim_strsave(prev_dir);
6458 /* Remember this local directory for the window. */
6459 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006460 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006461 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006462 curtab->tp_localdir = vim_strsave(NameBuff);
6463 else
6464 curwin->w_localdir = vim_strsave(NameBuff);
6465 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006466 }
6467 else
6468 {
6469 /* We are now in the global directory, no need to remember its
6470 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01006471 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006472 }
6473
6474 shorten_fnames(TRUE);
6475}
6476
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006477/*
6478 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6479 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6480 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6481 * Otherwise changes the global directory.
6482 * Returns TRUE if the directory is successfully changed.
6483 */
6484 int
6485changedir_func(
6486 char_u *new_dir,
6487 int forceit,
6488 cdscope_T scope)
6489{
6490 char_u *tofree;
6491 int dir_differs;
6492 int retval = FALSE;
6493
6494 if (allbuf_locked())
6495 return FALSE;
6496
6497 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6498 {
6499 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6500 return FALSE;
6501 }
6502
6503 // ":cd -": Change to previous directory
6504 if (STRCMP(new_dir, "-") == 0)
6505 {
6506 if (prev_dir == NULL)
6507 {
6508 emsg(_("E186: No previous directory"));
6509 return FALSE;
6510 }
6511 new_dir = prev_dir;
6512 }
6513
6514 // Save current directory for next ":cd -"
6515 tofree = prev_dir;
6516 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6517 prev_dir = vim_strsave(NameBuff);
6518 else
6519 prev_dir = NULL;
6520
6521#if defined(UNIX) || defined(VMS)
6522 // for UNIX ":cd" means: go to home directory
6523 if (*new_dir == NUL)
6524 {
6525 // use NameBuff for home directory name
6526# ifdef VMS
6527 char_u *p;
6528
6529 p = mch_getenv((char_u *)"SYS$LOGIN");
6530 if (p == NULL || *p == NUL) // empty is the same as not set
6531 NameBuff[0] = NUL;
6532 else
6533 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6534# else
6535 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6536# endif
6537 new_dir = NameBuff;
6538 }
6539#endif
6540 dir_differs = new_dir == NULL || prev_dir == NULL
6541 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6542 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6543 emsg(_(e_failed));
6544 else
6545 {
6546 char_u *acmd_fname;
6547
6548 post_chdir(scope);
6549
6550 if (dir_differs)
6551 {
6552 if (scope == CDSCOPE_WINDOW)
6553 acmd_fname = (char_u *)"window";
6554 else if (scope == CDSCOPE_TABPAGE)
6555 acmd_fname = (char_u *)"tabpage";
6556 else
6557 acmd_fname = (char_u *)"global";
6558 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6559 curbuf);
6560 }
6561 retval = TRUE;
6562 }
6563 vim_free(tofree);
6564
6565 return retval;
6566}
Bram Moolenaarea408852005-06-25 22:49:46 +00006567
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006569 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006571 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006572ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006574 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
6576 new_dir = eap->arg;
6577#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006578 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 if (*new_dir == NUL)
6580 ex_pwd(NULL);
6581 else
6582#endif
6583 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006584 cdscope_T scope = CDSCOPE_GLOBAL;
6585
6586 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6587 scope = CDSCOPE_WINDOW;
6588 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6589 scope = CDSCOPE_TABPAGE;
6590
6591 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006592 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006593 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006594 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 ex_pwd(eap);
6596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 }
6598}
6599
6600/*
6601 * ":pwd".
6602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006604ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6607 {
6608#ifdef BACKSLASH_IN_FILENAME
6609 slash_adjust(NameBuff);
6610#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006611 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
6613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006614 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615}
6616
6617/*
6618 * ":=".
6619 */
6620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006621ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006623 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006624 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
6626
6627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006628ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006630 int n;
6631 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632
6633 if (cursor_valid())
6634 {
6635 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6636 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006637 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006639
6640 len = eap->line2;
6641 switch (*eap->arg)
6642 {
6643 case 'm': break;
6644 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006645 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006646 }
6647 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648}
6649
6650/*
6651 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6652 */
6653 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006654do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
Bram Moolenaar52953192019-08-16 10:27:13 +02006656 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006657 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006658# ifdef ELAPSED_FUNC
6659 elapsed_T start_tv;
6660
6661 // Remember at what time we started, so that we know how much longer we
6662 // should wait after waiting for a bit.
6663 ELAPSED_INIT(start_tv);
6664# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
6666 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006667 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006668 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006670 wait_now = msec - done > 1000L ? 1000L : msec - done;
6671#ifdef FEAT_TIMERS
6672 {
6673 long due_time = check_due_timer();
6674
6675 if (due_time > 0 && due_time < wait_now)
6676 wait_now = due_time;
6677 }
6678#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006679#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006680 if (has_any_channel() && wait_now > 20L)
6681 wait_now = 20L;
6682#endif
6683#ifdef FEAT_SOUND
6684 if (has_any_sound_callback() && wait_now > 20L)
6685 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006686#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006687 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006688
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006689#ifdef FEAT_JOB_CHANNEL
6690 if (has_any_channel())
6691 ui_breakcheck_force(TRUE);
6692 else
6693#endif
6694 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006695#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006696 // Process the netbeans and clientserver messages that may have been
6697 // received in the call to ui_breakcheck() when the GUI is in use. This
6698 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006699 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006700#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006701
6702# ifdef ELAPSED_FUNC
6703 // actual time passed
6704 done = ELAPSED_FUNC(start_tv);
6705# else
6706 // guestimate time passed (will actually be more)
6707 done += wait_now;
6708# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006710
6711 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6712 // input buffer, otherwise a following call to input() fails.
6713 if (got_int)
6714 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715}
6716
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717/*
6718 * ":winsize" command (obsolete).
6719 */
6720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006721ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 int w, h;
6724 char_u *arg = eap->arg;
6725 char_u *p;
6726
6727 w = getdigits(&arg);
6728 arg = skipwhite(arg);
6729 p = arg;
6730 h = getdigits(&arg);
6731 if (*p != NUL && *arg == NUL)
6732 set_shellsize(w, h, TRUE);
6733 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006734 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735}
6736
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006738ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 int xchar = NUL;
6741 char_u *p;
6742
6743 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6744 {
6745 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
6746 if (eap->arg[1] == NUL)
6747 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006748 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 return;
6750 }
6751 xchar = eap->arg[1];
6752 p = eap->arg + 2;
6753 }
6754 else
6755 p = eap->arg + 1;
6756
6757 eap->nextcmd = check_nextcmd(p);
6758 p = skipwhite(p);
6759 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006760 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006761 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 /* Pass flags on for ":vertical wincmd ]". */
6764 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006765 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6767 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006768 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 }
6770}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
Bram Moolenaar843ee412004-06-30 16:16:41 +00006772#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773/*
6774 * ":winpos".
6775 */
6776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006777ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 int x, y;
6780 char_u *arg = eap->arg;
6781 char_u *p;
6782
6783 if (*arg == NUL)
6784 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006785# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006786# ifdef VIMDLL
6787 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6788 mch_get_winpos(&x, &y) != FAIL)
6789# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006791# else
6792 if (mch_get_winpos(&x, &y) != FAIL)
6793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {
6795 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006796 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 }
6798 else
6799# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006800 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 }
6802 else
6803 {
6804 x = getdigits(&arg);
6805 arg = skipwhite(arg);
6806 p = arg;
6807 y = getdigits(&arg);
6808 if (*p == NUL || *arg != NUL)
6809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006810 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 return;
6812 }
6813# ifdef FEAT_GUI
6814 if (gui.in_use)
6815 gui_mch_set_winpos(x, y);
6816 else if (gui.starting)
6817 {
6818 /* Remember the coordinates for when the window is opened. */
6819 gui_win_x = x;
6820 gui_win_y = y;
6821 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006822# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 else
6824# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006825# endif
6826# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006827 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828# endif
6829# ifdef HAVE_TGETENT
6830 if (*T_CWP)
6831 term_set_winpos(x, y);
6832# endif
6833 }
6834}
6835#endif
6836
6837/*
6838 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6839 */
6840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006841ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842{
6843 oparg_T oa;
6844
6845 clear_oparg(&oa);
6846 oa.regname = eap->regname;
6847 oa.start.lnum = eap->line1;
6848 oa.end.lnum = eap->line2;
6849 oa.line_count = eap->line2 - eap->line1 + 1;
6850 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
6853 {
6854 setpcmark();
6855 curwin->w_cursor.lnum = eap->line1;
6856 beginline(BL_SOL | BL_FIX);
6857 }
6858
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006859 if (VIsual_active)
6860 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006861
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 switch (eap->cmdidx)
6863 {
6864 case CMD_delete:
6865 oa.op_type = OP_DELETE;
6866 op_delete(&oa);
6867 break;
6868
6869 case CMD_yank:
6870 oa.op_type = OP_YANK;
6871 (void)op_yank(&oa, FALSE, TRUE);
6872 break;
6873
6874 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02006875 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006877 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6878#else
6879 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006881 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 oa.op_type = OP_RSHIFT;
6883 else
6884 oa.op_type = OP_LSHIFT;
6885 op_shift(&oa, FALSE, eap->amount);
6886 break;
6887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006889 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890}
6891
6892/*
6893 * ":put".
6894 */
6895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006896ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897{
6898 /* ":0put" works like ":1put!". */
6899 if (eap->line2 == 0)
6900 {
6901 eap->line2 = 1;
6902 eap->forceit = TRUE;
6903 }
6904 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006905 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6906 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907}
6908
6909/*
6910 * Handle ":copy" and ":move".
6911 */
6912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006913ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914{
6915 long n;
6916
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02006917 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (eap->arg == NULL) /* error detected */
6919 {
6920 eap->nextcmd = NULL;
6921 return;
6922 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00006923 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924
6925 /*
6926 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
6927 */
6928 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
6929 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02006930 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 return;
6932 }
6933
6934 if (eap->cmdidx == CMD_move)
6935 {
6936 if (do_move(eap->line1, eap->line2, n) == FAIL)
6937 return;
6938 }
6939 else
6940 ex_copy(eap->line1, eap->line2, n);
6941 u_clearline();
6942 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006943 ex_may_print(eap);
6944}
6945
6946/*
6947 * Print the current line if flags were given to the Ex command.
6948 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02006949 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006950ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006951{
6952 if (eap->flags != 0)
6953 {
6954 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
6955 (eap->flags & EXFLAG_LIST));
6956 ex_no_reprint = TRUE;
6957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958}
6959
6960/*
6961 * ":smagic" and ":snomagic".
6962 */
6963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006964ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965{
6966 int magic_save = p_magic;
6967
6968 p_magic = (eap->cmdidx == CMD_smagic);
6969 do_sub(eap);
6970 p_magic = magic_save;
6971}
6972
6973/*
6974 * ":join".
6975 */
6976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006977ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 curwin->w_cursor.lnum = eap->line1;
6980 if (eap->line1 == eap->line2)
6981 {
6982 if (eap->addr_count >= 2) /* :2,2join does nothing */
6983 return;
6984 if (eap->line2 == curbuf->b_ml.ml_line_count)
6985 {
6986 beep_flush();
6987 return;
6988 }
6989 ++eap->line2;
6990 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02006991 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006993 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994}
6995
6996/*
6997 * ":[addr]@r" or ":[addr]*r": execute register
6998 */
6999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007000ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001{
7002 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007003 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
7005 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007006 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
7008#ifdef USE_ON_FLY_SCROLL
7009 dont_scroll = TRUE; /* disallow scrolling here */
7010#endif
7011
7012 /* get the register name. No name means to use the previous one */
7013 c = *eap->arg;
7014 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7015 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007016 /* Put the register in the typeahead buffer with the "silent" flag. */
7017 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7018 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007019 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 else
7023 {
7024 int save_efr = exec_from_reg;
7025
7026 exec_from_reg = TRUE;
7027
7028 /*
7029 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007030 * Continue until the stuff buffer is empty and all added characters
7031 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007033 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7035
7036 exec_from_reg = save_efr;
7037 }
7038}
7039
7040/*
7041 * ":!".
7042 */
7043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007044ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7047}
7048
7049/*
7050 * ":undo".
7051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007053ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054{
Bram Moolenaard3667a22006-03-16 21:35:52 +00007055 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02007056 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007057 else
7058 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059}
7060
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007061#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007063ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007064{
7065 char_u hash[UNDO_HASH_SIZE];
7066
7067 u_compute_hash(hash);
7068 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7069}
7070
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007071 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007073{
7074 char_u hash[UNDO_HASH_SIZE];
7075
7076 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007077 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007078}
7079#endif
7080
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081/*
7082 * ":redo".
7083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007085ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
7087 u_redo(1);
7088}
7089
7090/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007091 * ":earlier" and ":later".
7092 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007094ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007095{
7096 long count = 0;
7097 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007098 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007099 char_u *p = eap->arg;
7100
7101 if (*p == NUL)
7102 count = 1;
7103 else if (isdigit(*p))
7104 {
7105 count = getdigits(&p);
7106 switch (*p)
7107 {
7108 case 's': ++p; sec = TRUE; break;
7109 case 'm': ++p; sec = TRUE; count *= 60; break;
7110 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007111 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7112 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007113 }
7114 }
7115
7116 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007117 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007118 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007119 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7120 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007121}
7122
7123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 * ":redir": start/stop redirection.
7125 */
7126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007127ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
7129 char *mode;
7130 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007131 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132
Bram Moolenaarba768492016-07-08 15:32:54 +02007133#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007134 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007136 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007137 return;
7138 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007139#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 if (STRICMP(eap->arg, "END") == 0)
7142 close_redir();
7143 else
7144 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007145 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007147 ++arg;
7148 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007150 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 mode = "a";
7152 }
7153 else
7154 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007155 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 close_redir();
7158
7159 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007160 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 if (fname == NULL)
7162 return;
7163#ifdef FEAT_BROWSE
7164 if (cmdmod.browse)
7165 {
7166 char_u *browseFile;
7167
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007168 browseFile = do_browse(BROWSE_SAVE,
7169 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007170 fname, NULL, NULL,
7171 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 if (browseFile == NULL)
7173 return; /* operation cancelled */
7174 vim_free(fname);
7175 fname = browseFile;
7176 eap->forceit = TRUE; /* since dialog already asked */
7177 }
7178#endif
7179
7180 redir_fd = open_exfile(fname, eap->forceit, mode);
7181 vim_free(fname);
7182 }
7183#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007184 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 {
7186 /* redirect to a register a-z (resp. A-Z for appending) */
7187 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007188 ++arg;
7189 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007191 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007192 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007194 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007196 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007197 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00007198 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007199 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00007201 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007202 if (*arg == '>')
7203 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007204 /* Make register empty when not using @A-@Z and the
7205 * command is valid. */
7206 if (*arg == NUL && !isupper(redir_reg))
7207 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007209 }
7210 if (*arg != NUL)
7211 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007212 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007213 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007214 }
7215 }
7216 else if (*arg == '=' && arg[1] == '>')
7217 {
7218 int append;
7219
7220 /* redirect to a variable */
7221 close_redir();
7222 arg += 2;
7223
7224 if (*arg == '>')
7225 {
7226 ++arg;
7227 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 }
7229 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007230 append = FALSE;
7231
7232 if (var_redir_start(skipwhite(arg), append) == OK)
7233 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 }
7235#endif
7236
7237 /* TODO: redirect to a buffer */
7238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007240 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007242
7243 /* Make sure redirection is not off. Can happen for cmdline completion
7244 * that indirectly invokes a command to catch its output. */
7245 if (redir_fd != NULL
7246#ifdef FEAT_EVAL
7247 || redir_reg || redir_vname
7248#endif
7249 )
7250 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251}
7252
7253/*
7254 * ":redraw": force redraw
7255 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007256 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007257ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 int r = RedrawingDisabled;
7260 int p = p_lz;
7261
7262 RedrawingDisabled = 0;
7263 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007264 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007266 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267#ifdef FEAT_TITLE
7268 if (need_maketitle)
7269 maketitle();
7270#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007271#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7272# ifdef VIMDLL
7273 if (!gui.in_use)
7274# endif
7275 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 RedrawingDisabled = r;
7278 p_lz = p;
7279
7280 /* Reset msg_didout, so that a message that's there is overwritten. */
7281 msg_didout = FALSE;
7282 msg_col = 0;
7283
Bram Moolenaar56667a52013-07-17 11:54:28 +02007284 /* No need to wait after an intentional redraw. */
7285 need_wait_return = FALSE;
7286
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 out_flush();
7288}
7289
7290/*
7291 * ":redrawstatus": force redraw of status line(s)
7292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007294ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 int r = RedrawingDisabled;
7297 int p = p_lz;
7298
7299 RedrawingDisabled = 0;
7300 p_lz = FALSE;
7301 if (eap->forceit)
7302 status_redraw_all();
7303 else
7304 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007305 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 RedrawingDisabled = r;
7307 p_lz = p;
7308 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309}
7310
Bram Moolenaare12bab32019-01-08 22:02:56 +01007311/*
7312 * ":redrawtabline": force redraw of the tabline
7313 */
7314 static void
7315ex_redrawtabline(exarg_T *eap UNUSED)
7316{
7317 int r = RedrawingDisabled;
7318 int p = p_lz;
7319
7320 RedrawingDisabled = 0;
7321 p_lz = FALSE;
7322
7323 draw_tabline();
7324
7325 RedrawingDisabled = r;
7326 p_lz = p;
7327 out_flush();
7328}
7329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007331close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333 if (redir_fd != NULL)
7334 {
7335 fclose(redir_fd);
7336 redir_fd = NULL;
7337 }
7338#ifdef FEAT_EVAL
7339 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007340 if (redir_vname)
7341 {
7342 var_redir_stop();
7343 redir_vname = 0;
7344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345#endif
7346}
7347
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007348#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007349 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007350vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007351{
7352 if (vim_mkdir(name, prot) != 0)
7353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007354 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007355 return FAIL;
7356 }
7357 return OK;
7358}
7359#endif
7360
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361/*
7362 * Open a file for writing for an Ex command, with some checks.
7363 * Return file descriptor, or NULL on failure.
7364 */
7365 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007366open_exfile(
7367 char_u *fname,
7368 int forceit,
7369 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370{
7371 FILE *fd;
7372
7373#ifdef UNIX
7374 /* with Unix it is possible to open a directory */
7375 if (mch_isdir(fname))
7376 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007377 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 return NULL;
7379 }
7380#endif
7381 if (!forceit && *mode != 'a' && vim_fexists(fname))
7382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007383 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 return NULL;
7385 }
7386
7387 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007388 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389
7390 return fd;
7391}
7392
7393/*
7394 * ":mark" and ":k".
7395 */
7396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007397ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
7399 pos_T pos;
7400
7401 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007402 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007404 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 else
7406 {
7407 pos = curwin->w_cursor; /* save curwin->w_cursor */
7408 curwin->w_cursor.lnum = eap->line2;
7409 beginline(BL_WHITE | BL_FIX);
7410 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007411 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 curwin->w_cursor = pos; /* restore curwin->w_cursor */
7413 }
7414}
7415
7416/*
7417 * Update w_topline, w_leftcol and the cursor position.
7418 */
7419 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007420update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 check_cursor(); /* put cursor on valid line */
7423 update_topline();
7424 if (!curwin->w_p_wrap)
7425 validate_cursor();
7426 update_curswant();
7427}
7428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007430 * Save the current State and go to Normal mode.
7431 * Return TRUE if the typeahead could be saved.
7432 */
7433 int
7434save_current_state(save_state_T *sst)
7435{
7436 sst->save_msg_scroll = msg_scroll;
7437 sst->save_restart_edit = restart_edit;
7438 sst->save_msg_didout = msg_didout;
7439 sst->save_State = State;
7440 sst->save_insertmode = p_im;
7441 sst->save_finish_op = finish_op;
7442 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007443 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007444
7445 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
7446 restart_edit = 0; /* don't go to Insert mode */
7447 p_im = FALSE; /* don't use 'insertmode' */
7448
7449 /*
7450 * Save the current typeahead. This is required to allow using ":normal"
7451 * from an event handler and makes sure we don't hang when the argument
7452 * ends with half a command.
7453 */
7454 save_typeahead(&sst->tabuf);
7455 return sst->tabuf.typebuf_valid;
7456}
7457
7458 void
7459restore_current_state(save_state_T *sst)
7460{
7461 /* Restore the previous typeahead. */
7462 restore_typeahead(&sst->tabuf);
7463
7464 msg_scroll = sst->save_msg_scroll;
7465 restart_edit = sst->save_restart_edit;
7466 p_im = sst->save_insertmode;
7467 finish_op = sst->save_finish_op;
7468 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007469 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007470 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
7471
7472 /* Restore the state (needed when called from a function executed for
7473 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
7474 State = sst->save_State;
7475#ifdef CURSOR_SHAPE
7476 ui_cursor_shape(); /* may show different cursor shape */
7477#endif
7478}
7479
7480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 * ":normal[!] {commands}": Execute normal mode commands.
7482 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007483 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007484ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007486 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487 char_u *arg = NULL;
7488 int l;
7489 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007491 if (ex_normal_lock > 0)
7492 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007493 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007494 return;
7495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 if (ex_normal_busy >= p_mmd)
7497 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007498 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 return;
7500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502 /*
7503 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7504 * this for the K_SPECIAL leading byte, otherwise special keys will not
7505 * work.
7506 */
7507 if (has_mbyte)
7508 {
7509 int len = 0;
7510
7511 /* Count the number of characters to be escaped. */
7512 for (p = eap->arg; *p != NUL; ++p)
7513 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007514#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 if (*p == CSI) /* leadbyte CSI */
7516 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007517#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007518 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01007520#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 )
7524 len += 2;
7525 }
7526 if (len > 0)
7527 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007528 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 if (arg != NULL)
7530 {
7531 len = 0;
7532 for (p = eap->arg; *p != NUL; ++p)
7533 {
7534 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007535#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 if (*p == CSI)
7537 {
7538 arg[len++] = KS_EXTRA;
7539 arg[len++] = (int)KE_CSI;
7540 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007541#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007542 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 {
7544 arg[len++] = *++p;
7545 if (*p == K_SPECIAL)
7546 {
7547 arg[len++] = KS_SPECIAL;
7548 arg[len++] = KE_FILLER;
7549 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007550#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 else if (*p == CSI)
7552 {
7553 arg[len++] = KS_EXTRA;
7554 arg[len++] = (int)KE_CSI;
7555 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 }
7558 arg[len] = NUL;
7559 }
7560 }
7561 }
7562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007564 ++ex_normal_busy;
7565 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 {
7567 /*
7568 * Repeat the :normal command for each line in the range. When no
7569 * range given, execute it just once, without positioning the cursor
7570 * first.
7571 */
7572 do
7573 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (eap->addr_count != 0)
7575 {
7576 curwin->w_cursor.lnum = eap->line1++;
7577 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007578 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 }
7580
Bram Moolenaar13505972019-01-24 15:04:48 +01007581 exec_normal_cmd(arg != NULL
7582 ? arg
7583 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 }
7585 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7586 }
7587
7588 /* Might not return to the main loop when in an event handler. */
7589 update_topline_cursor();
7590
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007591 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007593 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007594#ifdef CURSOR_SHAPE
7595 ui_cursor_shape(); /* may show different cursor shape */
7596#endif
7597
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599}
7600
7601/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007602 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 */
7604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007605ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007607 if (eap->forceit)
7608 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007609 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007610 if (!curwin->w_cursor.lnum)
7611 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007612 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007613 }
7614
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007615 // Ignore the command when already in Insert mode. Inserting an
7616 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007617 if (State & INSERT)
7618 return;
7619
Bram Moolenaar64969662005-12-14 21:59:55 +00007620 if (eap->cmdidx == CMD_startinsert)
7621 restart_edit = 'a';
7622 else if (eap->cmdidx == CMD_startreplace)
7623 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007625 restart_edit = 'V';
7626
7627 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007629 if (eap->cmdidx == CMD_startinsert)
7630 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007631 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
7633}
7634
7635/*
7636 * ":stopinsert"
7637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007639ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 restart_edit = 0;
7642 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007643 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007646/*
7647 * Execute normal mode command "cmd".
7648 * "remap" can be REMAP_NONE or REMAP_YES.
7649 */
7650 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007651exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007652{
Bram Moolenaar25281632016-01-21 23:32:32 +01007653 /* Stuff the argument into the typeahead buffer. */
7654 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007655 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007656}
Bram Moolenaar25281632016-01-21 23:32:32 +01007657
Bram Moolenaar25281632016-01-21 23:32:32 +01007658/*
7659 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007660 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007661 */
7662 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007663exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007664{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007665 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007666 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007667
Bram Moolenaar905dd902019-04-07 14:21:47 +02007668 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7669 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007670 clear_oparg(&oa);
7671 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007672 while ((!stuff_empty()
7673 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007674 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007675 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007676 {
7677 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007678#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007679 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007680 && oa.op_type == OP_NOP && oa.regname == NUL
7681 && !VIsual_active)
7682 {
7683 /* If terminal_loop() returns OK we got a key that is handled
7684 * in Normal model. With FAIL we first need to position the
7685 * cursor and the screen needs to be redrawn. */
7686 if (terminal_loop(TRUE) == OK)
7687 normal_cmd(&oa, TRUE);
7688 }
7689 else
7690#endif
7691 /* execute a Normal mode cmd */
7692 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007693 }
7694}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007695
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696#ifdef FEAT_FIND_ID
7697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7701 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7702 (linenr_T)1, (linenr_T)MAXLNUM);
7703}
7704
Bram Moolenaar4033c552017-09-16 20:54:51 +02007705#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706/*
7707 * ":psearch"
7708 */
7709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007710ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711{
7712 g_do_tagpreview = p_pvh;
7713 ex_findpat(eap);
7714 g_do_tagpreview = 0;
7715}
7716#endif
7717
7718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007719ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
7721 int whole = TRUE;
7722 long n;
7723 char_u *p;
7724 int action;
7725
7726 switch (cmdnames[eap->cmdidx].cmd_name[2])
7727 {
7728 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
7729 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7730 action = ACTION_GOTO;
7731 else
7732 action = ACTION_SHOW;
7733 break;
7734 case 'i': /* ":ilist" and ":dlist" */
7735 action = ACTION_SHOW_ALL;
7736 break;
7737 case 'u': /* ":ijump" and ":djump" */
7738 action = ACTION_GOTO;
7739 break;
7740 default: /* ":isplit" and ":dsplit" */
7741 action = ACTION_SPLIT;
7742 break;
7743 }
7744
7745 n = 1;
7746 if (vim_isdigit(*eap->arg)) /* get count */
7747 {
7748 n = getdigits(&eap->arg);
7749 eap->arg = skipwhite(eap->arg);
7750 }
7751 if (*eap->arg == '/') /* Match regexp, not just whole words */
7752 {
7753 whole = FALSE;
7754 ++eap->arg;
7755 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7756 if (*p)
7757 {
7758 *p++ = NUL;
7759 p = skipwhite(p);
7760
7761 /* Check for trailing illegal characters */
7762 if (!ends_excmd(*p))
7763 eap->errmsg = e_trailing;
7764 else
7765 eap->nextcmd = check_nextcmd(p);
7766 }
7767 }
7768 if (!eap->skip)
7769 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7770 whole, !eap->forceit,
7771 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7772 n, action, eap->line1, eap->line2);
7773}
7774#endif
7775
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776
Bram Moolenaar4033c552017-09-16 20:54:51 +02007777#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778/*
7779 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7780 */
7781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007782ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01007784 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7786}
7787
7788/*
7789 * ":pedit"
7790 */
7791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007792ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 win_T *curwin_save = curwin;
7795
Bram Moolenaar026587b2019-08-17 15:08:00 +02007796 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007798 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007799
Bram Moolenaar026587b2019-08-17 15:08:00 +02007800 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007802
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 if (curwin != curwin_save && win_valid(curwin_save))
7804 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007805 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 validate_cursor();
7807 redraw_later(VALID);
7808 win_enter(curwin_save, TRUE);
7809 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007810# ifdef FEAT_TEXT_PROP
7811 else if (WIN_IS_POPUP(curwin))
7812 {
7813 // can't keep focus in popup window
7814 win_enter(firstwin, TRUE);
7815 }
7816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 g_do_tagpreview = 0;
7818}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820
7821/*
7822 * ":stag", ":stselect" and ":stjump".
7823 */
7824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007825ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826{
7827 postponed_split = -1;
7828 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007829 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7831 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007832 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834
7835/*
7836 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7837 */
7838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007839ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840{
7841 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7842}
7843
7844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007845ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846{
7847 int cmd;
7848
7849 switch (name[1])
7850 {
7851 case 'j': cmd = DT_JUMP; /* ":tjump" */
7852 break;
7853 case 's': cmd = DT_SELECT; /* ":tselect" */
7854 break;
7855 case 'p': cmd = DT_PREV; /* ":tprevious" */
7856 break;
7857 case 'N': cmd = DT_PREV; /* ":tNext" */
7858 break;
7859 case 'n': cmd = DT_NEXT; /* ":tnext" */
7860 break;
7861 case 'o': cmd = DT_POP; /* ":pop" */
7862 break;
7863 case 'f': /* ":tfirst" */
7864 case 'r': cmd = DT_FIRST; /* ":trewind" */
7865 break;
7866 case 'l': cmd = DT_LAST; /* ":tlast" */
7867 break;
7868 default: /* ":tag" */
7869#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007870 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007872 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 return;
7874 }
7875#endif
7876 cmd = DT_TAG;
7877 break;
7878 }
7879
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007880 if (name[0] == 'l')
7881 {
7882#ifndef FEAT_QUICKFIX
7883 ex_ni(eap);
7884 return;
7885#else
7886 cmd = DT_LTAG;
7887#endif
7888 }
7889
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7891 eap->forceit, TRUE);
7892}
7893
7894/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007895 * Check "str" for starting with a special cmdline variable.
7896 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7897 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7898 */
7899 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007901{
7902 int len;
7903 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007904 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007905 "%",
7906#define SPEC_PERC 0
7907 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02007908#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007909 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007910#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007911 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007912#define SPEC_CCWORD (SPEC_CWORD + 1)
7913 "<cexpr>", /* expr under cursor */
7914#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007915 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007916#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007917 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007918#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007919 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007920#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007921 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007922#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007923 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007924#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007925 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007926#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007927 "<sflnum>", /* script file line number */
7928#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007929#ifdef FEAT_CLIENTSERVER
7930 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007931# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007932#endif
7933 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007934
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007935 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007936 {
7937 len = (int)STRLEN(spec_str[i]);
7938 if (STRNCMP(src, spec_str[i], len) == 0)
7939 {
7940 *usedlen = len;
7941 return i;
7942 }
7943 }
7944 return -1;
7945}
7946
7947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 * Evaluate cmdline variables.
7949 *
7950 * change '%' to curbuf->b_ffname
7951 * '#' to curwin->w_altfile
7952 * '<cword>' to word under the cursor
7953 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02007954 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 * '<cfile>' to path name under the cursor
7956 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007957 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 * '<afile>' to file name for autocommand
7959 * '<abuf>' to buffer number for autocommand
7960 * '<amatch>' to matching name for autocommand
7961 *
7962 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
7963 * "" for error without a message) and NULL is returned.
7964 * Returns an allocated string if a valid match was found.
7965 * Returns NULL if no match was found. "usedlen" then still contains the
7966 * number of characters to skip.
7967 */
7968 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969eval_vars(
7970 char_u *src, /* pointer into commandline */
7971 char_u *srcstart, /* beginning of valid memory for src */
7972 int *usedlen, /* characters after src that are used */
7973 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007974 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007975 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00007976 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
7978 int i;
7979 char_u *s;
7980 char_u *result;
7981 char_u *resultbuf = NULL;
7982 int resultlen;
7983 buf_T *buf;
7984 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
7985 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02007986 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00007991 if (escaped != NULL)
7992 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993
7994 /*
7995 * Check if there is something to do.
7996 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007997 spec_idx = find_cmdline_var(src, usedlen);
7998 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {
8000 *usedlen = 1;
8001 return NULL;
8002 }
8003
8004 /*
8005 * Skip when preceded with a backslash "\%" and "\#".
8006 * Note: In "\\%" the % is also not recognized!
8007 */
8008 if (src > srcstart && src[-1] == '\\')
8009 {
8010 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00008011 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 return NULL;
8013 }
8014
8015 /*
8016 * word or WORD under cursor
8017 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008018 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8019 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008021 resultlen = find_ident_under_cursor(&result,
8022 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8023 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8024 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (resultlen == 0)
8026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008027 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 return NULL;
8029 }
8030 }
8031
8032 /*
8033 * '#': Alternate file name
8034 * '%': Current file name
8035 * File name under the cursor
8036 * File name for autocommand
8037 * and following modifiers
8038 */
8039 else
8040 {
8041 switch (spec_idx)
8042 {
8043 case SPEC_PERC: /* '%': current file */
8044 if (curbuf->b_fname == NULL)
8045 {
8046 result = (char_u *)"";
8047 valid = 0; /* Must have ":p:h" to be valid */
8048 }
8049 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008050 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008052 tilde_file = STRCMP(result, "~") == 0;
8053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 break;
8055
8056 case SPEC_HASH: /* '#' or "#99": alternate file */
8057 if (src[1] == '#') /* "##": the argument list */
8058 {
8059 result = arg_all();
8060 resultbuf = result;
8061 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008062 if (escaped != NULL)
8063 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 break;
8066 }
8067 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008068 if (*s == '<') /* "#<99" uses v:oldfiles */
8069 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008071 if (s == src + 2 && src[1] == '-')
8072 /* just a minus sign, don't skip over it */
8073 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 *usedlen = (int)(s - src); /* length of what we expand */
8075
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008076 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008078 if (*usedlen < 2)
8079 {
8080 /* Should we give an error message for #<text? */
8081 *usedlen = 1;
8082 return NULL;
8083 }
8084#ifdef FEAT_EVAL
8085 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8086 (long)i);
8087 if (result == NULL)
8088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008089 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008090 return NULL;
8091 }
8092#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008093 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 }
8097 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008098 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008099 if (i == 0 && src[1] == '<' && *usedlen > 1)
8100 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008101 buf = buflist_findnr(i);
8102 if (buf == NULL)
8103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008104 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008105 return NULL;
8106 }
8107 if (lnump != NULL)
8108 *lnump = ECMD_LAST;
8109 if (buf->b_fname == NULL)
8110 {
8111 result = (char_u *)"";
8112 valid = 0; /* Must have ":p:h" to be valid */
8113 }
8114 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008115 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008116 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008117 tilde_file = STRCMP(result, "~") == 0;
8118 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 break;
8121
8122#ifdef FEAT_SEARCHPATH
8123 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008124 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 if (result == NULL)
8126 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008127 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 return NULL;
8129 }
8130 resultbuf = result; /* remember allocated string */
8131 break;
8132#endif
8133
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 case SPEC_AFILE: /* file name for autocommand */
8135 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008136 if (result != NULL && !autocmd_fname_full)
8137 {
8138 /* Still need to turn the fname into a full path. It is
8139 * postponed to avoid a delay when <afile> is not used. */
8140 autocmd_fname_full = TRUE;
8141 result = FullName_save(autocmd_fname, FALSE);
8142 vim_free(autocmd_fname);
8143 autocmd_fname = result;
8144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (result == NULL)
8146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008147 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 return NULL;
8149 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008150 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 break;
8152
8153 case SPEC_ABUF: /* buffer number for autocommand */
8154 if (autocmd_bufnr <= 0)
8155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008156 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 return NULL;
8158 }
8159 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8160 result = strbuf;
8161 break;
8162
8163 case SPEC_AMATCH: /* match name for autocommand */
8164 result = autocmd_match;
8165 if (result == NULL)
8166 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008167 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 return NULL;
8169 }
8170 break;
8171
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 case SPEC_SFILE: /* file name for ":so" command */
8173 result = sourcing_name;
8174 if (result == NULL)
8175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008176 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 return NULL;
8178 }
8179 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008180
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008181 case SPEC_SLNUM: /* line in file for ":so" command */
8182 if (sourcing_name == NULL || sourcing_lnum == 0)
8183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008184 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008185 return NULL;
8186 }
8187 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
8188 result = strbuf;
8189 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008190
8191#ifdef FEAT_EVAL
8192 case SPEC_SFLNUM: /* line in script file */
8193 if (current_sctx.sc_lnum + sourcing_lnum == 0)
8194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008195 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008196 return NULL;
8197 }
8198 sprintf((char *)strbuf, "%ld",
8199 (long)(current_sctx.sc_lnum + sourcing_lnum));
8200 result = strbuf;
8201 break;
8202#endif
8203
8204#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008206 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8207 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 result = strbuf;
8209 break;
8210#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008211
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008212 default:
8213 result = (char_u *)""; /* avoid gcc warning */
8214 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 }
8216
8217 resultlen = (int)STRLEN(result); /* length of new string */
8218 if (src[*usedlen] == '<') /* remove the file name extension */
8219 {
8220 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 resultlen = (int)(s - result);
8223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 else if (!skip_mod)
8225 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008226 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 &resultlen);
8228 if (result == NULL)
8229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008230 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 return NULL;
8232 }
8233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
8235
8236 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8237 {
8238 if (valid != VALID_HEAD + VALID_PATH)
8239 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008240 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008242 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 result = NULL;
8244 }
8245 else
8246 result = vim_strnsave(result, resultlen);
8247 vim_free(resultbuf);
8248 return result;
8249}
8250
8251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 * Expand the <sfile> string in "arg".
8253 *
8254 * Returns an allocated string, or NULL for any error.
8255 */
8256 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008257expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008259 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 int len;
8261 char_u *result;
8262 char_u *newres;
8263 char_u *repl;
8264 int srclen;
8265 char_u *p;
8266
8267 result = vim_strsave(arg);
8268 if (result == NULL)
8269 return NULL;
8270
8271 for (p = result; *p; )
8272 {
8273 if (STRNCMP(p, "<sfile>", 7) != 0)
8274 ++p;
8275 else
8276 {
8277 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00008278 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 if (errormsg != NULL)
8280 {
8281 if (*errormsg)
8282 emsg(errormsg);
8283 vim_free(result);
8284 return NULL;
8285 }
8286 if (repl == NULL) /* no match (cannot happen) */
8287 {
8288 p += srclen;
8289 continue;
8290 }
8291 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8292 newres = alloc(len);
8293 if (newres == NULL)
8294 {
8295 vim_free(repl);
8296 vim_free(result);
8297 return NULL;
8298 }
8299 mch_memmove(newres, result, (size_t)(p - result));
8300 STRCPY(newres + (p - result), repl);
8301 len = (int)STRLEN(newres);
8302 STRCAT(newres, p + srclen);
8303 vim_free(repl);
8304 vim_free(result);
8305 result = newres;
8306 p = newres + len; /* continue after the match */
8307 }
8308 }
8309
8310 return result;
8311}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008314/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008315 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008316 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008319dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (fname == NULL)
8322 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008323 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324}
8325#endif
8326
8327/*
8328 * ":behave {mswin,xterm}"
8329 */
8330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008331ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 if (STRCMP(eap->arg, "mswin") == 0)
8334 {
8335 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8336 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8337 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8338 set_option_value((char_u *)"keymodel", 0L,
8339 (char_u *)"startsel,stopsel", 0);
8340 }
8341 else if (STRCMP(eap->arg, "xterm") == 0)
8342 {
8343 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8344 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8345 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8346 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8347 }
8348 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008349 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350}
8351
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352static int filetype_detect = FALSE;
8353static int filetype_plugin = FALSE;
8354static int filetype_indent = FALSE;
8355
8356/*
8357 * ":filetype [plugin] [indent] {on,off,detect}"
8358 * on: Load the filetype.vim file to install autocommands for file types.
8359 * off: Load the ftoff.vim file to remove all autocommands for file types.
8360 * plugin on: load filetype.vim and ftplugin.vim
8361 * plugin off: load ftplugof.vim
8362 * indent on: load filetype.vim and indent.vim
8363 * indent off: load indoff.vim
8364 */
8365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008366ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 char_u *arg = eap->arg;
8369 int plugin = FALSE;
8370 int indent = FALSE;
8371
8372 if (*eap->arg == NUL)
8373 {
8374 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008375 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 filetype_detect ? "ON" : "OFF",
8377 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8378 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8379 return;
8380 }
8381
8382 /* Accept "plugin" and "indent" in any order. */
8383 for (;;)
8384 {
8385 if (STRNCMP(arg, "plugin", 6) == 0)
8386 {
8387 plugin = TRUE;
8388 arg = skipwhite(arg + 6);
8389 continue;
8390 }
8391 if (STRNCMP(arg, "indent", 6) == 0)
8392 {
8393 indent = TRUE;
8394 arg = skipwhite(arg + 6);
8395 continue;
8396 }
8397 break;
8398 }
8399 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8400 {
8401 if (*arg == 'o' || !filetype_detect)
8402 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008403 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 filetype_detect = TRUE;
8405 if (plugin)
8406 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008407 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 filetype_plugin = TRUE;
8409 }
8410 if (indent)
8411 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008412 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 filetype_indent = TRUE;
8414 }
8415 }
8416 if (*arg == 'd')
8417 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008418 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008419 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 }
8421 }
8422 else if (STRCMP(arg, "off") == 0)
8423 {
8424 if (plugin || indent)
8425 {
8426 if (plugin)
8427 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008428 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 filetype_plugin = FALSE;
8430 }
8431 if (indent)
8432 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008433 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 filetype_indent = FALSE;
8435 }
8436 }
8437 else
8438 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008439 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 filetype_detect = FALSE;
8441 }
8442 }
8443 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008444 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445}
8446
8447/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008448 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 */
8450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008451ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008454 {
8455 char_u *arg = eap->arg;
8456
8457 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8458 arg += 9;
8459
8460 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8461 if (arg != eap->arg)
8462 did_filetype = FALSE;
8463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008467ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469#ifdef FEAT_DIGRAPHS
8470 if (*eap->arg != NUL)
8471 putdigraph(eap->arg);
8472 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008473 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008475 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476#endif
8477}
8478
8479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008480ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
8482 int flags = 0;
8483
8484 if (eap->cmdidx == CMD_setlocal)
8485 flags = OPT_LOCAL;
8486 else if (eap->cmdidx == CMD_setglobal)
8487 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008488#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 if (cmdmod.browse && flags == 0)
8490 ex_options(eap);
8491 else
8492#endif
8493 (void)do_set(eap->arg, flags);
8494}
8495
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008496#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8497 void
8498set_no_hlsearch(int flag)
8499{
8500 no_hlsearch = flag;
8501# ifdef FEAT_EVAL
8502 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8503# endif
8504}
8505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506/*
8507 * ":nohlsearch"
8508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008510ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008512 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008513 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#endif
8516
8517#ifdef FEAT_CRYPT
8518/*
8519 * ":X": Get crypt key
8520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008522ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008524 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008525 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526}
8527#endif
8528
8529#ifdef FEAT_FOLDING
8530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008531ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 if (foldManualAllowed(TRUE))
8534 foldCreate(eap->line1, eap->line2);
8535}
8536
8537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008538ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
8540 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8541 eap->forceit, FALSE);
8542}
8543
8544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008545ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 linenr_T lnum;
8548
Bram Moolenaar4facea32019-10-12 20:17:40 +02008549# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008550 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008551# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008552
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 /* First set the marks for all lines closed/open. */
8554 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8555 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8556 ml_setmarked(lnum);
8557
8558 /* Execute the command on the marked lines. */
8559 global_exe(eap->arg);
8560 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar4facea32019-10-12 20:17:40 +02008561# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008562 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
8565#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008566
Bram Moolenaar39665952018-08-15 20:59:48 +02008567#ifdef FEAT_QUICKFIX
8568/*
8569 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8570 * instead of a quickfix command.
8571 */
8572 int
8573is_loclist_cmd(int cmdidx)
8574{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008575 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008576 return FALSE;
8577 return cmdnames[cmdidx].cmd_name[0] == 'l';
8578}
8579#endif
8580
Bram Moolenaar4facea32019-10-12 20:17:40 +02008581#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008582 int
8583get_pressedreturn(void)
8584{
8585 return ex_pressedreturn;
8586}
8587
8588 void
8589set_pressedreturn(int val)
8590{
8591 ex_pressedreturn = val;
8592}
8593#endif