blob: a0030281a4dc11117e4dcb4f0770afbfd87649fe [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
Bram Moolenaar958636c2014-10-21 20:01:58 +020052/* Wether a command index indicates a user command. */
53# define IS_USER_CMDIDX(idx) ((int)(idx) < 0)
54
Bram Moolenaar071d4272004-06-13 20:20:40 +000055#else
56# define ex_command ex_ni
57# define ex_comclear ex_ni
58# define ex_delcommand ex_ni
Bram Moolenaar958636c2014-10-21 20:01:58 +020059/* Wether a command index indicates a user command. */
60# define IS_USER_CMDIDX(idx) (FALSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
62
Bram Moolenaarb96a7f32014-11-27 16:22:48 +010063static int compute_buffer_local_count __ARGS((int addr_type, int lnum, int local));
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000065static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000067static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000068static int if_level = 0; /* depth in :if */
69#endif
Bram Moolenaara6f4d612011-09-21 19:10:46 +020070static void append_command __ARGS((char_u *cmd));
Bram Moolenaar071d4272004-06-13 20:20:40 +000071static char_u *find_command __ARGS((exarg_T *eap, int *full));
72
73static void ex_abbreviate __ARGS((exarg_T *eap));
74static void ex_map __ARGS((exarg_T *eap));
75static void ex_unmap __ARGS((exarg_T *eap));
76static void ex_mapclear __ARGS((exarg_T *eap));
77static void ex_abclear __ARGS((exarg_T *eap));
78#ifndef FEAT_MENU
79# define ex_emenu ex_ni
80# define ex_menu ex_ni
81# define ex_menutranslate ex_ni
82#endif
83#ifdef FEAT_AUTOCMD
84static void ex_autocmd __ARGS((exarg_T *eap));
85static void ex_doautocmd __ARGS((exarg_T *eap));
86#else
87# define ex_autocmd ex_ni
88# define ex_doautocmd ex_ni
89# define ex_doautoall ex_ni
90#endif
91#ifdef FEAT_LISTCMDS
92static void ex_bunload __ARGS((exarg_T *eap));
93static void ex_buffer __ARGS((exarg_T *eap));
94static void ex_bmodified __ARGS((exarg_T *eap));
95static void ex_bnext __ARGS((exarg_T *eap));
96static void ex_bprevious __ARGS((exarg_T *eap));
97static void ex_brewind __ARGS((exarg_T *eap));
98static void ex_blast __ARGS((exarg_T *eap));
99#else
100# define ex_bunload ex_ni
101# define ex_buffer ex_ni
102# define ex_bmodified ex_ni
103# define ex_bnext ex_ni
104# define ex_bprevious ex_ni
105# define ex_brewind ex_ni
106# define ex_blast ex_ni
107# define buflist_list ex_ni
108# define ex_checktime ex_ni
109#endif
110#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
111# define ex_buffer_all ex_ni
112#endif
113static char_u *getargcmd __ARGS((char_u **));
114static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
115static int getargopt __ARGS((exarg_T *eap));
116#ifndef FEAT_QUICKFIX
117# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# define ex_cc ex_ni
120# define ex_cnext ex_ni
121# define ex_cfile ex_ni
122# define qf_list ex_ni
123# define qf_age ex_ni
124# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000125# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126#endif
127#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
128# define ex_cclose ex_ni
129# define ex_copen ex_ni
130# define ex_cwindow ex_ni
131#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000132#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
133# define ex_cexpr ex_ni
134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136static int check_more __ARGS((int, int));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100137static linenr_T get_address __ARGS((char_u **, int addr_type, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000138static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200139#if !defined(FEAT_PERL) \
140 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
141 || !defined(FEAT_TCL) \
142 || !defined(FEAT_RUBY) \
143 || !defined(FEAT_LUA) \
144 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000145# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static void ex_script_ni __ARGS((exarg_T *eap));
147#endif
148static char_u *invalid_range __ARGS((exarg_T *eap));
149static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000150#ifdef FEAT_QUICKFIX
151static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
154static void ex_highlight __ARGS((exarg_T *eap));
155static void ex_colorscheme __ARGS((exarg_T *eap));
156static void ex_quit __ARGS((exarg_T *eap));
157static void ex_cquit __ARGS((exarg_T *eap));
158static void ex_quit_all __ARGS((exarg_T *eap));
159#ifdef FEAT_WINDOWS
160static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000161static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static void ex_resize __ARGS((exarg_T *eap));
164static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000165static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000166static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000167static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000168static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000169static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170#else
171# define ex_close ex_ni
172# define ex_only ex_ni
173# define ex_all ex_ni
174# define ex_resize ex_ni
175# define ex_splitview ex_ni
176# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000177# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000178# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000179# define ex_tabs ex_ni
180# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000181# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#endif
183#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
184static void ex_pclose __ARGS((exarg_T *eap));
185static void ex_ptag __ARGS((exarg_T *eap));
186static void ex_pedit __ARGS((exarg_T *eap));
187#else
188# define ex_pclose ex_ni
189# define ex_ptag ex_ni
190# define ex_pedit ex_ni
191#endif
192static void ex_hide __ARGS((exarg_T *eap));
193static void ex_stop __ARGS((exarg_T *eap));
194static void ex_exit __ARGS((exarg_T *eap));
195static void ex_print __ARGS((exarg_T *eap));
196#ifdef FEAT_BYTEOFF
197static void ex_goto __ARGS((exarg_T *eap));
198#else
199# define ex_goto ex_ni
200#endif
201static void ex_shell __ARGS((exarg_T *eap));
202static void ex_preserve __ARGS((exarg_T *eap));
203static void ex_recover __ARGS((exarg_T *eap));
204#ifndef FEAT_LISTCMDS
205# define ex_argedit ex_ni
206# define ex_argadd ex_ni
207# define ex_argdelete ex_ni
208# define ex_listdo ex_ni
209#endif
210static void ex_mode __ARGS((exarg_T *eap));
211static void ex_wrongmodifier __ARGS((exarg_T *eap));
212static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000213static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214static void ex_edit __ARGS((exarg_T *eap));
215#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
216# define ex_drop ex_ni
217#endif
218#ifndef FEAT_GUI
219# define ex_gui ex_nogui
220static void ex_nogui __ARGS((exarg_T *eap));
221#endif
222#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
223static void ex_tearoff __ARGS((exarg_T *eap));
224#else
225# define ex_tearoff ex_ni
226#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000227#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static void ex_popup __ARGS((exarg_T *eap));
229#else
230# define ex_popup ex_ni
231#endif
232#ifndef FEAT_GUI_MSWIN
233# define ex_simalt ex_ni
234#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000235#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# define gui_mch_find_dialog ex_ni
237# define gui_mch_replace_dialog ex_ni
238#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000239#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240# define ex_helpfind ex_ni
241#endif
242#ifndef FEAT_CSCOPE
243# define do_cscope ex_ni
244# define do_scscope ex_ni
245# define do_cstag ex_ni
246#endif
247#ifndef FEAT_SYN_HL
248# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200249# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000250#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200251#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200252# define ex_syntime ex_ni
253#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000254#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000255# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000256# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000257# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000258# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000259# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000260#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200261#ifndef FEAT_PERSISTENT_UNDO
262# define ex_rundo ex_ni
263# define ex_wundo ex_ni
264#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200265#ifndef FEAT_LUA
266# define ex_lua ex_script_ni
267# define ex_luado ex_ni
268# define ex_luafile ex_ni
269#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000270#ifndef FEAT_MZSCHEME
271# define ex_mzscheme ex_script_ni
272# define ex_mzfile ex_ni
273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274#ifndef FEAT_PERL
275# define ex_perl ex_script_ni
276# define ex_perldo ex_ni
277#endif
278#ifndef FEAT_PYTHON
279# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200280# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_pyfile ex_ni
282#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200283#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200284# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200285# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200286# define ex_py3file ex_ni
287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288#ifndef FEAT_TCL
289# define ex_tcl ex_script_ni
290# define ex_tcldo ex_ni
291# define ex_tclfile ex_ni
292#endif
293#ifndef FEAT_RUBY
294# define ex_ruby ex_script_ni
295# define ex_rubydo ex_ni
296# define ex_rubyfile ex_ni
297#endif
298#ifndef FEAT_SNIFF
299# define ex_sniff ex_ni
300#endif
301#ifndef FEAT_KEYMAP
302# define ex_loadkeymap ex_ni
303#endif
304static void ex_swapname __ARGS((exarg_T *eap));
305static void ex_syncbind __ARGS((exarg_T *eap));
306static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307static void ex_pwd __ARGS((exarg_T *eap));
308static void ex_equal __ARGS((exarg_T *eap));
309static void ex_sleep __ARGS((exarg_T *eap));
310static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
311static void ex_winsize __ARGS((exarg_T *eap));
312#ifdef FEAT_WINDOWS
313static void ex_wincmd __ARGS((exarg_T *eap));
314#else
315# define ex_wincmd ex_ni
316#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000317#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318static void ex_winpos __ARGS((exarg_T *eap));
319#else
320# define ex_winpos ex_ni
321#endif
322static void ex_operators __ARGS((exarg_T *eap));
323static void ex_put __ARGS((exarg_T *eap));
324static void ex_copymove __ARGS((exarg_T *eap));
325static void ex_submagic __ARGS((exarg_T *eap));
326static void ex_join __ARGS((exarg_T *eap));
327static void ex_at __ARGS((exarg_T *eap));
328static void ex_bang __ARGS((exarg_T *eap));
329static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200330#ifdef FEAT_PERSISTENT_UNDO
331static void ex_wundo __ARGS((exarg_T *eap));
332static void ex_rundo __ARGS((exarg_T *eap));
333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000335static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336static void ex_redir __ARGS((exarg_T *eap));
337static void ex_redraw __ARGS((exarg_T *eap));
338static void ex_redrawstatus __ARGS((exarg_T *eap));
339static void close_redir __ARGS((void));
340static void ex_mkrc __ARGS((exarg_T *eap));
341static void ex_mark __ARGS((exarg_T *eap));
342#ifdef FEAT_USR_CMDS
343static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000344static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#endif
346#ifdef FEAT_EX_EXTRA
347static void ex_normal __ARGS((exarg_T *eap));
348static void ex_startinsert __ARGS((exarg_T *eap));
349static void ex_stopinsert __ARGS((exarg_T *eap));
350#else
351# define ex_normal ex_ni
352# define ex_align ex_ni
353# define ex_retab ex_ni
354# define ex_startinsert ex_ni
355# define ex_stopinsert ex_ni
356# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000357# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#endif
359#ifdef FEAT_FIND_ID
360static void ex_checkpath __ARGS((exarg_T *eap));
361static void ex_findpat __ARGS((exarg_T *eap));
362#else
363# define ex_findpat ex_ni
364# define ex_checkpath ex_ni
365#endif
366#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
367static void ex_psearch __ARGS((exarg_T *eap));
368#else
369# define ex_psearch ex_ni
370#endif
371static void ex_tag __ARGS((exarg_T *eap));
372static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
373#ifndef FEAT_EVAL
374# define ex_scriptnames ex_ni
375# define ex_finish ex_ni
376# define ex_echo ex_ni
377# define ex_echohl ex_ni
378# define ex_execute ex_ni
379# define ex_call ex_ni
380# define ex_if ex_ni
381# define ex_endif ex_ni
382# define ex_else ex_ni
383# define ex_while ex_ni
384# define ex_continue ex_ni
385# define ex_break ex_ni
386# define ex_endwhile ex_ni
387# define ex_throw ex_ni
388# define ex_try ex_ni
389# define ex_catch ex_ni
390# define ex_finally ex_ni
391# define ex_endtry ex_ni
392# define ex_endfunction ex_ni
393# define ex_let ex_ni
394# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000395# define ex_lockvar ex_ni
396# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397# define ex_function ex_ni
398# define ex_delfunction ex_ni
399# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000400# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#endif
402static char_u *arg_all __ARGS((void));
403#ifdef FEAT_SESSION
404static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000405static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406static void ex_loadview __ARGS((exarg_T *eap));
407static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000408static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#else
410# define ex_loadview ex_ni
411#endif
412#ifndef FEAT_EVAL
413# define ex_compiler ex_ni
414#endif
415#ifdef FEAT_VIMINFO
416static void ex_viminfo __ARGS((exarg_T *eap));
417#else
418# define ex_viminfo ex_ni
419#endif
420static void ex_behave __ARGS((exarg_T *eap));
421#ifdef FEAT_AUTOCMD
422static void ex_filetype __ARGS((exarg_T *eap));
423static void ex_setfiletype __ARGS((exarg_T *eap));
424#else
425# define ex_filetype ex_ni
426# define ex_setfiletype ex_ni
427#endif
428#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000429# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430# define ex_diffpatch ex_ni
431# define ex_diffgetput ex_ni
432# define ex_diffsplit ex_ni
433# define ex_diffthis ex_ni
434# define ex_diffupdate ex_ni
435#endif
436static void ex_digraphs __ARGS((exarg_T *eap));
437static void ex_set __ARGS((exarg_T *eap));
438#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
439# define ex_options ex_ni
440#endif
441#ifdef FEAT_SEARCH_EXTRA
442static void ex_nohlsearch __ARGS((exarg_T *eap));
443static void ex_match __ARGS((exarg_T *eap));
444#else
445# define ex_nohlsearch ex_ni
446# define ex_match ex_ni
447#endif
448#ifdef FEAT_CRYPT
449static void ex_X __ARGS((exarg_T *eap));
450#else
451# define ex_X ex_ni
452#endif
453#ifdef FEAT_FOLDING
454static void ex_fold __ARGS((exarg_T *eap));
455static void ex_foldopen __ARGS((exarg_T *eap));
456static void ex_folddo __ARGS((exarg_T *eap));
457#else
458# define ex_fold ex_ni
459# define ex_foldopen ex_ni
460# define ex_folddo ex_ni
461#endif
462#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
463 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
464# define ex_language ex_ni
465#endif
466#ifndef FEAT_SIGNS
467# define ex_sign ex_ni
468#endif
469#ifndef FEAT_SUN_WORKSHOP
470# define ex_wsverb ex_ni
471#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000472#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200473# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000474# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200475# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478#ifndef FEAT_EVAL
479# define ex_debug ex_ni
480# define ex_breakadd ex_ni
481# define ex_debuggreedy ex_ni
482# define ex_breakdel ex_ni
483# define ex_breaklist ex_ni
484#endif
485
486#ifndef FEAT_CMDHIST
487# define ex_history ex_ni
488#endif
489#ifndef FEAT_JUMPLIST
490# define ex_jumps ex_ni
491# define ex_changes ex_ni
492#endif
493
Bram Moolenaar05159a02005-02-26 23:04:13 +0000494#ifndef FEAT_PROFILE
495# define ex_profile ex_ni
496#endif
497
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498/*
499 * Declare cmdnames[].
500 */
501#define DO_DECLARE_EXCMD
502#include "ex_cmds.h"
503
504/*
505 * Table used to quickly search for a command, based on its first character.
506 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000507static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508{
509 CMD_append,
510 CMD_buffer,
511 CMD_change,
512 CMD_delete,
513 CMD_edit,
514 CMD_file,
515 CMD_global,
516 CMD_help,
517 CMD_insert,
518 CMD_join,
519 CMD_k,
520 CMD_list,
521 CMD_move,
522 CMD_next,
523 CMD_open,
524 CMD_print,
525 CMD_quit,
526 CMD_read,
527 CMD_substitute,
528 CMD_t,
529 CMD_undo,
530 CMD_vglobal,
531 CMD_write,
532 CMD_xit,
533 CMD_yank,
534 CMD_z,
535 CMD_bang
536};
537
538static char_u dollar_command[2] = {'$', 0};
539
540
541#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000542/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543typedef struct
544{
545 char_u *line; /* command line */
546 linenr_T lnum; /* sourcing_lnum of the line */
547} wcmd_T;
548
549/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000550 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000552 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000554struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555{
556 garray_T *lines_gap; /* growarray with line info */
557 int current_line; /* last read line from growarray */
558 int repeating; /* TRUE when looping a second time */
559 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
560 char_u *(*getline) __ARGS((int, void *, int));
561 void *cookie;
562};
563
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000564static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
565static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000567
568/* Struct to save a few things while debugging. Used in do_cmdline() only. */
569struct dbg_stuff
570{
571 int trylevel;
572 int force_abort;
573 except_T *caught_stack;
574 char_u *vv_exception;
575 char_u *vv_throwpoint;
576 int did_emsg;
577 int got_int;
578 int did_throw;
579 int need_rethrow;
580 int check_cstack;
581 except_T *current_exception;
582};
583
584static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
585static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
586
587 static void
588save_dbg_stuff(dsp)
589 struct dbg_stuff *dsp;
590{
591 dsp->trylevel = trylevel; trylevel = 0;
592 dsp->force_abort = force_abort; force_abort = FALSE;
593 dsp->caught_stack = caught_stack; caught_stack = NULL;
594 dsp->vv_exception = v_exception(NULL);
595 dsp->vv_throwpoint = v_throwpoint(NULL);
596
597 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
598 dsp->did_emsg = did_emsg; did_emsg = FALSE;
599 dsp->got_int = got_int; got_int = FALSE;
600 dsp->did_throw = did_throw; did_throw = FALSE;
601 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
602 dsp->check_cstack = check_cstack; check_cstack = FALSE;
603 dsp->current_exception = current_exception; current_exception = NULL;
604}
605
606 static void
607restore_dbg_stuff(dsp)
608 struct dbg_stuff *dsp;
609{
610 suppress_errthrow = FALSE;
611 trylevel = dsp->trylevel;
612 force_abort = dsp->force_abort;
613 caught_stack = dsp->caught_stack;
614 (void)v_exception(dsp->vv_exception);
615 (void)v_throwpoint(dsp->vv_throwpoint);
616 did_emsg = dsp->did_emsg;
617 got_int = dsp->got_int;
618 did_throw = dsp->did_throw;
619 need_rethrow = dsp->need_rethrow;
620 check_cstack = dsp->check_cstack;
621 current_exception = dsp->current_exception;
622}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623#endif
624
625
626/*
627 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
628 * command is given.
629 */
630 void
631do_exmode(improved)
632 int improved; /* TRUE for "improved Ex" mode */
633{
634 int save_msg_scroll;
635 int prev_msg_row;
636 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000637 int changedtick;
638
639 if (improved)
640 exmode_active = EXMODE_VIM;
641 else
642 exmode_active = EXMODE_NORMAL;
643 State = NORMAL;
644
645 /* When using ":global /pat/ visual" and then "Q" we return to continue
646 * the :global command. */
647 if (global_busy)
648 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650 save_msg_scroll = msg_scroll;
651 ++RedrawingDisabled; /* don't redisplay the window */
652 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653#ifdef FEAT_GUI
654 /* Ignore scrollbar and mouse events in Ex mode */
655 ++hold_gui_events;
656#endif
657#ifdef FEAT_SNIFF
658 want_sniff_request = 0; /* No K_SNIFF wanted */
659#endif
660
661 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
662 while (exmode_active)
663 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000664#ifdef FEAT_EX_EXTRA
665 /* Check for a ":normal" command and no more characters left. */
666 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
667 {
668 exmode_active = FALSE;
669 break;
670 }
671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 msg_scroll = TRUE;
673 need_wait_return = FALSE;
674 ex_pressedreturn = FALSE;
675 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 prev_msg_row = msg_row;
678 prev_line = curwin->w_cursor.lnum;
679#ifdef FEAT_SNIFF
680 ProcessSniffRequests();
681#endif
682 if (improved)
683 {
684 cmdline_row = msg_row;
685 do_cmdline(NULL, getexline, NULL, 0);
686 }
687 else
688 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
689 lines_left = Rows - 1;
690
Bram Moolenaardf177f62005-02-22 08:39:57 +0000691 if ((prev_line != curwin->w_cursor.lnum
692 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000694 if (curbuf->b_ml.ml_flags & ML_EMPTY)
695 EMSG(_(e_emptybuf));
696 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000698 if (ex_pressedreturn)
699 {
700 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000701 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000702 msg_row = prev_msg_row;
703 if (prev_msg_row == Rows - 1)
704 msg_row--;
705 }
706 msg_col = 0;
707 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
708 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000711 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
712 {
713 if (curbuf->b_ml.ml_flags & ML_EMPTY)
714 EMSG(_(e_emptybuf));
715 else
716 EMSG(_("E501: At end-of-file"));
717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 }
719
720#ifdef FEAT_GUI
721 --hold_gui_events;
722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 --RedrawingDisabled;
724 --no_wait_return;
725 update_screen(CLEAR);
726 need_wait_return = FALSE;
727 msg_scroll = save_msg_scroll;
728}
729
730/*
731 * Execute a simple command line. Used for translated commands like "*".
732 */
733 int
734do_cmdline_cmd(cmd)
735 char_u *cmd;
736{
737 return do_cmdline(cmd, NULL, NULL,
738 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
739}
740
741/*
742 * do_cmdline(): execute one Ex command line
743 *
744 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100745 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * 2. Split up in parts separated with '|'.
747 *
748 * This function can be called recursively!
749 *
750 * flags:
751 * DOCMD_VERBOSE - The command will be included in the error message.
752 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 * DOCMD_KEYTYPED - Don't reset KeyTyped.
755 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
756 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
757 *
758 * return FAIL if cmdline could not be executed, OK otherwise
759 */
760 int
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100761do_cmdline(cmdline, fgetline, cookie, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 char_u *cmdline;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 char_u *(*fgetline) __ARGS((int, void *, int));
764 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 int flags;
766{
767 char_u *next_cmdline; /* next cmd to execute */
768 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 static int recursive = 0; /* recursive depth */
771 int msg_didout_before_start = 0;
772 int count = 0; /* line number count */
773 int did_inc = FALSE; /* incremented RedrawingDisabled */
774 int retval = OK;
775#ifdef FEAT_EVAL
776 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000777 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 int current_line = 0; /* active line in lines_ga */
779 char_u *fname = NULL; /* function or script name */
780 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
781 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000782 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 int initial_trylevel;
784 struct msglist **saved_msg_list = NULL;
785 struct msglist *private_msg_list;
786
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100787 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 char_u *(*cmd_getline) __ARGS((int, void *, int));
789 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100794# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795# define cmd_cookie cookie
796#endif
797 static int call_depth = 0; /* recursiveness */
798
799#ifdef FEAT_EVAL
800 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
801 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000802 * This ensures that the do_errthrow() call in do_one_cmd() does not
803 * combine the messages stored by an earlier invocation of do_one_cmd()
804 * with the command name of the later one. This would happen when
805 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 saved_msg_list = msg_list;
807 msg_list = &private_msg_list;
808 private_msg_list = NULL;
809#endif
810
811 /* It's possible to create an endless loop with ":execute", catch that
812 * here. The value of 200 allows nested function calls, ":source", etc. */
813 if (call_depth == 200)
814 {
815 EMSG(_("E169: Command too recursive"));
816#ifdef FEAT_EVAL
817 /* When converting to an exception, we do not include the command name
818 * since this is not an error of the specific command. */
819 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
820 msg_list = saved_msg_list;
821#endif
822 return FAIL;
823 }
824 ++call_depth;
825
826#ifdef FEAT_EVAL
827 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000828 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 cstack.cs_trylevel = 0;
830 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000831 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
833
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100834 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
836 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000838 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 ++ex_nesting_level;
840
841 /* Get the function or script name and the address where the next breakpoint
842 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 {
845 fname = func_name(real_cookie);
846 breakpoint = func_breakpoint(real_cookie);
847 dbg_tick = func_dbg_tick(real_cookie);
848 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 {
851 fname = sourcing_name;
852 breakpoint = source_breakpoint(real_cookie);
853 dbg_tick = source_dbg_tick(real_cookie);
854 }
855
856 /*
857 * Initialize "force_abort" and "suppress_errthrow" at the top level.
858 */
859 if (!recursive)
860 {
861 force_abort = FALSE;
862 suppress_errthrow = FALSE;
863 }
864
865 /*
866 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000867 * exception handling (used when debugging). Otherwise clear it to avoid
868 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000870 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000871 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000872 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200873 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875 initial_trylevel = trylevel;
876
877 /*
878 * "did_throw" will be set to TRUE when an exception is being thrown.
879 */
880 did_throw = FALSE;
881#endif
882 /*
883 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000884 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * If force_abort is set, we cancel everything.
886 */
887 did_emsg = FALSE;
888
889 /*
890 * KeyTyped is only set when calling vgetc(). Reset it here when not
891 * calling vgetc() (sourced command lines).
892 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100893 if (!(flags & DOCMD_KEYTYPED)
894 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 KeyTyped = FALSE;
896
897 /*
898 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000899 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 * - for multiple commands on one line, separated with '|'
901 * - when repeating until there are no more lines (for ":source")
902 */
903 next_cmdline = cmdline;
904 do
905 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000906#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100907 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000908#endif
909
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000910 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (next_cmdline == NULL
912#ifdef FEAT_EVAL
913 && !force_abort
914 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000915 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916#endif
917 )
918 did_emsg = FALSE;
919
920 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000921 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100922 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 * 3. If a line is given: Make a copy, so we can mess with it.
924 */
925
926#ifdef FEAT_EVAL
927 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930 /* Each '|' separated command is stored separately in lines_ga, to
931 * be able to jump to it. Don't use next_cmdline now. */
932 vim_free(cmdline_copy);
933 cmdline_copy = NULL;
934
935 /* Check if a function has returned or, unless it has an unclosed
936 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000937 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000941 func_line_end(real_cookie);
942# endif
943 if (func_has_ended(real_cookie))
944 {
945 retval = FAIL;
946 break;
947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000949#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000950 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100951 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000952 script_line_end();
953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100956 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
958 retval = FAIL;
959 break;
960 }
961
962 /* If breakpoints have been added/deleted need to check for it. */
963 if (breakpoint != NULL && dbg_tick != NULL
964 && *dbg_tick != debug_tick)
965 {
966 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100967 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 fname, sourcing_lnum);
969 *dbg_tick = debug_tick;
970 }
971
972 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
973 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
974
975 /* Did we encounter a breakpoint? */
976 if (breakpoint != NULL && *breakpoint != 0
977 && *breakpoint <= sourcing_lnum)
978 {
979 dbg_breakpoint(fname, sourcing_lnum);
980 /* Find next breakpoint. */
981 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100982 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 fname, sourcing_lnum);
984 *dbg_tick = debug_tick;
985 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000986# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000987 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000988 {
989 if (getline_is_func)
990 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100991 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000992 script_line_start();
993 }
994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000997 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000999 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001000 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 * below, so that it stores lines in or reads them from
1002 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 * while/for loop. */
1004 cmd_getline = get_loop_line;
1005 cmd_cookie = (void *)&cmd_loop_cookie;
1006 cmd_loop_cookie.lines_gap = &lines_ga;
1007 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001008 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001009 cmd_loop_cookie.cookie = cookie;
1010 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012 else
1013 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 cmd_cookie = cookie;
1016 }
1017#endif
1018
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001019 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (next_cmdline == NULL)
1021 {
1022 /*
1023 * Need to set msg_didout for the first line after an ":if",
1024 * otherwise the ":if" will be overwritten.
1025 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001026 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001028 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029#ifdef FEAT_EVAL
1030 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1031#else
1032 0
1033#endif
1034 )) == NULL)
1035 {
1036 /* Don't call wait_return for aborted command line. The NULL
1037 * returned for the end of a sourced file or executed function
1038 * doesn't do this. */
1039 if (KeyTyped && !(flags & DOCMD_REPEAT))
1040 need_wait_return = FALSE;
1041 retval = FAIL;
1042 break;
1043 }
1044 used_getline = TRUE;
1045
1046 /*
1047 * Keep the first typed line. Clear it when more lines are typed.
1048 */
1049 if (flags & DOCMD_KEEPLINE)
1050 {
1051 vim_free(repeat_cmdline);
1052 if (count == 0)
1053 repeat_cmdline = vim_strsave(next_cmdline);
1054 else
1055 repeat_cmdline = NULL;
1056 }
1057 }
1058
1059 /* 3. Make a copy of the command so we can mess with it. */
1060 else if (cmdline_copy == NULL)
1061 {
1062 next_cmdline = vim_strsave(next_cmdline);
1063 if (next_cmdline == NULL)
1064 {
1065 EMSG(_(e_outofmem));
1066 retval = FAIL;
1067 break;
1068 }
1069 }
1070 cmdline_copy = next_cmdline;
1071
1072#ifdef FEAT_EVAL
1073 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 * Save the current line when inside a ":while" or ":for", and when
1075 * the command looks like a ":while" or ":for", because we may need it
1076 * later. When there is a '|' and another command, it is stored
1077 * separately, because we need to be able to jump back to it from an
1078 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 if (current_line == lines_ga.ga_len
1081 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
1085 retval = FAIL;
1086 break;
1087 }
1088 }
1089 did_endif = FALSE;
1090#endif
1091
1092 if (count++ == 0)
1093 {
1094 /*
1095 * All output from the commands is put below each other, without
1096 * waiting for a return. Don't do this when executing commands
1097 * from a script or when being called recursive (e.g. for ":e
1098 * +command file").
1099 */
1100 if (!(flags & DOCMD_NOWAIT) && !recursive)
1101 {
1102 msg_didout_before_start = msg_didout;
1103 msg_didany = FALSE; /* no output yet */
1104 msg_start();
1105 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001106 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 ++RedrawingDisabled;
1108 did_inc = TRUE;
1109 }
1110 }
1111
1112 if (p_verbose >= 15 && sourcing_name != NULL)
1113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001115 verbose_enter_scroll();
1116
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 smsg((char_u *)_("line %ld: %s"),
1118 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001119 if (msg_silent == 0)
1120 msg_puts((char_u *)"\n"); /* don't overwrite this */
1121
1122 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 --no_wait_return;
1124 }
1125
1126 /*
1127 * 2. Execute one '|' separated command.
1128 * do_one_cmd() will return NULL if there is no trailing '|'.
1129 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1130 */
1131 ++recursive;
1132 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1133#ifdef FEAT_EVAL
1134 &cstack,
1135#endif
1136 cmd_getline, cmd_cookie);
1137 --recursive;
1138
1139#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 if (cmd_cookie == (void *)&cmd_loop_cookie)
1141 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001143 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
1145
1146 if (next_cmdline == NULL)
1147 {
1148 vim_free(cmdline_copy);
1149 cmdline_copy = NULL;
1150#ifdef FEAT_CMDHIST
1151 /*
1152 * If the command was typed, remember it for the ':' register.
1153 * Do this AFTER executing the command to make :@: work.
1154 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001155 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 && new_last_cmdline != NULL)
1157 {
1158 vim_free(last_cmdline);
1159 last_cmdline = new_last_cmdline;
1160 new_last_cmdline = NULL;
1161 }
1162#endif
1163 }
1164 else
1165 {
1166 /* need to copy the command after the '|' to cmdline_copy, for the
1167 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001168 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 next_cmdline = cmdline_copy;
1170 }
1171
1172
1173#ifdef FEAT_EVAL
1174 /* reset did_emsg for a function that is not aborted by an error */
1175 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001176 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 && !func_has_abort(real_cookie))
1178 did_emsg = FALSE;
1179
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
1182 ++current_line;
1183
1184 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001185 * An ":endwhile", ":endfor" and ":continue" is handled here.
1186 * If we were executing commands, jump back to the ":while" or
1187 * ":for".
1188 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001192 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 /* Jump back to the matching ":while" or ":for". Be careful
1195 * not to use a cs_line[] from an entry that isn't a ":while"
1196 * or ":for": It would make "current_line" invalid and can
1197 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (!did_emsg && !got_int && !did_throw
1199 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 && (cstack.cs_flags[cstack.cs_idx]
1201 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 && cstack.cs_line[cstack.cs_idx] >= 0
1203 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1204 {
1205 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001206 /* remember we jumped there */
1207 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 line_breakcheck(); /* check if CTRL-C typed */
1209
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001210 /* Check for the next breakpoint at or after the ":while"
1211 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (breakpoint != NULL)
1213 {
1214 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001215 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 fname,
1217 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1218 *dbg_tick = debug_tick;
1219 }
1220 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001225 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1226 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 }
1228 }
1229
1230 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001231 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001235 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1237 }
1238 }
1239
1240 /*
1241 * When not inside any ":while" loop, clear remembered lines.
1242 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001243 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 {
1245 if (lines_ga.ga_len > 0)
1246 {
1247 sourcing_lnum =
1248 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1249 free_cmdlines(&lines_ga);
1250 }
1251 current_line = 0;
1252 }
1253
1254 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001255 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1256 * being restored at the ":endtry". Reset them here and set the
1257 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1258 * This includes the case where a missing ":endif", ":endwhile" or
1259 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001261 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001263 cstack.cs_lflags &= ~CSL_HAD_FINA;
1264 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1265 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 did_throw ? (void *)current_exception : NULL);
1267 did_emsg = got_int = did_throw = FALSE;
1268 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1269 }
1270
1271 /* Update global "trylevel" for recursive calls to do_cmdline() from
1272 * within this loop. */
1273 trylevel = initial_trylevel + cstack.cs_trylevel;
1274
1275 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001276 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 * files) is aborted because of an error, an interrupt, or an uncaught
1278 * exception, cancel everything. If it is left normally, reset
1279 * force_abort to get the non-EH compatible abortion behavior for
1280 * the rest of the script.
1281 */
1282 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1283 force_abort = FALSE;
1284
1285 /* Convert an interrupt to an exception if appropriate. */
1286 (void)do_intthrow(&cstack);
1287#endif /* FEAT_EVAL */
1288
1289 }
1290 /*
1291 * Continue executing command lines when:
1292 * - no CTRL-C typed, no aborting error, no exception thrown or try
1293 * conditionals need to be checked for executing finally clauses or
1294 * catching an interrupt exception
1295 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001296 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 * looping for ":source" command or function call.
1298 */
1299 while (!((got_int
1300#ifdef FEAT_EVAL
1301 || (did_emsg && force_abort) || did_throw
1302#endif
1303 )
1304#ifdef FEAT_EVAL
1305 && cstack.cs_trylevel == 0
1306#endif
1307 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001308 && !(did_emsg
1309#ifdef FEAT_EVAL
1310 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001311 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001312 * the :endtry to be missed. */
1313 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1314#endif
1315 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 && (getline_equal(fgetline, cookie, getexmodeline)
1317 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && (next_cmdline != NULL
1319#ifdef FEAT_EVAL
1320 || cstack.cs_idx >= 0
1321#endif
1322 || (flags & DOCMD_REPEAT)));
1323
1324 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001325 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326#ifdef FEAT_EVAL
1327 free_cmdlines(&lines_ga);
1328 ga_clear(&lines_ga);
1329
1330 if (cstack.cs_idx >= 0)
1331 {
1332 /*
1333 * If a sourced file or executed function ran to its end, report the
1334 * unclosed conditional.
1335 */
1336 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 && ((getline_equal(fgetline, cookie, getsourceline)
1338 && !source_finished(fgetline, cookie))
1339 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && !func_has_ended(real_cookie))))
1341 {
1342 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1343 EMSG(_(e_endtry));
1344 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1345 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001346 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1347 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else
1349 EMSG(_(e_endif));
1350 }
1351
1352 /*
1353 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1354 * ":endtry" in a sourced file or executed function. If the try
1355 * conditional is in its finally clause, ignore anything pending.
1356 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001357 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 */
1359 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001360 {
1361 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1362
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001363 if (idx >= 0)
1364 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001365 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1366 &cstack.cs_looplevel);
1367 }
1368 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 trylevel = initial_trylevel;
1370 }
1371
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001372 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1373 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001375 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 ? (char_u *)"endfunction" : (char_u *)NULL);
1377
1378 if (trylevel == 0)
1379 {
1380 /*
1381 * When an exception is being thrown out of the outermost try
1382 * conditional, discard the uncaught exception, disable the conversion
1383 * of interrupts or errors to exceptions, and ensure that no more
1384 * commands are executed.
1385 */
1386 if (did_throw)
1387 {
1388 void *p = NULL;
1389 char_u *saved_sourcing_name;
1390 int saved_sourcing_lnum;
1391 struct msglist *messages = NULL, *next;
1392
1393 /*
1394 * If the uncaught exception is a user exception, report it as an
1395 * error. If it is an error exception, display the saved error
1396 * message now. For an interrupt exception, do nothing; the
1397 * interrupt message is given elsewhere.
1398 */
1399 switch (current_exception->type)
1400 {
1401 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001402 vim_snprintf((char *)IObuff, IOSIZE,
1403 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 current_exception->value);
1405 p = vim_strsave(IObuff);
1406 break;
1407 case ET_ERROR:
1408 messages = current_exception->messages;
1409 current_exception->messages = NULL;
1410 break;
1411 case ET_INTERRUPT:
1412 break;
1413 default:
1414 p = vim_strsave((char_u *)_(e_internal));
1415 }
1416
1417 saved_sourcing_name = sourcing_name;
1418 saved_sourcing_lnum = sourcing_lnum;
1419 sourcing_name = current_exception->throw_name;
1420 sourcing_lnum = current_exception->throw_lnum;
1421 current_exception->throw_name = NULL;
1422
1423 discard_current_exception(); /* uses IObuff if 'verbose' */
1424 suppress_errthrow = TRUE;
1425 force_abort = TRUE;
1426
1427 if (messages != NULL)
1428 {
1429 do
1430 {
1431 next = messages->next;
1432 emsg(messages->msg);
1433 vim_free(messages->msg);
1434 vim_free(messages);
1435 messages = next;
1436 }
1437 while (messages != NULL);
1438 }
1439 else if (p != NULL)
1440 {
1441 emsg(p);
1442 vim_free(p);
1443 }
1444 vim_free(sourcing_name);
1445 sourcing_name = saved_sourcing_name;
1446 sourcing_lnum = saved_sourcing_lnum;
1447 }
1448
1449 /*
1450 * On an interrupt or an aborting error not converted to an exception,
1451 * disable the conversion of errors to exceptions. (Interrupts are not
1452 * converted any more, here.) This enables also the interrupt message
1453 * when force_abort is set and did_emsg unset in case of an interrupt
1454 * from a finally clause after an error.
1455 */
1456 else if (got_int || (did_emsg && force_abort))
1457 suppress_errthrow = TRUE;
1458 }
1459
1460 /*
1461 * The current cstack will be freed when do_cmdline() returns. An uncaught
1462 * exception will have to be rethrown in the previous cstack. If a function
1463 * has just returned or a script file was just finished and the previous
1464 * cstack belongs to the same function or, respectively, script file, it
1465 * will have to be checked for finally clauses to be executed due to the
1466 * ":return" or ":finish". This is done in do_one_cmd().
1467 */
1468 if (did_throw)
1469 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001470 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001472 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 && ex_nesting_level > func_level(real_cookie) + 1))
1474 {
1475 if (!did_throw)
1476 check_cstack = TRUE;
1477 }
1478 else
1479 {
1480 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001481 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 --ex_nesting_level;
1483 /*
1484 * Go to debug mode when returning from a function in which we are
1485 * single-stepping.
1486 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001487 if ((getline_equal(fgetline, cookie, getsourceline)
1488 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001490 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 ? (char_u *)_("End of sourced file")
1492 : (char_u *)_("End of function"));
1493 }
1494
1495 /*
1496 * Restore the exception environment (done after returning from the
1497 * debugger).
1498 */
1499 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001500 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
1502 msg_list = saved_msg_list;
1503#endif /* FEAT_EVAL */
1504
1505 /*
1506 * If there was too much output to fit on the command line, ask the user to
1507 * hit return before redrawing the screen. With the ":global" command we do
1508 * this only once after the command is finished.
1509 */
1510 if (did_inc)
1511 {
1512 --RedrawingDisabled;
1513 --no_wait_return;
1514 msg_scroll = FALSE;
1515
1516 /*
1517 * When just finished an ":if"-":else" which was typed, no need to
1518 * wait for hit-return. Also for an error situation.
1519 */
1520 if (retval == FAIL
1521#ifdef FEAT_EVAL
1522 || (did_endif && KeyTyped && !did_emsg)
1523#endif
1524 )
1525 {
1526 need_wait_return = FALSE;
1527 msg_didany = FALSE; /* don't wait when restarting edit */
1528 }
1529 else if (need_wait_return)
1530 {
1531 /*
1532 * The msg_start() above clears msg_didout. The wait_return we do
1533 * here should not overwrite the command that may be shown before
1534 * doing that.
1535 */
1536 msg_didout |= msg_didout_before_start;
1537 wait_return(FALSE);
1538 }
1539 }
1540
Bram Moolenaar2a942252012-11-28 23:03:07 +01001541#ifdef FEAT_EVAL
1542 did_endif = FALSE; /* in case do_cmdline used recursively */
1543#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 /*
1545 * Reset if_level, in case a sourced script file contains more ":if" than
1546 * ":endif" (could be ":if x | foo | endif").
1547 */
1548 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001549#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 --call_depth;
1552 return retval;
1553}
1554
1555#ifdef FEAT_EVAL
1556/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001557 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 */
1559 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001560get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 int c;
1562 void *cookie;
1563 int indent;
1564{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001565 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 wcmd_T *wp;
1567 char_u *line;
1568
1569 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1570 {
1571 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001572 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001574 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (cp->getline == NULL)
1576 line = getcmdline(c, 0L, indent);
1577 else
1578 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001579 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 ++cp->current_line;
1581
1582 return line;
1583 }
1584
1585 KeyTyped = FALSE;
1586 ++cp->current_line;
1587 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1588 sourcing_lnum = wp->lnum;
1589 return vim_strsave(wp->line);
1590}
1591
1592/*
1593 * Store a line in "gap" so that a ":while" loop can execute it again.
1594 */
1595 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001596store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 garray_T *gap;
1598 char_u *line;
1599{
1600 if (ga_grow(gap, 1) == FAIL)
1601 return FAIL;
1602 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1603 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1604 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 return OK;
1606}
1607
1608/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001609 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 */
1611 static void
1612free_cmdlines(gap)
1613 garray_T *gap;
1614{
1615 while (gap->ga_len > 0)
1616 {
1617 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1618 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620}
1621#endif
1622
1623/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001624 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1625 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628getline_equal(fgetline, cookie, func)
1629 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001630 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 char_u *(*func) __ARGS((int, void *, int));
1632{
1633#ifdef FEAT_EVAL
1634 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001635 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636
Bram Moolenaar89d40322006-08-29 15:30:07 +00001637 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001638 * function that's originally used to obtain the lines. This may be
1639 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001640 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001641 cp = (struct loop_cookie *)cookie;
1642 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 {
1644 gp = cp->getline;
1645 cp = cp->cookie;
1646 }
1647 return gp == func;
1648#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001649 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650#endif
1651}
1652
1653#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1654/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001655 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 * getline function. Otherwise return "cookie".
1657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001659getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001660 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001661 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663# ifdef FEAT_EVAL
1664 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001665 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaar89d40322006-08-29 15:30:07 +00001667 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001668 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001670 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001671 cp = (struct loop_cookie *)cookie;
1672 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 {
1674 gp = cp->getline;
1675 cp = cp->cookie;
1676 }
1677 return cp;
1678# else
1679 return cookie;
1680# endif
1681}
1682#endif
1683
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684
1685/*
1686 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1687 * ":bwipeout", etc.
1688 * Returns the buffer number.
1689 */
1690 static int
1691compute_buffer_local_count(addr_type, lnum, offset)
1692 int addr_type;
1693 int lnum;
1694 int offset;
1695{
1696 buf_T *buf;
1697 int count = offset;
1698
1699 buf = firstbuf;
1700 while (buf->b_next != NULL && buf->b_fnum < lnum)
1701 buf = buf->b_next;
1702 while (count != 0)
1703 {
1704 count += (count < 0) ? 1 : -1;
1705 if (buf->b_prev == NULL)
1706 break;
1707 buf = (count < 0) ? buf->b_prev : buf->b_next;
1708 if (addr_type == ADDR_LOADED_BUFFERS)
1709 /* skip over unloaded buffers */
1710 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
1711 buf = (count < 0) ? buf->b_prev : buf->b_next;
1712 }
1713 return buf->b_fnum;
1714}
1715
1716
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717/*
1718 * Execute one Ex command.
1719 *
1720 * If 'sourcing' is TRUE, the command will be included in the error message.
1721 *
1722 * 1. skip comment lines and leading space
1723 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001724 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001725 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001726 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001727 * 6. parse arguments
1728 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001730 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 *
1732 * This function may be called recursively!
1733 */
1734#if (_MSC_VER == 1200)
1735/*
Bram Moolenaared203462004-06-16 11:19:22 +00001736 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001738 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739#endif
1740 static char_u *
1741do_one_cmd(cmdlinep, sourcing,
1742#ifdef FEAT_EVAL
1743 cstack,
1744#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001745 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 char_u **cmdlinep;
1747 int sourcing;
1748#ifdef FEAT_EVAL
1749 struct condstack *cstack;
1750#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001751 char_u *(*fgetline) __ARGS((int, void *, int));
1752 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753{
1754 char_u *p;
1755 linenr_T lnum;
1756 long n;
1757 char_u *errormsg = NULL; /* error message */
1758 exarg_T ea; /* Ex command arguments */
1759 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001760 int save_msg_scroll = msg_scroll;
1761 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001763#ifdef HAVE_SANDBOX
1764 int did_sandbox = FALSE;
1765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 cmdmod_T save_cmdmod;
1767 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001768 win_T *wp;
1769 tabpage_T *tp;
1770 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772 vim_memset(&ea, 0, sizeof(ea));
1773 ea.line1 = 1;
1774 ea.line2 = 1;
1775#ifdef FEAT_EVAL
1776 ++ex_nesting_level;
1777#endif
1778
Bram Moolenaar21691f82012-12-05 19:13:18 +01001779 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 if (quitmore
1781#ifdef FEAT_EVAL
1782 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001783 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001784#endif
1785#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001786 /* avoid that an autocommand, e.g. QuitPre, does this */
1787 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
1789 )
1790 --quitmore;
1791
1792 /*
1793 * Reset browse, confirm, etc.. They are restored when returning, for
1794 * recursive calls.
1795 */
1796 save_cmdmod = cmdmod;
1797 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1798
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001799 /* "#!anything" is handled like a comment. */
1800 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1801 goto doend;
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 /*
1804 * Repeat until no more command modifiers are found.
1805 */
1806 ea.cmd = *cmdlinep;
1807 for (;;)
1808 {
1809/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001810 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 */
1812 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1813 ++ea.cmd;
1814
1815 /* in ex mode, an empty line works like :+ */
1816 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001817 && (getline_equal(fgetline, cookie, getexmodeline)
1818 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001819 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 {
1821 ea.cmd = (char_u *)"+";
1822 ex_pressedreturn = TRUE;
1823 }
1824
1825 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001826 if (*ea.cmd == '"')
1827 goto doend;
1828 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001829 {
1830 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833
1834/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001835 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 */
1837 p = ea.cmd;
1838 if (VIM_ISDIGIT(*ea.cmd))
1839 p = skipwhite(skipdigits(ea.cmd));
1840 switch (*p)
1841 {
1842 /* When adding an entry, also modify cmd_exists(). */
1843 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1844 break;
1845#ifdef FEAT_WINDOWS
1846 cmdmod.split |= WSP_ABOVE;
1847#endif
1848 continue;
1849
1850 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1851 {
1852#ifdef FEAT_WINDOWS
1853 cmdmod.split |= WSP_BELOW;
1854#endif
1855 continue;
1856 }
1857 if (checkforcmd(&ea.cmd, "browse", 3))
1858 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001859#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 cmdmod.browse = TRUE;
1861#endif
1862 continue;
1863 }
1864 if (!checkforcmd(&ea.cmd, "botright", 2))
1865 break;
1866#ifdef FEAT_WINDOWS
1867 cmdmod.split |= WSP_BOT;
1868#endif
1869 continue;
1870
1871 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1872 break;
1873#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1874 cmdmod.confirm = TRUE;
1875#endif
1876 continue;
1877
1878 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1879 {
1880 cmdmod.keepmarks = TRUE;
1881 continue;
1882 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001883 if (checkforcmd(&ea.cmd, "keepalt", 5))
1884 {
1885 cmdmod.keepalt = TRUE;
1886 continue;
1887 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001888 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1889 {
1890 cmdmod.keeppatterns = TRUE;
1891 continue;
1892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1894 break;
1895 cmdmod.keepjumps = TRUE;
1896 continue;
1897
1898 /* ":hide" and ":hide | cmd" are not modifiers */
1899 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1900 || *p == NUL || ends_excmd(*p))
1901 break;
1902 ea.cmd = p;
1903 cmdmod.hide = TRUE;
1904 continue;
1905
1906 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1907 {
1908 cmdmod.lockmarks = TRUE;
1909 continue;
1910 }
1911
1912 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1913 break;
1914#ifdef FEAT_WINDOWS
1915 cmdmod.split |= WSP_ABOVE;
1916#endif
1917 continue;
1918
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001919 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001920 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001921#ifdef FEAT_AUTOCMD
1922 if (cmdmod.save_ei == NULL)
1923 {
1924 /* Set 'eventignore' to "all". Restore the
1925 * existing option value later. */
1926 cmdmod.save_ei = vim_strsave(p_ei);
1927 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001928 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001929 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001930#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001931 continue;
1932 }
1933 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1934 break;
1935 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001936 continue;
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1939 break;
1940#ifdef FEAT_WINDOWS
1941 cmdmod.split |= WSP_BELOW;
1942#endif
1943 continue;
1944
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001945 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1946 {
1947#ifdef HAVE_SANDBOX
1948 if (!did_sandbox)
1949 ++sandbox;
1950 did_sandbox = TRUE;
1951#endif
1952 continue;
1953 }
1954 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001956 if (save_msg_silent == -1)
1957 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1960 {
1961 /* ":silent!", but not "silent !cmd" */
1962 ea.cmd = skipwhite(ea.cmd + 1);
1963 ++emsg_silent;
1964 ++did_esilent;
1965 }
1966 continue;
1967
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001968 case 't': if (checkforcmd(&p, "tab", 3))
1969 {
1970#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001971 if (vim_isdigit(*ea.cmd))
1972 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1973 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001974 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001975 ea.cmd = p;
1976#endif
1977 continue;
1978 }
1979 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 break;
1981#ifdef FEAT_WINDOWS
1982 cmdmod.split |= WSP_TOP;
1983#endif
1984 continue;
1985
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001986 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1987 break;
1988 if (save_msg_silent == -1)
1989 save_msg_silent = msg_silent;
1990 msg_silent = 0;
1991 continue;
1992
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1994 {
1995#ifdef FEAT_VERTSPLIT
1996 cmdmod.split |= WSP_VERT;
1997#endif
1998 continue;
1999 }
2000 if (!checkforcmd(&p, "verbose", 4))
2001 break;
2002 if (verbose_save < 0)
2003 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002004 if (vim_isdigit(*ea.cmd))
2005 p_verbose = atoi((char *)ea.cmd);
2006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 p_verbose = 1;
2008 ea.cmd = p;
2009 continue;
2010 }
2011 break;
2012 }
2013
2014#ifdef FEAT_EVAL
2015 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2016 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2017#else
2018 ea.skip = (if_level > 0);
2019#endif
2020
2021#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002022# ifdef FEAT_PROFILE
2023 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002024 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002025 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002026 if (getline_equal(fgetline, cookie, get_func_line))
2027 func_line_exec(getline_cookie(fgetline, cookie));
2028 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002029 script_line_exec();
2030 }
2031#endif
2032
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 /* May go to debug mode. If this happens and the ">quit" debug command is
2034 * used, throw an interrupt exception and skip the next command. */
2035 dbg_check_breakpoint(&ea);
2036 if (!ea.skip && got_int)
2037 {
2038 ea.skip = TRUE;
2039 (void)do_intthrow(cstack);
2040 }
2041#endif
2042
2043/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002044 * 3. Skip over the range to find the command. Let "p" point to after it.
2045 *
2046 * We need the command to know what kind of range it uses.
2047 */
2048 cmd = ea.cmd;
2049 ea.cmd = skip_range(ea.cmd, NULL);
2050 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2051 ea.cmd = skipwhite(ea.cmd + 1);
2052 p = find_command(&ea, NULL);
2053
2054/*
2055 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 *
2057 * where 'addr' is:
2058 *
2059 * % (entire file)
2060 * $ [+-NUM]
2061 * 'x [+-NUM] (where x denotes a currently defined mark)
2062 * . [+-NUM]
2063 * [+-NUM]..
2064 * NUM
2065 *
2066 * The ea.cmd pointer is updated to point to the first character following the
2067 * range spec. If an initial address is found, but no second, the upper bound
2068 * is equal to the lower.
2069 */
2070
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002071 if (ea.cmdidx != CMD_SIZE)
2072 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2073 else
2074 ea.addr_type = ADDR_LINES;
2075 ea.cmd = cmd;
2076
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 /* repeat for all ',' or ';' separated addresses */
2078 for (;;)
2079 {
2080 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002081 switch (ea.addr_type)
2082 {
2083 case ADDR_LINES:
2084 /* default is current line number */
2085 ea.line2 = curwin->w_cursor.lnum;
2086 break;
2087 case ADDR_WINDOWS:
2088 lnum = 0;
2089 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2090 {
2091 lnum++;
2092 if (wp == curwin)
2093 break;
2094 }
2095 ea.line2 = lnum;
2096 break;
2097 case ADDR_ARGUMENTS:
2098 ea.line2 = curwin->w_arg_idx + 1;
2099 break;
2100 case ADDR_LOADED_BUFFERS:
2101 case ADDR_UNLOADED_BUFFERS:
2102 ea.line2 = curbuf->b_fnum;
2103 break;
2104 case ADDR_TABS:
2105 lnum = 0;
2106 for(tp = first_tabpage; tp != NULL; tp = tp->tp_next)
2107 {
2108 lnum++;
2109 if (tp == curtab)
2110 break;
2111 }
2112 ea.line2 = lnum;
2113 break;
2114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002116 lnum = get_address(&ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 if (ea.cmd == NULL) /* error detected */
2118 goto doend;
2119 if (lnum == MAXLNUM)
2120 {
2121 if (*ea.cmd == '%') /* '%' - all lines */
2122 {
2123 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002124 switch (ea.addr_type)
2125 {
2126 case ADDR_LINES:
2127 ea.line1 = 1;
2128 ea.line2 = curbuf->b_ml.ml_line_count;
2129 break;
2130 case ADDR_WINDOWS:
2131 case ADDR_LOADED_BUFFERS:
2132 case ADDR_UNLOADED_BUFFERS:
2133 case ADDR_TABS:
2134 errormsg = (char_u *)_(e_invrange);
2135 goto doend;
2136 break;
2137 case ADDR_ARGUMENTS:
2138 ea.line1 = 1;
2139 ea.line2 = ARGCOUNT;
2140 break;
2141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 ++ea.addr_count;
2143 }
2144 /* '*' - visual area */
2145 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2146 {
2147 pos_T *fp;
2148
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002149 if (ea.addr_type != ADDR_LINES)
2150 {
2151 errormsg = (char_u *)_(e_invrange);
2152 goto doend;
2153 }
2154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 ++ea.cmd;
2156 if (!ea.skip)
2157 {
2158 fp = getmark('<', FALSE);
2159 if (check_mark(fp) == FAIL)
2160 goto doend;
2161 ea.line1 = fp->lnum;
2162 fp = getmark('>', FALSE);
2163 if (check_mark(fp) == FAIL)
2164 goto doend;
2165 ea.line2 = fp->lnum;
2166 ++ea.addr_count;
2167 }
2168 }
2169 }
2170 else
2171 ea.line2 = lnum;
2172 ea.addr_count++;
2173
2174 if (*ea.cmd == ';')
2175 {
2176 if (!ea.skip)
2177 curwin->w_cursor.lnum = ea.line2;
2178 }
2179 else if (*ea.cmd != ',')
2180 break;
2181 ++ea.cmd;
2182 }
2183
2184 /* One address given: set start and end lines */
2185 if (ea.addr_count == 1)
2186 {
2187 ea.line1 = ea.line2;
2188 /* ... but only implicit: really no address given */
2189 if (lnum == MAXLNUM)
2190 ea.addr_count = 0;
2191 }
2192
2193 /* Don't leave the cursor on an illegal line (caused by ';') */
2194 check_cursor_lnum();
2195
2196/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002197 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 */
2199
2200 /*
2201 * Skip ':' and any white space
2202 */
2203 ea.cmd = skipwhite(ea.cmd);
2204 while (*ea.cmd == ':')
2205 ea.cmd = skipwhite(ea.cmd + 1);
2206
2207 /*
2208 * If we got a line, but no command, then go to the line.
2209 * If we find a '|' or '\n' we set ea.nextcmd.
2210 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002211 if (*ea.cmd == NUL || *ea.cmd == '"'
2212 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 /*
2215 * strange vi behaviour:
2216 * ":3" jumps to line 3
2217 * ":3|..." prints line 3
2218 * ":|" prints current line
2219 */
2220 if (ea.skip) /* skip this if inside :if */
2221 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002222 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
2224 ea.cmdidx = CMD_print;
2225 ea.argt = RANGE+COUNT+TRLBAR;
2226 if ((errormsg = invalid_range(&ea)) == NULL)
2227 {
2228 correct_range(&ea);
2229 ex_print(&ea);
2230 }
2231 }
2232 else if (ea.addr_count != 0)
2233 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002234 if (ea.line2 > curbuf->b_ml.ml_line_count)
2235 {
2236 /* With '-' in 'cpoptions' a line number past the file is an
2237 * error, otherwise put it at the end of the file. */
2238 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2239 ea.line2 = -1;
2240 else
2241 ea.line2 = curbuf->b_ml.ml_line_count;
2242 }
2243
2244 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002245 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 else
2247 {
2248 if (ea.line2 == 0)
2249 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 else
2251 curwin->w_cursor.lnum = ea.line2;
2252 beginline(BL_SOL | BL_FIX);
2253 }
2254 }
2255 goto doend;
2256 }
2257
Bram Moolenaard5005162014-08-22 23:05:54 +02002258#ifdef FEAT_AUTOCMD
2259 /* If this looks like an undefined user command and there are CmdUndefined
2260 * autocommands defined, trigger the matching autocommands. */
2261 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2262 && ASCII_ISUPPER(*ea.cmd)
2263 && has_cmdundefined())
2264 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002265 int ret;
2266
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002267 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002268 while (ASCII_ISALNUM(*p))
2269 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002270 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002271 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2272 vim_free(p);
2273 if (ret && !aborting())
2274 p = find_command(&ea, NULL);
2275 }
2276#endif
2277
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#ifdef FEAT_USR_CMDS
2279 if (p == NULL)
2280 {
2281 if (!ea.skip)
2282 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2283 goto doend;
2284 }
2285 /* Check for wrong commands. */
2286 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2287 {
2288 errormsg = uc_fun_cmd();
2289 goto doend;
2290 }
2291#endif
2292 if (ea.cmdidx == CMD_SIZE)
2293 {
2294 if (!ea.skip)
2295 {
2296 STRCPY(IObuff, _("E492: Not an editor command"));
2297 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002298 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002300 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
2302 goto doend;
2303 }
2304
Bram Moolenaar958636c2014-10-21 20:01:58 +02002305 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2306 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002307#ifdef HAVE_EX_SCRIPT_NI
2308 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2309#endif
2310 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311
2312#ifndef FEAT_EVAL
2313 /*
2314 * When the expression evaluation is disabled, recognize the ":if" and
2315 * ":endif" commands and ignore everything in between it.
2316 */
2317 if (ea.cmdidx == CMD_if)
2318 ++if_level;
2319 if (if_level)
2320 {
2321 if (ea.cmdidx == CMD_endif)
2322 --if_level;
2323 goto doend;
2324 }
2325
2326#endif
2327
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002328 /* forced commands */
2329 if (*p == '!' && ea.cmdidx != CMD_substitute
2330 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
2332 ++p;
2333 ea.forceit = TRUE;
2334 }
2335 else
2336 ea.forceit = FALSE;
2337
2338/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002339 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002341 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002342 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
2344 if (!ea.skip)
2345 {
2346#ifdef HAVE_SANDBOX
2347 if (sandbox != 0 && !(ea.argt & SBOXOK))
2348 {
2349 /* Command not allowed in sandbox. */
2350 errormsg = (char_u *)_(e_sandbox);
2351 goto doend;
2352 }
2353#endif
2354 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2355 {
2356 /* Command not allowed in non-'modifiable' buffer */
2357 errormsg = (char_u *)_(e_modifiable);
2358 goto doend;
2359 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002360
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002361 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002362 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002364 /* Command not allowed when editing the command line. */
2365#ifdef FEAT_CMDWIN
2366 if (cmdwin_type != 0)
2367 errormsg = (char_u *)_(e_cmdwin);
2368 else
2369#endif
2370 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 goto doend;
2372 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002373#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002374 /* Disallow editing another buffer when "curbuf_lock" is set.
2375 * Do allow ":edit" (check for argument later).
2376 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002377 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002378 && ea.cmdidx != CMD_edit
2379 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002380 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002381 && curbuf_locked())
2382 goto doend;
2383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2386 {
2387 /* no range allowed */
2388 errormsg = (char_u *)_(e_norange);
2389 goto doend;
2390 }
2391 }
2392
2393 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2394 {
2395 errormsg = (char_u *)_(e_nobang);
2396 goto doend;
2397 }
2398
2399 /*
2400 * Don't complain about the range if it is not used
2401 * (could happen if line_count is accidentally set to 0).
2402 */
2403 if (!ea.skip && !ni)
2404 {
2405 /*
2406 * If the range is backwards, ask for confirmation and, if given, swap
2407 * ea.line1 & ea.line2 so it's forwards again.
2408 * When global command is busy, don't ask, will fail below.
2409 */
2410 if (!global_busy && ea.line1 > ea.line2)
2411 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002412 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002414 if (sourcing || exmode_active)
2415 {
2416 errormsg = (char_u *)_("E493: Backwards range given");
2417 goto doend;
2418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 if (ask_yesno((char_u *)
2420 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002421 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423 lnum = ea.line1;
2424 ea.line1 = ea.line2;
2425 ea.line2 = lnum;
2426 }
2427 if ((errormsg = invalid_range(&ea)) != NULL)
2428 goto doend;
2429 }
2430
2431 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2432 ea.line2 = 1;
2433
2434 correct_range(&ea);
2435
2436#ifdef FEAT_FOLDING
2437 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2438 {
2439 /* Put the first line at the start of a closed fold, put the last line
2440 * at the end of a closed fold. */
2441 (void)hasFolding(ea.line1, &ea.line1, NULL);
2442 (void)hasFolding(ea.line2, NULL, &ea.line2);
2443 }
2444#endif
2445
2446#ifdef FEAT_QUICKFIX
2447 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002448 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 * option here, so things like % get expanded.
2450 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002451 p = replace_makeprg(&ea, p, cmdlinep);
2452 if (p == NULL)
2453 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454#endif
2455
2456 /*
2457 * Skip to start of argument.
2458 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2459 */
2460 if (ea.cmdidx == CMD_bang)
2461 ea.arg = p;
2462 else
2463 ea.arg = skipwhite(p);
2464
2465 /*
2466 * Check for "++opt=val" argument.
2467 * Must be first, allow ":w ++enc=utf8 !cmd"
2468 */
2469 if (ea.argt & ARGOPT)
2470 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2471 if (getargopt(&ea) == FAIL && !ni)
2472 {
2473 errormsg = (char_u *)_(e_invarg);
2474 goto doend;
2475 }
2476
2477 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2478 {
2479 if (*ea.arg == '>') /* append */
2480 {
2481 if (*++ea.arg != '>') /* typed wrong */
2482 {
2483 errormsg = (char_u *)_("E494: Use w or w>>");
2484 goto doend;
2485 }
2486 ea.arg = skipwhite(ea.arg + 1);
2487 ea.append = TRUE;
2488 }
2489 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2490 {
2491 ++ea.arg;
2492 ea.usefilter = TRUE;
2493 }
2494 }
2495
2496 if (ea.cmdidx == CMD_read)
2497 {
2498 if (ea.forceit)
2499 {
2500 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2501 ea.forceit = FALSE;
2502 }
2503 else if (*ea.arg == '!') /* :r !filter */
2504 {
2505 ++ea.arg;
2506 ea.usefilter = TRUE;
2507 }
2508 }
2509
2510 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2511 {
2512 ea.amount = 1;
2513 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2514 {
2515 ++ea.arg;
2516 ++ea.amount;
2517 }
2518 ea.arg = skipwhite(ea.arg);
2519 }
2520
2521 /*
2522 * Check for "+command" argument, before checking for next command.
2523 * Don't do this for ":read !cmd" and ":write !cmd".
2524 */
2525 if ((ea.argt & EDITCMD) && !ea.usefilter)
2526 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2527
2528 /*
2529 * Check for '|' to separate commands and '"' to start comments.
2530 * Don't do this for ":read !cmd" and ":write !cmd".
2531 */
2532 if ((ea.argt & TRLBAR) && !ea.usefilter)
2533 separate_nextcmd(&ea);
2534
2535 /*
2536 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002537 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2538 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002540 else if (ea.cmdidx == CMD_bang
2541 || ea.cmdidx == CMD_global
2542 || ea.cmdidx == CMD_vglobal
2543 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 for (p = ea.arg; *p; ++p)
2546 {
2547 /* Remove one backslash before a newline, so that it's possible to
2548 * pass a newline to the shell and also a newline that is preceded
2549 * with a backslash. This makes it impossible to end a shell
2550 * command in a backslash, but that doesn't appear useful.
2551 * Halving the number of backslashes is incompatible with previous
2552 * versions. */
2553 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002554 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 else if (*p == '\n')
2556 {
2557 ea.nextcmd = p + 1;
2558 *p = NUL;
2559 break;
2560 }
2561 }
2562 }
2563
2564 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2565 {
2566 ea.line1 = 1;
2567 ea.line2 = curbuf->b_ml.ml_line_count;
2568 }
2569
2570 /* accept numbered register only when no count allowed (:put) */
2571 if ( (ea.argt & REGSTR)
2572 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002573 /* Do not allow register = for user commands */
2574 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2576 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002577#ifndef FEAT_CLIPBOARD
2578 /* check these explicitly for a more specific error message */
2579 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002581 errormsg = (char_u *)_(e_invalidreg);
2582 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002585 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2586 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002587 {
2588 ea.regname = *ea.arg++;
2589#ifdef FEAT_EVAL
2590 /* for '=' register: accept the rest of the line as an expression */
2591 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2592 {
2593 set_expr_line(vim_strsave(ea.arg));
2594 ea.arg += STRLEN(ea.arg);
2595 }
2596#endif
2597 ea.arg = skipwhite(ea.arg);
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600
2601 /*
2602 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2603 * count, it's a buffer name.
2604 */
2605 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2606 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2607 || vim_iswhite(*p)))
2608 {
2609 n = getdigits(&ea.arg);
2610 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002611 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 {
2613 errormsg = (char_u *)_(e_zerocount);
2614 goto doend;
2615 }
2616 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2617 {
2618 ea.line2 = n;
2619 if (ea.addr_count == 0)
2620 ea.addr_count = 1;
2621 }
2622 else
2623 {
2624 ea.line1 = ea.line2;
2625 ea.line2 += n - 1;
2626 ++ea.addr_count;
2627 /*
2628 * Be vi compatible: no error message for out of range.
2629 */
2630 if (ea.line2 > curbuf->b_ml.ml_line_count)
2631 ea.line2 = curbuf->b_ml.ml_line_count;
2632 }
2633 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002634
2635 /*
2636 * Check for flags: 'l', 'p' and '#'.
2637 */
2638 if (ea.argt & EXFLAGS)
2639 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002641 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002642 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 {
2644 errormsg = (char_u *)_(e_trailing);
2645 goto doend;
2646 }
2647
2648 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2649 {
2650 errormsg = (char_u *)_(e_argreq);
2651 goto doend;
2652 }
2653
2654#ifdef FEAT_EVAL
2655 /*
2656 * Skip the command when it's not going to be executed.
2657 * The commands like :if, :endif, etc. always need to be executed.
2658 * Also make an exception for commands that handle a trailing command
2659 * themselves.
2660 */
2661 if (ea.skip)
2662 {
2663 switch (ea.cmdidx)
2664 {
2665 /* commands that need evaluation */
2666 case CMD_while:
2667 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002668 case CMD_for:
2669 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 case CMD_if:
2671 case CMD_elseif:
2672 case CMD_else:
2673 case CMD_endif:
2674 case CMD_try:
2675 case CMD_catch:
2676 case CMD_finally:
2677 case CMD_endtry:
2678 case CMD_function:
2679 break;
2680
2681 /* Commands that handle '|' themselves. Check: A command should
2682 * either have the TRLBAR flag, appear in this list or appear in
2683 * the list at ":help :bar". */
2684 case CMD_aboveleft:
2685 case CMD_and:
2686 case CMD_belowright:
2687 case CMD_botright:
2688 case CMD_browse:
2689 case CMD_call:
2690 case CMD_confirm:
2691 case CMD_delfunction:
2692 case CMD_djump:
2693 case CMD_dlist:
2694 case CMD_dsearch:
2695 case CMD_dsplit:
2696 case CMD_echo:
2697 case CMD_echoerr:
2698 case CMD_echomsg:
2699 case CMD_echon:
2700 case CMD_execute:
2701 case CMD_help:
2702 case CMD_hide:
2703 case CMD_ijump:
2704 case CMD_ilist:
2705 case CMD_isearch:
2706 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002707 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 case CMD_keepjumps:
2709 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002710 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 case CMD_leftabove:
2712 case CMD_let:
2713 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002714 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002716 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002717 case CMD_noautocmd:
2718 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 case CMD_perl:
2720 case CMD_psearch:
2721 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002722 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002723 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 case CMD_return:
2725 case CMD_rightbelow:
2726 case CMD_ruby:
2727 case CMD_silent:
2728 case CMD_smagic:
2729 case CMD_snomagic:
2730 case CMD_substitute:
2731 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002732 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 case CMD_tcl:
2734 case CMD_throw:
2735 case CMD_tilde:
2736 case CMD_topleft:
2737 case CMD_unlet:
2738 case CMD_verbose:
2739 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002740 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 break;
2742
2743 default: goto doend;
2744 }
2745 }
2746#endif
2747
2748 if (ea.argt & XFILE)
2749 {
2750 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2751 goto doend;
2752 }
2753
2754#ifdef FEAT_LISTCMDS
2755 /*
2756 * Accept buffer name. Cannot be used at the same time with a buffer
2757 * number. Don't do this for a user command.
2758 */
2759 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002760 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
2762 /*
2763 * :bdelete, :bwipeout and :bunload take several arguments, separated
2764 * by spaces: find next space (skipping over escaped characters).
2765 * The others take one argument: ignore trailing spaces.
2766 */
2767 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2768 || ea.cmdidx == CMD_bunload)
2769 p = skiptowhite_esc(ea.arg);
2770 else
2771 {
2772 p = ea.arg + STRLEN(ea.arg);
2773 while (p > ea.arg && vim_iswhite(p[-1]))
2774 --p;
2775 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002776 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2777 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 if (ea.line2 < 0) /* failed */
2779 goto doend;
2780 ea.addr_count = 1;
2781 ea.arg = skipwhite(p);
2782 }
2783#endif
2784
2785/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01002786 * 7. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 *
2788 * The "ea" structure holds the arguments that can be used.
2789 */
2790 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002791 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 ea.cookie = cookie;
2793#ifdef FEAT_EVAL
2794 ea.cstack = cstack;
2795#endif
2796
2797#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002798 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
2800 /*
2801 * Execute a user-defined command.
2802 */
2803 do_ucmd(&ea);
2804 }
2805 else
2806#endif
2807 {
2808 /*
2809 * Call the function to execute the command.
2810 */
2811 ea.errmsg = NULL;
2812 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2813 if (ea.errmsg != NULL)
2814 errormsg = (char_u *)_(ea.errmsg);
2815 }
2816
2817#ifdef FEAT_EVAL
2818 /*
2819 * If the command just executed called do_cmdline(), any throw or ":return"
2820 * or ":finish" encountered there must also check the cstack of the still
2821 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2822 * exception, or reanimate a returned function or finished script file and
2823 * return or finish it again.
2824 */
2825 if (need_rethrow)
2826 do_throw(cstack);
2827 else if (check_cstack)
2828 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002829 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002831 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 && current_func_returned())
2833 do_return(&ea, TRUE, FALSE, NULL);
2834 }
2835 need_rethrow = check_cstack = FALSE;
2836#endif
2837
2838doend:
2839 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2840 curwin->w_cursor.lnum = 1;
2841
2842 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2843 {
2844 if (sourcing)
2845 {
2846 if (errormsg != IObuff)
2847 {
2848 STRCPY(IObuff, errormsg);
2849 errormsg = IObuff;
2850 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002851 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853 emsg(errormsg);
2854 }
2855#ifdef FEAT_EVAL
2856 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002857 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2858 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859#endif
2860
2861 if (verbose_save >= 0)
2862 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002863#ifdef FEAT_AUTOCMD
2864 if (cmdmod.save_ei != NULL)
2865 {
2866 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002867 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2868 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002869 free_string_option(cmdmod.save_ei);
2870 }
2871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872
2873 cmdmod = save_cmdmod;
2874
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002875 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 {
2877 /* messages could be enabled for a serious error, need to check if the
2878 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002879 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002880 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 emsg_silent -= did_esilent;
2882 if (emsg_silent < 0)
2883 emsg_silent = 0;
2884 /* Restore msg_scroll, it's set by file I/O commands, even when no
2885 * message is actually displayed. */
2886 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002887
2888 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2889 * somewhere in the line. Put it back in the first column. */
2890 if (redirecting())
2891 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
2893
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002894#ifdef HAVE_SANDBOX
2895 if (did_sandbox)
2896 --sandbox;
2897#endif
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2900 ea.nextcmd = NULL;
2901
2902#ifdef FEAT_EVAL
2903 --ex_nesting_level;
2904#endif
2905
2906 return ea.nextcmd;
2907}
2908#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002909 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910#endif
2911
2912/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002913 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 * If there is a match advance "pp" to the argument and return TRUE.
2915 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002916 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002918 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 char *cmd; /* name of command */
2920 int len; /* required length */
2921{
2922 int i;
2923
2924 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002925 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 break;
2927 if (i >= len && !isalpha((*pp)[i]))
2928 {
2929 *pp = skipwhite(*pp + i);
2930 return TRUE;
2931 }
2932 return FALSE;
2933}
2934
2935/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002936 * Append "cmd" to the error message in IObuff.
2937 * Takes care of limiting the length and handling 0xa0, which would be
2938 * invisible otherwise.
2939 */
2940 static void
2941append_command(cmd)
2942 char_u *cmd;
2943{
2944 char_u *s = cmd;
2945 char_u *d;
2946
2947 STRCAT(IObuff, ": ");
2948 d = IObuff + STRLEN(IObuff);
2949 while (*s != NUL && d - IObuff < IOSIZE - 7)
2950 {
2951 if (
2952#ifdef FEAT_MBYTE
2953 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2954#endif
2955 *s == 0xa0)
2956 {
2957 s +=
2958#ifdef FEAT_MBYTE
2959 enc_utf8 ? 2 :
2960#endif
2961 1;
2962 STRCPY(d, "<a0>");
2963 d += 4;
2964 }
2965 else
2966 MB_COPY_CHAR(s, d);
2967 }
2968 *d = NUL;
2969}
2970
2971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002973 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002975 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 * Returns NULL for an ambiguous user command.
2977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 static char_u *
2979find_command(eap, full)
2980 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002981 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 int len;
2984 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002985 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986
2987 /*
2988 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002989 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 * - the 'k' command can directly be followed by any character.
2991 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2992 * but :sre[wind] is another command, as are :scrip[tnames],
2993 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002994 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 */
2996 p = eap->cmd;
2997 if (*p == 'k')
2998 {
2999 eap->cmdidx = CMD_k;
3000 ++p;
3001 }
3002 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003003 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
3004 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 || p[1] == 'g'
3006 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3007 || p[1] == 'I'
3008 || (p[1] == 'r' && p[2] != 'e')))
3009 {
3010 eap->cmdidx = CMD_substitute;
3011 ++p;
3012 }
3013 else
3014 {
3015 while (ASCII_ISALPHA(*p))
3016 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003017 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003018 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003019 while (ASCII_ISALNUM(*p))
3020 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 /* check for non-alpha command */
3023 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3024 ++p;
3025 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003026 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3027 {
3028 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3029 * :delete with the 'l' flag. Same for 'p'. */
3030 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003031 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003032 break;
3033 if (i == len - 1)
3034 {
3035 --len;
3036 if (p[-1] == 'l')
3037 eap->flags |= EXFLAG_LIST;
3038 else
3039 eap->flags |= EXFLAG_PRINT;
3040 }
3041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 if (ASCII_ISLOWER(*eap->cmd))
3044 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3045 else
3046 eap->cmdidx = cmdidxs[26];
3047
3048 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3049 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3050 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3051 (size_t)len) == 0)
3052 {
3053#ifdef FEAT_EVAL
3054 if (full != NULL
3055 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3056 *full = TRUE;
3057#endif
3058 break;
3059 }
3060
3061#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003062 /* Look for a user defined command as a last resort. Let ":Print" be
3063 * overruled by a user defined command. */
3064 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3065 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003067 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 while (ASCII_ISALNUM(*p))
3069 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003070 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 }
3072#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003073 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 eap->cmdidx = CMD_SIZE;
3075 }
3076
3077 return p;
3078}
3079
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003080#ifdef FEAT_USR_CMDS
3081/*
3082 * Search for a user command that matches "eap->cmd".
3083 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3084 * Return a pointer to just after the command.
3085 * Return NULL if there is no matching command.
3086 */
3087 static char_u *
3088find_ucmd(eap, p, full, xp, compl)
3089 exarg_T *eap;
3090 char_u *p; /* end of the command (possibly including count) */
3091 int *full; /* set to TRUE for a full match */
3092 expand_T *xp; /* used for completion, NULL otherwise */
3093 int *compl; /* completion flags or NULL */
3094{
3095 int len = (int)(p - eap->cmd);
3096 int j, k, matchlen = 0;
3097 ucmd_T *uc;
3098 int found = FALSE;
3099 int possible = FALSE;
3100 char_u *cp, *np; /* Point into typed cmd and test name */
3101 garray_T *gap;
3102 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3103 only full match global is accepted. */
3104
3105 /*
3106 * Look for buffer-local user commands first, then global ones.
3107 */
3108 gap = &curbuf->b_ucmds;
3109 for (;;)
3110 {
3111 for (j = 0; j < gap->ga_len; ++j)
3112 {
3113 uc = USER_CMD_GA(gap, j);
3114 cp = eap->cmd;
3115 np = uc->uc_name;
3116 k = 0;
3117 while (k < len && *np != NUL && *cp++ == *np++)
3118 k++;
3119 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3120 {
3121 /* If finding a second match, the command is ambiguous. But
3122 * not if a buffer-local command wasn't a full match and a
3123 * global command is a full match. */
3124 if (k == len && found && *np != NUL)
3125 {
3126 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003127 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003128 amb_local = TRUE;
3129 }
3130
3131 if (!found || (k == len && *np == NUL))
3132 {
3133 /* If we matched up to a digit, then there could
3134 * be another command including the digit that we
3135 * should use instead.
3136 */
3137 if (k == len)
3138 found = TRUE;
3139 else
3140 possible = TRUE;
3141
3142 if (gap == &ucmds)
3143 eap->cmdidx = CMD_USER;
3144 else
3145 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003146 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003147 eap->useridx = j;
3148
3149# ifdef FEAT_CMDL_COMPL
3150 if (compl != NULL)
3151 *compl = uc->uc_compl;
3152# ifdef FEAT_EVAL
3153 if (xp != NULL)
3154 {
3155 xp->xp_arg = uc->uc_compl_arg;
3156 xp->xp_scriptID = uc->uc_scriptID;
3157 }
3158# endif
3159# endif
3160 /* Do not search for further abbreviations
3161 * if this is an exact match. */
3162 matchlen = k;
3163 if (k == len && *np == NUL)
3164 {
3165 if (full != NULL)
3166 *full = TRUE;
3167 amb_local = FALSE;
3168 break;
3169 }
3170 }
3171 }
3172 }
3173
3174 /* Stop if we found a full match or searched all. */
3175 if (j < gap->ga_len || gap == &ucmds)
3176 break;
3177 gap = &ucmds;
3178 }
3179
3180 /* Only found ambiguous matches. */
3181 if (amb_local)
3182 {
3183 if (xp != NULL)
3184 xp->xp_context = EXPAND_UNSUCCESSFUL;
3185 return NULL;
3186 }
3187
3188 /* The match we found may be followed immediately by a number. Move "p"
3189 * back to point to it. */
3190 if (found || possible)
3191 return p + (matchlen - len);
3192 return p;
3193}
3194#endif
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003197static struct cmdmod
3198{
3199 char *name;
3200 int minlen;
3201 int has_count; /* :123verbose :3tab */
3202} cmdmods[] = {
3203 {"aboveleft", 3, FALSE},
3204 {"belowright", 3, FALSE},
3205 {"botright", 2, FALSE},
3206 {"browse", 3, FALSE},
3207 {"confirm", 4, FALSE},
3208 {"hide", 3, FALSE},
3209 {"keepalt", 5, FALSE},
3210 {"keepjumps", 5, FALSE},
3211 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003212 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003213 {"leftabove", 5, FALSE},
3214 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003215 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003216 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003217 {"rightbelow", 6, FALSE},
3218 {"sandbox", 3, FALSE},
3219 {"silent", 3, FALSE},
3220 {"tab", 3, TRUE},
3221 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003222 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003223 {"verbose", 4, TRUE},
3224 {"vertical", 4, FALSE},
3225};
3226
3227/*
3228 * Return length of a command modifier (including optional count).
3229 * Return zero when it's not a modifier.
3230 */
3231 int
3232modifier_len(cmd)
3233 char_u *cmd;
3234{
3235 int i, j;
3236 char_u *p = cmd;
3237
3238 if (VIM_ISDIGIT(*cmd))
3239 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003240 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003241 {
3242 for (j = 0; p[j] != NUL; ++j)
3243 if (p[j] != cmdmods[i].name[j])
3244 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003245 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003246 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003247 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003248 }
3249 return 0;
3250}
3251
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252/*
3253 * Return > 0 if an Ex command "name" exists.
3254 * Return 2 if there is an exact match.
3255 * Return 3 if there is an ambiguous match.
3256 */
3257 int
3258cmd_exists(name)
3259 char_u *name;
3260{
3261 exarg_T ea;
3262 int full = FALSE;
3263 int i;
3264 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003265 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266
3267 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003268 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
3270 for (j = 0; name[j] != NUL; ++j)
3271 if (name[j] != cmdmods[i].name[j])
3272 break;
3273 if (name[j] == NUL && j >= cmdmods[i].minlen)
3274 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3275 }
3276
Bram Moolenaara9587612006-05-04 21:47:50 +00003277 /* Check built-in commands and user defined commands.
3278 * For ":2match" and ":3match" we need to skip the number. */
3279 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003281 p = find_command(&ea, &full);
3282 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003284 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3285 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003286 if (*skipwhite(p) != NUL)
3287 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3289}
3290#endif
3291
3292/*
3293 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3294 * we don't need/want deleted. Maybe this could be done better if we didn't
3295 * repeat all this stuff. The only problem is that they may not stay
3296 * perfectly compatible with each other, but then the command line syntax
3297 * probably won't change that much -- webb.
3298 */
3299 char_u *
3300set_one_cmd_context(xp, buff)
3301 expand_T *xp;
3302 char_u *buff; /* buffer for command string */
3303{
3304 char_u *p;
3305 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003306 int len = 0;
3307 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3309 int compl = EXPAND_NOTHING;
3310#endif
3311#ifdef FEAT_CMDL_COMPL
3312 int delim;
3313#endif
3314 int forceit = FALSE;
3315 int usefilter = FALSE; /* filter instead of file name */
3316
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003317 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 xp->xp_pattern = buff;
3319 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003320 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321
3322/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003323 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 */
3325 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3326 ;
3327 xp->xp_pattern = cmd;
3328
3329 if (*cmd == NUL)
3330 return NULL;
3331 if (*cmd == '"') /* ignore comment lines */
3332 {
3333 xp->xp_context = EXPAND_NOTHING;
3334 return NULL;
3335 }
3336
3337/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003338 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 */
3340 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 xp->xp_pattern = cmd;
3342 if (*cmd == NUL)
3343 return NULL;
3344 if (*cmd == '"')
3345 {
3346 xp->xp_context = EXPAND_NOTHING;
3347 return NULL;
3348 }
3349
3350 if (*cmd == '|' || *cmd == '\n')
3351 return cmd + 1; /* There's another command */
3352
3353 /*
3354 * Isolate the command and search for it in the command table.
3355 * Exceptions:
3356 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003357 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3359 */
3360 if (*cmd == 'k' && cmd[1] != 'e')
3361 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003362 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 p = cmd + 1;
3364 }
3365 else
3366 {
3367 p = cmd;
3368 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3369 ++p;
3370 /* check for non-alpha command */
3371 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3372 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003373 /* for python 3.x: ":py3*" commands completion */
3374 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003375 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003376 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003377 while (ASCII_ISALPHA(*p) || *p == '*')
3378 ++p;
3379 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003380 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003382 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 {
3384 xp->xp_context = EXPAND_UNSUCCESSFUL;
3385 return NULL;
3386 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003387 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003388 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3389 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3390 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 break;
3392
3393#ifdef FEAT_USR_CMDS
3394 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3396 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397#endif
3398 }
3399
3400 /*
3401 * If the cursor is touching the command, and it ends in an alpha-numeric
3402 * character, complete the command name.
3403 */
3404 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3405 return NULL;
3406
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003407 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
3409 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3410 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003411 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 p = cmd + 1;
3413 }
3414#ifdef FEAT_USR_CMDS
3415 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3416 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003417 ea.cmd = cmd;
3418 p = find_ucmd(&ea, p, NULL, xp,
3419# if defined(FEAT_CMDL_COMPL)
3420 &compl
3421# else
3422 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003424 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003425 if (p == NULL)
3426 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428#endif
3429 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003430 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
3432 /* Not still touching the command and it was an illegal one */
3433 xp->xp_context = EXPAND_UNSUCCESSFUL;
3434 return NULL;
3435 }
3436
3437 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3438
3439 if (*p == '!') /* forced commands */
3440 {
3441 forceit = TRUE;
3442 ++p;
3443 }
3444
3445/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003446 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003448 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003449 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 arg = skipwhite(p);
3452
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
3455 if (*arg == '>') /* append */
3456 {
3457 if (*++arg == '>')
3458 ++arg;
3459 arg = skipwhite(arg);
3460 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003461 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 ++arg;
3464 usefilter = TRUE;
3465 }
3466 }
3467
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 usefilter = forceit; /* :r! filter if forced */
3471 if (*arg == '!') /* :r !filter */
3472 {
3473 ++arg;
3474 usefilter = TRUE;
3475 }
3476 }
3477
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003478 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
3480 while (*arg == *cmd) /* allow any number of '>' or '<' */
3481 ++arg;
3482 arg = skipwhite(arg);
3483 }
3484
3485 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003486 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
3488 /* Check if we're in the +command */
3489 p = arg + 1;
3490 arg = skip_cmd_arg(arg, FALSE);
3491
3492 /* Still touching the command after '+'? */
3493 if (*arg == NUL)
3494 return p;
3495
3496 /* Skip space(s) after +command to get to the real argument */
3497 arg = skipwhite(arg);
3498 }
3499
3500 /*
3501 * Check for '|' to separate commands and '"' to start comments.
3502 * Don't do this for ":read !cmd" and ":write !cmd".
3503 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003504 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 {
3506 p = arg;
3507 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003508 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 p += 2;
3510 while (*p)
3511 {
3512 if (*p == Ctrl_V)
3513 {
3514 if (p[1] != NUL)
3515 ++p;
3516 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003517 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 || *p == '|' || *p == '\n')
3519 {
3520 if (*(p - 1) != '\\')
3521 {
3522 if (*p == '|' || *p == '\n')
3523 return p + 1;
3524 return NULL; /* It's a comment */
3525 }
3526 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003527 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
3529 }
3530
3531 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003532 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 vim_strchr((char_u *)"|\"", *arg) == NULL)
3534 return NULL;
3535
3536 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003537 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003539 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003540 while (*p && p < buff + len)
3541 {
3542 if (*p == ' ' || *p == TAB)
3543 {
3544 /* argument starts after a space */
3545 xp->xp_pattern = ++p;
3546 }
3547 else
3548 {
3549 if (*p == '\\' && *(p + 1) != NUL)
3550 ++p; /* skip over escaped character */
3551 mb_ptr_adv(p);
3552 }
3553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003555 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003557 int c;
3558 int in_quote = FALSE;
3559 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 /*
3562 * Allow spaces within back-quotes to count as part of the argument
3563 * being expanded.
3564 */
3565 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003566 p = xp->xp_pattern;
3567 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003569#ifdef FEAT_MBYTE
3570 if (has_mbyte)
3571 c = mb_ptr2char(p);
3572 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003574 c = *p;
3575 if (c == '\\' && p[1] != NUL)
3576 ++p;
3577 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
3579 if (!in_quote)
3580 {
3581 xp->xp_pattern = p;
3582 bow = p + 1;
3583 }
3584 in_quote = !in_quote;
3585 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003586 /* An argument can contain just about everything, except
3587 * characters that end the command and white space. */
3588 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003589#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003590 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003591#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003592 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003593 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003594 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003595 while (*p != NUL)
3596 {
3597#ifdef FEAT_MBYTE
3598 if (has_mbyte)
3599 c = mb_ptr2char(p);
3600 else
3601#endif
3602 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003603 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003604 break;
3605#ifdef FEAT_MBYTE
3606 if (has_mbyte)
3607 len = (*mb_ptr2len)(p);
3608 else
3609#endif
3610 len = 1;
3611 mb_ptr_adv(p);
3612 }
3613 if (in_quote)
3614 bow = p;
3615 else
3616 xp->xp_pattern = p;
3617 p -= len;
3618 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003619 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 }
3621
3622 /*
3623 * If we are still inside the quotes, and we passed a space, just
3624 * expand from there.
3625 */
3626 if (bow != NULL && in_quote)
3627 xp->xp_pattern = bow;
3628 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003629
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003630 /* For a shell command more chars need to be escaped. */
3631 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003632 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003633#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003634 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003635#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003636 /* When still after the command name expand executables. */
3637 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003638 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
3641 /* Check for environment variable */
3642 if (*xp->xp_pattern == '$'
3643#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3644 || *xp->xp_pattern == '%'
3645#endif
3646 )
3647 {
3648 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3649 if (!vim_isIDc(*p))
3650 break;
3651 if (*p == NUL)
3652 {
3653 xp->xp_context = EXPAND_ENV_VARS;
3654 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003655#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3656 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003657 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003658 compl = EXPAND_ENV_VARS;
3659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003662#if defined(FEAT_CMDL_COMPL)
3663 /* Check for user names */
3664 if (*xp->xp_pattern == '~')
3665 {
3666 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3667 ;
3668 /* Complete ~user only if it partially matches a user name.
3669 * A full match ~user<Tab> will be replaced by user's home
3670 * directory i.e. something like ~user<Tab> -> /home/user/ */
3671 if (*p == NUL && p > xp->xp_pattern + 1
3672 && match_user(xp->xp_pattern + 1) == 1)
3673 {
3674 xp->xp_context = EXPAND_USER;
3675 ++xp->xp_pattern;
3676 }
3677 }
3678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680
3681/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003682 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003684 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003686 case CMD_find:
3687 case CMD_sfind:
3688 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003689 if (xp->xp_context == EXPAND_FILES)
3690 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003691 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 case CMD_cd:
3693 case CMD_chdir:
3694 case CMD_lcd:
3695 case CMD_lchdir:
3696 if (xp->xp_context == EXPAND_FILES)
3697 xp->xp_context = EXPAND_DIRECTORIES;
3698 break;
3699 case CMD_help:
3700 xp->xp_context = EXPAND_HELP;
3701 xp->xp_pattern = arg;
3702 break;
3703
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003704 /* Command modifiers: return the argument.
3705 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003707 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 case CMD_belowright:
3709 case CMD_botright:
3710 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003711 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003713 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 case CMD_folddoclosed:
3715 case CMD_folddoopen:
3716 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003717 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 case CMD_keepjumps:
3719 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003720 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 case CMD_leftabove:
3722 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003723 case CMD_noautocmd:
3724 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003726 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003728 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003729 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 case CMD_topleft:
3731 case CMD_verbose:
3732 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003733 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 return arg;
3735
Bram Moolenaar4f688582007-07-24 12:34:30 +00003736#ifdef FEAT_CMDL_COMPL
3737# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 case CMD_match:
3739 if (*arg == NUL || !ends_excmd(*arg))
3740 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003741 /* also complete "None" */
3742 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 arg = skipwhite(skiptowhite(arg));
3744 if (*arg != NUL)
3745 {
3746 xp->xp_context = EXPAND_NOTHING;
3747 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3748 }
3749 }
3750 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753/*
3754 * All completion for the +cmdline_compl feature goes here.
3755 */
3756
3757# ifdef FEAT_USR_CMDS
3758 case CMD_command:
3759 /* Check for attributes */
3760 while (*arg == '-')
3761 {
3762 arg++; /* Skip "-" */
3763 p = skiptowhite(arg);
3764 if (*p == NUL)
3765 {
3766 /* Cursor is still in the attribute */
3767 p = vim_strchr(arg, '=');
3768 if (p == NULL)
3769 {
3770 /* No "=", so complete attribute names */
3771 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3772 xp->xp_pattern = arg;
3773 return NULL;
3774 }
3775
3776 /* For the -complete and -nargs attributes, we complete
3777 * their arguments as well.
3778 */
3779 if (STRNICMP(arg, "complete", p - arg) == 0)
3780 {
3781 xp->xp_context = EXPAND_USER_COMPLETE;
3782 xp->xp_pattern = p + 1;
3783 return NULL;
3784 }
3785 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3786 {
3787 xp->xp_context = EXPAND_USER_NARGS;
3788 xp->xp_pattern = p + 1;
3789 return NULL;
3790 }
3791 return NULL;
3792 }
3793 arg = skipwhite(p);
3794 }
3795
3796 /* After the attributes comes the new command name */
3797 p = skiptowhite(arg);
3798 if (*p == NUL)
3799 {
3800 xp->xp_context = EXPAND_USER_COMMANDS;
3801 xp->xp_pattern = arg;
3802 break;
3803 }
3804
3805 /* And finally comes a normal command */
3806 return skipwhite(p);
3807
3808 case CMD_delcommand:
3809 xp->xp_context = EXPAND_USER_COMMANDS;
3810 xp->xp_pattern = arg;
3811 break;
3812# endif
3813
3814 case CMD_global:
3815 case CMD_vglobal:
3816 delim = *arg; /* get the delimiter */
3817 if (delim)
3818 ++arg; /* skip delimiter if there is one */
3819
3820 while (arg[0] != NUL && arg[0] != delim)
3821 {
3822 if (arg[0] == '\\' && arg[1] != NUL)
3823 ++arg;
3824 ++arg;
3825 }
3826 if (arg[0] != NUL)
3827 return arg + 1;
3828 break;
3829 case CMD_and:
3830 case CMD_substitute:
3831 delim = *arg;
3832 if (delim)
3833 {
3834 /* skip "from" part */
3835 ++arg;
3836 arg = skip_regexp(arg, delim, p_magic, NULL);
3837 }
3838 /* skip "to" part */
3839 while (arg[0] != NUL && arg[0] != delim)
3840 {
3841 if (arg[0] == '\\' && arg[1] != NUL)
3842 ++arg;
3843 ++arg;
3844 }
3845 if (arg[0] != NUL) /* skip delimiter */
3846 ++arg;
3847 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3848 ++arg;
3849 if (arg[0] != NUL)
3850 return arg;
3851 break;
3852 case CMD_isearch:
3853 case CMD_dsearch:
3854 case CMD_ilist:
3855 case CMD_dlist:
3856 case CMD_ijump:
3857 case CMD_psearch:
3858 case CMD_djump:
3859 case CMD_isplit:
3860 case CMD_dsplit:
3861 arg = skipwhite(skipdigits(arg)); /* skip count */
3862 if (*arg == '/') /* Match regexp, not just whole words */
3863 {
3864 for (++arg; *arg && *arg != '/'; arg++)
3865 if (*arg == '\\' && arg[1] != NUL)
3866 arg++;
3867 if (*arg)
3868 {
3869 arg = skipwhite(arg + 1);
3870
3871 /* Check for trailing illegal characters */
3872 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3873 xp->xp_context = EXPAND_NOTHING;
3874 else
3875 return arg;
3876 }
3877 }
3878 break;
3879#ifdef FEAT_AUTOCMD
3880 case CMD_autocmd:
3881 return set_context_in_autocmd(xp, arg, FALSE);
3882
3883 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003884 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return set_context_in_autocmd(xp, arg, TRUE);
3886#endif
3887 case CMD_set:
3888 set_context_in_set_cmd(xp, arg, 0);
3889 break;
3890 case CMD_setglobal:
3891 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3892 break;
3893 case CMD_setlocal:
3894 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3895 break;
3896 case CMD_tag:
3897 case CMD_stag:
3898 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003899 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 case CMD_tselect:
3901 case CMD_stselect:
3902 case CMD_ptselect:
3903 case CMD_tjump:
3904 case CMD_stjump:
3905 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003906 if (*p_wop != NUL)
3907 xp->xp_context = EXPAND_TAGS_LISTFILES;
3908 else
3909 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 xp->xp_pattern = arg;
3911 break;
3912 case CMD_augroup:
3913 xp->xp_context = EXPAND_AUGROUP;
3914 xp->xp_pattern = arg;
3915 break;
3916#ifdef FEAT_SYN_HL
3917 case CMD_syntax:
3918 set_context_in_syntax_cmd(xp, arg);
3919 break;
3920#endif
3921#ifdef FEAT_EVAL
3922 case CMD_let:
3923 case CMD_if:
3924 case CMD_elseif:
3925 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003926 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 case CMD_echo:
3928 case CMD_echon:
3929 case CMD_execute:
3930 case CMD_echomsg:
3931 case CMD_echoerr:
3932 case CMD_call:
3933 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003934 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
3936
3937 case CMD_unlet:
3938 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3939 arg = xp->xp_pattern + 1;
3940 xp->xp_context = EXPAND_USER_VARS;
3941 xp->xp_pattern = arg;
3942 break;
3943
3944 case CMD_function:
3945 case CMD_delfunction:
3946 xp->xp_context = EXPAND_USER_FUNC;
3947 xp->xp_pattern = arg;
3948 break;
3949
3950 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003951 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 break;
3953#endif
3954 case CMD_highlight:
3955 set_context_in_highlight_cmd(xp, arg);
3956 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003957#ifdef FEAT_CSCOPE
3958 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003959 case CMD_lcscope:
3960 case CMD_scscope:
3961 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003962 break;
3963#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003964#ifdef FEAT_SIGNS
3965 case CMD_sign:
3966 set_context_in_sign_cmd(xp, arg);
3967 break;
3968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969#ifdef FEAT_LISTCMDS
3970 case CMD_bdelete:
3971 case CMD_bwipeout:
3972 case CMD_bunload:
3973 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3974 arg = xp->xp_pattern + 1;
3975 /*FALLTHROUGH*/
3976 case CMD_buffer:
3977 case CMD_sbuffer:
3978 case CMD_checktime:
3979 xp->xp_context = EXPAND_BUFFERS;
3980 xp->xp_pattern = arg;
3981 break;
3982#endif
3983#ifdef FEAT_USR_CMDS
3984 case CMD_USER:
3985 case CMD_USER_BUF:
3986 if (compl != EXPAND_NOTHING)
3987 {
3988 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003989 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
3991# ifdef FEAT_MENU
3992 if (compl == EXPAND_MENUS)
3993 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3994# endif
3995 if (compl == EXPAND_COMMANDS)
3996 return arg;
3997 if (compl == EXPAND_MAPPINGS)
3998 return set_context_in_map_cmd(xp, (char_u *)"map",
3999 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004000 /* Find start of last argument. */
4001 p = arg;
4002 while (*p)
4003 {
4004 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004005 /* argument starts after a space */
4006 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004007 else if (*p == '\\' && *(p + 1) != NUL)
4008 ++p; /* skip over escaped character */
4009 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 xp->xp_pattern = arg;
4012 }
4013 xp->xp_context = compl;
4014 }
4015 break;
4016#endif
4017 case CMD_map: case CMD_noremap:
4018 case CMD_nmap: case CMD_nnoremap:
4019 case CMD_vmap: case CMD_vnoremap:
4020 case CMD_omap: case CMD_onoremap:
4021 case CMD_imap: case CMD_inoremap:
4022 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004023 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004024 case CMD_smap: case CMD_snoremap:
4025 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004027 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 case CMD_unmap:
4029 case CMD_nunmap:
4030 case CMD_vunmap:
4031 case CMD_ounmap:
4032 case CMD_iunmap:
4033 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004034 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004035 case CMD_sunmap:
4036 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004038 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 case CMD_abbreviate: case CMD_noreabbrev:
4040 case CMD_cabbrev: case CMD_cnoreabbrev:
4041 case CMD_iabbrev: case CMD_inoreabbrev:
4042 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004043 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 case CMD_unabbreviate:
4045 case CMD_cunabbrev:
4046 case CMD_iunabbrev:
4047 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004048 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#ifdef FEAT_MENU
4050 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4051 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4052 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4053 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4054 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4055 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4056 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4057 case CMD_tmenu: case CMD_tunmenu:
4058 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4059 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4060#endif
4061
4062 case CMD_colorscheme:
4063 xp->xp_context = EXPAND_COLORS;
4064 xp->xp_pattern = arg;
4065 break;
4066
4067 case CMD_compiler:
4068 xp->xp_context = EXPAND_COMPILER;
4069 xp->xp_pattern = arg;
4070 break;
4071
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004072 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004073 xp->xp_context = EXPAND_OWNSYNTAX;
4074 xp->xp_pattern = arg;
4075 break;
4076
4077 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004078 xp->xp_context = EXPAND_FILETYPE;
4079 xp->xp_pattern = arg;
4080 break;
4081
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4083 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4084 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004085 p = skiptowhite(arg);
4086 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
4088 xp->xp_context = EXPAND_LANGUAGE;
4089 xp->xp_pattern = arg;
4090 }
4091 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004092 {
4093 if ( STRNCMP(arg, "messages", p - arg) == 0
4094 || STRNCMP(arg, "ctype", p - arg) == 0
4095 || STRNCMP(arg, "time", p - arg) == 0)
4096 {
4097 xp->xp_context = EXPAND_LOCALES;
4098 xp->xp_pattern = skipwhite(p);
4099 }
4100 else
4101 xp->xp_context = EXPAND_NOTHING;
4102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 break;
4104#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004105#if defined(FEAT_PROFILE)
4106 case CMD_profile:
4107 set_context_in_profile_cmd(xp, arg);
4108 break;
4109#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004110 case CMD_behave:
4111 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004112 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004115#if defined(FEAT_CMDHIST)
4116 case CMD_history:
4117 xp->xp_context = EXPAND_HISTORY;
4118 xp->xp_pattern = arg;
4119 break;
4120#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004121#if defined(FEAT_PROFILE)
4122 case CMD_syntime:
4123 xp->xp_context = EXPAND_SYNTIME;
4124 xp->xp_pattern = arg;
4125 break;
4126#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128#endif /* FEAT_CMDL_COMPL */
4129
4130 default:
4131 break;
4132 }
4133 return NULL;
4134}
4135
4136/*
4137 * skip a range specifier of the form: addr [,addr] [;addr] ..
4138 *
4139 * Backslashed delimiters after / or ? will be skipped, and commands will
4140 * not be expanded between /'s and ?'s or after "'".
4141 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004142 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 * Returns the "cmd" pointer advanced to beyond the range.
4144 */
4145 char_u *
4146skip_range(cmd, ctx)
4147 char_u *cmd;
4148 int *ctx; /* pointer to xp_context or NULL */
4149{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004150 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
Bram Moolenaardf177f62005-02-22 08:39:57 +00004152 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
4154 if (*cmd == '\'')
4155 {
4156 if (*++cmd == NUL && ctx != NULL)
4157 *ctx = EXPAND_NOTHING;
4158 }
4159 else if (*cmd == '/' || *cmd == '?')
4160 {
4161 delim = *cmd++;
4162 while (*cmd != NUL && *cmd != delim)
4163 if (*cmd++ == '\\' && *cmd != NUL)
4164 ++cmd;
4165 if (*cmd == NUL && ctx != NULL)
4166 *ctx = EXPAND_NOTHING;
4167 }
4168 if (*cmd != NUL)
4169 ++cmd;
4170 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004171
4172 /* Skip ":" and white space. */
4173 while (*cmd == ':')
4174 cmd = skipwhite(cmd + 1);
4175
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 return cmd;
4177}
4178
4179/*
4180 * get a single EX address
4181 *
4182 * Set ptr to the next character after the part that was interpreted.
4183 * Set ptr to NULL when an error is encountered.
4184 *
4185 * Return MAXLNUM when no Ex address was found.
4186 */
4187 static linenr_T
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004188get_address(ptr, addr_type, skip, to_other_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 char_u **ptr;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004190 int addr_type; /* flag: one of ADDR_LINES, ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 int skip; /* only skip the address, don't use it */
4192 int to_other_file; /* flag: may jump to other file */
4193{
4194 int c;
4195 int i;
4196 long n;
4197 char_u *cmd;
4198 pos_T pos;
4199 pos_T *fp;
4200 linenr_T lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004201 win_T *wp;
4202 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
4204 cmd = skipwhite(*ptr);
4205 lnum = MAXLNUM;
4206 do
4207 {
4208 switch (*cmd)
4209 {
4210 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004211 ++cmd;
4212 switch (addr_type)
4213 {
4214 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 lnum = curwin->w_cursor.lnum;
4216 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004217 case ADDR_WINDOWS:
4218 lnum = 0;
4219 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4220 {
4221 lnum++;
4222 if (wp == curwin)
4223 break;
4224 }
4225 break;
4226 case ADDR_ARGUMENTS:
4227 lnum = curwin->w_arg_idx + 1;
4228 break;
4229 case ADDR_LOADED_BUFFERS:
4230 case ADDR_UNLOADED_BUFFERS:
4231 lnum = curbuf->b_fnum;
4232 break;
4233 case ADDR_TABS:
4234 lnum = 0;
4235 for(tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4236 {
4237 lnum++;
4238 if (tp == curtab)
4239 break;
4240 }
4241 break;
4242 }
4243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004246 ++cmd;
4247 switch (addr_type)
4248 {
4249 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 lnum = curbuf->b_ml.ml_line_count;
4251 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004252 case ADDR_WINDOWS:
4253 lnum = 0;
4254 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4255 lnum++;
4256 break;
4257 case ADDR_ARGUMENTS:
4258 lnum = ARGCOUNT;
4259 break;
4260 case ADDR_LOADED_BUFFERS:
4261 case ADDR_UNLOADED_BUFFERS:
4262 lnum = lastbuf->b_fnum;
4263 break;
4264 case ADDR_TABS:
4265 lnum = 0;
4266 for(tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4267 lnum++;
4268 break;
4269 }
4270 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271
4272 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004273 if (*++cmd == NUL)
4274 {
4275 cmd = NULL;
4276 goto error;
4277 }
4278 if (addr_type != ADDR_LINES)
4279 {
4280 EMSG(_(e_invaddr));
4281 goto error;
4282 }
4283 if (skip)
4284 ++cmd;
4285 else
4286 {
4287 /* Only accept a mark in another file when it is
4288 * used by itself: ":'M". */
4289 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4290 ++cmd;
4291 if (fp == (pos_T *)-1)
4292 /* Jumped to another file. */
4293 lnum = curwin->w_cursor.lnum;
4294 else
4295 {
4296 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 {
4298 cmd = NULL;
4299 goto error;
4300 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004301 lnum = fp->lnum;
4302 }
4303 }
4304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 case '/':
4307 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004308 c = *cmd++;
4309 if (addr_type != ADDR_LINES)
4310 {
4311 EMSG(_(e_invaddr));
4312 goto error;
4313 }
4314 if (skip) /* skip "/pat/" */
4315 {
4316 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4317 if (*cmd == c)
4318 ++cmd;
4319 }
4320 else
4321 {
4322 pos = curwin->w_cursor; /* save curwin->w_cursor */
4323 /*
4324 * When '/' or '?' follows another address, start
4325 * from there.
4326 */
4327 if (lnum != MAXLNUM)
4328 curwin->w_cursor.lnum = lnum;
4329 /*
4330 * Start a forward search at the end of the line.
4331 * Start a backward search at the start of the line.
4332 * This makes sure we never match in the current
4333 * line, and can match anywhere in the
4334 * next/previous line.
4335 */
4336 if (c == '/')
4337 curwin->w_cursor.col = MAXCOL;
4338 else
4339 curwin->w_cursor.col = 0;
4340 searchcmdlen = 0;
4341 if (!do_search(NULL, c, cmd, 1L,
4342 SEARCH_HIS | SEARCH_MSG, NULL))
4343 {
4344 curwin->w_cursor = pos;
4345 cmd = NULL;
4346 goto error;
4347 }
4348 lnum = curwin->w_cursor.lnum;
4349 curwin->w_cursor = pos;
4350 /* adjust command string pointer */
4351 cmd += searchcmdlen;
4352 }
4353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004356 ++cmd;
4357 if (addr_type != ADDR_LINES)
4358 {
4359 EMSG(_(e_invaddr));
4360 goto error;
4361 }
4362 if (*cmd == '&')
4363 i = RE_SUBST;
4364 else if (*cmd == '?' || *cmd == '/')
4365 i = RE_SEARCH;
4366 else
4367 {
4368 EMSG(_(e_backslash));
4369 cmd = NULL;
4370 goto error;
4371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 if (!skip)
4374 {
4375 /*
4376 * When search follows another address, start from
4377 * there.
4378 */
4379 if (lnum != MAXLNUM)
4380 pos.lnum = lnum;
4381 else
4382 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004384 /*
4385 * Start the search just like for the above
4386 * do_search().
4387 */
4388 if (*cmd != '?')
4389 pos.col = MAXCOL;
4390 else
4391 pos.col = 0;
4392 if (searchit(curwin, curbuf, &pos,
4393 *cmd == '?' ? BACKWARD : FORWARD,
4394 (char_u *)"", 1L, SEARCH_MSG,
4395 i, (linenr_T)0, NULL) != FAIL)
4396 lnum = pos.lnum;
4397 else
4398 {
4399 cmd = NULL;
4400 goto error;
4401 }
4402 }
4403 ++cmd;
4404 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4408 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410
4411 for (;;)
4412 {
4413 cmd = skipwhite(cmd);
4414 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4415 break;
4416
4417 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004418 {
4419 switch (addr_type)
4420 {
4421 case ADDR_LINES:
4422 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4423 break;
4424 case ADDR_WINDOWS:
4425 lnum = 0;
4426 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4427 {
4428 lnum++;
4429 if (wp == curwin)
4430 break;
4431 }
4432 break;
4433 case ADDR_ARGUMENTS:
4434 lnum = curwin->w_arg_idx + 1;
4435 break;
4436 case ADDR_LOADED_BUFFERS:
4437 case ADDR_UNLOADED_BUFFERS:
4438 lnum = curbuf->b_fnum;
4439 break;
4440 case ADDR_TABS:
4441 lnum = 0;
4442 for(tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4443 {
4444 lnum++;
4445 if (tp == curtab)
4446 break;
4447 }
4448 break;
4449 }
4450 }
4451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (VIM_ISDIGIT(*cmd))
4453 i = '+'; /* "number" is same as "+number" */
4454 else
4455 i = *cmd++;
4456 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4457 n = 1;
4458 else
4459 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004460 if (addr_type == ADDR_LOADED_BUFFERS
4461 || addr_type == ADDR_UNLOADED_BUFFERS)
4462 lnum = compute_buffer_local_count(addr_type, lnum, n);
4463 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 lnum -= n;
4465 else
4466 lnum += n;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004467
4468 switch (addr_type)
4469 {
4470 case ADDR_LINES:
4471 break;
4472 case ADDR_ARGUMENTS:
4473 if (lnum < 0)
4474 lnum = 0;
4475 else if (lnum >= ARGCOUNT)
4476 lnum = ARGCOUNT;
4477 break;
4478 case ADDR_TABS:
4479 if (lnum < 0)
4480 {
4481 lnum = 0;
4482 break;
4483 }
4484 c = 0;
4485 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4486 c++;
4487 if (lnum >= c)
4488 lnum = c;
4489 break;
4490 case ADDR_WINDOWS:
4491 if (lnum < 0)
4492 {
4493 lnum = 0;
4494 break;
4495 }
4496 c = 0;
4497 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4498 c++;
4499 if (lnum > c)
4500 lnum = c;
4501 break;
4502 case ADDR_LOADED_BUFFERS:
4503 case ADDR_UNLOADED_BUFFERS:
4504 if (lnum < firstbuf->b_fnum)
4505 {
4506 lnum = firstbuf->b_fnum;
4507 break;
4508 }
4509 if (lnum > lastbuf->b_fnum)
4510 lnum = lastbuf->b_fnum;
4511 break;
4512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 } while (*cmd == '/' || *cmd == '?');
4515
4516error:
4517 *ptr = cmd;
4518 return lnum;
4519}
4520
4521/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004522 * Get flags from an Ex command argument.
4523 */
4524 static void
4525get_flags(eap)
4526 exarg_T *eap;
4527{
4528 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4529 {
4530 if (*eap->arg == 'l')
4531 eap->flags |= EXFLAG_LIST;
4532 else if (*eap->arg == 'p')
4533 eap->flags |= EXFLAG_PRINT;
4534 else
4535 eap->flags |= EXFLAG_NR;
4536 eap->arg = skipwhite(eap->arg + 1);
4537 }
4538}
4539
4540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 * Function called for command which is Not Implemented. NI!
4542 */
4543 void
4544ex_ni(eap)
4545 exarg_T *eap;
4546{
4547 if (!eap->skip)
4548 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4549}
4550
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004551#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552/*
4553 * Function called for script command which is Not Implemented. NI!
4554 * Skips over ":perl <<EOF" constructs.
4555 */
4556 static void
4557ex_script_ni(eap)
4558 exarg_T *eap;
4559{
4560 if (!eap->skip)
4561 ex_ni(eap);
4562 else
4563 vim_free(script_get(eap, eap->arg));
4564}
4565#endif
4566
4567/*
4568 * Check range in Ex command for validity.
4569 * Return NULL when valid, error message when invalid.
4570 */
4571 static char_u *
4572invalid_range(eap)
4573 exarg_T *eap;
4574{
4575 if ( eap->line1 < 0
4576 || eap->line2 < 0
4577 || eap->line1 > eap->line2
4578 || ((eap->argt & RANGE)
4579 && !(eap->argt & NOTADR)
4580 && eap->line2 > curbuf->b_ml.ml_line_count
4581#ifdef FEAT_DIFF
4582 + (eap->cmdidx == CMD_diffget)
4583#endif
4584 ))
4585 return (char_u *)_(e_invrange);
4586 return NULL;
4587}
4588
4589/*
4590 * Correct the range for zero line number, if required.
4591 */
4592 static void
4593correct_range(eap)
4594 exarg_T *eap;
4595{
4596 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4597 {
4598 if (eap->line1 == 0)
4599 eap->line1 = 1;
4600 if (eap->line2 == 0)
4601 eap->line2 = 1;
4602 }
4603}
4604
Bram Moolenaar748bf032005-02-02 23:04:36 +00004605#ifdef FEAT_QUICKFIX
4606static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4607
4608/*
4609 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4610 * pattern. Otherwise return eap->arg.
4611 */
4612 static char_u *
4613skip_grep_pat(eap)
4614 exarg_T *eap;
4615{
4616 char_u *p = eap->arg;
4617
Bram Moolenaara37420f2006-02-04 22:37:47 +00004618 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4619 || eap->cmdidx == CMD_vimgrepadd
4620 || eap->cmdidx == CMD_lvimgrepadd
4621 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004622 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004623 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004624 if (p == NULL)
4625 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004626 }
4627 return p;
4628}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004629
4630/*
4631 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4632 * in the command line, so that things like % get expanded.
4633 */
4634 static char_u *
4635replace_makeprg(eap, p, cmdlinep)
4636 exarg_T *eap;
4637 char_u *p;
4638 char_u **cmdlinep;
4639{
4640 char_u *new_cmdline;
4641 char_u *program;
4642 char_u *pos;
4643 char_u *ptr;
4644 int len;
4645 int i;
4646
4647 /*
4648 * Don't do it when ":vimgrep" is used for ":grep".
4649 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004650 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4651 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4652 || eap->cmdidx == CMD_grepadd
4653 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004654 && !grep_internal(eap->cmdidx))
4655 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004656 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4657 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004658 {
4659 if (*curbuf->b_p_gp == NUL)
4660 program = p_gp;
4661 else
4662 program = curbuf->b_p_gp;
4663 }
4664 else
4665 {
4666 if (*curbuf->b_p_mp == NUL)
4667 program = p_mp;
4668 else
4669 program = curbuf->b_p_mp;
4670 }
4671
4672 p = skipwhite(p);
4673
4674 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4675 {
4676 /* replace $* by given arguments */
4677 i = 1;
4678 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4679 ++i;
4680 len = (int)STRLEN(p);
4681 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4682 if (new_cmdline == NULL)
4683 return NULL; /* out of memory */
4684 ptr = new_cmdline;
4685 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4686 {
4687 i = (int)(pos - program);
4688 STRNCPY(ptr, program, i);
4689 STRCPY(ptr += i, p);
4690 ptr += len;
4691 program = pos + 2;
4692 }
4693 STRCPY(ptr, program);
4694 }
4695 else
4696 {
4697 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4698 if (new_cmdline == NULL)
4699 return NULL; /* out of memory */
4700 STRCPY(new_cmdline, program);
4701 STRCAT(new_cmdline, " ");
4702 STRCAT(new_cmdline, p);
4703 }
4704 msg_make(p);
4705
4706 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4707 vim_free(*cmdlinep);
4708 *cmdlinep = new_cmdline;
4709 p = new_cmdline;
4710 }
4711 return p;
4712}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004713#endif
4714
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715/*
4716 * Expand file name in Ex command argument.
4717 * Return FAIL for failure, OK otherwise.
4718 */
4719 int
4720expand_filename(eap, cmdlinep, errormsgp)
4721 exarg_T *eap;
4722 char_u **cmdlinep;
4723 char_u **errormsgp;
4724{
4725 int has_wildcards; /* need to expand wildcards */
4726 char_u *repl;
4727 int srclen;
4728 char_u *p;
4729 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004730 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
Bram Moolenaar748bf032005-02-02 23:04:36 +00004732#ifdef FEAT_QUICKFIX
4733 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4734 p = skip_grep_pat(eap);
4735#else
4736 p = eap->arg;
4737#endif
4738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 /*
4740 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4741 * the file name contains a wildcard it should not cause expanding.
4742 * (it will be expanded anyway if there is a wildcard before replacing).
4743 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004744 has_wildcards = mch_has_wildcard(p);
4745 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004747#ifdef FEAT_EVAL
4748 /* Skip over `=expr`, wildcards in it are not expanded. */
4749 if (p[0] == '`' && p[1] == '=')
4750 {
4751 p += 2;
4752 (void)skip_expr(&p);
4753 if (*p == '`')
4754 ++p;
4755 continue;
4756 }
4757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 /*
4759 * Quick check if this cannot be the start of a special string.
4760 * Also removes backslash before '%', '#' and '<'.
4761 */
4762 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4763 {
4764 ++p;
4765 continue;
4766 }
4767
4768 /*
4769 * Try to find a match at this position.
4770 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004771 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4772 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (*errormsgp != NULL) /* error detected */
4774 return FAIL;
4775 if (repl == NULL) /* no match found */
4776 {
4777 p += srclen;
4778 continue;
4779 }
4780
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004781 /* Wildcards won't be expanded below, the replacement is taken
4782 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4783 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4784 {
4785 char_u *l = repl;
4786
4787 repl = expand_env_save(repl);
4788 vim_free(l);
4789 }
4790
Bram Moolenaar63b92542007-03-27 14:57:09 +00004791 /* Need to escape white space et al. with a backslash.
4792 * Don't do this for:
4793 * - replacement that already has been escaped: "##"
4794 * - shell commands (may have to use quotes instead).
4795 * - non-unix systems when there is a single argument (spaces don't
4796 * separate arguments then).
4797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004799 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 && eap->cmdidx != CMD_bang
4801 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004802 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004804 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004806 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807#ifndef UNIX
4808 && !(eap->argt & NOSPC)
4809#endif
4810 )
4811 {
4812 char_u *l;
4813#ifdef BACKSLASH_IN_FILENAME
4814 /* Don't escape a backslash here, because rem_backslash() doesn't
4815 * remove it later. */
4816 static char_u *nobslash = (char_u *)" \t\"|";
4817# define ESCAPE_CHARS nobslash
4818#else
4819# define ESCAPE_CHARS escape_chars
4820#endif
4821
4822 for (l = repl; *l; ++l)
4823 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4824 {
4825 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4826 if (l != NULL)
4827 {
4828 vim_free(repl);
4829 repl = l;
4830 }
4831 break;
4832 }
4833 }
4834
4835 /* For a shell command a '!' must be escaped. */
4836 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004837 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 char_u *l;
4840
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004841 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (l != NULL)
4843 {
4844 vim_free(repl);
4845 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847 }
4848
4849 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4850 vim_free(repl);
4851 if (p == NULL)
4852 return FAIL;
4853 }
4854
4855 /*
4856 * One file argument: Expand wildcards.
4857 * Don't do this with ":r !command" or ":w !command".
4858 */
4859 if ((eap->argt & NOSPC) && !eap->usefilter)
4860 {
4861 /*
4862 * May do this twice:
4863 * 1. Replace environment variables.
4864 * 2. Replace any other wildcards, remove backslashes.
4865 */
4866 for (n = 1; n <= 2; ++n)
4867 {
4868 if (n == 2)
4869 {
4870#ifdef UNIX
4871 /*
4872 * Only for Unix we check for more than one file name.
4873 * For other systems spaces are considered to be part
4874 * of the file name.
4875 * Only check here if there is no wildcard, otherwise
4876 * ExpandOne() will check for errors. This allows
4877 * ":e `ls ve*.c`" on Unix.
4878 */
4879 if (!has_wildcards)
4880 for (p = eap->arg; *p; ++p)
4881 {
4882 /* skip escaped characters */
4883 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4884 ++p;
4885 else if (vim_iswhite(*p))
4886 {
4887 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4888 return FAIL;
4889 }
4890 }
4891#endif
4892
4893 /*
4894 * Halve the number of backslashes (this is Vi compatible).
4895 * For Unix and OS/2, when wildcards are expanded, this is
4896 * done by ExpandOne() below.
4897 */
4898#if defined(UNIX) || defined(OS2)
4899 if (!has_wildcards)
4900#endif
4901 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903
4904 if (has_wildcards)
4905 {
4906 if (n == 1)
4907 {
4908 /*
4909 * First loop: May expand environment variables. This
4910 * can be done much faster with expand_env() than with
4911 * something else (e.g., calling a shell).
4912 * After expanding environment variables, check again
4913 * if there are still wildcards present.
4914 */
4915 if (vim_strchr(eap->arg, '$') != NULL
4916 || vim_strchr(eap->arg, '~') != NULL)
4917 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004918 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004919 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 has_wildcards = mch_has_wildcard(NameBuff);
4921 p = NameBuff;
4922 }
4923 else
4924 p = NULL;
4925 }
4926 else /* n == 2 */
4927 {
4928 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004929 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
4931 ExpandInit(&xpc);
4932 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004933 if (p_wic)
4934 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004936 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (p == NULL)
4938 return FAIL;
4939 }
4940 if (p != NULL)
4941 {
4942 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4943 p, cmdlinep);
4944 if (n == 2) /* p came from ExpandOne() */
4945 vim_free(p);
4946 }
4947 }
4948 }
4949 }
4950 return OK;
4951}
4952
4953/*
4954 * Replace part of the command line, keeping eap->cmd, eap->arg and
4955 * eap->nextcmd correct.
4956 * "src" points to the part that is to be replaced, of length "srclen".
4957 * "repl" is the replacement string.
4958 * Returns a pointer to the character after the replaced string.
4959 * Returns NULL for failure.
4960 */
4961 static char_u *
4962repl_cmdline(eap, src, srclen, repl, cmdlinep)
4963 exarg_T *eap;
4964 char_u *src;
4965 int srclen;
4966 char_u *repl;
4967 char_u **cmdlinep;
4968{
4969 int len;
4970 int i;
4971 char_u *new_cmdline;
4972
4973 /*
4974 * The new command line is build in new_cmdline[].
4975 * First allocate it.
4976 * Careful: a "+cmd" argument may have been NUL terminated.
4977 */
4978 len = (int)STRLEN(repl);
4979 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004980 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4982 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4983 return NULL; /* out of memory! */
4984
4985 /*
4986 * Copy the stuff before the expanded part.
4987 * Copy the expanded stuff.
4988 * Copy what came after the expanded part.
4989 * Copy the next commands, if there are any.
4990 */
4991 i = (int)(src - *cmdlinep); /* length of part before match */
4992 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004993
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 mch_memmove(new_cmdline + i, repl, (size_t)len);
4995 i += len; /* remember the end of the string */
4996 STRCPY(new_cmdline + i, src + srclen);
4997 src = new_cmdline + i; /* remember where to continue */
4998
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004999 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001 i = (int)STRLEN(new_cmdline) + 1;
5002 STRCPY(new_cmdline + i, eap->nextcmd);
5003 eap->nextcmd = new_cmdline + i;
5004 }
5005 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5006 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5007 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5008 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5009 vim_free(*cmdlinep);
5010 *cmdlinep = new_cmdline;
5011
5012 return src;
5013}
5014
5015/*
5016 * Check for '|' to separate commands and '"' to start comments.
5017 */
5018 void
5019separate_nextcmd(eap)
5020 exarg_T *eap;
5021{
5022 char_u *p;
5023
Bram Moolenaar86b68352004-12-27 21:59:20 +00005024#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005025 p = skip_grep_pat(eap);
5026#else
5027 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005028#endif
5029
5030 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 if (*p == Ctrl_V)
5033 {
5034 if (eap->argt & (USECTRLV | XFILE))
5035 ++p; /* skip CTRL-V and next char */
5036 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005037 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005038 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 if (*p == NUL) /* stop at NUL after CTRL-V */
5040 break;
5041 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005042
5043#ifdef FEAT_EVAL
5044 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005045 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005046 {
5047 p += 2;
5048 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005049 }
5050#endif
5051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 /* Check for '"': start of comment or '|': next command */
5053 /* :@" and :*" do not start a comment!
5054 * :redir @" doesn't either. */
5055 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5056 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5057 || p != eap->arg)
5058 && (eap->cmdidx != CMD_redir
5059 || p != eap->arg + 1 || p[-1] != '@'))
5060 || *p == '|' || *p == '\n')
5061 {
5062 /*
5063 * We remove the '\' before the '|', unless USECTRLV is used
5064 * AND 'b' is present in 'cpoptions'.
5065 */
5066 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5067 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5068 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005069 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 --p;
5071 }
5072 else
5073 {
5074 eap->nextcmd = check_nextcmd(p);
5075 *p = NUL;
5076 break;
5077 }
5078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005080
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5082 del_trailing_spaces(eap->arg);
5083}
5084
5085/*
5086 * get + command from ex argument
5087 */
5088 static char_u *
5089getargcmd(argp)
5090 char_u **argp;
5091{
5092 char_u *arg = *argp;
5093 char_u *command = NULL;
5094
5095 if (*arg == '+') /* +[command] */
5096 {
5097 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005098 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 command = dollar_command;
5100 else
5101 {
5102 command = arg;
5103 arg = skip_cmd_arg(command, TRUE);
5104 if (*arg != NUL)
5105 *arg++ = NUL; /* terminate command with NUL */
5106 }
5107
5108 arg = skipwhite(arg); /* skip over spaces */
5109 *argp = arg;
5110 }
5111 return command;
5112}
5113
5114/*
5115 * Find end of "+command" argument. Skip over "\ " and "\\".
5116 */
5117 static char_u *
5118skip_cmd_arg(p, rembs)
5119 char_u *p;
5120 int rembs; /* TRUE to halve the number of backslashes */
5121{
5122 while (*p && !vim_isspace(*p))
5123 {
5124 if (*p == '\\' && p[1] != NUL)
5125 {
5126 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005127 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else
5129 ++p;
5130 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005131 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133 return p;
5134}
5135
5136/*
5137 * Get "++opt=arg" argument.
5138 * Return FAIL or OK.
5139 */
5140 static int
5141getargopt(eap)
5142 exarg_T *eap;
5143{
5144 char_u *arg = eap->arg + 2;
5145 int *pp = NULL;
5146#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005147 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 char_u *p;
5149#endif
5150
5151 /* ":edit ++[no]bin[ary] file" */
5152 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5153 {
5154 if (*arg == 'n')
5155 {
5156 arg += 2;
5157 eap->force_bin = FORCE_NOBIN;
5158 }
5159 else
5160 eap->force_bin = FORCE_BIN;
5161 if (!checkforcmd(&arg, "binary", 3))
5162 return FAIL;
5163 eap->arg = skipwhite(arg);
5164 return OK;
5165 }
5166
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005167 /* ":read ++edit file" */
5168 if (STRNCMP(arg, "edit", 4) == 0)
5169 {
5170 eap->read_edit = TRUE;
5171 eap->arg = skipwhite(arg + 4);
5172 return OK;
5173 }
5174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (STRNCMP(arg, "ff", 2) == 0)
5176 {
5177 arg += 2;
5178 pp = &eap->force_ff;
5179 }
5180 else if (STRNCMP(arg, "fileformat", 10) == 0)
5181 {
5182 arg += 10;
5183 pp = &eap->force_ff;
5184 }
5185#ifdef FEAT_MBYTE
5186 else if (STRNCMP(arg, "enc", 3) == 0)
5187 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005188 if (STRNCMP(arg, "encoding", 8) == 0)
5189 arg += 8;
5190 else
5191 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 pp = &eap->force_enc;
5193 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005194 else if (STRNCMP(arg, "bad", 3) == 0)
5195 {
5196 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005197 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199#endif
5200
5201 if (pp == NULL || *arg != '=')
5202 return FAIL;
5203
5204 ++arg;
5205 *pp = (int)(arg - eap->cmd);
5206 arg = skip_cmd_arg(arg, FALSE);
5207 eap->arg = skipwhite(arg);
5208 *arg = NUL;
5209
5210#ifdef FEAT_MBYTE
5211 if (pp == &eap->force_ff)
5212 {
5213#endif
5214 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5215 return FAIL;
5216#ifdef FEAT_MBYTE
5217 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005218 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
5220 /* Make 'fileencoding' lower case. */
5221 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5222 *p = TOLOWER_ASC(*p);
5223 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005224 else
5225 {
5226 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5227 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005228 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005229 if (STRICMP(p, "keep") == 0)
5230 eap->bad_char = BAD_KEEP;
5231 else if (STRICMP(p, "drop") == 0)
5232 eap->bad_char = BAD_DROP;
5233 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5234 eap->bad_char = *p;
5235 else
5236 return FAIL;
5237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238#endif
5239
5240 return OK;
5241}
5242
5243/*
5244 * ":abbreviate" and friends.
5245 */
5246 static void
5247ex_abbreviate(eap)
5248 exarg_T *eap;
5249{
5250 do_exmap(eap, TRUE); /* almost the same as mapping */
5251}
5252
5253/*
5254 * ":map" and friends.
5255 */
5256 static void
5257ex_map(eap)
5258 exarg_T *eap;
5259{
5260 /*
5261 * If we are sourcing .exrc or .vimrc in current directory we
5262 * print the mappings for security reasons.
5263 */
5264 if (secure)
5265 {
5266 secure = 2;
5267 msg_outtrans(eap->cmd);
5268 msg_putchar('\n');
5269 }
5270 do_exmap(eap, FALSE);
5271}
5272
5273/*
5274 * ":unmap" and friends.
5275 */
5276 static void
5277ex_unmap(eap)
5278 exarg_T *eap;
5279{
5280 do_exmap(eap, FALSE);
5281}
5282
5283/*
5284 * ":mapclear" and friends.
5285 */
5286 static void
5287ex_mapclear(eap)
5288 exarg_T *eap;
5289{
5290 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5291}
5292
5293/*
5294 * ":abclear" and friends.
5295 */
5296 static void
5297ex_abclear(eap)
5298 exarg_T *eap;
5299{
5300 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5301}
5302
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005303#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 static void
5305ex_autocmd(eap)
5306 exarg_T *eap;
5307{
5308 /*
5309 * Disallow auto commands from .exrc and .vimrc in current
5310 * directory for security reasons.
5311 */
5312 if (secure)
5313 {
5314 secure = 2;
5315 eap->errmsg = e_curdir;
5316 }
5317 else if (eap->cmdidx == CMD_autocmd)
5318 do_autocmd(eap->arg, eap->forceit);
5319 else
5320 do_augroup(eap->arg, eap->forceit);
5321}
5322
5323/*
5324 * ":doautocmd": Apply the automatic commands to the current buffer.
5325 */
5326 static void
5327ex_doautocmd(eap)
5328 exarg_T *eap;
5329{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005330 char_u *arg = eap->arg;
5331 int call_do_modelines = check_nomodeline(&arg);
5332
5333 (void)do_doautocmd(arg, TRUE);
5334 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5335 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336}
5337#endif
5338
5339#ifdef FEAT_LISTCMDS
5340/*
5341 * :[N]bunload[!] [N] [bufname] unload buffer
5342 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5343 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5344 */
5345 static void
5346ex_bunload(eap)
5347 exarg_T *eap;
5348{
5349 eap->errmsg = do_bufdel(
5350 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5351 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5352 : DOBUF_UNLOAD, eap->arg,
5353 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5354}
5355
5356/*
5357 * :[N]buffer [N] to buffer N
5358 * :[N]sbuffer [N] to buffer N
5359 */
5360 static void
5361ex_buffer(eap)
5362 exarg_T *eap;
5363{
5364 if (*eap->arg)
5365 eap->errmsg = e_trailing;
5366 else
5367 {
5368 if (eap->addr_count == 0) /* default is current buffer */
5369 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5370 else
5371 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005372 if (eap->do_ecmd_cmd != NULL)
5373 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 }
5375}
5376
5377/*
5378 * :[N]bmodified [N] to next mod. buffer
5379 * :[N]sbmodified [N] to next mod. buffer
5380 */
5381 static void
5382ex_bmodified(eap)
5383 exarg_T *eap;
5384{
5385 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005386 if (eap->do_ecmd_cmd != NULL)
5387 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388}
5389
5390/*
5391 * :[N]bnext [N] to next buffer
5392 * :[N]sbnext [N] split and to next buffer
5393 */
5394 static void
5395ex_bnext(eap)
5396 exarg_T *eap;
5397{
5398 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005399 if (eap->do_ecmd_cmd != NULL)
5400 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401}
5402
5403/*
5404 * :[N]bNext [N] to previous buffer
5405 * :[N]bprevious [N] to previous buffer
5406 * :[N]sbNext [N] split and to previous buffer
5407 * :[N]sbprevious [N] split and to previous buffer
5408 */
5409 static void
5410ex_bprevious(eap)
5411 exarg_T *eap;
5412{
5413 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005414 if (eap->do_ecmd_cmd != NULL)
5415 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416}
5417
5418/*
5419 * :brewind to first buffer
5420 * :bfirst to first buffer
5421 * :sbrewind split and to first buffer
5422 * :sbfirst split and to first buffer
5423 */
5424 static void
5425ex_brewind(eap)
5426 exarg_T *eap;
5427{
5428 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005429 if (eap->do_ecmd_cmd != NULL)
5430 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431}
5432
5433/*
5434 * :blast to last buffer
5435 * :sblast split and to last buffer
5436 */
5437 static void
5438ex_blast(eap)
5439 exarg_T *eap;
5440{
5441 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005442 if (eap->do_ecmd_cmd != NULL)
5443 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444}
5445#endif
5446
5447 int
5448ends_excmd(c)
5449 int c;
5450{
5451 return (c == NUL || c == '|' || c == '"' || c == '\n');
5452}
5453
5454#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5455 || defined(PROTO)
5456/*
5457 * Return the next command, after the first '|' or '\n'.
5458 * Return NULL if not found.
5459 */
5460 char_u *
5461find_nextcmd(p)
5462 char_u *p;
5463{
5464 while (*p != '|' && *p != '\n')
5465 {
5466 if (*p == NUL)
5467 return NULL;
5468 ++p;
5469 }
5470 return (p + 1);
5471}
5472#endif
5473
5474/*
5475 * Check if *p is a separator between Ex commands.
5476 * Return NULL if it isn't, (p + 1) if it is.
5477 */
5478 char_u *
5479check_nextcmd(p)
5480 char_u *p;
5481{
5482 p = skipwhite(p);
5483 if (*p == '|' || *p == '\n')
5484 return (p + 1);
5485 else
5486 return NULL;
5487}
5488
5489/*
5490 * - if there are more files to edit
5491 * - and this is the last window
5492 * - and forceit not used
5493 * - and not repeated twice on a row
5494 * return FAIL and give error message if 'message' TRUE
5495 * return OK otherwise
5496 */
5497 static int
5498check_more(message, forceit)
5499 int message; /* when FALSE check only, no messages */
5500 int forceit;
5501{
5502 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5503
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005504 if (!forceit && only_one_window()
5505 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
5507 if (message)
5508 {
5509#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5510 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5511 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005512 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005515 vim_strncpy(buff,
5516 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005517 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005519 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 _("%d more files to edit. Quit anyway?"), n);
5521 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5522 return OK;
5523 return FAIL;
5524 }
5525#endif
5526 if (n == 1)
5527 EMSG(_("E173: 1 more file to edit"));
5528 else
5529 EMSGN(_("E173: %ld more files to edit"), n);
5530 quitmore = 2; /* next try to quit is allowed */
5531 }
5532 return FAIL;
5533 }
5534 return OK;
5535}
5536
5537#ifdef FEAT_CMDL_COMPL
5538/*
5539 * Function given to ExpandGeneric() to obtain the list of command names.
5540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 char_u *
5542get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005543 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 int idx;
5545{
5546 if (idx >= (int)CMD_SIZE)
5547# ifdef FEAT_USR_CMDS
5548 return get_user_command_name(idx);
5549# else
5550 return NULL;
5551# endif
5552 return cmdnames[idx].cmd_name;
5553}
5554#endif
5555
5556#if defined(FEAT_USR_CMDS) || defined(PROTO)
5557static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force));
5558static void uc_list __ARGS((char_u *name, size_t name_len));
5559static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5560static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5561static size_t uc_check_code __ARGS((char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len));
5562
5563 static int
5564uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5565 char_u *name;
5566 size_t name_len;
5567 char_u *rep;
5568 long argt;
5569 long def;
5570 int flags;
5571 int compl;
5572 char_u *compl_arg;
5573 int force;
5574{
5575 ucmd_T *cmd = NULL;
5576 char_u *p;
5577 int i;
5578 int cmp = 1;
5579 char_u *rep_buf = NULL;
5580 garray_T *gap;
5581
Bram Moolenaar9c102382006-05-03 21:26:49 +00005582 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 if (rep_buf == NULL)
5584 {
5585 /* Can't replace termcodes - try using the string as is */
5586 rep_buf = vim_strsave(rep);
5587
5588 /* Give up if out of memory */
5589 if (rep_buf == NULL)
5590 return FAIL;
5591 }
5592
5593 /* get address of growarray: global or in curbuf */
5594 if (flags & UC_BUFFER)
5595 {
5596 gap = &curbuf->b_ucmds;
5597 if (gap->ga_itemsize == 0)
5598 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5599 }
5600 else
5601 gap = &ucmds;
5602
5603 /* Search for the command in the already defined commands. */
5604 for (i = 0; i < gap->ga_len; ++i)
5605 {
5606 size_t len;
5607
5608 cmd = USER_CMD_GA(gap, i);
5609 len = STRLEN(cmd->uc_name);
5610 cmp = STRNCMP(name, cmd->uc_name, name_len);
5611 if (cmp == 0)
5612 {
5613 if (name_len < len)
5614 cmp = -1;
5615 else if (name_len > len)
5616 cmp = 1;
5617 }
5618
5619 if (cmp == 0)
5620 {
5621 if (!force)
5622 {
5623 EMSG(_("E174: Command already exists: add ! to replace it"));
5624 goto fail;
5625 }
5626
5627 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005628 cmd->uc_rep = NULL;
5629#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5630 vim_free(cmd->uc_compl_arg);
5631 cmd->uc_compl_arg = NULL;
5632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 break;
5634 }
5635
5636 /* Stop as soon as we pass the name to add */
5637 if (cmp < 0)
5638 break;
5639 }
5640
5641 /* Extend the array unless we're replacing an existing command */
5642 if (cmp != 0)
5643 {
5644 if (ga_grow(gap, 1) != OK)
5645 goto fail;
5646 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5647 goto fail;
5648
5649 cmd = USER_CMD_GA(gap, i);
5650 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5651
5652 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653
5654 cmd->uc_name = p;
5655 }
5656
5657 cmd->uc_rep = rep_buf;
5658 cmd->uc_argt = argt;
5659 cmd->uc_def = def;
5660 cmd->uc_compl = compl;
5661#ifdef FEAT_EVAL
5662 cmd->uc_scriptID = current_SID;
5663# ifdef FEAT_CMDL_COMPL
5664 cmd->uc_compl_arg = compl_arg;
5665# endif
5666#endif
5667
5668 return OK;
5669
5670fail:
5671 vim_free(rep_buf);
5672#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5673 vim_free(compl_arg);
5674#endif
5675 return FAIL;
5676}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005679#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680/*
5681 * List of names for completion for ":command" with the EXPAND_ flag.
5682 * Must be alphabetical for completion.
5683 */
5684static struct
5685{
5686 int expand;
5687 char *name;
5688} command_complete[] =
5689{
5690 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005691 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005693 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005695 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005696#if defined(FEAT_CSCOPE)
5697 {EXPAND_CSCOPE, "cscope"},
5698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5700 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005701 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702#endif
5703 {EXPAND_DIRECTORIES, "dir"},
5704 {EXPAND_ENV_VARS, "environment"},
5705 {EXPAND_EVENTS, "event"},
5706 {EXPAND_EXPRESSION, "expression"},
5707 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005708 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005709 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {EXPAND_FUNCTIONS, "function"},
5711 {EXPAND_HELP, "help"},
5712 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005713#if defined(FEAT_CMDHIST)
5714 {EXPAND_HISTORY, "history"},
5715#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005716#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005717 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005718 {EXPAND_LOCALES, "locale"},
5719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 {EXPAND_MAPPINGS, "mapping"},
5721 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005722 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005723#if defined(FEAT_PROFILE)
5724 {EXPAND_SYNTIME, "syntime"},
5725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005727 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005728#if defined(FEAT_SIGNS)
5729 {EXPAND_SIGN, "sign"},
5730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 {EXPAND_TAGS, "tag"},
5732 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005733 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {EXPAND_USER_VARS, "var"},
5735 {0, NULL}
5736};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005739#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 static void
5741uc_list(name, name_len)
5742 char_u *name;
5743 size_t name_len;
5744{
5745 int i, j;
5746 int found = FALSE;
5747 ucmd_T *cmd;
5748 int len;
5749 long a;
5750 garray_T *gap;
5751
5752 gap = &curbuf->b_ucmds;
5753 for (;;)
5754 {
5755 for (i = 0; i < gap->ga_len; ++i)
5756 {
5757 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005758 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759
5760 /* Skip commands which don't match the requested prefix */
5761 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5762 continue;
5763
5764 /* Put out the title first time */
5765 if (!found)
5766 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5767 found = TRUE;
5768 msg_putchar('\n');
5769 if (got_int)
5770 break;
5771
5772 /* Special cases */
5773 msg_putchar(a & BANG ? '!' : ' ');
5774 msg_putchar(a & REGSTR ? '"' : ' ');
5775 msg_putchar(gap != &ucmds ? 'b' : ' ');
5776 msg_putchar(' ');
5777
5778 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5779 len = (int)STRLEN(cmd->uc_name) + 4;
5780
5781 do {
5782 msg_putchar(' ');
5783 ++len;
5784 } while (len < 16);
5785
5786 len = 0;
5787
5788 /* Arguments */
5789 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5790 {
5791 case 0: IObuff[len++] = '0'; break;
5792 case (EXTRA): IObuff[len++] = '*'; break;
5793 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5794 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5795 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5796 }
5797
5798 do {
5799 IObuff[len++] = ' ';
5800 } while (len < 5);
5801
5802 /* Range */
5803 if (a & (RANGE|COUNT))
5804 {
5805 if (a & COUNT)
5806 {
5807 /* -count=N */
5808 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5809 len += (int)STRLEN(IObuff + len);
5810 }
5811 else if (a & DFLALL)
5812 IObuff[len++] = '%';
5813 else if (cmd->uc_def >= 0)
5814 {
5815 /* -range=N */
5816 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5817 len += (int)STRLEN(IObuff + len);
5818 }
5819 else
5820 IObuff[len++] = '.';
5821 }
5822
5823 do {
5824 IObuff[len++] = ' ';
5825 } while (len < 11);
5826
5827 /* Completion */
5828 for (j = 0; command_complete[j].expand != 0; ++j)
5829 if (command_complete[j].expand == cmd->uc_compl)
5830 {
5831 STRCPY(IObuff + len, command_complete[j].name);
5832 len += (int)STRLEN(IObuff + len);
5833 break;
5834 }
5835
5836 do {
5837 IObuff[len++] = ' ';
5838 } while (len < 21);
5839
5840 IObuff[len] = '\0';
5841 msg_outtrans(IObuff);
5842
5843 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005844#ifdef FEAT_EVAL
5845 if (p_verbose > 0)
5846 last_set_msg(cmd->uc_scriptID);
5847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 out_flush();
5849 ui_breakcheck();
5850 if (got_int)
5851 break;
5852 }
5853 if (gap == &ucmds || i < gap->ga_len)
5854 break;
5855 gap = &ucmds;
5856 }
5857
5858 if (!found)
5859 MSG(_("No user-defined commands found"));
5860}
5861
5862 static char_u *
5863uc_fun_cmd()
5864{
5865 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5866 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5867 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5868 0xb9, 0x7f, 0};
5869 int i;
5870
5871 for (i = 0; fcmd[i]; ++i)
5872 IObuff[i] = fcmd[i] - 0x40;
5873 IObuff[i] = 0;
5874 return IObuff;
5875}
5876
5877 static int
5878uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5879 char_u *attr;
5880 size_t len;
5881 long *argt;
5882 long *def;
5883 int *flags;
5884 int *compl;
5885 char_u **compl_arg;
5886{
5887 char_u *p;
5888
5889 if (len == 0)
5890 {
5891 EMSG(_("E175: No attribute specified"));
5892 return FAIL;
5893 }
5894
5895 /* First, try the simple attributes (no arguments) */
5896 if (STRNICMP(attr, "bang", len) == 0)
5897 *argt |= BANG;
5898 else if (STRNICMP(attr, "buffer", len) == 0)
5899 *flags |= UC_BUFFER;
5900 else if (STRNICMP(attr, "register", len) == 0)
5901 *argt |= REGSTR;
5902 else if (STRNICMP(attr, "bar", len) == 0)
5903 *argt |= TRLBAR;
5904 else
5905 {
5906 int i;
5907 char_u *val = NULL;
5908 size_t vallen = 0;
5909 size_t attrlen = len;
5910
5911 /* Look for the attribute name - which is the part before any '=' */
5912 for (i = 0; i < (int)len; ++i)
5913 {
5914 if (attr[i] == '=')
5915 {
5916 val = &attr[i + 1];
5917 vallen = len - i - 1;
5918 attrlen = i;
5919 break;
5920 }
5921 }
5922
5923 if (STRNICMP(attr, "nargs", attrlen) == 0)
5924 {
5925 if (vallen == 1)
5926 {
5927 if (*val == '0')
5928 /* Do nothing - this is the default */;
5929 else if (*val == '1')
5930 *argt |= (EXTRA | NOSPC | NEEDARG);
5931 else if (*val == '*')
5932 *argt |= EXTRA;
5933 else if (*val == '?')
5934 *argt |= (EXTRA | NOSPC);
5935 else if (*val == '+')
5936 *argt |= (EXTRA | NEEDARG);
5937 else
5938 goto wrong_nargs;
5939 }
5940 else
5941 {
5942wrong_nargs:
5943 EMSG(_("E176: Invalid number of arguments"));
5944 return FAIL;
5945 }
5946 }
5947 else if (STRNICMP(attr, "range", attrlen) == 0)
5948 {
5949 *argt |= RANGE;
5950 if (vallen == 1 && *val == '%')
5951 *argt |= DFLALL;
5952 else if (val != NULL)
5953 {
5954 p = val;
5955 if (*def >= 0)
5956 {
5957two_count:
5958 EMSG(_("E177: Count cannot be specified twice"));
5959 return FAIL;
5960 }
5961
5962 *def = getdigits(&p);
5963 *argt |= (ZEROR | NOTADR);
5964
5965 if (p != val + vallen || vallen == 0)
5966 {
5967invalid_count:
5968 EMSG(_("E178: Invalid default value for count"));
5969 return FAIL;
5970 }
5971 }
5972 }
5973 else if (STRNICMP(attr, "count", attrlen) == 0)
5974 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005975 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976
5977 if (val != NULL)
5978 {
5979 p = val;
5980 if (*def >= 0)
5981 goto two_count;
5982
5983 *def = getdigits(&p);
5984
5985 if (p != val + vallen)
5986 goto invalid_count;
5987 }
5988
5989 if (*def < 0)
5990 *def = 0;
5991 }
5992 else if (STRNICMP(attr, "complete", attrlen) == 0)
5993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 if (val == NULL)
5995 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005996 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 return FAIL;
5998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006000 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6001 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 }
6004 else
6005 {
6006 char_u ch = attr[len];
6007 attr[len] = '\0';
6008 EMSG2(_("E181: Invalid attribute: %s"), attr);
6009 attr[len] = ch;
6010 return FAIL;
6011 }
6012 }
6013
6014 return OK;
6015}
6016
Bram Moolenaara850a712009-01-28 14:42:59 +00006017/*
6018 * ":command ..."
6019 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 static void
6021ex_command(eap)
6022 exarg_T *eap;
6023{
6024 char_u *name;
6025 char_u *end;
6026 char_u *p;
6027 long argt = 0;
6028 long def = -1;
6029 int flags = 0;
6030 int compl = EXPAND_NOTHING;
6031 char_u *compl_arg = NULL;
6032 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006033 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034
6035 p = eap->arg;
6036
6037 /* Check for attributes */
6038 while (*p == '-')
6039 {
6040 ++p;
6041 end = skiptowhite(p);
6042 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
6043 == FAIL)
6044 return;
6045 p = skipwhite(end);
6046 }
6047
6048 /* Get the name (if any) and skip to the following argument */
6049 name = p;
6050 if (ASCII_ISALPHA(*p))
6051 while (ASCII_ISALNUM(*p))
6052 ++p;
6053 if (!ends_excmd(*p) && !vim_iswhite(*p))
6054 {
6055 EMSG(_("E182: Invalid command name"));
6056 return;
6057 }
6058 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006059 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060
6061 /* If there is nothing after the name, and no attributes were specified,
6062 * we are listing commands
6063 */
6064 p = skipwhite(end);
6065 if (!has_attr && ends_excmd(*p))
6066 {
6067 uc_list(name, end - name);
6068 }
6069 else if (!ASCII_ISUPPER(*name))
6070 {
6071 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6072 return;
6073 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006074 else if ((name_len == 1 && *name == 'X')
6075 || (name_len <= 4
6076 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6077 {
6078 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6079 return;
6080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 else
6082 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
6083 eap->forceit);
6084}
6085
6086/*
6087 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 * Clear all user commands, global and for current buffer.
6089 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006090 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006092 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093{
6094 uc_clear(&ucmds);
6095 uc_clear(&curbuf->b_ucmds);
6096}
6097
6098/*
6099 * Clear all user commands for "gap".
6100 */
6101 void
6102uc_clear(gap)
6103 garray_T *gap;
6104{
6105 int i;
6106 ucmd_T *cmd;
6107
6108 for (i = 0; i < gap->ga_len; ++i)
6109 {
6110 cmd = USER_CMD_GA(gap, i);
6111 vim_free(cmd->uc_name);
6112 vim_free(cmd->uc_rep);
6113# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6114 vim_free(cmd->uc_compl_arg);
6115# endif
6116 }
6117 ga_clear(gap);
6118}
6119
6120 static void
6121ex_delcommand(eap)
6122 exarg_T *eap;
6123{
6124 int i = 0;
6125 ucmd_T *cmd = NULL;
6126 int cmp = -1;
6127 garray_T *gap;
6128
6129 gap = &curbuf->b_ucmds;
6130 for (;;)
6131 {
6132 for (i = 0; i < gap->ga_len; ++i)
6133 {
6134 cmd = USER_CMD_GA(gap, i);
6135 cmp = STRCMP(eap->arg, cmd->uc_name);
6136 if (cmp <= 0)
6137 break;
6138 }
6139 if (gap == &ucmds || cmp == 0)
6140 break;
6141 gap = &ucmds;
6142 }
6143
6144 if (cmp != 0)
6145 {
6146 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6147 return;
6148 }
6149
6150 vim_free(cmd->uc_name);
6151 vim_free(cmd->uc_rep);
6152# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6153 vim_free(cmd->uc_compl_arg);
6154# endif
6155
6156 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
6158 if (i < gap->ga_len)
6159 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6160}
6161
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006162/*
6163 * split and quote args for <f-args>
6164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 static char_u *
6166uc_split_args(arg, lenp)
6167 char_u *arg;
6168 size_t *lenp;
6169{
6170 char_u *buf;
6171 char_u *p;
6172 char_u *q;
6173 int len;
6174
6175 /* Precalculate length */
6176 p = arg;
6177 len = 2; /* Initial and final quotes */
6178
6179 while (*p)
6180 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006181 if (p[0] == '\\' && p[1] == '\\')
6182 {
6183 len += 2;
6184 p += 2;
6185 }
6186 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {
6188 len += 1;
6189 p += 2;
6190 }
6191 else if (*p == '\\' || *p == '"')
6192 {
6193 len += 2;
6194 p += 1;
6195 }
6196 else if (vim_iswhite(*p))
6197 {
6198 p = skipwhite(p);
6199 if (*p == NUL)
6200 break;
6201 len += 3; /* "," */
6202 }
6203 else
6204 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006205#ifdef FEAT_MBYTE
6206 int charlen = (*mb_ptr2len)(p);
6207 len += charlen;
6208 p += charlen;
6209#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 ++len;
6211 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 }
6214 }
6215
6216 buf = alloc(len + 1);
6217 if (buf == NULL)
6218 {
6219 *lenp = 0;
6220 return buf;
6221 }
6222
6223 p = arg;
6224 q = buf;
6225 *q++ = '"';
6226 while (*p)
6227 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006228 if (p[0] == '\\' && p[1] == '\\')
6229 {
6230 *q++ = '\\';
6231 *q++ = '\\';
6232 p += 2;
6233 }
6234 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
6236 *q++ = p[1];
6237 p += 2;
6238 }
6239 else if (*p == '\\' || *p == '"')
6240 {
6241 *q++ = '\\';
6242 *q++ = *p++;
6243 }
6244 else if (vim_iswhite(*p))
6245 {
6246 p = skipwhite(p);
6247 if (*p == NUL)
6248 break;
6249 *q++ = '"';
6250 *q++ = ',';
6251 *q++ = '"';
6252 }
6253 else
6254 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006255 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 }
6257 }
6258 *q++ = '"';
6259 *q = 0;
6260
6261 *lenp = len;
6262 return buf;
6263}
6264
6265/*
6266 * Check for a <> code in a user command.
6267 * "code" points to the '<'. "len" the length of the <> (inclusive).
6268 * "buf" is where the result is to be added.
6269 * "split_buf" points to a buffer used for splitting, caller should free it.
6270 * "split_len" is the length of what "split_buf" contains.
6271 * Returns the length of the replacement, which has been added to "buf".
6272 * Returns -1 if there was no match, and only the "<" has been copied.
6273 */
6274 static size_t
6275uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6276 char_u *code;
6277 size_t len;
6278 char_u *buf;
6279 ucmd_T *cmd; /* the user command we're expanding */
6280 exarg_T *eap; /* ex arguments */
6281 char_u **split_buf;
6282 size_t *split_len;
6283{
6284 size_t result = 0;
6285 char_u *p = code + 1;
6286 size_t l = len - 2;
6287 int quote = 0;
6288 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6289 ct_LT, ct_NONE } type = ct_NONE;
6290
6291 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6292 {
6293 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6294 p += 2;
6295 l -= 2;
6296 }
6297
Bram Moolenaar371d5402006-03-20 21:47:49 +00006298 ++l;
6299 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006301 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006303 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006305 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006307 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006309 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006311 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006313 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 type = ct_REGISTER;
6315
6316 switch (type)
6317 {
6318 case ct_ARGS:
6319 /* Simple case first */
6320 if (*eap->arg == NUL)
6321 {
6322 if (quote == 1)
6323 {
6324 result = 2;
6325 if (buf != NULL)
6326 STRCPY(buf, "''");
6327 }
6328 else
6329 result = 0;
6330 break;
6331 }
6332
6333 /* When specified there is a single argument don't split it.
6334 * Works for ":Cmd %" when % is "a b c". */
6335 if ((eap->argt & NOSPC) && quote == 2)
6336 quote = 1;
6337
6338 switch (quote)
6339 {
6340 case 0: /* No quoting, no splitting */
6341 result = STRLEN(eap->arg);
6342 if (buf != NULL)
6343 STRCPY(buf, eap->arg);
6344 break;
6345 case 1: /* Quote, but don't split */
6346 result = STRLEN(eap->arg) + 2;
6347 for (p = eap->arg; *p; ++p)
6348 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006349#ifdef FEAT_MBYTE
6350 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6351 /* DBCS can contain \ in a trail byte, skip the
6352 * double-byte character. */
6353 ++p;
6354 else
6355#endif
6356 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 ++result;
6358 }
6359
6360 if (buf != NULL)
6361 {
6362 *buf++ = '"';
6363 for (p = eap->arg; *p; ++p)
6364 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006365#ifdef FEAT_MBYTE
6366 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6367 /* DBCS can contain \ in a trail byte, copy the
6368 * double-byte character to avoid escaping. */
6369 *buf++ = *p++;
6370 else
6371#endif
6372 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 *buf++ = '\\';
6374 *buf++ = *p;
6375 }
6376 *buf = '"';
6377 }
6378
6379 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006380 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 /* This is hard, so only do it once, and cache the result */
6382 if (*split_buf == NULL)
6383 *split_buf = uc_split_args(eap->arg, split_len);
6384
6385 result = *split_len;
6386 if (buf != NULL && result != 0)
6387 STRCPY(buf, *split_buf);
6388
6389 break;
6390 }
6391 break;
6392
6393 case ct_BANG:
6394 result = eap->forceit ? 1 : 0;
6395 if (quote)
6396 result += 2;
6397 if (buf != NULL)
6398 {
6399 if (quote)
6400 *buf++ = '"';
6401 if (eap->forceit)
6402 *buf++ = '!';
6403 if (quote)
6404 *buf = '"';
6405 }
6406 break;
6407
6408 case ct_LINE1:
6409 case ct_LINE2:
6410 case ct_COUNT:
6411 {
6412 char num_buf[20];
6413 long num = (type == ct_LINE1) ? eap->line1 :
6414 (type == ct_LINE2) ? eap->line2 :
6415 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6416 size_t num_len;
6417
6418 sprintf(num_buf, "%ld", num);
6419 num_len = STRLEN(num_buf);
6420 result = num_len;
6421
6422 if (quote)
6423 result += 2;
6424
6425 if (buf != NULL)
6426 {
6427 if (quote)
6428 *buf++ = '"';
6429 STRCPY(buf, num_buf);
6430 buf += num_len;
6431 if (quote)
6432 *buf = '"';
6433 }
6434
6435 break;
6436 }
6437
6438 case ct_REGISTER:
6439 result = eap->regname ? 1 : 0;
6440 if (quote)
6441 result += 2;
6442 if (buf != NULL)
6443 {
6444 if (quote)
6445 *buf++ = '\'';
6446 if (eap->regname)
6447 *buf++ = eap->regname;
6448 if (quote)
6449 *buf = '\'';
6450 }
6451 break;
6452
6453 case ct_LT:
6454 result = 1;
6455 if (buf != NULL)
6456 *buf = '<';
6457 break;
6458
6459 default:
6460 /* Not recognized: just copy the '<' and return -1. */
6461 result = (size_t)-1;
6462 if (buf != NULL)
6463 *buf = '<';
6464 break;
6465 }
6466
6467 return result;
6468}
6469
6470 static void
6471do_ucmd(eap)
6472 exarg_T *eap;
6473{
6474 char_u *buf;
6475 char_u *p;
6476 char_u *q;
6477
6478 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006479 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006480 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 size_t len, totlen;
6482
6483 size_t split_len = 0;
6484 char_u *split_buf = NULL;
6485 ucmd_T *cmd;
6486#ifdef FEAT_EVAL
6487 scid_T save_current_SID = current_SID;
6488#endif
6489
6490 if (eap->cmdidx == CMD_USER)
6491 cmd = USER_CMD(eap->useridx);
6492 else
6493 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6494
6495 /*
6496 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006497 * First round: "buf" is NULL, compute length, allocate "buf".
6498 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 */
6500 buf = NULL;
6501 for (;;)
6502 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006503 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006504 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006506
6507 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006509 start = vim_strchr(p, '<');
6510 if (start != NULL)
6511 end = vim_strchr(start + 1, '>');
6512 if (buf != NULL)
6513 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006514 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6515 ;
6516 if (*ksp == K_SPECIAL
6517 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006518 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6519# ifdef FEAT_GUI
6520 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6521# endif
6522 ))
6523 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006524 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006525 * KS_SPECIAL KE_FILLER, like for mappings, but
6526 * do_cmdline() doesn't handle that, so convert it back.
6527 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6528 len = ksp - p;
6529 if (len > 0)
6530 {
6531 mch_memmove(q, p, len);
6532 q += len;
6533 }
6534 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6535 p = ksp + 3;
6536 continue;
6537 }
6538 }
6539
6540 /* break if there no <item> is found */
6541 if (start == NULL || end == NULL)
6542 break;
6543
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 /* Include the '>' */
6545 ++end;
6546
6547 /* Take everything up to the '<' */
6548 len = start - p;
6549 if (buf == NULL)
6550 totlen += len;
6551 else
6552 {
6553 mch_memmove(q, p, len);
6554 q += len;
6555 }
6556
6557 len = uc_check_code(start, end - start, q, cmd, eap,
6558 &split_buf, &split_len);
6559 if (len == (size_t)-1)
6560 {
6561 /* no match, continue after '<' */
6562 p = start + 1;
6563 len = 1;
6564 }
6565 else
6566 p = end;
6567 if (buf == NULL)
6568 totlen += len;
6569 else
6570 q += len;
6571 }
6572 if (buf != NULL) /* second time here, finished */
6573 {
6574 STRCPY(q, p);
6575 break;
6576 }
6577
6578 totlen += STRLEN(p); /* Add on the trailing characters */
6579 buf = alloc((unsigned)(totlen + 1));
6580 if (buf == NULL)
6581 {
6582 vim_free(split_buf);
6583 return;
6584 }
6585 }
6586
6587#ifdef FEAT_EVAL
6588 current_SID = cmd->uc_scriptID;
6589#endif
6590 (void)do_cmdline(buf, eap->getline, eap->cookie,
6591 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6592#ifdef FEAT_EVAL
6593 current_SID = save_current_SID;
6594#endif
6595 vim_free(buf);
6596 vim_free(split_buf);
6597}
6598
6599# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6600 static char_u *
6601get_user_command_name(idx)
6602 int idx;
6603{
6604 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6605}
6606
6607/*
6608 * Function given to ExpandGeneric() to obtain the list of user command names.
6609 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 char_u *
6611get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006612 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 int idx;
6614{
6615 if (idx < curbuf->b_ucmds.ga_len)
6616 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6617 idx -= curbuf->b_ucmds.ga_len;
6618 if (idx < ucmds.ga_len)
6619 return USER_CMD(idx)->uc_name;
6620 return NULL;
6621}
6622
6623/*
6624 * Function given to ExpandGeneric() to obtain the list of user command
6625 * attributes.
6626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 char_u *
6628get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006629 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 int idx;
6631{
6632 static char *user_cmd_flags[] =
6633 {"bang", "bar", "buffer", "complete", "count",
6634 "nargs", "range", "register"};
6635
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006636 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 return NULL;
6638 return (char_u *)user_cmd_flags[idx];
6639}
6640
6641/*
6642 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 char_u *
6645get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006646 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 int idx;
6648{
6649 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6650
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006651 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 return NULL;
6653 return (char_u *)user_cmd_nargs[idx];
6654}
6655
6656/*
6657 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 char_u *
6660get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006661 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 int idx;
6663{
6664 return (char_u *)command_complete[idx].name;
6665}
6666# endif /* FEAT_CMDL_COMPL */
6667
6668#endif /* FEAT_USR_CMDS */
6669
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006670#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6671/*
6672 * Parse a completion argument "value[vallen]".
6673 * The detected completion goes in "*complp", argument type in "*argt".
6674 * When there is an argument, for function and user defined completion, it's
6675 * copied to allocated memory and stored in "*compl_arg".
6676 * Returns FAIL if something is wrong.
6677 */
6678 int
6679parse_compl_arg(value, vallen, complp, argt, compl_arg)
6680 char_u *value;
6681 int vallen;
6682 int *complp;
6683 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006684 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006685{
6686 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006687# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006688 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006689# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006690 int i;
6691 int valend = vallen;
6692
6693 /* Look for any argument part - which is the part after any ',' */
6694 for (i = 0; i < vallen; ++i)
6695 {
6696 if (value[i] == ',')
6697 {
6698 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006699# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006700 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006701# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006702 valend = i;
6703 break;
6704 }
6705 }
6706
6707 for (i = 0; command_complete[i].expand != 0; ++i)
6708 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006709 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006710 && STRNCMP(value, command_complete[i].name, valend) == 0)
6711 {
6712 *complp = command_complete[i].expand;
6713 if (command_complete[i].expand == EXPAND_BUFFERS)
6714 *argt |= BUFNAME;
6715 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6716 || command_complete[i].expand == EXPAND_FILES)
6717 *argt |= XFILE;
6718 break;
6719 }
6720 }
6721
6722 if (command_complete[i].expand == 0)
6723 {
6724 EMSG2(_("E180: Invalid complete value: %s"), value);
6725 return FAIL;
6726 }
6727
6728# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6729 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6730 && arg != NULL)
6731# else
6732 if (arg != NULL)
6733# endif
6734 {
6735 EMSG(_("E468: Completion argument only allowed for custom completion"));
6736 return FAIL;
6737 }
6738
6739# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6740 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6741 && arg == NULL)
6742 {
6743 EMSG(_("E467: Custom completion requires a function argument"));
6744 return FAIL;
6745 }
6746
6747 if (arg != NULL)
6748 *compl_arg = vim_strnsave(arg, (int)arglen);
6749# endif
6750 return OK;
6751}
6752#endif
6753
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 static void
6755ex_colorscheme(eap)
6756 exarg_T *eap;
6757{
Bram Moolenaare6850792010-05-14 15:28:44 +02006758 if (*eap->arg == NUL)
6759 {
6760#ifdef FEAT_EVAL
6761 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6762 char_u *p = NULL;
6763
6764 if (expr != NULL)
6765 {
6766 ++emsg_off;
6767 p = eval_to_string(expr, NULL, FALSE);
6768 --emsg_off;
6769 vim_free(expr);
6770 }
6771 if (p != NULL)
6772 {
6773 MSG(p);
6774 vim_free(p);
6775 }
6776 else
6777 MSG("default");
6778#else
6779 MSG(_("unknown"));
6780#endif
6781 }
6782 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006783 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784}
6785
6786 static void
6787ex_highlight(eap)
6788 exarg_T *eap;
6789{
6790 if (*eap->arg == NUL && eap->cmd[2] == '!')
6791 MSG(_("Greetings, Vim user!"));
6792 do_highlight(eap->arg, eap->forceit, FALSE);
6793}
6794
6795
6796/*
6797 * Call this function if we thought we were going to exit, but we won't
6798 * (because of an error). May need to restore the terminal mode.
6799 */
6800 void
6801not_exiting()
6802{
6803 exiting = FALSE;
6804 settmode(TMODE_RAW);
6805}
6806
6807/*
6808 * ":quit": quit current window, quit Vim if closed the last window.
6809 */
6810 static void
6811ex_quit(eap)
6812 exarg_T *eap;
6813{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006814 win_T *wp;
6815 buf_T *buf;
6816 int wnr;
6817
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818#ifdef FEAT_CMDWIN
6819 if (cmdwin_type != 0)
6820 {
6821 cmdwin_result = Ctrl_C;
6822 return;
6823 }
6824#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006825 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006826 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006827 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006828 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006829 return;
6830 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006831 if (eap->addr_count > 0)
6832 {
6833 wnr = eap->line2;
6834 for (wp = firstwin; --wnr > 0; )
6835 {
6836 if (wp->w_next == NULL)
6837 break;
6838 else
6839 wp = wp->w_next;
6840 }
6841 buf = wp->w_buffer;
6842 }
6843 else
6844 {
6845 wp = curwin;
6846 buf = curbuf;
6847 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006848#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006849 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006850 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006851 * being closed (can only happen in autocommands). */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006852 if (curbuf_locked() || (buf->b_nwindows == 1 && buf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006853 return;
6854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856#ifdef FEAT_NETBEANS_INTG
6857 netbeansForcedQuit = eap->forceit;
6858#endif
6859
6860 /*
6861 * If there are more files or windows we won't exit.
6862 */
6863 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6864 exiting = TRUE;
6865 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006866 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6867 | (eap->forceit ? CCGD_FORCEIT : 0)
6868 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 || check_more(TRUE, eap->forceit) == FAIL
6870 || (only_one_window() && check_changed_any(eap->forceit)))
6871 {
6872 not_exiting();
6873 }
6874 else
6875 {
6876#ifdef FEAT_WINDOWS
6877 if (only_one_window()) /* quit last window */
6878#endif
6879 getout(0);
6880#ifdef FEAT_WINDOWS
6881# ifdef FEAT_GUI
6882 need_mouse_correct = TRUE;
6883# endif
6884 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006885 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886#endif
6887 }
6888}
6889
6890/*
6891 * ":cquit".
6892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 static void
6894ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006895 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
6897 getout(1); /* this does not always pass on the exit code to the Manx
6898 compiler. why? */
6899}
6900
6901/*
6902 * ":qall": try to quit all windows
6903 */
6904 static void
6905ex_quit_all(eap)
6906 exarg_T *eap;
6907{
6908# ifdef FEAT_CMDWIN
6909 if (cmdwin_type != 0)
6910 {
6911 if (eap->forceit)
6912 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6913 else
6914 cmdwin_result = K_XF2;
6915 return;
6916 }
6917# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006918
6919 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006920 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006921 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006922 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006923 return;
6924 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006925#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006926 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6927 /* Refuse to quit when locked or when the buffer in the last window is
6928 * being closed (can only happen in autocommands). */
6929 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006930 return;
6931#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006932
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 exiting = TRUE;
6934 if (eap->forceit || !check_changed_any(FALSE))
6935 getout(0);
6936 not_exiting();
6937}
6938
Bram Moolenaard9967712006-03-11 21:18:15 +00006939#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940/*
6941 * ":close": close current window, unless it is the last one
6942 */
6943 static void
6944ex_close(eap)
6945 exarg_T *eap;
6946{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006947 win_T *win;
6948 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949# ifdef FEAT_CMDWIN
6950 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006951 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 else
6953# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006954 if (!text_locked()
6955#ifdef FEAT_AUTOCMD
6956 && !curbuf_locked()
6957#endif
6958 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006959 {
6960 if (eap->addr_count == 0)
6961 ex_win_close(eap->forceit, curwin, NULL);
6962 else {
6963 for (win = firstwin; win != NULL; win = win->w_next)
6964 {
6965 winnr++;
6966 if (winnr == eap->line2)
6967 break;
6968 }
6969 if (win == NULL)
6970 win = lastwin;
6971 ex_win_close(eap->forceit, win, NULL);
6972 }
6973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974}
6975
Bram Moolenaard9967712006-03-11 21:18:15 +00006976# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006977/*
6978 * ":pclose": Close any preview window.
6979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006981ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006983{
6984 win_T *win;
6985
6986 for (win = firstwin; win != NULL; win = win->w_next)
6987 if (win->w_p_pvw)
6988 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006989 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006990 break;
6991 }
6992}
Bram Moolenaard9967712006-03-11 21:18:15 +00006993# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006994
Bram Moolenaarf740b292006-02-16 22:11:02 +00006995/*
6996 * Close window "win" and take care of handling closing the last window for a
6997 * modified buffer.
6998 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006999 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00007000ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007001 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007003 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 int need_hide;
7006 buf_T *buf = win->w_buffer;
7007
7008 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007009 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007011# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 if ((p_confirm || cmdmod.confirm) && p_write)
7013 {
7014 dialog_changed(buf, FALSE);
7015 if (buf_valid(buf) && bufIsChanged(buf))
7016 return;
7017 need_hide = FALSE;
7018 }
7019 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007020# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 {
7022 EMSG(_(e_nowrtmsg));
7023 return;
7024 }
7025 }
7026
Bram Moolenaard9967712006-03-11 21:18:15 +00007027# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007029# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007030
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007032 if (tp == NULL)
7033 win_close(win, !need_hide && !P_HID(buf));
7034 else
7035 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036}
7037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007039 * ":tabclose": close current tab page, unless it is the last one.
7040 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 */
7042 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007043ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 exarg_T *eap;
7045{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007046 tabpage_T *tp;
7047
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007048# ifdef FEAT_CMDWIN
7049 if (cmdwin_type != 0)
7050 cmdwin_result = K_IGNORE;
7051 else
7052# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007053 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007054 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007055 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007057 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007058 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007059 tp = find_tabpage((int)eap->line2);
7060 if (tp == NULL)
7061 {
7062 beep_flush();
7063 return;
7064 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007065 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007066 {
7067 tabpage_close_other(tp, eap->forceit);
7068 return;
7069 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007070 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007071 if (!text_locked()
7072#ifdef FEAT_AUTOCMD
7073 && !curbuf_locked()
7074#endif
7075 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007076 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007078}
7079
7080/*
7081 * ":tabonly": close all tab pages except the current one
7082 */
7083 static void
7084ex_tabonly(eap)
7085 exarg_T *eap;
7086{
7087 tabpage_T *tp;
7088 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007089
7090# ifdef FEAT_CMDWIN
7091 if (cmdwin_type != 0)
7092 cmdwin_result = K_IGNORE;
7093 else
7094# endif
7095 if (first_tabpage->tp_next == NULL)
7096 MSG(_("Already only one tab page"));
7097 else
7098 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007099 if (eap->addr_count > 0)
7100 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007101 /* Repeat this up to a 1000 times, because autocommands may mess
7102 * up the lists. */
7103 for (done = 0; done < 1000; ++done)
7104 {
7105 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7106 if (tp->tp_topframe != topframe)
7107 {
7108 tabpage_close_other(tp, eap->forceit);
7109 /* if we failed to close it quit */
7110 if (valid_tabpage(tp))
7111 done = 1000;
7112 /* start over, "tp" is now invalid */
7113 break;
7114 }
7115 if (first_tabpage->tp_next == NULL)
7116 break;
7117 }
7118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
7121/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007122 * Close the current tab page.
7123 */
7124 void
7125tabpage_close(forceit)
7126 int forceit;
7127{
7128 /* First close all the windows but the current one. If that worked then
7129 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007130 if (lastwin != firstwin)
7131 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007132 if (lastwin == firstwin)
7133 ex_win_close(forceit, curwin, NULL);
7134# ifdef FEAT_GUI
7135 need_mouse_correct = TRUE;
7136# endif
7137}
7138
7139/*
7140 * Close tab page "tp", which is not the current tab page.
7141 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007142 * Also takes care of the tab pages line disappearing when closing the
7143 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007144 */
7145 void
7146tabpage_close_other(tp, forceit)
7147 tabpage_T *tp;
7148 int forceit;
7149{
7150 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007151 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007152 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007153
7154 /* Limit to 1000 windows, autocommands may add a window while we close
7155 * one. OK, so I'm paranoid... */
7156 while (++done < 1000)
7157 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007158 wp = tp->tp_firstwin;
7159 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007160
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007161 /* Autocommands may delete the tab page under our fingers and we may
7162 * fail to close a window with a modified buffer. */
7163 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007164 break;
7165 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007166
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007167 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007168 if (h != tabline_height())
7169 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007170}
7171
7172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 * ":only".
7174 */
7175 static void
7176ex_only(eap)
7177 exarg_T *eap;
7178{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007179 win_T *wp;
7180 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181# ifdef FEAT_GUI
7182 need_mouse_correct = TRUE;
7183# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007184 if (eap->addr_count > 0)
7185 {
7186 wnr = eap->line2;
7187 for (wp = firstwin; --wnr > 0; )
7188 {
7189 if (wp->w_next == NULL)
7190 break;
7191 else
7192 wp = wp->w_next;
7193 }
7194 win_goto(wp);
7195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 close_others(TRUE, eap->forceit);
7197}
7198
7199/*
7200 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007201 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007203 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204ex_all(eap)
7205 exarg_T *eap;
7206{
7207 if (eap->addr_count == 0)
7208 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007209 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210}
7211#endif /* FEAT_WINDOWS */
7212
7213 static void
7214ex_hide(eap)
7215 exarg_T *eap;
7216{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007217 win_T *win;
7218 int winnr = 0;
7219
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7221 eap->errmsg = e_invarg;
7222 else
7223 {
7224 /* ":hide" or ":hide | cmd": hide current window */
7225 eap->nextcmd = check_nextcmd(eap->arg);
7226#ifdef FEAT_WINDOWS
7227 if (!eap->skip)
7228 {
7229# ifdef FEAT_GUI
7230 need_mouse_correct = TRUE;
7231# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007232 if (eap->addr_count == 0)
7233 win_close(curwin, FALSE); /* don't free buffer */
7234 else {
7235 for (win = firstwin; win != NULL; win = win->w_next)
7236 {
7237 winnr++;
7238 if (winnr == eap->line2)
7239 break;
7240 }
7241 if (win == NULL)
7242 win = lastwin;
7243 win_close(win, FALSE);
7244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 }
7246#endif
7247 }
7248}
7249
7250/*
7251 * ":stop" and ":suspend": Suspend Vim.
7252 */
7253 static void
7254ex_stop(eap)
7255 exarg_T *eap;
7256{
7257 /*
7258 * Disallow suspending for "rvim".
7259 */
7260 if (!check_restricted()
7261#ifdef WIN3264
7262 /*
7263 * Check if external commands are allowed now.
7264 */
7265 && can_end_termcap_mode(TRUE)
7266#endif
7267 )
7268 {
7269 if (!eap->forceit)
7270 autowrite_all();
7271 windgoto((int)Rows - 1, 0);
7272 out_char('\n');
7273 out_flush();
7274 stoptermcap();
7275 out_flush(); /* needed for SUN to restore xterm buffer */
7276#ifdef FEAT_TITLE
7277 mch_restore_title(3); /* restore window titles */
7278#endif
7279 ui_suspend(); /* call machine specific function */
7280#ifdef FEAT_TITLE
7281 maketitle();
7282 resettitle(); /* force updating the title */
7283#endif
7284 starttermcap();
7285 scroll_start(); /* scroll screen before redrawing */
7286 redraw_later_clear();
7287 shell_resized(); /* may have resized window */
7288 }
7289}
7290
7291/*
7292 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7293 */
7294 static void
7295ex_exit(eap)
7296 exarg_T *eap;
7297{
7298#ifdef FEAT_CMDWIN
7299 if (cmdwin_type != 0)
7300 {
7301 cmdwin_result = Ctrl_C;
7302 return;
7303 }
7304#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007305 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007306 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007307 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007308 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007309 return;
7310 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007311#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007312 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7313 /* Refuse to quit when locked or when the buffer in the last window is
7314 * being closed (can only happen in autocommands). */
7315 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007316 return;
7317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
7319 /*
7320 * if more files or windows we won't exit
7321 */
7322 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7323 exiting = TRUE;
7324 if ( ((eap->cmdidx == CMD_wq
7325 || curbufIsChanged())
7326 && do_write(eap) == FAIL)
7327 || check_more(TRUE, eap->forceit) == FAIL
7328 || (only_one_window() && check_changed_any(eap->forceit)))
7329 {
7330 not_exiting();
7331 }
7332 else
7333 {
7334#ifdef FEAT_WINDOWS
7335 if (only_one_window()) /* quit last window, exit Vim */
7336#endif
7337 getout(0);
7338#ifdef FEAT_WINDOWS
7339# ifdef FEAT_GUI
7340 need_mouse_correct = TRUE;
7341# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007342 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 win_close(curwin, !P_HID(curwin->w_buffer));
7344#endif
7345 }
7346}
7347
7348/*
7349 * ":print", ":list", ":number".
7350 */
7351 static void
7352ex_print(eap)
7353 exarg_T *eap;
7354{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007355 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7356 EMSG(_(e_emptybuf));
7357 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007359 for ( ;!got_int; ui_breakcheck())
7360 {
7361 print_line(eap->line1,
7362 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7363 || (eap->flags & EXFLAG_NR)),
7364 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7365 if (++eap->line1 > eap->line2)
7366 break;
7367 out_flush(); /* show one line at a time */
7368 }
7369 setpcmark();
7370 /* put cursor at last line */
7371 curwin->w_cursor.lnum = eap->line2;
7372 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 }
7374
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376}
7377
7378#ifdef FEAT_BYTEOFF
7379 static void
7380ex_goto(eap)
7381 exarg_T *eap;
7382{
7383 goto_byte(eap->line2);
7384}
7385#endif
7386
7387/*
7388 * ":shell".
7389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 static void
7391ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007392 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393{
7394 do_shell(NULL, 0);
7395}
7396
7397#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7398 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007399 || defined(FEAT_GUI_MSWIN) \
7400 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 || defined(PROTO)
7402
7403/*
7404 * Handle a file drop. The code is here because a drop is *nearly* like an
7405 * :args command, but not quite (we have a list of exact filenames, so we
7406 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7407 * code is very similar to :args and hence needs access to a lot of the static
7408 * functions in this file.
7409 *
7410 * The list should be allocated using alloc(), as should each item in the
7411 * list. This function takes over responsibility for freeing the list.
7412 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007413 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7415 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7416 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7417 * file functionality is (currently) not in EMX this is not presently a
7418 * problem.
7419 */
7420 void
7421handle_drop(filec, filev, split)
7422 int filec; /* the number of files dropped */
7423 char_u **filev; /* the list of files dropped */
7424 int split; /* force splitting the window */
7425{
7426 exarg_T ea;
7427 int save_msg_scroll = msg_scroll;
7428
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007429 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007430 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007432#ifdef FEAT_AUTOCMD
7433 if (curbuf_locked())
7434 return;
7435#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007436 /* When the screen is being updated we should not change buffers and
7437 * windows structures, it may cause freed memory to be used. */
7438 if (updating_screen)
7439 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
7441 /* Check whether the current buffer is changed. If so, we will need
7442 * to split the current window or data could be lost.
7443 * We don't need to check if the 'hidden' option is set, as in this
7444 * case the buffer won't be lost.
7445 */
7446 if (!P_HID(curbuf) && !split)
7447 {
7448 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007449 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 --emsg_off;
7451 }
7452 if (split)
7453 {
7454# ifdef FEAT_WINDOWS
7455 if (win_split(0, 0) == FAIL)
7456 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007457 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459 /* When splitting the window, create a new alist. Otherwise the
7460 * existing one is overwritten. */
7461 alist_unlink(curwin->w_alist);
7462 alist_new();
7463# else
7464 return; /* can't split, always fail */
7465# endif
7466 }
7467
7468 /*
7469 * Set up the new argument list.
7470 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007471 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473 /*
7474 * Move to the first file.
7475 */
7476 /* Fake up a minimal "next" command for do_argfile() */
7477 vim_memset(&ea, 0, sizeof(ea));
7478 ea.cmd = (char_u *)"next";
7479 do_argfile(&ea, 0);
7480
7481 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7482 * mode that is not needed here. */
7483 need_start_insertmode = FALSE;
7484
7485 /* Restore msg_scroll, otherwise a following command may cause scrolling
7486 * unexpectedly. The screen will be redrawn by the caller, thus
7487 * msg_scroll being set by displaying a message is irrelevant. */
7488 msg_scroll = save_msg_scroll;
7489}
7490#endif
7491
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492/*
7493 * Clear an argument list: free all file names and reset it to zero entries.
7494 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007495 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496alist_clear(al)
7497 alist_T *al;
7498{
7499 while (--al->al_ga.ga_len >= 0)
7500 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7501 ga_clear(&al->al_ga);
7502}
7503
7504/*
7505 * Init an argument list.
7506 */
7507 void
7508alist_init(al)
7509 alist_T *al;
7510{
7511 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7512}
7513
7514#if defined(FEAT_WINDOWS) || defined(PROTO)
7515
7516/*
7517 * Remove a reference from an argument list.
7518 * Ignored when the argument list is the global one.
7519 * If the argument list is no longer used by any window, free it.
7520 */
7521 void
7522alist_unlink(al)
7523 alist_T *al;
7524{
7525 if (al != &global_alist && --al->al_refcount <= 0)
7526 {
7527 alist_clear(al);
7528 vim_free(al);
7529 }
7530}
7531
7532# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7533/*
7534 * Create a new argument list and use it for the current window.
7535 */
7536 void
7537alist_new()
7538{
7539 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7540 if (curwin->w_alist == NULL)
7541 {
7542 curwin->w_alist = &global_alist;
7543 ++global_alist.al_refcount;
7544 }
7545 else
7546 {
7547 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007548 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 alist_init(curwin->w_alist);
7550 }
7551}
7552# endif
7553#endif
7554
7555#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7556/*
7557 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007558 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7559 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 */
7561 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007562alist_expand(fnum_list, fnum_len)
7563 int *fnum_list;
7564 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565{
7566 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007567 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 char_u **new_arg_files;
7569 int new_arg_file_count;
7570 char_u *save_p_su = p_su;
7571 int i;
7572
7573 /* Don't use 'suffixes' here. This should work like the shell did the
7574 * expansion. Also, the vimrc file isn't read yet, thus the user
7575 * can't set the options. */
7576 p_su = empty_option;
7577 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7578 if (old_arg_files != NULL)
7579 {
7580 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007581 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7582 old_arg_count = GARGCOUNT;
7583 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007585 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 && new_arg_file_count > 0)
7587 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007588 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7589 TRUE, fnum_list, fnum_len);
7590 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
7593 p_su = save_p_su;
7594}
7595#endif
7596
7597/*
7598 * Set the argument list for the current window.
7599 * Takes over the allocated files[] and the allocated fnames in it.
7600 */
7601 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007602alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 alist_T *al;
7604 int count;
7605 char_u **files;
7606 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007607 int *fnum_list;
7608 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 int i;
7611
7612 alist_clear(al);
7613 if (ga_grow(&al->al_ga, count) == OK)
7614 {
7615 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007616 {
7617 if (got_int)
7618 {
7619 /* When adding many buffers this can take a long time. Allow
7620 * interrupting here. */
7621 while (i < count)
7622 vim_free(files[i++]);
7623 break;
7624 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007625
7626 /* May set buffer name of a buffer previously used for the
7627 * argument list, so that it's re-used by alist_add. */
7628 if (fnum_list != NULL && i < fnum_len)
7629 buf_set_name(fnum_list[i], files[i]);
7630
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007632 ui_breakcheck();
7633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 vim_free(files);
7635 }
7636 else
7637 FreeWild(count, files);
7638#ifdef FEAT_WINDOWS
7639 if (al == &global_alist)
7640#endif
7641 arg_had_last = FALSE;
7642}
7643
7644/*
7645 * Add file "fname" to argument list "al".
7646 * "fname" must have been allocated and "al" must have been checked for room.
7647 */
7648 void
7649alist_add(al, fname, set_fnum)
7650 alist_T *al;
7651 char_u *fname;
7652 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7653{
7654 if (fname == NULL) /* don't add NULL file names */
7655 return;
7656#ifdef BACKSLASH_IN_FILENAME
7657 slash_adjust(fname);
7658#endif
7659 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7660 if (set_fnum > 0)
7661 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7662 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7663 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664}
7665
7666#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7667/*
7668 * Adjust slashes in file names. Called after 'shellslash' was set.
7669 */
7670 void
7671alist_slash_adjust()
7672{
7673 int i;
7674# ifdef FEAT_WINDOWS
7675 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007676 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677# endif
7678
7679 for (i = 0; i < GARGCOUNT; ++i)
7680 if (GARGLIST[i].ae_fname != NULL)
7681 slash_adjust(GARGLIST[i].ae_fname);
7682# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007683 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 if (wp->w_alist != &global_alist)
7685 for (i = 0; i < WARGCOUNT(wp); ++i)
7686 if (WARGLIST(wp)[i].ae_fname != NULL)
7687 slash_adjust(WARGLIST(wp)[i].ae_fname);
7688# endif
7689}
7690#endif
7691
7692/*
7693 * ":preserve".
7694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 static void
7696ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007697 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007699 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 ml_preserve(curbuf, TRUE);
7701}
7702
7703/*
7704 * ":recover".
7705 */
7706 static void
7707ex_recover(eap)
7708 exarg_T *eap;
7709{
7710 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7711 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007712 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7713 | CCGD_MULTWIN
7714 | (eap->forceit ? CCGD_FORCEIT : 0)
7715 | CCGD_EXCMD)
7716
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 && (*eap->arg == NUL
7718 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7719 ml_recover();
7720 recoverymode = FALSE;
7721}
7722
7723/*
7724 * Command modifier used in a wrong way.
7725 */
7726 static void
7727ex_wrongmodifier(eap)
7728 exarg_T *eap;
7729{
7730 eap->errmsg = e_invcmd;
7731}
7732
7733#ifdef FEAT_WINDOWS
7734/*
7735 * :sview [+command] file split window with new file, read-only
7736 * :split [[+command] file] split window with current or new file
7737 * :vsplit [[+command] file] split window vertically with current or new file
7738 * :new [[+command] file] split window with no or new file
7739 * :vnew [[+command] file] split vertically window with no or new file
7740 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007741 *
7742 * :tabedit open new Tab page with empty window
7743 * :tabedit [+command] file open new Tab page and edit "file"
7744 * :tabnew [[+command] file] just like :tabedit
7745 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 */
7747 void
7748ex_splitview(eap)
7749 exarg_T *eap;
7750{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007751 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007752# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007754# endif
7755# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007759# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7761 {
7762 ex_ni(eap);
7763 return;
7764 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007765# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007767# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007771# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007773 * quickfix windows. But it's OK when doing ":tab split". */
7774 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 {
7776 if (eap->cmdidx == CMD_split)
7777 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007778# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 if (eap->cmdidx == CMD_vsplit)
7780 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007785# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007786 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 {
7788 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7789 FNAME_MESS, TRUE, curbuf->b_ffname);
7790 if (fname == NULL)
7791 goto theend;
7792 eap->arg = fname;
7793 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007794# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007798# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007800# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 && eap->cmdidx != CMD_new)
7804 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007805# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007806 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007807# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007808 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007809# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007810 au_has_group((char_u *)"FileExplorer"))
7811 {
7812 /* No browsing supported but we do have the file explorer:
7813 * Edit the directory. */
7814 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7815 eap->arg = (char_u *)".";
7816 }
7817 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007818# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007819 {
7820 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007822 if (fname == NULL)
7823 goto theend;
7824 eap->arg = fname;
7825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
7827 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007830 /*
7831 * Either open new tab page or split the window.
7832 */
7833 if (eap->cmdidx == CMD_tabedit
7834 || eap->cmdidx == CMD_tabfind
7835 || eap->cmdidx == CMD_tabnew)
7836 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007837 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7838 : eap->addr_count == 0 ? 0
7839 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007840 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007841 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007842
7843 /* set the alternate buffer for the window we came from */
7844 if (curwin != old_curwin
7845 && win_valid(old_curwin)
7846 && old_curwin->w_buffer != curbuf
7847 && !cmdmod.keepalt)
7848 old_curwin->w_alt_fnum = curbuf->b_fnum;
7849 }
7850 }
7851 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7853 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007854# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 /* Reset 'scrollbind' when editing another file, but keep it when
7856 * doing ":split" without arguments. */
7857 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007858# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007862 {
7863 RESET_BINDING(curwin);
7864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 else
7866 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 do_exedit(eap, old_curwin);
7869 }
7870
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007871# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007875# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876theend:
7877 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007880
7881/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007882 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007883 */
7884 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007885tabpage_new()
7886{
7887 exarg_T ea;
7888
7889 vim_memset(&ea, 0, sizeof(ea));
7890 ea.cmdidx = CMD_tabnew;
7891 ea.cmd = (char_u *)"tabn";
7892 ea.arg = (char_u *)"";
7893 ex_splitview(&ea);
7894}
7895
7896/*
7897 * :tabnext command
7898 */
7899 static void
7900ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007901 exarg_T *eap;
7902{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007903 switch (eap->cmdidx)
7904 {
7905 case CMD_tabfirst:
7906 case CMD_tabrewind:
7907 goto_tabpage(1);
7908 break;
7909 case CMD_tablast:
7910 goto_tabpage(9999);
7911 break;
7912 case CMD_tabprevious:
7913 case CMD_tabNext:
7914 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7915 break;
7916 default: /* CMD_tabnext */
7917 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7918 break;
7919 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007920}
7921
7922/*
7923 * :tabmove command
7924 */
7925 static void
7926ex_tabmove(eap)
7927 exarg_T *eap;
7928{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007929 int tab_number = 9999;
7930
7931 if (eap->arg && *eap->arg != NUL)
7932 {
7933 char_u *p = eap->arg;
7934 int relative = 0; /* argument +N/-N means: move N places to the
7935 * right/left relative to the current position. */
7936
7937 if (*eap->arg == '-')
7938 {
7939 relative = -1;
7940 p = eap->arg + 1;
7941 }
7942 else if (*eap->arg == '+')
7943 {
7944 relative = 1;
7945 p = eap->arg + 1;
7946 }
7947 else
7948 p = eap->arg;
7949
7950 if (p == skipdigits(p))
7951 {
7952 /* No numbers as argument. */
7953 eap->errmsg = e_invarg;
7954 return;
7955 }
7956
7957 tab_number = getdigits(&p);
7958 if (relative != 0)
7959 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7960 }
7961 else if (eap->addr_count != 0)
7962 tab_number = eap->line2;
7963
7964 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007965}
7966
7967/*
7968 * :tabs command: List tabs and their contents.
7969 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007970 static void
7971ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007972 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007973{
7974 tabpage_T *tp;
7975 win_T *wp;
7976 int tabcount = 1;
7977
7978 msg_start();
7979 msg_scroll = TRUE;
7980 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7981 {
7982 msg_putchar('\n');
7983 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7984 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7985 out_flush(); /* output one line at a time */
7986 ui_breakcheck();
7987
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007988 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007989 wp = firstwin;
7990 else
7991 wp = tp->tp_firstwin;
7992 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7993 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007994 msg_putchar('\n');
7995 msg_putchar(wp == curwin ? '>' : ' ');
7996 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007997 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7998 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007999 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008000 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008001 else
8002 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8003 IObuff, IOSIZE, TRUE);
8004 msg_outtrans(IObuff);
8005 out_flush(); /* output one line at a time */
8006 ui_breakcheck();
8007 }
8008 }
8009}
8010
8011#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012
8013/*
8014 * ":mode": Set screen mode.
8015 * If no argument given, just get the screen size and redraw.
8016 */
8017 static void
8018ex_mode(eap)
8019 exarg_T *eap;
8020{
8021 if (*eap->arg == NUL)
8022 shell_resized();
8023 else
8024 mch_screenmode(eap->arg);
8025}
8026
8027#ifdef FEAT_WINDOWS
8028/*
8029 * ":resize".
8030 * set, increment or decrement current window height
8031 */
8032 static void
8033ex_resize(eap)
8034 exarg_T *eap;
8035{
8036 int n;
8037 win_T *wp = curwin;
8038
8039 if (eap->addr_count > 0)
8040 {
8041 n = eap->line2;
8042 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8043 ;
8044 }
8045
8046#ifdef FEAT_GUI
8047 need_mouse_correct = TRUE;
8048#endif
8049 n = atol((char *)eap->arg);
8050#ifdef FEAT_VERTSPLIT
8051 if (cmdmod.split & WSP_VERT)
8052 {
8053 if (*eap->arg == '-' || *eap->arg == '+')
8054 n += W_WIDTH(curwin);
8055 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8056 n = 9999;
8057 win_setwidth_win((int)n, wp);
8058 }
8059 else
8060#endif
8061 {
8062 if (*eap->arg == '-' || *eap->arg == '+')
8063 n += curwin->w_height;
8064 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8065 n = 9999;
8066 win_setheight_win((int)n, wp);
8067 }
8068}
8069#endif
8070
8071/*
8072 * ":find [+command] <file>" command.
8073 */
8074 static void
8075ex_find(eap)
8076 exarg_T *eap;
8077{
8078#ifdef FEAT_SEARCHPATH
8079 char_u *fname;
8080 int count;
8081
8082 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8083 TRUE, curbuf->b_ffname);
8084 if (eap->addr_count > 0)
8085 {
8086 /* Repeat finding the file "count" times. This matters when it
8087 * appears several times in the path. */
8088 count = eap->line2;
8089 while (fname != NULL && --count > 0)
8090 {
8091 vim_free(fname);
8092 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8093 FALSE, curbuf->b_ffname);
8094 }
8095 }
8096
8097 if (fname != NULL)
8098 {
8099 eap->arg = fname;
8100#endif
8101 do_exedit(eap, NULL);
8102#ifdef FEAT_SEARCHPATH
8103 vim_free(fname);
8104 }
8105#endif
8106}
8107
8108/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008109 * ":open" simulation: for now just work like ":visual".
8110 */
8111 static void
8112ex_open(eap)
8113 exarg_T *eap;
8114{
8115 regmatch_T regmatch;
8116 char_u *p;
8117
8118 curwin->w_cursor.lnum = eap->line2;
8119 beginline(BL_SOL | BL_FIX);
8120 if (*eap->arg == '/')
8121 {
8122 /* ":open /pattern/": put cursor in column found with pattern */
8123 ++eap->arg;
8124 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8125 *p = NUL;
8126 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8127 if (regmatch.regprog != NULL)
8128 {
8129 regmatch.rm_ic = p_ic;
8130 p = ml_get_curline();
8131 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008132 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008133 else
8134 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008135 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008136 }
8137 /* Move to the NUL, ignore any other arguments. */
8138 eap->arg += STRLEN(eap->arg);
8139 }
8140 check_cursor();
8141
8142 eap->cmdidx = CMD_visual;
8143 do_exedit(eap, NULL);
8144}
8145
8146/*
8147 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 */
8149 static void
8150ex_edit(eap)
8151 exarg_T *eap;
8152{
8153 do_exedit(eap, NULL);
8154}
8155
8156/*
8157 * ":edit <file>" command and alikes.
8158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 void
8160do_exedit(eap, old_curwin)
8161 exarg_T *eap;
8162 win_T *old_curwin; /* curwin before doing a split or NULL */
8163{
8164 int n;
8165#ifdef FEAT_WINDOWS
8166 int need_hide;
8167#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008168 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169
8170 /*
8171 * ":vi" command ends Ex mode.
8172 */
8173 if (exmode_active && (eap->cmdidx == CMD_visual
8174 || eap->cmdidx == CMD_view))
8175 {
8176 exmode_active = FALSE;
8177 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008178 {
8179 /* Special case: ":global/pat/visual\NLvi-commands" */
8180 if (global_busy)
8181 {
8182 int rd = RedrawingDisabled;
8183 int nwr = no_wait_return;
8184 int ms = msg_scroll;
8185#ifdef FEAT_GUI
8186 int he = hold_gui_events;
8187#endif
8188
8189 if (eap->nextcmd != NULL)
8190 {
8191 stuffReadbuff(eap->nextcmd);
8192 eap->nextcmd = NULL;
8193 }
8194
8195 if (exmode_was != EXMODE_VIM)
8196 settmode(TMODE_RAW);
8197 RedrawingDisabled = 0;
8198 no_wait_return = 0;
8199 need_wait_return = FALSE;
8200 msg_scroll = 0;
8201#ifdef FEAT_GUI
8202 hold_gui_events = 0;
8203#endif
8204 must_redraw = CLEAR;
8205
8206 main_loop(FALSE, TRUE);
8207
8208 RedrawingDisabled = rd;
8209 no_wait_return = nwr;
8210 msg_scroll = ms;
8211#ifdef FEAT_GUI
8212 hold_gui_events = he;
8213#endif
8214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 }
8218
8219 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008220 || eap->cmdidx == CMD_tabnew
8221 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222#ifdef FEAT_VERTSPLIT
8223 || eap->cmdidx == CMD_vnew
8224#endif
8225 ) && *eap->arg == NUL)
8226 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008227 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 setpcmark();
8229 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008230 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8231 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 }
8233 else if ((eap->cmdidx != CMD_split
8234#ifdef FEAT_VERTSPLIT
8235 && eap->cmdidx != CMD_vsplit
8236#endif
8237 )
8238 || *eap->arg != NUL
8239#ifdef FEAT_BROWSE
8240 || cmdmod.browse
8241#endif
8242 )
8243 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008244#ifdef FEAT_AUTOCMD
8245 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8246 * can bring us here, others are stopped earlier. */
8247 if (*eap->arg != NUL && curbuf_locked())
8248 return;
8249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 n = readonlymode;
8251 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8252 readonlymode = TRUE;
8253 else if (eap->cmdidx == CMD_enew)
8254 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8255 empty buffer */
8256 setpcmark();
8257 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8258 NULL, eap,
8259 /* ":edit" goes to first line if Vi compatible */
8260 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8261 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8262 ? ECMD_ONE : eap->do_ecmd_lnum,
8263 (P_HID(curbuf) ? ECMD_HIDE : 0)
8264 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008265 /* after a split we can use an existing buffer */
8266 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267#ifdef FEAT_LISTCMDS
8268 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8269#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008270 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {
8272 /* Editing the file failed. If the window was split, close it. */
8273#ifdef FEAT_WINDOWS
8274 if (old_curwin != NULL)
8275 {
8276 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8277 if (!need_hide || P_HID(curbuf))
8278 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008279# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8280 cleanup_T cs;
8281
8282 /* Reset the error/interrupt/exception state here so that
8283 * aborting() returns FALSE when closing a window. */
8284 enter_cleanup(&cs);
8285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286# ifdef FEAT_GUI
8287 need_mouse_correct = TRUE;
8288# endif
8289 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008290
8291# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8292 /* Restore the error/interrupt/exception state if not
8293 * discarded by a new aborting error, interrupt, or
8294 * uncaught exception. */
8295 leave_cleanup(&cs);
8296# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 }
8298 }
8299#endif
8300 }
8301 else if (readonlymode && curbuf->b_nwindows == 1)
8302 {
8303 /* When editing an already visited buffer, 'readonly' won't be set
8304 * but the previous value is kept. With ":view" and ":sview" we
8305 * want the file to be readonly, except when another window is
8306 * editing the same buffer. */
8307 curbuf->b_p_ro = TRUE;
8308 }
8309 readonlymode = n;
8310 }
8311 else
8312 {
8313 if (eap->do_ecmd_cmd != NULL)
8314 do_cmdline_cmd(eap->do_ecmd_cmd);
8315#ifdef FEAT_TITLE
8316 n = curwin->w_arg_idx_invalid;
8317#endif
8318 check_arg_idx(curwin);
8319#ifdef FEAT_TITLE
8320 if (n != curwin->w_arg_idx_invalid)
8321 maketitle();
8322#endif
8323 }
8324
8325#ifdef FEAT_WINDOWS
8326 /*
8327 * if ":split file" worked, set alternate file name in old window to new
8328 * file
8329 */
8330 if (old_curwin != NULL
8331 && *eap->arg != NUL
8332 && curwin != old_curwin
8333 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008334 && old_curwin->w_buffer != curbuf
8335 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 old_curwin->w_alt_fnum = curbuf->b_fnum;
8337#endif
8338
8339 ex_no_reprint = TRUE;
8340}
8341
8342#ifndef FEAT_GUI
8343/*
8344 * ":gui" and ":gvim" when there is no GUI.
8345 */
8346 static void
8347ex_nogui(eap)
8348 exarg_T *eap;
8349{
8350 eap->errmsg = e_nogvim;
8351}
8352#endif
8353
8354#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8355 static void
8356ex_tearoff(eap)
8357 exarg_T *eap;
8358{
8359 gui_make_tearoff(eap->arg);
8360}
8361#endif
8362
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008363#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 static void
8365ex_popup(eap)
8366 exarg_T *eap;
8367{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008368 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369}
8370#endif
8371
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 static void
8373ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008374 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375{
8376 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8377 MSG(_("No swap file"));
8378 else
8379 msg(curbuf->b_ml.ml_mfp->mf_fname);
8380}
8381
8382/*
8383 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8384 * offset.
8385 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 static void
8388ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008389 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390{
8391#ifdef FEAT_SCROLLBIND
8392 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008393 win_T *save_curwin = curwin;
8394 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 long topline;
8396 long y;
8397 linenr_T old_linenr = curwin->w_cursor.lnum;
8398
8399 setpcmark();
8400
8401 /*
8402 * determine max topline
8403 */
8404 if (curwin->w_p_scb)
8405 {
8406 topline = curwin->w_topline;
8407 for (wp = firstwin; wp; wp = wp->w_next)
8408 {
8409 if (wp->w_p_scb && wp->w_buffer)
8410 {
8411 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8412 if (topline > y)
8413 topline = y;
8414 }
8415 }
8416 if (topline < 1)
8417 topline = 1;
8418 }
8419 else
8420 {
8421 topline = 1;
8422 }
8423
8424
8425 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008426 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8429 {
8430 if (curwin->w_p_scb)
8431 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008432 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 y = topline - curwin->w_topline;
8434 if (y > 0)
8435 scrollup(y, TRUE);
8436 else
8437 scrolldown(-y, TRUE);
8438 curwin->w_scbind_pos = topline;
8439 redraw_later(VALID);
8440 cursor_correct();
8441#ifdef FEAT_WINDOWS
8442 curwin->w_redr_status = TRUE;
8443#endif
8444 }
8445 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008446 curwin = save_curwin;
8447 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 if (curwin->w_p_scb)
8449 {
8450 did_syncbind = TRUE;
8451 checkpcmark();
8452 if (old_linenr != curwin->w_cursor.lnum)
8453 {
8454 char_u ctrl_o[2];
8455
8456 ctrl_o[0] = Ctrl_O;
8457 ctrl_o[1] = 0;
8458 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8459 }
8460 }
8461#endif
8462}
8463
8464
8465 static void
8466ex_read(eap)
8467 exarg_T *eap;
8468{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008469 int i;
8470 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8471 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472
8473 if (eap->usefilter) /* :r!cmd */
8474 do_bang(1, eap, FALSE, FALSE, TRUE);
8475 else
8476 {
8477 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8478 return;
8479
8480#ifdef FEAT_BROWSE
8481 if (cmdmod.browse)
8482 {
8483 char_u *browseFile;
8484
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008485 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 NULL, NULL, NULL, curbuf);
8487 if (browseFile != NULL)
8488 {
8489 i = readfile(browseFile, NULL,
8490 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8491 vim_free(browseFile);
8492 }
8493 else
8494 i = OK;
8495 }
8496 else
8497#endif
8498 if (*eap->arg == NUL)
8499 {
8500 if (check_fname() == FAIL) /* check for no file name */
8501 return;
8502 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8503 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8504 }
8505 else
8506 {
8507 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8508 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8509 i = readfile(eap->arg, NULL,
8510 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8511
8512 }
8513 if (i == FAIL)
8514 {
8515#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8516 if (!aborting())
8517#endif
8518 EMSG2(_(e_notopen), eap->arg);
8519 }
8520 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008521 {
8522 if (empty && exmode_active)
8523 {
8524 /* Delete the empty line that remains. Historically ex does
8525 * this but vi doesn't. */
8526 if (eap->line2 == 0)
8527 lnum = curbuf->b_ml.ml_line_count;
8528 else
8529 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008530 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008531 {
8532 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008533 if (curwin->w_cursor.lnum > 1
8534 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008535 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008536 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008537 }
8538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 }
8542}
8543
Bram Moolenaarea408852005-06-25 22:49:46 +00008544static char_u *prev_dir = NULL;
8545
8546#if defined(EXITFREE) || defined(PROTO)
8547 void
8548free_cd_dir()
8549{
8550 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008551 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008552
8553 vim_free(globaldir);
8554 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008555}
8556#endif
8557
Bram Moolenaarf4258302013-06-02 18:20:17 +02008558/*
8559 * Deal with the side effects of changing the current directory.
8560 * When "local" is TRUE then this was after an ":lcd" command.
8561 */
8562 void
8563post_chdir(local)
8564 int local;
8565{
8566 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008567 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008568 if (local)
8569 {
8570 /* If still in global directory, need to remember current
8571 * directory as global directory. */
8572 if (globaldir == NULL && prev_dir != NULL)
8573 globaldir = vim_strsave(prev_dir);
8574 /* Remember this local directory for the window. */
8575 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8576 curwin->w_localdir = vim_strsave(NameBuff);
8577 }
8578 else
8579 {
8580 /* We are now in the global directory, no need to remember its
8581 * name. */
8582 vim_free(globaldir);
8583 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008584 }
8585
8586 shorten_fnames(TRUE);
8587}
8588
Bram Moolenaarea408852005-06-25 22:49:46 +00008589
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590/*
8591 * ":cd", ":lcd", ":chdir" and ":lchdir".
8592 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008593 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594ex_cd(eap)
8595 exarg_T *eap;
8596{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 char_u *new_dir;
8598 char_u *tofree;
8599
8600 new_dir = eap->arg;
8601#if !defined(UNIX) && !defined(VMS)
8602 /* for non-UNIX ":cd" means: print current directory */
8603 if (*new_dir == NUL)
8604 ex_pwd(NULL);
8605 else
8606#endif
8607 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008608#ifdef FEAT_AUTOCMD
8609 if (allbuf_locked())
8610 return;
8611#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008612 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8613 && !eap->forceit)
8614 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008615 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008616 return;
8617 }
8618
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 /* ":cd -": Change to previous directory */
8620 if (STRCMP(new_dir, "-") == 0)
8621 {
8622 if (prev_dir == NULL)
8623 {
8624 EMSG(_("E186: No previous directory"));
8625 return;
8626 }
8627 new_dir = prev_dir;
8628 }
8629
8630 /* Save current directory for next ":cd -" */
8631 tofree = prev_dir;
8632 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8633 prev_dir = vim_strsave(NameBuff);
8634 else
8635 prev_dir = NULL;
8636
8637#if defined(UNIX) || defined(VMS)
8638 /* for UNIX ":cd" means: go to home directory */
8639 if (*new_dir == NUL)
8640 {
8641 /* use NameBuff for home directory name */
8642# ifdef VMS
8643 char_u *p;
8644
8645 p = mch_getenv((char_u *)"SYS$LOGIN");
8646 if (p == NULL || *p == NUL) /* empty is the same as not set */
8647 NameBuff[0] = NUL;
8648 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008649 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650# else
8651 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8652# endif
8653 new_dir = NameBuff;
8654 }
8655#endif
8656 if (new_dir == NULL || vim_chdir(new_dir))
8657 EMSG(_(e_failed));
8658 else
8659 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008660 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661
8662 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008663 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 ex_pwd(eap);
8665 }
8666 vim_free(tofree);
8667 }
8668}
8669
8670/*
8671 * ":pwd".
8672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 static void
8674ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008675 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676{
8677 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8678 {
8679#ifdef BACKSLASH_IN_FILENAME
8680 slash_adjust(NameBuff);
8681#endif
8682 msg(NameBuff);
8683 }
8684 else
8685 EMSG(_("E187: Unknown"));
8686}
8687
8688/*
8689 * ":=".
8690 */
8691 static void
8692ex_equal(eap)
8693 exarg_T *eap;
8694{
8695 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008696 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697}
8698
8699 static void
8700ex_sleep(eap)
8701 exarg_T *eap;
8702{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008703 int n;
8704 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
8706 if (cursor_valid())
8707 {
8708 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8709 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008710 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008712
8713 len = eap->line2;
8714 switch (*eap->arg)
8715 {
8716 case 'm': break;
8717 case NUL: len *= 1000L; break;
8718 default: EMSG2(_(e_invarg2), eap->arg); return;
8719 }
8720 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721}
8722
8723/*
8724 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8725 */
8726 void
8727do_sleep(msec)
8728 long msec;
8729{
8730 long done;
8731
8732 cursor_on();
8733 out_flush();
8734 for (done = 0; !got_int && done < msec; done += 1000L)
8735 {
8736 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8737 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008738#ifdef FEAT_NETBEANS_INTG
8739 /* Process the netbeans messages that may have been received in the
8740 * call to ui_breakcheck() when the GUI is in use. This may occur when
8741 * running a test case. */
8742 netbeans_parse_messages();
8743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 }
8745}
8746
8747 static void
8748do_exmap(eap, isabbrev)
8749 exarg_T *eap;
8750 int isabbrev;
8751{
8752 int mode;
8753 char_u *cmdp;
8754
8755 cmdp = eap->cmd;
8756 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8757
8758 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8759 eap->arg, mode, isabbrev))
8760 {
8761 case 1: EMSG(_(e_invarg));
8762 break;
8763 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8764 break;
8765 }
8766}
8767
8768/*
8769 * ":winsize" command (obsolete).
8770 */
8771 static void
8772ex_winsize(eap)
8773 exarg_T *eap;
8774{
8775 int w, h;
8776 char_u *arg = eap->arg;
8777 char_u *p;
8778
8779 w = getdigits(&arg);
8780 arg = skipwhite(arg);
8781 p = arg;
8782 h = getdigits(&arg);
8783 if (*p != NUL && *arg == NUL)
8784 set_shellsize(w, h, TRUE);
8785 else
8786 EMSG(_("E465: :winsize requires two number arguments"));
8787}
8788
8789#ifdef FEAT_WINDOWS
8790 static void
8791ex_wincmd(eap)
8792 exarg_T *eap;
8793{
8794 int xchar = NUL;
8795 char_u *p;
8796
8797 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8798 {
8799 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8800 if (eap->arg[1] == NUL)
8801 {
8802 EMSG(_(e_invarg));
8803 return;
8804 }
8805 xchar = eap->arg[1];
8806 p = eap->arg + 2;
8807 }
8808 else
8809 p = eap->arg + 1;
8810
8811 eap->nextcmd = check_nextcmd(p);
8812 p = skipwhite(p);
8813 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8814 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008815 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 {
8817 /* Pass flags on for ":vertical wincmd ]". */
8818 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008819 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8821 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008822 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 }
8824}
8825#endif
8826
Bram Moolenaar843ee412004-06-30 16:16:41 +00008827#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828/*
8829 * ":winpos".
8830 */
8831 static void
8832ex_winpos(eap)
8833 exarg_T *eap;
8834{
8835 int x, y;
8836 char_u *arg = eap->arg;
8837 char_u *p;
8838
8839 if (*arg == NUL)
8840 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008841# if defined(FEAT_GUI) || defined(MSWIN)
8842# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008844# else
8845 if (mch_get_winpos(&x, &y) != FAIL)
8846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 {
8848 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8849 msg(IObuff);
8850 }
8851 else
8852# endif
8853 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8854 }
8855 else
8856 {
8857 x = getdigits(&arg);
8858 arg = skipwhite(arg);
8859 p = arg;
8860 y = getdigits(&arg);
8861 if (*p == NUL || *arg != NUL)
8862 {
8863 EMSG(_("E466: :winpos requires two number arguments"));
8864 return;
8865 }
8866# ifdef FEAT_GUI
8867 if (gui.in_use)
8868 gui_mch_set_winpos(x, y);
8869 else if (gui.starting)
8870 {
8871 /* Remember the coordinates for when the window is opened. */
8872 gui_win_x = x;
8873 gui_win_y = y;
8874 }
8875# ifdef HAVE_TGETENT
8876 else
8877# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008878# else
8879# ifdef MSWIN
8880 mch_set_winpos(x, y);
8881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882# endif
8883# ifdef HAVE_TGETENT
8884 if (*T_CWP)
8885 term_set_winpos(x, y);
8886# endif
8887 }
8888}
8889#endif
8890
8891/*
8892 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8893 */
8894 static void
8895ex_operators(eap)
8896 exarg_T *eap;
8897{
8898 oparg_T oa;
8899
8900 clear_oparg(&oa);
8901 oa.regname = eap->regname;
8902 oa.start.lnum = eap->line1;
8903 oa.end.lnum = eap->line2;
8904 oa.line_count = eap->line2 - eap->line1 + 1;
8905 oa.motion_type = MLINE;
8906#ifdef FEAT_VIRTUALEDIT
8907 virtual_op = FALSE;
8908#endif
8909 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8910 {
8911 setpcmark();
8912 curwin->w_cursor.lnum = eap->line1;
8913 beginline(BL_SOL | BL_FIX);
8914 }
8915
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008916 if (VIsual_active)
8917 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008918
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 switch (eap->cmdidx)
8920 {
8921 case CMD_delete:
8922 oa.op_type = OP_DELETE;
8923 op_delete(&oa);
8924 break;
8925
8926 case CMD_yank:
8927 oa.op_type = OP_YANK;
8928 (void)op_yank(&oa, FALSE, TRUE);
8929 break;
8930
8931 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008932 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008934 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8935#else
8936 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008938 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 oa.op_type = OP_RSHIFT;
8940 else
8941 oa.op_type = OP_LSHIFT;
8942 op_shift(&oa, FALSE, eap->amount);
8943 break;
8944 }
8945#ifdef FEAT_VIRTUALEDIT
8946 virtual_op = MAYBE;
8947#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008948 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949}
8950
8951/*
8952 * ":put".
8953 */
8954 static void
8955ex_put(eap)
8956 exarg_T *eap;
8957{
8958 /* ":0put" works like ":1put!". */
8959 if (eap->line2 == 0)
8960 {
8961 eap->line2 = 1;
8962 eap->forceit = TRUE;
8963 }
8964 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008965 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8966 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967}
8968
8969/*
8970 * Handle ":copy" and ":move".
8971 */
8972 static void
8973ex_copymove(eap)
8974 exarg_T *eap;
8975{
8976 long n;
8977
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01008978 n = get_address(&eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 if (eap->arg == NULL) /* error detected */
8980 {
8981 eap->nextcmd = NULL;
8982 return;
8983 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008984 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985
8986 /*
8987 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8988 */
8989 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8990 {
8991 EMSG(_(e_invaddr));
8992 return;
8993 }
8994
8995 if (eap->cmdidx == CMD_move)
8996 {
8997 if (do_move(eap->line1, eap->line2, n) == FAIL)
8998 return;
8999 }
9000 else
9001 ex_copy(eap->line1, eap->line2, n);
9002 u_clearline();
9003 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009004 ex_may_print(eap);
9005}
9006
9007/*
9008 * Print the current line if flags were given to the Ex command.
9009 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009010 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00009011ex_may_print(eap)
9012 exarg_T *eap;
9013{
9014 if (eap->flags != 0)
9015 {
9016 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9017 (eap->flags & EXFLAG_LIST));
9018 ex_no_reprint = TRUE;
9019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020}
9021
9022/*
9023 * ":smagic" and ":snomagic".
9024 */
9025 static void
9026ex_submagic(eap)
9027 exarg_T *eap;
9028{
9029 int magic_save = p_magic;
9030
9031 p_magic = (eap->cmdidx == CMD_smagic);
9032 do_sub(eap);
9033 p_magic = magic_save;
9034}
9035
9036/*
9037 * ":join".
9038 */
9039 static void
9040ex_join(eap)
9041 exarg_T *eap;
9042{
9043 curwin->w_cursor.lnum = eap->line1;
9044 if (eap->line1 == eap->line2)
9045 {
9046 if (eap->addr_count >= 2) /* :2,2join does nothing */
9047 return;
9048 if (eap->line2 == curbuf->b_ml.ml_line_count)
9049 {
9050 beep_flush();
9051 return;
9052 }
9053 ++eap->line2;
9054 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009055 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009057 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058}
9059
9060/*
9061 * ":[addr]@r" or ":[addr]*r": execute register
9062 */
9063 static void
9064ex_at(eap)
9065 exarg_T *eap;
9066{
9067 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009068 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
9070 curwin->w_cursor.lnum = eap->line2;
9071
9072#ifdef USE_ON_FLY_SCROLL
9073 dont_scroll = TRUE; /* disallow scrolling here */
9074#endif
9075
9076 /* get the register name. No name means to use the previous one */
9077 c = *eap->arg;
9078 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9079 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009080 /* Put the register in the typeahead buffer with the "silent" flag. */
9081 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9082 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 else
9087 {
9088 int save_efr = exec_from_reg;
9089
9090 exec_from_reg = TRUE;
9091
9092 /*
9093 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009094 * Continue until the stuff buffer is empty and all added characters
9095 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009097 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9099
9100 exec_from_reg = save_efr;
9101 }
9102}
9103
9104/*
9105 * ":!".
9106 */
9107 static void
9108ex_bang(eap)
9109 exarg_T *eap;
9110{
9111 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9112}
9113
9114/*
9115 * ":undo".
9116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 static void
9118ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009119 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009121 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009122 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009123 else
9124 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009127#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009128 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009129ex_wundo(eap)
9130 exarg_T *eap;
9131{
9132 char_u hash[UNDO_HASH_SIZE];
9133
9134 u_compute_hash(hash);
9135 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9136}
9137
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009138 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009139ex_rundo(eap)
9140 exarg_T *eap;
9141{
9142 char_u hash[UNDO_HASH_SIZE];
9143
9144 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009145 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009146}
9147#endif
9148
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149/*
9150 * ":redo".
9151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 static void
9153ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009154 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
9156 u_redo(1);
9157}
9158
9159/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009160 * ":earlier" and ":later".
9161 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009162 static void
9163ex_later(eap)
9164 exarg_T *eap;
9165{
9166 long count = 0;
9167 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009168 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009169 char_u *p = eap->arg;
9170
9171 if (*p == NUL)
9172 count = 1;
9173 else if (isdigit(*p))
9174 {
9175 count = getdigits(&p);
9176 switch (*p)
9177 {
9178 case 's': ++p; sec = TRUE; break;
9179 case 'm': ++p; sec = TRUE; count *= 60; break;
9180 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009181 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9182 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009183 }
9184 }
9185
9186 if (*p != NUL)
9187 EMSG2(_(e_invarg2), eap->arg);
9188 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009189 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9190 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009191}
9192
9193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 * ":redir": start/stop redirection.
9195 */
9196 static void
9197ex_redir(eap)
9198 exarg_T *eap;
9199{
9200 char *mode;
9201 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009202 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203
9204 if (STRICMP(eap->arg, "END") == 0)
9205 close_redir();
9206 else
9207 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009208 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009210 ++arg;
9211 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009213 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 mode = "a";
9215 }
9216 else
9217 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009218 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219
9220 close_redir();
9221
9222 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009223 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224 if (fname == NULL)
9225 return;
9226#ifdef FEAT_BROWSE
9227 if (cmdmod.browse)
9228 {
9229 char_u *browseFile;
9230
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009231 browseFile = do_browse(BROWSE_SAVE,
9232 (char_u *)_("Save Redirection"),
9233 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 if (browseFile == NULL)
9235 return; /* operation cancelled */
9236 vim_free(fname);
9237 fname = browseFile;
9238 eap->forceit = TRUE; /* since dialog already asked */
9239 }
9240#endif
9241
9242 redir_fd = open_exfile(fname, eap->forceit, mode);
9243 vim_free(fname);
9244 }
9245#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009246 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247 {
9248 /* redirect to a register a-z (resp. A-Z for appending) */
9249 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009250 ++arg;
9251 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009253 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009254 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009256 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009258 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009259 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009260 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009263 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009264 if (*arg == '>')
9265 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009266 /* Make register empty when not using @A-@Z and the
9267 * command is valid. */
9268 if (*arg == NUL && !isupper(redir_reg))
9269 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009271 }
9272 if (*arg != NUL)
9273 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009274 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009275 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009276 }
9277 }
9278 else if (*arg == '=' && arg[1] == '>')
9279 {
9280 int append;
9281
9282 /* redirect to a variable */
9283 close_redir();
9284 arg += 2;
9285
9286 if (*arg == '>')
9287 {
9288 ++arg;
9289 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 }
9291 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009292 append = FALSE;
9293
9294 if (var_redir_start(skipwhite(arg), append) == OK)
9295 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 }
9297#endif
9298
9299 /* TODO: redirect to a buffer */
9300
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009302 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009304
9305 /* Make sure redirection is not off. Can happen for cmdline completion
9306 * that indirectly invokes a command to catch its output. */
9307 if (redir_fd != NULL
9308#ifdef FEAT_EVAL
9309 || redir_reg || redir_vname
9310#endif
9311 )
9312 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313}
9314
9315/*
9316 * ":redraw": force redraw
9317 */
9318 static void
9319ex_redraw(eap)
9320 exarg_T *eap;
9321{
9322 int r = RedrawingDisabled;
9323 int p = p_lz;
9324
9325 RedrawingDisabled = 0;
9326 p_lz = FALSE;
9327 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009328 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329#ifdef FEAT_TITLE
9330 if (need_maketitle)
9331 maketitle();
9332#endif
9333 RedrawingDisabled = r;
9334 p_lz = p;
9335
9336 /* Reset msg_didout, so that a message that's there is overwritten. */
9337 msg_didout = FALSE;
9338 msg_col = 0;
9339
Bram Moolenaar56667a52013-07-17 11:54:28 +02009340 /* No need to wait after an intentional redraw. */
9341 need_wait_return = FALSE;
9342
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 out_flush();
9344}
9345
9346/*
9347 * ":redrawstatus": force redraw of status line(s)
9348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 static void
9350ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009351 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352{
9353#if defined(FEAT_WINDOWS)
9354 int r = RedrawingDisabled;
9355 int p = p_lz;
9356
9357 RedrawingDisabled = 0;
9358 p_lz = FALSE;
9359 if (eap->forceit)
9360 status_redraw_all();
9361 else
9362 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009363 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 RedrawingDisabled = r;
9365 p_lz = p;
9366 out_flush();
9367#endif
9368}
9369
9370 static void
9371close_redir()
9372{
9373 if (redir_fd != NULL)
9374 {
9375 fclose(redir_fd);
9376 redir_fd = NULL;
9377 }
9378#ifdef FEAT_EVAL
9379 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009380 if (redir_vname)
9381 {
9382 var_redir_stop();
9383 redir_vname = 0;
9384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385#endif
9386}
9387
9388#if defined(FEAT_SESSION) && defined(USE_CRNL)
9389# define MKSESSION_NL
9390static int mksession_nl = FALSE; /* use NL only in put_eol() */
9391#endif
9392
9393/*
9394 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9395 */
9396 static void
9397ex_mkrc(eap)
9398 exarg_T *eap;
9399{
9400 FILE *fd;
9401 int failed = FALSE;
9402 char_u *fname;
9403#ifdef FEAT_BROWSE
9404 char_u *browseFile = NULL;
9405#endif
9406#ifdef FEAT_SESSION
9407 int view_session = FALSE;
9408 int using_vdir = FALSE; /* using 'viewdir'? */
9409 char_u *viewFile = NULL;
9410 unsigned *flagp;
9411#endif
9412
9413 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9414 {
9415#ifdef FEAT_SESSION
9416 view_session = TRUE;
9417#else
9418 ex_ni(eap);
9419 return;
9420#endif
9421 }
9422
9423#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009424 /* Use the short file name until ":lcd" is used. We also don't use the
9425 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009426 did_lcd = FALSE;
9427
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9429 if (eap->cmdidx == CMD_mkview
9430 && (*eap->arg == NUL
9431 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9432 {
9433 eap->forceit = TRUE;
9434 fname = get_view_file(*eap->arg);
9435 if (fname == NULL)
9436 return;
9437 viewFile = fname;
9438 using_vdir = TRUE;
9439 }
9440 else
9441#endif
9442 if (*eap->arg != NUL)
9443 fname = eap->arg;
9444 else if (eap->cmdidx == CMD_mkvimrc)
9445 fname = (char_u *)VIMRC_FILE;
9446#ifdef FEAT_SESSION
9447 else if (eap->cmdidx == CMD_mksession)
9448 fname = (char_u *)SESSION_FILE;
9449#endif
9450 else
9451 fname = (char_u *)EXRC_FILE;
9452
9453#ifdef FEAT_BROWSE
9454 if (cmdmod.browse)
9455 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009456 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457# ifdef FEAT_SESSION
9458 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9459 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9460# endif
9461 (char_u *)_("Save Setup"),
9462 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9463 if (browseFile == NULL)
9464 goto theend;
9465 fname = browseFile;
9466 eap->forceit = TRUE; /* since dialog already asked */
9467 }
9468#endif
9469
9470#if defined(FEAT_SESSION) && defined(vim_mkdir)
9471 /* When using 'viewdir' may have to create the directory. */
9472 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009473 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474#endif
9475
9476 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9477 if (fd != NULL)
9478 {
9479#ifdef FEAT_SESSION
9480 if (eap->cmdidx == CMD_mkview)
9481 flagp = &vop_flags;
9482 else
9483 flagp = &ssop_flags;
9484#endif
9485
9486#ifdef MKSESSION_NL
9487 /* "unix" in 'sessionoptions': use NL line separator */
9488 if (view_session && (*flagp & SSOP_UNIX))
9489 mksession_nl = TRUE;
9490#endif
9491
9492 /* Write the version command for :mkvimrc */
9493 if (eap->cmdidx == CMD_mkvimrc)
9494 (void)put_line(fd, "version 6.0");
9495
9496#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009497 if (eap->cmdidx == CMD_mksession)
9498 {
9499 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9500 failed = TRUE;
9501 }
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 if (eap->cmdidx != CMD_mkview)
9504#endif
9505 {
9506 /* Write setting 'compatible' first, because it has side effects.
9507 * For that same reason only do it when needed. */
9508 if (p_cp)
9509 (void)put_line(fd, "if !&cp | set cp | endif");
9510 else
9511 (void)put_line(fd, "if &cp | set nocp | endif");
9512 }
9513
9514#ifdef FEAT_SESSION
9515 if (!view_session
9516 || (eap->cmdidx == CMD_mksession
9517 && (*flagp & SSOP_OPTIONS)))
9518#endif
9519 failed |= (makemap(fd, NULL) == FAIL
9520 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9521
9522#ifdef FEAT_SESSION
9523 if (!failed && view_session)
9524 {
9525 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9526 failed = TRUE;
9527 if (eap->cmdidx == CMD_mksession)
9528 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009529 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530
Bram Moolenaard9462e32011-04-11 21:35:11 +02009531 dirnow = alloc(MAXPATHL);
9532 if (dirnow == NULL)
9533 failed = TRUE;
9534 else
9535 {
9536 /*
9537 * Change to session file's dir.
9538 */
9539 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009541 *dirnow = NUL;
9542 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9543 {
9544 if (vim_chdirfile(fname) == OK)
9545 shorten_fnames(TRUE);
9546 }
9547 else if (*dirnow != NUL
9548 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9549 {
9550 if (mch_chdir((char *)globaldir) == 0)
9551 shorten_fnames(TRUE);
9552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553
Bram Moolenaard9462e32011-04-11 21:35:11 +02009554 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555
Bram Moolenaard9462e32011-04-11 21:35:11 +02009556 /* restore original dir */
9557 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009559 {
9560 if (mch_chdir((char *)dirnow) != 0)
9561 EMSG(_(e_prev_dir));
9562 shorten_fnames(TRUE);
9563 }
9564 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 }
9566 }
9567 else
9568 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009569 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9570 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 }
9572 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9573 == FAIL)
9574 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009575 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9576 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009577 if (eap->cmdidx == CMD_mksession)
9578 {
9579 if (put_line(fd, "unlet SessionLoad") == FAIL)
9580 failed = TRUE;
9581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 }
9583#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009584 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9585 failed = TRUE;
9586
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 failed |= fclose(fd);
9588
9589 if (failed)
9590 EMSG(_(e_write));
9591#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9592 else if (eap->cmdidx == CMD_mksession)
9593 {
9594 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009595 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
Bram Moolenaard9462e32011-04-11 21:35:11 +02009597 tbuf = alloc(MAXPATHL);
9598 if (tbuf != NULL)
9599 {
9600 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9601 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9602 vim_free(tbuf);
9603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 }
9605#endif
9606#ifdef MKSESSION_NL
9607 mksession_nl = FALSE;
9608#endif
9609 }
9610
9611#ifdef FEAT_BROWSE
9612theend:
9613 vim_free(browseFile);
9614#endif
9615#ifdef FEAT_SESSION
9616 vim_free(viewFile);
9617#endif
9618}
9619
Bram Moolenaardf177f62005-02-22 08:39:57 +00009620#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9621 || defined(PROTO)
9622 int
9623vim_mkdir_emsg(name, prot)
9624 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009625 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009626{
9627 if (vim_mkdir(name, prot) != 0)
9628 {
9629 EMSG2(_("E739: Cannot create directory: %s"), name);
9630 return FAIL;
9631 }
9632 return OK;
9633}
9634#endif
9635
Bram Moolenaar071d4272004-06-13 20:20:40 +00009636/*
9637 * Open a file for writing for an Ex command, with some checks.
9638 * Return file descriptor, or NULL on failure.
9639 */
9640 FILE *
9641open_exfile(fname, forceit, mode)
9642 char_u *fname;
9643 int forceit;
9644 char *mode; /* "w" for create new file or "a" for append */
9645{
9646 FILE *fd;
9647
9648#ifdef UNIX
9649 /* with Unix it is possible to open a directory */
9650 if (mch_isdir(fname))
9651 {
9652 EMSG2(_(e_isadir2), fname);
9653 return NULL;
9654 }
9655#endif
9656 if (!forceit && *mode != 'a' && vim_fexists(fname))
9657 {
9658 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9659 return NULL;
9660 }
9661
9662 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9663 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9664
9665 return fd;
9666}
9667
9668/*
9669 * ":mark" and ":k".
9670 */
9671 static void
9672ex_mark(eap)
9673 exarg_T *eap;
9674{
9675 pos_T pos;
9676
9677 if (*eap->arg == NUL) /* No argument? */
9678 EMSG(_(e_argreq));
9679 else if (eap->arg[1] != NUL) /* more than one character? */
9680 EMSG(_(e_trailing));
9681 else
9682 {
9683 pos = curwin->w_cursor; /* save curwin->w_cursor */
9684 curwin->w_cursor.lnum = eap->line2;
9685 beginline(BL_WHITE | BL_FIX);
9686 if (setmark(*eap->arg) == FAIL) /* set mark */
9687 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9688 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9689 }
9690}
9691
9692/*
9693 * Update w_topline, w_leftcol and the cursor position.
9694 */
9695 void
9696update_topline_cursor()
9697{
9698 check_cursor(); /* put cursor on valid line */
9699 update_topline();
9700 if (!curwin->w_p_wrap)
9701 validate_cursor();
9702 update_curswant();
9703}
9704
9705#ifdef FEAT_EX_EXTRA
9706/*
9707 * ":normal[!] {commands}": Execute normal mode commands.
9708 */
9709 static void
9710ex_normal(eap)
9711 exarg_T *eap;
9712{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 int save_msg_scroll = msg_scroll;
9714 int save_restart_edit = restart_edit;
9715 int save_msg_didout = msg_didout;
9716 int save_State = State;
9717 tasave_T tabuf;
9718 int save_insertmode = p_im;
9719 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009720 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721#ifdef FEAT_MBYTE
9722 char_u *arg = NULL;
9723 int l;
9724 char_u *p;
9725#endif
9726
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009727 if (ex_normal_lock > 0)
9728 {
9729 EMSG(_(e_secure));
9730 return;
9731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 if (ex_normal_busy >= p_mmd)
9733 {
9734 EMSG(_("E192: Recursive use of :normal too deep"));
9735 return;
9736 }
9737 ++ex_normal_busy;
9738
9739 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9740 restart_edit = 0; /* don't go to Insert mode */
9741 p_im = FALSE; /* don't use 'insertmode' */
9742
9743#ifdef FEAT_MBYTE
9744 /*
9745 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9746 * this for the K_SPECIAL leading byte, otherwise special keys will not
9747 * work.
9748 */
9749 if (has_mbyte)
9750 {
9751 int len = 0;
9752
9753 /* Count the number of characters to be escaped. */
9754 for (p = eap->arg; *p != NUL; ++p)
9755 {
9756# ifdef FEAT_GUI
9757 if (*p == CSI) /* leadbyte CSI */
9758 len += 2;
9759# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009760 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9762# ifdef FEAT_GUI
9763 || *p == CSI
9764# endif
9765 )
9766 len += 2;
9767 }
9768 if (len > 0)
9769 {
9770 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9771 if (arg != NULL)
9772 {
9773 len = 0;
9774 for (p = eap->arg; *p != NUL; ++p)
9775 {
9776 arg[len++] = *p;
9777# ifdef FEAT_GUI
9778 if (*p == CSI)
9779 {
9780 arg[len++] = KS_EXTRA;
9781 arg[len++] = (int)KE_CSI;
9782 }
9783# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009784 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 {
9786 arg[len++] = *++p;
9787 if (*p == K_SPECIAL)
9788 {
9789 arg[len++] = KS_SPECIAL;
9790 arg[len++] = KE_FILLER;
9791 }
9792# ifdef FEAT_GUI
9793 else if (*p == CSI)
9794 {
9795 arg[len++] = KS_EXTRA;
9796 arg[len++] = (int)KE_CSI;
9797 }
9798# endif
9799 }
9800 arg[len] = NUL;
9801 }
9802 }
9803 }
9804 }
9805#endif
9806
9807 /*
9808 * Save the current typeahead. This is required to allow using ":normal"
9809 * from an event handler and makes sure we don't hang when the argument
9810 * ends with half a command.
9811 */
9812 save_typeahead(&tabuf);
9813 if (tabuf.typebuf_valid)
9814 {
9815 /*
9816 * Repeat the :normal command for each line in the range. When no
9817 * range given, execute it just once, without positioning the cursor
9818 * first.
9819 */
9820 do
9821 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822 if (eap->addr_count != 0)
9823 {
9824 curwin->w_cursor.lnum = eap->line1++;
9825 curwin->w_cursor.col = 0;
9826 }
9827
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009828 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829#ifdef FEAT_MBYTE
9830 arg != NULL ? arg :
9831#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009832 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833 }
9834 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9835 }
9836
9837 /* Might not return to the main loop when in an event handler. */
9838 update_topline_cursor();
9839
9840 /* Restore the previous typeahead. */
9841 restore_typeahead(&tabuf);
9842
9843 --ex_normal_busy;
9844 msg_scroll = save_msg_scroll;
9845 restart_edit = save_restart_edit;
9846 p_im = save_insertmode;
9847 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009848 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9850
9851 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +01009852 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +01009854#ifdef FEAT_MOUSE
9855 setmouse();
9856#endif
9857#ifdef CURSOR_SHAPE
9858 ui_cursor_shape(); /* may show different cursor shape */
9859#endif
9860
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861#ifdef FEAT_MBYTE
9862 vim_free(arg);
9863#endif
9864}
9865
9866/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009867 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 */
9869 static void
9870ex_startinsert(eap)
9871 exarg_T *eap;
9872{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009873 if (eap->forceit)
9874 {
9875 coladvance((colnr_T)MAXCOL);
9876 curwin->w_curswant = MAXCOL;
9877 curwin->w_set_curswant = FALSE;
9878 }
9879
Bram Moolenaara40c5002005-01-09 21:16:21 +00009880 /* Ignore the command when already in Insert mode. Inserting an
9881 * expression register that invokes a function can do this. */
9882 if (State & INSERT)
9883 return;
9884
Bram Moolenaar64969662005-12-14 21:59:55 +00009885 if (eap->cmdidx == CMD_startinsert)
9886 restart_edit = 'a';
9887 else if (eap->cmdidx == CMD_startreplace)
9888 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009890 restart_edit = 'V';
9891
9892 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009894 if (eap->cmdidx == CMD_startinsert)
9895 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 curwin->w_curswant = 0; /* avoid MAXCOL */
9897 }
9898}
9899
9900/*
9901 * ":stopinsert"
9902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 static void
9904ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009905 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906{
9907 restart_edit = 0;
9908 stop_insert_mode = TRUE;
9909}
9910#endif
9911
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009912#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9913/*
9914 * Execute normal mode command "cmd".
9915 * "remap" can be REMAP_NONE or REMAP_YES.
9916 */
9917 void
9918exec_normal_cmd(cmd, remap, silent)
9919 char_u *cmd;
9920 int remap;
9921 int silent;
9922{
9923 oparg_T oa;
9924
9925 /*
9926 * Stuff the argument into the typeahead buffer.
9927 * Execute normal_cmd() until there is no typeahead left.
9928 */
9929 clear_oparg(&oa);
9930 finish_op = FALSE;
9931 ins_typebuf(cmd, remap, 0, TRUE, silent);
9932 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9933 && !got_int)
9934 {
9935 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009936 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009937 }
9938}
9939#endif
9940
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941#ifdef FEAT_FIND_ID
9942 static void
9943ex_checkpath(eap)
9944 exarg_T *eap;
9945{
9946 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9947 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9948 (linenr_T)1, (linenr_T)MAXLNUM);
9949}
9950
9951#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9952/*
9953 * ":psearch"
9954 */
9955 static void
9956ex_psearch(eap)
9957 exarg_T *eap;
9958{
9959 g_do_tagpreview = p_pvh;
9960 ex_findpat(eap);
9961 g_do_tagpreview = 0;
9962}
9963#endif
9964
9965 static void
9966ex_findpat(eap)
9967 exarg_T *eap;
9968{
9969 int whole = TRUE;
9970 long n;
9971 char_u *p;
9972 int action;
9973
9974 switch (cmdnames[eap->cmdidx].cmd_name[2])
9975 {
9976 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9977 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9978 action = ACTION_GOTO;
9979 else
9980 action = ACTION_SHOW;
9981 break;
9982 case 'i': /* ":ilist" and ":dlist" */
9983 action = ACTION_SHOW_ALL;
9984 break;
9985 case 'u': /* ":ijump" and ":djump" */
9986 action = ACTION_GOTO;
9987 break;
9988 default: /* ":isplit" and ":dsplit" */
9989 action = ACTION_SPLIT;
9990 break;
9991 }
9992
9993 n = 1;
9994 if (vim_isdigit(*eap->arg)) /* get count */
9995 {
9996 n = getdigits(&eap->arg);
9997 eap->arg = skipwhite(eap->arg);
9998 }
9999 if (*eap->arg == '/') /* Match regexp, not just whole words */
10000 {
10001 whole = FALSE;
10002 ++eap->arg;
10003 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10004 if (*p)
10005 {
10006 *p++ = NUL;
10007 p = skipwhite(p);
10008
10009 /* Check for trailing illegal characters */
10010 if (!ends_excmd(*p))
10011 eap->errmsg = e_trailing;
10012 else
10013 eap->nextcmd = check_nextcmd(p);
10014 }
10015 }
10016 if (!eap->skip)
10017 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10018 whole, !eap->forceit,
10019 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10020 n, action, eap->line1, eap->line2);
10021}
10022#endif
10023
10024#ifdef FEAT_WINDOWS
10025
10026# ifdef FEAT_QUICKFIX
10027/*
10028 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10029 */
10030 static void
10031ex_ptag(eap)
10032 exarg_T *eap;
10033{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010034 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10036}
10037
10038/*
10039 * ":pedit"
10040 */
10041 static void
10042ex_pedit(eap)
10043 exarg_T *eap;
10044{
10045 win_T *curwin_save = curwin;
10046
10047 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010048 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049 keep_help_flag = curwin_save->w_buffer->b_help;
10050 do_exedit(eap, NULL);
10051 keep_help_flag = FALSE;
10052 if (curwin != curwin_save && win_valid(curwin_save))
10053 {
10054 /* Return cursor to where we were */
10055 validate_cursor();
10056 redraw_later(VALID);
10057 win_enter(curwin_save, TRUE);
10058 }
10059 g_do_tagpreview = 0;
10060}
10061# endif
10062
10063/*
10064 * ":stag", ":stselect" and ":stjump".
10065 */
10066 static void
10067ex_stag(eap)
10068 exarg_T *eap;
10069{
10070 postponed_split = -1;
10071 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010072 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10074 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010075 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076}
10077#endif
10078
10079/*
10080 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10081 */
10082 static void
10083ex_tag(eap)
10084 exarg_T *eap;
10085{
10086 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10087}
10088
10089 static void
10090ex_tag_cmd(eap, name)
10091 exarg_T *eap;
10092 char_u *name;
10093{
10094 int cmd;
10095
10096 switch (name[1])
10097 {
10098 case 'j': cmd = DT_JUMP; /* ":tjump" */
10099 break;
10100 case 's': cmd = DT_SELECT; /* ":tselect" */
10101 break;
10102 case 'p': cmd = DT_PREV; /* ":tprevious" */
10103 break;
10104 case 'N': cmd = DT_PREV; /* ":tNext" */
10105 break;
10106 case 'n': cmd = DT_NEXT; /* ":tnext" */
10107 break;
10108 case 'o': cmd = DT_POP; /* ":pop" */
10109 break;
10110 case 'f': /* ":tfirst" */
10111 case 'r': cmd = DT_FIRST; /* ":trewind" */
10112 break;
10113 case 'l': cmd = DT_LAST; /* ":tlast" */
10114 break;
10115 default: /* ":tag" */
10116#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010117 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 {
10119 do_cstag(eap);
10120 return;
10121 }
10122#endif
10123 cmd = DT_TAG;
10124 break;
10125 }
10126
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010127 if (name[0] == 'l')
10128 {
10129#ifndef FEAT_QUICKFIX
10130 ex_ni(eap);
10131 return;
10132#else
10133 cmd = DT_LTAG;
10134#endif
10135 }
10136
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10138 eap->forceit, TRUE);
10139}
10140
10141/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010142 * Check "str" for starting with a special cmdline variable.
10143 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10144 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10145 */
10146 int
10147find_cmdline_var(src, usedlen)
10148 char_u *src;
10149 int *usedlen;
10150{
10151 int len;
10152 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010153 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010154 "%",
10155#define SPEC_PERC 0
10156 "#",
10157#define SPEC_HASH 1
10158 "<cword>", /* cursor word */
10159#define SPEC_CWORD 2
10160 "<cWORD>", /* cursor WORD */
10161#define SPEC_CCWORD 3
10162 "<cfile>", /* cursor path name */
10163#define SPEC_CFILE 4
10164 "<sfile>", /* ":so" file name */
10165#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010166 "<slnum>", /* ":so" file line number */
10167#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010168#ifdef FEAT_AUTOCMD
10169 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010170# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010171 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010172# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010173 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010174# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010175#endif
10176#ifdef FEAT_CLIENTSERVER
10177 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010178# ifdef FEAT_AUTOCMD
10179# define SPEC_CLIENT 10
10180# else
10181# define SPEC_CLIENT 7
10182# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010183#endif
10184 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010185
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010186 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010187 {
10188 len = (int)STRLEN(spec_str[i]);
10189 if (STRNCMP(src, spec_str[i], len) == 0)
10190 {
10191 *usedlen = len;
10192 return i;
10193 }
10194 }
10195 return -1;
10196}
10197
10198/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010199 * Evaluate cmdline variables.
10200 *
10201 * change '%' to curbuf->b_ffname
10202 * '#' to curwin->w_altfile
10203 * '<cword>' to word under the cursor
10204 * '<cWORD>' to WORD under the cursor
10205 * '<cfile>' to path name under the cursor
10206 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010207 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 * '<afile>' to file name for autocommand
10209 * '<abuf>' to buffer number for autocommand
10210 * '<amatch>' to matching name for autocommand
10211 *
10212 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10213 * "" for error without a message) and NULL is returned.
10214 * Returns an allocated string if a valid match was found.
10215 * Returns NULL if no match was found. "usedlen" then still contains the
10216 * number of characters to skip.
10217 */
10218 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +000010219eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010221 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 int *usedlen; /* characters after src that are used */
10223 linenr_T *lnump; /* line number for :e command, or NULL */
10224 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010225 int *escaped; /* return value has escaped white space (can
10226 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227{
10228 int i;
10229 char_u *s;
10230 char_u *result;
10231 char_u *resultbuf = NULL;
10232 int resultlen;
10233 buf_T *buf;
10234 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10235 int spec_idx;
10236#ifdef FEAT_MODIFY_FNAME
10237 int skip_mod = FALSE;
10238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240
10241 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010242 if (escaped != NULL)
10243 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244
10245 /*
10246 * Check if there is something to do.
10247 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010248 spec_idx = find_cmdline_var(src, usedlen);
10249 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250 {
10251 *usedlen = 1;
10252 return NULL;
10253 }
10254
10255 /*
10256 * Skip when preceded with a backslash "\%" and "\#".
10257 * Note: In "\\%" the % is also not recognized!
10258 */
10259 if (src > srcstart && src[-1] == '\\')
10260 {
10261 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010262 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 return NULL;
10264 }
10265
10266 /*
10267 * word or WORD under cursor
10268 */
10269 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10270 {
10271 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10272 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10273 if (resultlen == 0)
10274 {
10275 *errormsg = (char_u *)"";
10276 return NULL;
10277 }
10278 }
10279
10280 /*
10281 * '#': Alternate file name
10282 * '%': Current file name
10283 * File name under the cursor
10284 * File name for autocommand
10285 * and following modifiers
10286 */
10287 else
10288 {
10289 switch (spec_idx)
10290 {
10291 case SPEC_PERC: /* '%': current file */
10292 if (curbuf->b_fname == NULL)
10293 {
10294 result = (char_u *)"";
10295 valid = 0; /* Must have ":p:h" to be valid */
10296 }
10297 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 break;
10300
10301 case SPEC_HASH: /* '#' or "#99": alternate file */
10302 if (src[1] == '#') /* "##": the argument list */
10303 {
10304 result = arg_all();
10305 resultbuf = result;
10306 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010307 if (escaped != NULL)
10308 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309#ifdef FEAT_MODIFY_FNAME
10310 skip_mod = TRUE;
10311#endif
10312 break;
10313 }
10314 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010315 if (*s == '<') /* "#<99" uses v:oldfiles */
10316 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 i = (int)getdigits(&s);
10318 *usedlen = (int)(s - src); /* length of what we expand */
10319
Bram Moolenaard812df62008-11-09 12:46:09 +000010320 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010322 if (*usedlen < 2)
10323 {
10324 /* Should we give an error message for #<text? */
10325 *usedlen = 1;
10326 return NULL;
10327 }
10328#ifdef FEAT_EVAL
10329 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10330 (long)i);
10331 if (result == NULL)
10332 {
10333 *errormsg = (char_u *)"";
10334 return NULL;
10335 }
10336#else
10337 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 }
10341 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010342 {
10343 buf = buflist_findnr(i);
10344 if (buf == NULL)
10345 {
10346 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10347 return NULL;
10348 }
10349 if (lnump != NULL)
10350 *lnump = ECMD_LAST;
10351 if (buf->b_fname == NULL)
10352 {
10353 result = (char_u *)"";
10354 valid = 0; /* Must have ":p:h" to be valid */
10355 }
10356 else
10357 result = buf->b_fname;
10358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 break;
10360
10361#ifdef FEAT_SEARCHPATH
10362 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010363 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 if (result == NULL)
10365 {
10366 *errormsg = (char_u *)"";
10367 return NULL;
10368 }
10369 resultbuf = result; /* remember allocated string */
10370 break;
10371#endif
10372
10373#ifdef FEAT_AUTOCMD
10374 case SPEC_AFILE: /* file name for autocommand */
10375 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010376 if (result != NULL && !autocmd_fname_full)
10377 {
10378 /* Still need to turn the fname into a full path. It is
10379 * postponed to avoid a delay when <afile> is not used. */
10380 autocmd_fname_full = TRUE;
10381 result = FullName_save(autocmd_fname, FALSE);
10382 vim_free(autocmd_fname);
10383 autocmd_fname = result;
10384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 if (result == NULL)
10386 {
10387 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10388 return NULL;
10389 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010390 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 break;
10392
10393 case SPEC_ABUF: /* buffer number for autocommand */
10394 if (autocmd_bufnr <= 0)
10395 {
10396 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10397 return NULL;
10398 }
10399 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10400 result = strbuf;
10401 break;
10402
10403 case SPEC_AMATCH: /* match name for autocommand */
10404 result = autocmd_match;
10405 if (result == NULL)
10406 {
10407 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10408 return NULL;
10409 }
10410 break;
10411
10412#endif
10413 case SPEC_SFILE: /* file name for ":so" command */
10414 result = sourcing_name;
10415 if (result == NULL)
10416 {
10417 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10418 return NULL;
10419 }
10420 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010421 case SPEC_SLNUM: /* line in file for ":so" command */
10422 if (sourcing_name == NULL || sourcing_lnum == 0)
10423 {
10424 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10425 return NULL;
10426 }
10427 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10428 result = strbuf;
10429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430#if defined(FEAT_CLIENTSERVER)
10431 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010432 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10433 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 result = strbuf;
10435 break;
10436#endif
10437 }
10438
10439 resultlen = (int)STRLEN(result); /* length of new string */
10440 if (src[*usedlen] == '<') /* remove the file name extension */
10441 {
10442 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 resultlen = (int)(s - result);
10445 }
10446#ifdef FEAT_MODIFY_FNAME
10447 else if (!skip_mod)
10448 {
10449 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10450 &resultlen);
10451 if (result == NULL)
10452 {
10453 *errormsg = (char_u *)"";
10454 return NULL;
10455 }
10456 }
10457#endif
10458 }
10459
10460 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10461 {
10462 if (valid != VALID_HEAD + VALID_PATH)
10463 /* xgettext:no-c-format */
10464 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10465 else
10466 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10467 result = NULL;
10468 }
10469 else
10470 result = vim_strnsave(result, resultlen);
10471 vim_free(resultbuf);
10472 return result;
10473}
10474
10475/*
10476 * Concatenate all files in the argument list, separated by spaces, and return
10477 * it in one allocated string.
10478 * Spaces and backslashes in the file names are escaped with a backslash.
10479 * Returns NULL when out of memory.
10480 */
10481 static char_u *
10482arg_all()
10483{
10484 int len;
10485 int idx;
10486 char_u *retval = NULL;
10487 char_u *p;
10488
10489 /*
10490 * Do this loop two times:
10491 * first time: compute the total length
10492 * second time: concatenate the names
10493 */
10494 for (;;)
10495 {
10496 len = 0;
10497 for (idx = 0; idx < ARGCOUNT; ++idx)
10498 {
10499 p = alist_name(&ARGLIST[idx]);
10500 if (p != NULL)
10501 {
10502 if (len > 0)
10503 {
10504 /* insert a space in between names */
10505 if (retval != NULL)
10506 retval[len] = ' ';
10507 ++len;
10508 }
10509 for ( ; *p != NUL; ++p)
10510 {
10511 if (*p == ' ' || *p == '\\')
10512 {
10513 /* insert a backslash */
10514 if (retval != NULL)
10515 retval[len] = '\\';
10516 ++len;
10517 }
10518 if (retval != NULL)
10519 retval[len] = *p;
10520 ++len;
10521 }
10522 }
10523 }
10524
10525 /* second time: break here */
10526 if (retval != NULL)
10527 {
10528 retval[len] = NUL;
10529 break;
10530 }
10531
10532 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010533 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 if (retval == NULL)
10535 break;
10536 }
10537
10538 return retval;
10539}
10540
10541#if defined(FEAT_AUTOCMD) || defined(PROTO)
10542/*
10543 * Expand the <sfile> string in "arg".
10544 *
10545 * Returns an allocated string, or NULL for any error.
10546 */
10547 char_u *
10548expand_sfile(arg)
10549 char_u *arg;
10550{
10551 char_u *errormsg;
10552 int len;
10553 char_u *result;
10554 char_u *newres;
10555 char_u *repl;
10556 int srclen;
10557 char_u *p;
10558
10559 result = vim_strsave(arg);
10560 if (result == NULL)
10561 return NULL;
10562
10563 for (p = result; *p; )
10564 {
10565 if (STRNCMP(p, "<sfile>", 7) != 0)
10566 ++p;
10567 else
10568 {
10569 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010570 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 if (errormsg != NULL)
10572 {
10573 if (*errormsg)
10574 emsg(errormsg);
10575 vim_free(result);
10576 return NULL;
10577 }
10578 if (repl == NULL) /* no match (cannot happen) */
10579 {
10580 p += srclen;
10581 continue;
10582 }
10583 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10584 newres = alloc(len);
10585 if (newres == NULL)
10586 {
10587 vim_free(repl);
10588 vim_free(result);
10589 return NULL;
10590 }
10591 mch_memmove(newres, result, (size_t)(p - result));
10592 STRCPY(newres + (p - result), repl);
10593 len = (int)STRLEN(newres);
10594 STRCAT(newres, p + srclen);
10595 vim_free(repl);
10596 vim_free(result);
10597 result = newres;
10598 p = newres + len; /* continue after the match */
10599 }
10600 }
10601
10602 return result;
10603}
10604#endif
10605
10606#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010607static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10608 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10610static frame_T *ses_skipframe __ARGS((frame_T *fr));
10611static int ses_do_frame __ARGS((frame_T *fr));
10612static int ses_do_win __ARGS((win_T *wp));
10613static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10614static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10615static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10616
10617/*
10618 * Write openfile commands for the current buffers to an .exrc file.
10619 * Return FAIL on error, OK otherwise.
10620 */
10621 static int
10622makeopens(fd, dirnow)
10623 FILE *fd;
10624 char_u *dirnow; /* Current directory name */
10625{
10626 buf_T *buf;
10627 int only_save_windows = TRUE;
10628 int nr;
10629 int cnr = 1;
10630 int restore_size = TRUE;
10631 win_T *wp;
10632 char_u *sname;
10633 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010634 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010635 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010636 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010637 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010638 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010639 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640
10641 if (ssop_flags & SSOP_BUFFERS)
10642 only_save_windows = FALSE; /* Save ALL buffers */
10643
10644 /*
10645 * Begin by setting the this_session variable, and then other
10646 * sessionable variables.
10647 */
10648#ifdef FEAT_EVAL
10649 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10650 return FAIL;
10651 if (ssop_flags & SSOP_GLOBALS)
10652 if (store_session_globals(fd) == FAIL)
10653 return FAIL;
10654#endif
10655
10656 /*
10657 * Close all windows but one.
10658 */
10659 if (put_line(fd, "silent only") == FAIL)
10660 return FAIL;
10661
10662 /*
10663 * Now a :cd command to the session directory or the current directory
10664 */
10665 if (ssop_flags & SSOP_SESDIR)
10666 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010667 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10668 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 return FAIL;
10670 }
10671 else if (ssop_flags & SSOP_CURDIR)
10672 {
10673 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10674 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010675 || fputs("cd ", fd) < 0
10676 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10677 || put_eol(fd) == FAIL)
10678 {
10679 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 vim_free(sname);
10683 }
10684
10685 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010686 * If there is an empty, unnamed buffer we will wipe it out later.
10687 * Remember the buffer number.
10688 */
10689 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10690 return FAIL;
10691 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10692 return FAIL;
10693 if (put_line(fd, "endif") == FAIL)
10694 return FAIL;
10695
10696 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 * Now save the current files, current buffer first.
10698 */
10699 if (put_line(fd, "set shortmess=aoO") == FAIL)
10700 return FAIL;
10701
10702 /* Now put the other buffers into the buffer list */
10703 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10704 {
10705 if (!(only_save_windows && buf->b_nwindows == 0)
10706 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10707 && buf->b_fname != NULL
10708 && buf->b_p_bl)
10709 {
10710 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10711 : buf->b_wininfo->wi_fpos.lnum) < 0
10712 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10713 return FAIL;
10714 }
10715 }
10716
10717 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010718 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10720 return FAIL;
10721
10722 if (ssop_flags & SSOP_RESIZE)
10723 {
10724 /* Note: after the restore we still check it worked!*/
10725 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10726 || put_eol(fd) == FAIL)
10727 return FAIL;
10728 }
10729
10730#ifdef FEAT_GUI
10731 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10732 {
10733 int x, y;
10734
10735 if (gui_mch_get_winpos(&x, &y) == OK)
10736 {
10737 /* Note: after the restore we still check it worked!*/
10738 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10739 return FAIL;
10740 }
10741 }
10742#endif
10743
10744 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010745 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10746 * will be displayed when creating the next tab. That resizes the windows
10747 * in the first tab, which may cause problems. Set 'showtabline' to 2
10748 * temporarily to avoid that.
10749 */
10750 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10751 {
10752 if (put_line(fd, "set stal=2") == FAIL)
10753 return FAIL;
10754 restore_stal = TRUE;
10755 }
10756
10757 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010758 * May repeat putting Windows for each tab, when "tabpages" is in
10759 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010760 * Don't use goto_tabpage(), it may change directory and trigger
10761 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010762 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010763 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010764 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010765 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010767 int need_tabnew = FALSE;
10768
Bram Moolenaar18144c82006-04-12 21:52:12 +000010769 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010771 tabpage_T *tp = find_tabpage(tabnr);
10772
10773 if (tp == NULL)
10774 break; /* done all tab pages */
10775 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010776 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010777 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010778 tab_topframe = topframe;
10779 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010780 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010781 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010782 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010783 tab_topframe = tp->tp_topframe;
10784 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010785 if (tabnr > 1)
10786 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788
Bram Moolenaar18144c82006-04-12 21:52:12 +000010789 /*
10790 * Before creating the window layout, try loading one file. If this
10791 * is aborted we don't end up with a number of useless windows.
10792 * This may have side effects! (e.g., compressed or network file).
10793 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010794 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010795 {
10796 if (ses_do_win(wp)
10797 && wp->w_buffer->b_ffname != NULL
10798 && !wp->w_buffer->b_help
10799#ifdef FEAT_QUICKFIX
10800 && !bt_nofile(wp->w_buffer)
10801#endif
10802 )
10803 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010804 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010805 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10806 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010807 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010808 if (!wp->w_arg_idx_invalid)
10809 edited_win = wp;
10810 break;
10811 }
10812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010814 /* If no file got edited create an empty tab page. */
10815 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10816 return FAIL;
10817
Bram Moolenaar18144c82006-04-12 21:52:12 +000010818 /*
10819 * Save current window layout.
10820 */
10821 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010822 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010823 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010825 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10826 return FAIL;
10827 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10828 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829
Bram Moolenaar18144c82006-04-12 21:52:12 +000010830 /*
10831 * Check if window sizes can be restored (no windows omitted).
10832 * Remember the window number of the current window after restoring.
10833 */
10834 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010835 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010836 {
10837 if (ses_do_win(wp))
10838 ++nr;
10839 else
10840 restore_size = FALSE;
10841 if (curwin == wp)
10842 cnr = nr;
10843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844
Bram Moolenaar18144c82006-04-12 21:52:12 +000010845 /* Go to the first window. */
10846 if (put_line(fd, "wincmd t") == FAIL)
10847 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010848
Bram Moolenaar18144c82006-04-12 21:52:12 +000010849 /*
10850 * If more than one window, see if sizes can be restored.
10851 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10852 * resized when moving between windows.
10853 * Do this before restoring the view, so that the topline and the
10854 * cursor can be set. This is done again below.
10855 */
10856 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10857 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010858 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010859 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860
Bram Moolenaar18144c82006-04-12 21:52:12 +000010861 /*
10862 * Restore the view of the window (options, file, cursor, etc.).
10863 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010864 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010865 {
10866 if (!ses_do_win(wp))
10867 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010868 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10869 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010870 return FAIL;
10871 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10872 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010873 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010874 }
10875
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010876 /* The argument index in the first tab page is zero, need to set it in
10877 * each window. For further tab pages it's the window where we do
10878 * "tabedit". */
10879 cur_arg_idx = next_arg_idx;
10880
Bram Moolenaar18144c82006-04-12 21:52:12 +000010881 /*
10882 * Restore cursor to the current window if it's not the first one.
10883 */
10884 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10885 || put_eol(fd) == FAIL))
10886 return FAIL;
10887
10888 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010889 * Restore window sizes again after jumping around in windows, because
10890 * the current window has a minimum size while others may not.
10891 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010892 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010893 return FAIL;
10894
Bram Moolenaar18144c82006-04-12 21:52:12 +000010895 /* Don't continue in another tab page when doing only the current one
10896 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010897 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010898 break;
10899 }
10900
10901 if (ssop_flags & SSOP_TABPAGES)
10902 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010903 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10904 || put_eol(fd) == FAIL)
10905 return FAIL;
10906 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010907 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10908 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010909
Bram Moolenaar9c102382006-05-03 21:26:49 +000010910 /*
10911 * Wipe out an empty unnamed buffer we started in.
10912 */
10913 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10914 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010915 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010916 return FAIL;
10917 if (put_line(fd, "endif") == FAIL)
10918 return FAIL;
10919 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10920 return FAIL;
10921
10922 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10923 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10924 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926
10927 /*
10928 * Lastly, execute the x.vim file if it exists.
10929 */
10930 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10931 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010932 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 || put_line(fd, "endif") == FAIL)
10934 return FAIL;
10935
10936 return OK;
10937}
10938
10939 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010940ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010941 FILE *fd;
10942 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010943 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944{
10945 int n = 0;
10946 win_T *wp;
10947
10948 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10949 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010950 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 {
10952 if (!ses_do_win(wp))
10953 continue;
10954 ++n;
10955
10956 /* restore height when not full height */
10957 if (wp->w_height + wp->w_status_height < topframe->fr_height
10958 && (fprintf(fd,
10959 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10960 n, (long)wp->w_height, Rows / 2, Rows) < 0
10961 || put_eol(fd) == FAIL))
10962 return FAIL;
10963
10964 /* restore width when not full width */
10965 if (wp->w_width < Columns && (fprintf(fd,
10966 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10967 n, (long)wp->w_width, Columns / 2, Columns) < 0
10968 || put_eol(fd) == FAIL))
10969 return FAIL;
10970 }
10971 }
10972 else
10973 {
10974 /* Just equalise window sizes */
10975 if (put_line(fd, "wincmd =") == FAIL)
10976 return FAIL;
10977 }
10978 return OK;
10979}
10980
10981/*
10982 * Write commands to "fd" to recursively create windows for frame "fr",
10983 * horizontally and vertically split.
10984 * After the commands the last window in the frame is the current window.
10985 * Returns FAIL when writing the commands to "fd" fails.
10986 */
10987 static int
10988ses_win_rec(fd, fr)
10989 FILE *fd;
10990 frame_T *fr;
10991{
10992 frame_T *frc;
10993 int count = 0;
10994
10995 if (fr->fr_layout != FR_LEAF)
10996 {
10997 /* Find first frame that's not skipped and then create a window for
10998 * each following one (first frame is already there). */
10999 frc = ses_skipframe(fr->fr_child);
11000 if (frc != NULL)
11001 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11002 {
11003 /* Make window as big as possible so that we have lots of room
11004 * to split. */
11005 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11006 || put_line(fd, fr->fr_layout == FR_COL
11007 ? "split" : "vsplit") == FAIL)
11008 return FAIL;
11009 ++count;
11010 }
11011
11012 /* Go back to the first window. */
11013 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11014 ? "%dwincmd k" : "%dwincmd h", count) < 0
11015 || put_eol(fd) == FAIL))
11016 return FAIL;
11017
11018 /* Recursively create frames/windows in each window of this column or
11019 * row. */
11020 frc = ses_skipframe(fr->fr_child);
11021 while (frc != NULL)
11022 {
11023 ses_win_rec(fd, frc);
11024 frc = ses_skipframe(frc->fr_next);
11025 /* Go to next window. */
11026 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11027 return FAIL;
11028 }
11029 }
11030 return OK;
11031}
11032
11033/*
11034 * Skip frames that don't contain windows we want to save in the Session.
11035 * Returns NULL when there none.
11036 */
11037 static frame_T *
11038ses_skipframe(fr)
11039 frame_T *fr;
11040{
11041 frame_T *frc;
11042
11043 for (frc = fr; frc != NULL; frc = frc->fr_next)
11044 if (ses_do_frame(frc))
11045 break;
11046 return frc;
11047}
11048
11049/*
11050 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11051 * the Session.
11052 */
11053 static int
11054ses_do_frame(fr)
11055 frame_T *fr;
11056{
11057 frame_T *frc;
11058
11059 if (fr->fr_layout == FR_LEAF)
11060 return ses_do_win(fr->fr_win);
11061 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11062 if (ses_do_frame(frc))
11063 return TRUE;
11064 return FALSE;
11065}
11066
11067/*
11068 * Return non-zero if window "wp" is to be stored in the Session.
11069 */
11070 static int
11071ses_do_win(wp)
11072 win_T *wp;
11073{
11074 if (wp->w_buffer->b_fname == NULL
11075#ifdef FEAT_QUICKFIX
11076 /* When 'buftype' is "nofile" can't restore the window contents. */
11077 || bt_nofile(wp->w_buffer)
11078#endif
11079 )
11080 return (ssop_flags & SSOP_BLANK);
11081 if (wp->w_buffer->b_help)
11082 return (ssop_flags & SSOP_HELP);
11083 return TRUE;
11084}
11085
11086/*
11087 * Write commands to "fd" to restore the view of a window.
11088 * Caller must make sure 'scrolloff' is zero.
11089 */
11090 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011091put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 FILE *fd;
11093 win_T *wp;
11094 int add_edit; /* add ":edit" command to view */
11095 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011096 int current_arg_idx; /* current argument index of the window, use
11097 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098{
11099 win_T *save_curwin;
11100 int f;
11101 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011102 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
11104 /* Always restore cursor position for ":mksession". For ":mkview" only
11105 * when 'viewoptions' contains "cursor". */
11106 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11107
11108 /*
11109 * Local argument list.
11110 */
11111 if (wp->w_alist == &global_alist)
11112 {
11113 if (put_line(fd, "argglobal") == FAIL)
11114 return FAIL;
11115 }
11116 else
11117 {
11118 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11119 flagp == &vop_flags
11120 || !(*flagp & SSOP_CURDIR)
11121 || wp->w_localdir != NULL, flagp) == FAIL)
11122 return FAIL;
11123 }
11124
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011125 /* Only when part of a session: restore the argument index. Some
11126 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011127 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011128 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011130 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 || put_eol(fd) == FAIL)
11132 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011133 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 }
11135
11136 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011137 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 {
11139 /*
11140 * Load the file.
11141 */
11142 if (wp->w_buffer->b_ffname != NULL
11143#ifdef FEAT_QUICKFIX
11144 && !bt_nofile(wp->w_buffer)
11145#endif
11146 )
11147 {
11148 /*
11149 * Editing a file in this buffer: use ":edit file".
11150 * This may have side effects! (e.g., compressed or network file).
11151 */
11152 if (fputs("edit ", fd) < 0
11153 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11154 return FAIL;
11155 }
11156 else
11157 {
11158 /* No file in this buffer, just make it empty. */
11159 if (put_line(fd, "enew") == FAIL)
11160 return FAIL;
11161#ifdef FEAT_QUICKFIX
11162 if (wp->w_buffer->b_ffname != NULL)
11163 {
11164 /* The buffer does have a name, but it's not a file name. */
11165 if (fputs("file ", fd) < 0
11166 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11167 return FAIL;
11168 }
11169#endif
11170 do_cursor = FALSE;
11171 }
11172 }
11173
11174 /*
11175 * Local mappings and abbreviations.
11176 */
11177 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11178 && makemap(fd, wp->w_buffer) == FAIL)
11179 return FAIL;
11180
11181 /*
11182 * Local options. Need to go to the window temporarily.
11183 * Store only local values when using ":mkview" and when ":mksession" is
11184 * used and 'sessionoptions' doesn't include "options".
11185 * Some folding options are always stored when "folds" is included,
11186 * otherwise the folds would not be restored correctly.
11187 */
11188 save_curwin = curwin;
11189 curwin = wp;
11190 curbuf = curwin->w_buffer;
11191 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11192 f = makeset(fd, OPT_LOCAL,
11193 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11194#ifdef FEAT_FOLDING
11195 else if (*flagp & SSOP_FOLDS)
11196 f = makefoldset(fd);
11197#endif
11198 else
11199 f = OK;
11200 curwin = save_curwin;
11201 curbuf = curwin->w_buffer;
11202 if (f == FAIL)
11203 return FAIL;
11204
11205#ifdef FEAT_FOLDING
11206 /*
11207 * Save Folds when 'buftype' is empty and for help files.
11208 */
11209 if ((*flagp & SSOP_FOLDS)
11210 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011211# ifdef FEAT_QUICKFIX
11212 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11213# endif
11214 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 {
11216 if (put_folds(fd, wp) == FAIL)
11217 return FAIL;
11218 }
11219#endif
11220
11221 /*
11222 * Set the cursor after creating folds, since that moves the cursor.
11223 */
11224 if (do_cursor)
11225 {
11226
11227 /* Restore the cursor line in the file and relatively in the
11228 * window. Don't use "G", it changes the jumplist. */
11229 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11230 (long)wp->w_cursor.lnum,
11231 (long)(wp->w_cursor.lnum - wp->w_topline),
11232 (long)wp->w_height / 2, (long)wp->w_height) < 0
11233 || put_eol(fd) == FAIL
11234 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11235 || put_line(fd, "exe s:l") == FAIL
11236 || put_line(fd, "normal! zt") == FAIL
11237 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11238 || put_eol(fd) == FAIL)
11239 return FAIL;
11240 /* Restore the cursor column and left offset when not wrapping. */
11241 if (wp->w_cursor.col == 0)
11242 {
11243 if (put_line(fd, "normal! 0") == FAIL)
11244 return FAIL;
11245 }
11246 else
11247 {
11248 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11249 {
11250 if (fprintf(fd,
11251 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011252 (long)wp->w_virtcol + 1,
11253 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 (long)wp->w_width / 2, (long)wp->w_width) < 0
11255 || put_eol(fd) == FAIL
11256 || put_line(fd, "if s:c > 0") == FAIL
11257 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011258 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11259 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 || put_eol(fd) == FAIL
11261 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011262 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 || put_eol(fd) == FAIL
11264 || put_line(fd, "endif") == FAIL)
11265 return FAIL;
11266 }
11267 else
11268 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011269 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 || put_eol(fd) == FAIL)
11271 return FAIL;
11272 }
11273 }
11274 }
11275
11276 /*
11277 * Local directory.
11278 */
11279 if (wp->w_localdir != NULL)
11280 {
11281 if (fputs("lcd ", fd) < 0
11282 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11283 || put_eol(fd) == FAIL)
11284 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011285 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 }
11287
11288 return OK;
11289}
11290
11291/*
11292 * Write an argument list to the session file.
11293 * Returns FAIL if writing fails.
11294 */
11295 static int
11296ses_arglist(fd, cmd, gap, fullname, flagp)
11297 FILE *fd;
11298 char *cmd;
11299 garray_T *gap;
11300 int fullname; /* TRUE: use full path name */
11301 unsigned *flagp;
11302{
11303 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011304 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 char_u *s;
11306
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011307 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11308 return FAIL;
11309 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 return FAIL;
11311 for (i = 0; i < gap->ga_len; ++i)
11312 {
11313 /* NULL file names are skipped (only happens when out of memory). */
11314 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11315 if (s != NULL)
11316 {
11317 if (fullname)
11318 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011319 buf = alloc(MAXPATHL);
11320 if (buf != NULL)
11321 {
11322 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11323 s = buf;
11324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011326 if (fputs("argadd ", fd) < 0
11327 || ses_put_fname(fd, s, flagp) == FAIL
11328 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011329 {
11330 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011332 }
11333 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 }
11335 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011336 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337}
11338
11339/*
11340 * Write a buffer name to the session file.
11341 * Also ends the line.
11342 * Returns FAIL if writing fails.
11343 */
11344 static int
11345ses_fname(fd, buf, flagp)
11346 FILE *fd;
11347 buf_T *buf;
11348 unsigned *flagp;
11349{
11350 char_u *name;
11351
11352 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011353 * the session file will be sourced.
11354 * Don't do this for ":mkview", we don't know the current directory.
11355 * Don't do this after ":lcd", we don't keep track of what the current
11356 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011357 if (buf->b_sfname != NULL
11358 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011359 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011360#ifdef FEAT_AUTOCHDIR
11361 && !p_acd
11362#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011363 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 name = buf->b_sfname;
11365 else
11366 name = buf->b_ffname;
11367 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11368 return FAIL;
11369 return OK;
11370}
11371
11372/*
11373 * Write a file name to the session file.
11374 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11375 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011376 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 */
11378 static int
11379ses_put_fname(fd, name, flagp)
11380 FILE *fd;
11381 char_u *name;
11382 unsigned *flagp;
11383{
11384 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011385 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387
11388 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011389 if (sname == NULL)
11390 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011392 if (*flagp & SSOP_SLASH)
11393 {
11394 /* change all backslashes to forward slashes */
11395 for (p = sname; *p != NUL; mb_ptr_adv(p))
11396 if (*p == '\\')
11397 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011399
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011400 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011401 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011403 if (p == NULL)
11404 return FAIL;
11405
11406 /* write the result */
11407 if (fputs((char *)p, fd) < 0)
11408 retval = FAIL;
11409
11410 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 return retval;
11412}
11413
11414/*
11415 * ":loadview [nr]"
11416 */
11417 static void
11418ex_loadview(eap)
11419 exarg_T *eap;
11420{
11421 char_u *fname;
11422
11423 fname = get_view_file(*eap->arg);
11424 if (fname != NULL)
11425 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011426 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 vim_free(fname);
11428 }
11429}
11430
11431/*
11432 * Get the name of the view file for the current buffer.
11433 */
11434 static char_u *
11435get_view_file(c)
11436 int c;
11437{
11438 int len = 0;
11439 char_u *p, *s;
11440 char_u *retval;
11441 char_u *sname;
11442
11443 if (curbuf->b_ffname == NULL)
11444 {
11445 EMSG(_(e_noname));
11446 return NULL;
11447 }
11448 sname = home_replace_save(NULL, curbuf->b_ffname);
11449 if (sname == NULL)
11450 return NULL;
11451
11452 /*
11453 * We want a file name without separators, because we're not going to make
11454 * a directory.
11455 * "normal" path separator -> "=+"
11456 * "=" -> "=="
11457 * ":" path separator -> "=-"
11458 */
11459 for (p = sname; *p; ++p)
11460 if (*p == '=' || vim_ispathsep(*p))
11461 ++len;
11462 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11463 if (retval != NULL)
11464 {
11465 STRCPY(retval, p_vdir);
11466 add_pathsep(retval);
11467 s = retval + STRLEN(retval);
11468 for (p = sname; *p; ++p)
11469 {
11470 if (*p == '=')
11471 {
11472 *s++ = '=';
11473 *s++ = '=';
11474 }
11475 else if (vim_ispathsep(*p))
11476 {
11477 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011478#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 if (*p == ':')
11480 *s++ = '-';
11481 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011483 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 }
11485 else
11486 *s++ = *p;
11487 }
11488 *s++ = '=';
11489 *s++ = c;
11490 STRCPY(s, ".vim");
11491 }
11492
11493 vim_free(sname);
11494 return retval;
11495}
11496
11497#endif /* FEAT_SESSION */
11498
11499/*
11500 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11501 * Return FAIL for a write error.
11502 */
11503 int
11504put_eol(fd)
11505 FILE *fd;
11506{
11507 if (
11508#ifdef USE_CRNL
11509 (
11510# ifdef MKSESSION_NL
11511 !mksession_nl &&
11512# endif
11513 (putc('\r', fd) < 0)) ||
11514#endif
11515 (putc('\n', fd) < 0))
11516 return FAIL;
11517 return OK;
11518}
11519
11520/*
11521 * Write a line to "fd".
11522 * Return FAIL for a write error.
11523 */
11524 int
11525put_line(fd, s)
11526 FILE *fd;
11527 char *s;
11528{
11529 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11530 return FAIL;
11531 return OK;
11532}
11533
11534#ifdef FEAT_VIMINFO
11535/*
11536 * ":rviminfo" and ":wviminfo".
11537 */
11538 static void
11539ex_viminfo(eap)
11540 exarg_T *eap;
11541{
11542 char_u *save_viminfo;
11543
11544 save_viminfo = p_viminfo;
11545 if (*p_viminfo == NUL)
11546 p_viminfo = (char_u *)"'100";
11547 if (eap->cmdidx == CMD_rviminfo)
11548 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011549 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11550 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011551 EMSG(_("E195: Cannot open viminfo file for reading"));
11552 }
11553 else
11554 write_viminfo(eap->arg, eap->forceit);
11555 p_viminfo = save_viminfo;
11556}
11557#endif
11558
11559#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011560/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011561 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011562 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 void
11565dialog_msg(buff, format, fname)
11566 char_u *buff;
11567 char *format;
11568 char_u *fname;
11569{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 if (fname == NULL)
11571 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011572 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573}
11574#endif
11575
11576/*
11577 * ":behave {mswin,xterm}"
11578 */
11579 static void
11580ex_behave(eap)
11581 exarg_T *eap;
11582{
11583 if (STRCMP(eap->arg, "mswin") == 0)
11584 {
11585 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11586 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11587 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11588 set_option_value((char_u *)"keymodel", 0L,
11589 (char_u *)"startsel,stopsel", 0);
11590 }
11591 else if (STRCMP(eap->arg, "xterm") == 0)
11592 {
11593 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11594 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11595 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11596 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11597 }
11598 else
11599 EMSG2(_(e_invarg2), eap->arg);
11600}
11601
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011602#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11603/*
11604 * Function given to ExpandGeneric() to obtain the possible arguments of the
11605 * ":behave {mswin,xterm}" command.
11606 */
11607 char_u *
11608get_behave_arg(xp, idx)
11609 expand_T *xp UNUSED;
11610 int idx;
11611{
11612 if (idx == 0)
11613 return (char_u *)"mswin";
11614 if (idx == 1)
11615 return (char_u *)"xterm";
11616 return NULL;
11617}
11618#endif
11619
Bram Moolenaar071d4272004-06-13 20:20:40 +000011620#ifdef FEAT_AUTOCMD
11621static int filetype_detect = FALSE;
11622static int filetype_plugin = FALSE;
11623static int filetype_indent = FALSE;
11624
11625/*
11626 * ":filetype [plugin] [indent] {on,off,detect}"
11627 * on: Load the filetype.vim file to install autocommands for file types.
11628 * off: Load the ftoff.vim file to remove all autocommands for file types.
11629 * plugin on: load filetype.vim and ftplugin.vim
11630 * plugin off: load ftplugof.vim
11631 * indent on: load filetype.vim and indent.vim
11632 * indent off: load indoff.vim
11633 */
11634 static void
11635ex_filetype(eap)
11636 exarg_T *eap;
11637{
11638 char_u *arg = eap->arg;
11639 int plugin = FALSE;
11640 int indent = FALSE;
11641
11642 if (*eap->arg == NUL)
11643 {
11644 /* Print current status. */
11645 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11646 filetype_detect ? "ON" : "OFF",
11647 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11648 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11649 return;
11650 }
11651
11652 /* Accept "plugin" and "indent" in any order. */
11653 for (;;)
11654 {
11655 if (STRNCMP(arg, "plugin", 6) == 0)
11656 {
11657 plugin = TRUE;
11658 arg = skipwhite(arg + 6);
11659 continue;
11660 }
11661 if (STRNCMP(arg, "indent", 6) == 0)
11662 {
11663 indent = TRUE;
11664 arg = skipwhite(arg + 6);
11665 continue;
11666 }
11667 break;
11668 }
11669 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11670 {
11671 if (*arg == 'o' || !filetype_detect)
11672 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011673 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674 filetype_detect = TRUE;
11675 if (plugin)
11676 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011677 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678 filetype_plugin = TRUE;
11679 }
11680 if (indent)
11681 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011682 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683 filetype_indent = TRUE;
11684 }
11685 }
11686 if (*arg == 'd')
11687 {
11688 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011689 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 }
11691 }
11692 else if (STRCMP(arg, "off") == 0)
11693 {
11694 if (plugin || indent)
11695 {
11696 if (plugin)
11697 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011698 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699 filetype_plugin = FALSE;
11700 }
11701 if (indent)
11702 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011703 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 filetype_indent = FALSE;
11705 }
11706 }
11707 else
11708 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011709 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 filetype_detect = FALSE;
11711 }
11712 }
11713 else
11714 EMSG2(_(e_invarg2), arg);
11715}
11716
11717/*
11718 * ":setfiletype {name}"
11719 */
11720 static void
11721ex_setfiletype(eap)
11722 exarg_T *eap;
11723{
11724 if (!did_filetype)
11725 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11726}
11727#endif
11728
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729 static void
11730ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011731 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732{
11733#ifdef FEAT_DIGRAPHS
11734 if (*eap->arg != NUL)
11735 putdigraph(eap->arg);
11736 else
11737 listdigraphs();
11738#else
11739 EMSG(_("E196: No digraphs in this version"));
11740#endif
11741}
11742
11743 static void
11744ex_set(eap)
11745 exarg_T *eap;
11746{
11747 int flags = 0;
11748
11749 if (eap->cmdidx == CMD_setlocal)
11750 flags = OPT_LOCAL;
11751 else if (eap->cmdidx == CMD_setglobal)
11752 flags = OPT_GLOBAL;
11753#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11754 if (cmdmod.browse && flags == 0)
11755 ex_options(eap);
11756 else
11757#endif
11758 (void)do_set(eap->arg, flags);
11759}
11760
11761#ifdef FEAT_SEARCH_EXTRA
11762/*
11763 * ":nohlsearch"
11764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 static void
11766ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011767 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011769 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011770 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771}
11772
11773/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011774 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 * Sets nextcmd to the start of the next command, if any. Also called when
11776 * skipping commands to find the next command.
11777 */
11778 static void
11779ex_match(eap)
11780 exarg_T *eap;
11781{
11782 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011783 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784 char_u *end;
11785 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011786 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011787
11788 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011789 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011790 else
11791 {
11792 EMSG(e_invcmd);
11793 return;
11794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795
11796 /* First clear any old pattern. */
11797 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011798 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799
11800 if (ends_excmd(*eap->arg))
11801 end = eap->arg;
11802 else if ((STRNICMP(eap->arg, "none", 4) == 0
11803 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11804 end = eap->arg + 4;
11805 else
11806 {
11807 p = skiptowhite(eap->arg);
11808 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011809 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 p = skipwhite(p);
11811 if (*p == NUL)
11812 {
11813 /* There must be two arguments. */
11814 EMSG2(_(e_invarg2), eap->arg);
11815 return;
11816 }
11817 end = skip_regexp(p + 1, *p, TRUE, NULL);
11818 if (!eap->skip)
11819 {
11820 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11821 {
11822 eap->errmsg = e_trailing;
11823 return;
11824 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011825 if (*end != *p)
11826 {
11827 EMSG2(_(e_invarg2), p);
11828 return;
11829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830
11831 c = *end;
11832 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011833 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011834 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011835 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 }
11837 }
11838 eap->nextcmd = find_nextcmd(end);
11839}
11840#endif
11841
11842#ifdef FEAT_CRYPT
11843/*
11844 * ":X": Get crypt key
11845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011846 static void
11847ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011848 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011850 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011851 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852}
11853#endif
11854
11855#ifdef FEAT_FOLDING
11856 static void
11857ex_fold(eap)
11858 exarg_T *eap;
11859{
11860 if (foldManualAllowed(TRUE))
11861 foldCreate(eap->line1, eap->line2);
11862}
11863
11864 static void
11865ex_foldopen(eap)
11866 exarg_T *eap;
11867{
11868 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11869 eap->forceit, FALSE);
11870}
11871
11872 static void
11873ex_folddo(eap)
11874 exarg_T *eap;
11875{
11876 linenr_T lnum;
11877
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011878#ifdef FEAT_CLIPBOARD
11879 start_global_changes();
11880#endif
11881
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 /* First set the marks for all lines closed/open. */
11883 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11884 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11885 ml_setmarked(lnum);
11886
11887 /* Execute the command on the marked lines. */
11888 global_exe(eap->arg);
11889 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011890#ifdef FEAT_CLIPBOARD
11891 end_global_changes();
11892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893}
11894#endif