blob: 7b25cdb8fe0027d6259df5cace192f1ba4ee7eba [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 Moolenaarb96a7f32014-11-27 16:22:48 +01001724 * 3. parse command
1725 * 4. parse range
1726 * 6. parse arguments
1727 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001729 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 *
1731 * This function may be called recursively!
1732 */
1733#if (_MSC_VER == 1200)
1734/*
Bram Moolenaared203462004-06-16 11:19:22 +00001735 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001737 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#endif
1739 static char_u *
1740do_one_cmd(cmdlinep, sourcing,
1741#ifdef FEAT_EVAL
1742 cstack,
1743#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001744 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 char_u **cmdlinep;
1746 int sourcing;
1747#ifdef FEAT_EVAL
1748 struct condstack *cstack;
1749#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001750 char_u *(*fgetline) __ARGS((int, void *, int));
1751 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
1753 char_u *p;
1754 linenr_T lnum;
1755 long n;
1756 char_u *errormsg = NULL; /* error message */
1757 exarg_T ea; /* Ex command arguments */
1758 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001759 int save_msg_scroll = msg_scroll;
1760 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001762#ifdef HAVE_SANDBOX
1763 int did_sandbox = FALSE;
1764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 cmdmod_T save_cmdmod;
1766 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001767 win_T *wp;
1768 tabpage_T *tp;
1769 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
1771 vim_memset(&ea, 0, sizeof(ea));
1772 ea.line1 = 1;
1773 ea.line2 = 1;
1774#ifdef FEAT_EVAL
1775 ++ex_nesting_level;
1776#endif
1777
Bram Moolenaar21691f82012-12-05 19:13:18 +01001778 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 if (quitmore
1780#ifdef FEAT_EVAL
1781 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001782 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001783#endif
1784#ifdef FEAT_AUTOCMD
Bram Moolenaar21691f82012-12-05 19:13:18 +01001785 /* avoid that an autocommand, e.g. QuitPre, does this */
1786 && !getline_equal(fgetline, cookie, getnextac)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
1788 )
1789 --quitmore;
1790
1791 /*
1792 * Reset browse, confirm, etc.. They are restored when returning, for
1793 * recursive calls.
1794 */
1795 save_cmdmod = cmdmod;
1796 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1797
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001798 /* "#!anything" is handled like a comment. */
1799 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1800 goto doend;
1801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 /*
1803 * Repeat until no more command modifiers are found.
1804 */
1805 ea.cmd = *cmdlinep;
1806 for (;;)
1807 {
1808/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001809 * 1. Skip comment lines and leading white space and colons.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 */
1811 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1812 ++ea.cmd;
1813
1814 /* in ex mode, an empty line works like :+ */
1815 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001816 && (getline_equal(fgetline, cookie, getexmodeline)
1817 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001818 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
1820 ea.cmd = (char_u *)"+";
1821 ex_pressedreturn = TRUE;
1822 }
1823
1824 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001825 if (*ea.cmd == '"')
1826 goto doend;
1827 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001828 {
1829 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001834 * 2. Handle command modifiers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 */
1836 p = ea.cmd;
1837 if (VIM_ISDIGIT(*ea.cmd))
1838 p = skipwhite(skipdigits(ea.cmd));
1839 switch (*p)
1840 {
1841 /* When adding an entry, also modify cmd_exists(). */
1842 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1843 break;
1844#ifdef FEAT_WINDOWS
1845 cmdmod.split |= WSP_ABOVE;
1846#endif
1847 continue;
1848
1849 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1850 {
1851#ifdef FEAT_WINDOWS
1852 cmdmod.split |= WSP_BELOW;
1853#endif
1854 continue;
1855 }
1856 if (checkforcmd(&ea.cmd, "browse", 3))
1857 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001858#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 cmdmod.browse = TRUE;
1860#endif
1861 continue;
1862 }
1863 if (!checkforcmd(&ea.cmd, "botright", 2))
1864 break;
1865#ifdef FEAT_WINDOWS
1866 cmdmod.split |= WSP_BOT;
1867#endif
1868 continue;
1869
1870 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1871 break;
1872#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1873 cmdmod.confirm = TRUE;
1874#endif
1875 continue;
1876
1877 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1878 {
1879 cmdmod.keepmarks = TRUE;
1880 continue;
1881 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001882 if (checkforcmd(&ea.cmd, "keepalt", 5))
1883 {
1884 cmdmod.keepalt = TRUE;
1885 continue;
1886 }
Bram Moolenaara939e432013-11-09 05:30:26 +01001887 if (checkforcmd(&ea.cmd, "keeppatterns", 5))
1888 {
1889 cmdmod.keeppatterns = TRUE;
1890 continue;
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1893 break;
1894 cmdmod.keepjumps = TRUE;
1895 continue;
1896
1897 /* ":hide" and ":hide | cmd" are not modifiers */
1898 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1899 || *p == NUL || ends_excmd(*p))
1900 break;
1901 ea.cmd = p;
1902 cmdmod.hide = TRUE;
1903 continue;
1904
1905 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1906 {
1907 cmdmod.lockmarks = TRUE;
1908 continue;
1909 }
1910
1911 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1912 break;
1913#ifdef FEAT_WINDOWS
1914 cmdmod.split |= WSP_ABOVE;
1915#endif
1916 continue;
1917
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001918 case 'n': if (checkforcmd(&ea.cmd, "noautocmd", 3))
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001919 {
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001920#ifdef FEAT_AUTOCMD
1921 if (cmdmod.save_ei == NULL)
1922 {
1923 /* Set 'eventignore' to "all". Restore the
1924 * existing option value later. */
1925 cmdmod.save_ei = vim_strsave(p_ei);
1926 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001927 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001928 }
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001929#endif
Bram Moolenaar5803ae62014-03-23 16:04:02 +01001930 continue;
1931 }
1932 if (!checkforcmd(&ea.cmd, "noswapfile", 6))
1933 break;
1934 cmdmod.noswapfile = TRUE;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001935 continue;
1936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1938 break;
1939#ifdef FEAT_WINDOWS
1940 cmdmod.split |= WSP_BELOW;
1941#endif
1942 continue;
1943
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001944 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1945 {
1946#ifdef HAVE_SANDBOX
1947 if (!did_sandbox)
1948 ++sandbox;
1949 did_sandbox = TRUE;
1950#endif
1951 continue;
1952 }
1953 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001955 if (save_msg_silent == -1)
1956 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1959 {
1960 /* ":silent!", but not "silent !cmd" */
1961 ea.cmd = skipwhite(ea.cmd + 1);
1962 ++emsg_silent;
1963 ++did_esilent;
1964 }
1965 continue;
1966
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001967 case 't': if (checkforcmd(&p, "tab", 3))
1968 {
1969#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001970 if (vim_isdigit(*ea.cmd))
1971 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1972 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001973 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001974 ea.cmd = p;
1975#endif
1976 continue;
1977 }
1978 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 break;
1980#ifdef FEAT_WINDOWS
1981 cmdmod.split |= WSP_TOP;
1982#endif
1983 continue;
1984
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001985 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1986 break;
1987 if (save_msg_silent == -1)
1988 save_msg_silent = msg_silent;
1989 msg_silent = 0;
1990 continue;
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1993 {
1994#ifdef FEAT_VERTSPLIT
1995 cmdmod.split |= WSP_VERT;
1996#endif
1997 continue;
1998 }
1999 if (!checkforcmd(&p, "verbose", 4))
2000 break;
2001 if (verbose_save < 0)
2002 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00002003 if (vim_isdigit(*ea.cmd))
2004 p_verbose = atoi((char *)ea.cmd);
2005 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 p_verbose = 1;
2007 ea.cmd = p;
2008 continue;
2009 }
2010 break;
2011 }
2012
2013#ifdef FEAT_EVAL
2014 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
2015 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
2016#else
2017 ea.skip = (if_level > 0);
2018#endif
2019
2020#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00002021# ifdef FEAT_PROFILE
2022 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00002023 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002024 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002025 if (getline_equal(fgetline, cookie, get_func_line))
2026 func_line_exec(getline_cookie(fgetline, cookie));
2027 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00002028 script_line_exec();
2029 }
2030#endif
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 /* May go to debug mode. If this happens and the ">quit" debug command is
2033 * used, throw an interrupt exception and skip the next command. */
2034 dbg_check_breakpoint(&ea);
2035 if (!ea.skip && got_int)
2036 {
2037 ea.skip = TRUE;
2038 (void)do_intthrow(cstack);
2039 }
2040#endif
2041
2042/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002043 * 3. Skip over the range to find the command. Let "p" point to after it.
2044 *
2045 * We need the command to know what kind of range it uses.
2046 */
2047 cmd = ea.cmd;
2048 ea.cmd = skip_range(ea.cmd, NULL);
2049 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2050 ea.cmd = skipwhite(ea.cmd + 1);
2051 p = find_command(&ea, NULL);
2052
2053/*
2054 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 *
2056 * where 'addr' is:
2057 *
2058 * % (entire file)
2059 * $ [+-NUM]
2060 * 'x [+-NUM] (where x denotes a currently defined mark)
2061 * . [+-NUM]
2062 * [+-NUM]..
2063 * NUM
2064 *
2065 * The ea.cmd pointer is updated to point to the first character following the
2066 * range spec. If an initial address is found, but no second, the upper bound
2067 * is equal to the lower.
2068 */
2069
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002070 if (ea.cmdidx != CMD_SIZE)
2071 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
2072 else
2073 ea.addr_type = ADDR_LINES;
2074 ea.cmd = cmd;
2075
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 /* repeat for all ',' or ';' separated addresses */
2077 for (;;)
2078 {
2079 ea.line1 = ea.line2;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002080 switch (ea.addr_type)
2081 {
2082 case ADDR_LINES:
2083 /* default is current line number */
2084 ea.line2 = curwin->w_cursor.lnum;
2085 break;
2086 case ADDR_WINDOWS:
2087 lnum = 0;
2088 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2089 {
2090 lnum++;
2091 if (wp == curwin)
2092 break;
2093 }
2094 ea.line2 = lnum;
2095 break;
2096 case ADDR_ARGUMENTS:
2097 ea.line2 = curwin->w_arg_idx + 1;
2098 break;
2099 case ADDR_LOADED_BUFFERS:
2100 case ADDR_UNLOADED_BUFFERS:
2101 ea.line2 = curbuf->b_fnum;
2102 break;
2103 case ADDR_TABS:
2104 lnum = 0;
2105 for(tp = first_tabpage; tp != NULL; tp = tp->tp_next)
2106 {
2107 lnum++;
2108 if (tp == curtab)
2109 break;
2110 }
2111 ea.line2 = lnum;
2112 break;
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 ea.cmd = skipwhite(ea.cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002115 lnum = get_address(&ea.cmd, ea.addr_type, ea.skip, ea.addr_count == 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 if (ea.cmd == NULL) /* error detected */
2117 goto doend;
2118 if (lnum == MAXLNUM)
2119 {
2120 if (*ea.cmd == '%') /* '%' - all lines */
2121 {
2122 ++ea.cmd;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002123 switch (ea.addr_type)
2124 {
2125 case ADDR_LINES:
2126 ea.line1 = 1;
2127 ea.line2 = curbuf->b_ml.ml_line_count;
2128 break;
2129 case ADDR_WINDOWS:
2130 case ADDR_LOADED_BUFFERS:
2131 case ADDR_UNLOADED_BUFFERS:
2132 case ADDR_TABS:
2133 errormsg = (char_u *)_(e_invrange);
2134 goto doend;
2135 break;
2136 case ADDR_ARGUMENTS:
2137 ea.line1 = 1;
2138 ea.line2 = ARGCOUNT;
2139 break;
2140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 ++ea.addr_count;
2142 }
2143 /* '*' - visual area */
2144 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2145 {
2146 pos_T *fp;
2147
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002148 if (ea.addr_type != ADDR_LINES)
2149 {
2150 errormsg = (char_u *)_(e_invrange);
2151 goto doend;
2152 }
2153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 ++ea.cmd;
2155 if (!ea.skip)
2156 {
2157 fp = getmark('<', FALSE);
2158 if (check_mark(fp) == FAIL)
2159 goto doend;
2160 ea.line1 = fp->lnum;
2161 fp = getmark('>', FALSE);
2162 if (check_mark(fp) == FAIL)
2163 goto doend;
2164 ea.line2 = fp->lnum;
2165 ++ea.addr_count;
2166 }
2167 }
2168 }
2169 else
2170 ea.line2 = lnum;
2171 ea.addr_count++;
2172
2173 if (*ea.cmd == ';')
2174 {
2175 if (!ea.skip)
2176 curwin->w_cursor.lnum = ea.line2;
2177 }
2178 else if (*ea.cmd != ',')
2179 break;
2180 ++ea.cmd;
2181 }
2182
2183 /* One address given: set start and end lines */
2184 if (ea.addr_count == 1)
2185 {
2186 ea.line1 = ea.line2;
2187 /* ... but only implicit: really no address given */
2188 if (lnum == MAXLNUM)
2189 ea.addr_count = 0;
2190 }
2191
2192 /* Don't leave the cursor on an illegal line (caused by ';') */
2193 check_cursor_lnum();
2194
2195/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002196 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 */
2198
2199 /*
2200 * Skip ':' and any white space
2201 */
2202 ea.cmd = skipwhite(ea.cmd);
2203 while (*ea.cmd == ':')
2204 ea.cmd = skipwhite(ea.cmd + 1);
2205
2206 /*
2207 * If we got a line, but no command, then go to the line.
2208 * If we find a '|' or '\n' we set ea.nextcmd.
2209 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002210 if (*ea.cmd == NUL || *ea.cmd == '"'
2211 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
2213 /*
2214 * strange vi behaviour:
2215 * ":3" jumps to line 3
2216 * ":3|..." prints line 3
2217 * ":|" prints current line
2218 */
2219 if (ea.skip) /* skip this if inside :if */
2220 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002221 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 {
2223 ea.cmdidx = CMD_print;
2224 ea.argt = RANGE+COUNT+TRLBAR;
2225 if ((errormsg = invalid_range(&ea)) == NULL)
2226 {
2227 correct_range(&ea);
2228 ex_print(&ea);
2229 }
2230 }
2231 else if (ea.addr_count != 0)
2232 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002233 if (ea.line2 > curbuf->b_ml.ml_line_count)
2234 {
2235 /* With '-' in 'cpoptions' a line number past the file is an
2236 * error, otherwise put it at the end of the file. */
2237 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2238 ea.line2 = -1;
2239 else
2240 ea.line2 = curbuf->b_ml.ml_line_count;
2241 }
2242
2243 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002244 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 else
2246 {
2247 if (ea.line2 == 0)
2248 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 else
2250 curwin->w_cursor.lnum = ea.line2;
2251 beginline(BL_SOL | BL_FIX);
2252 }
2253 }
2254 goto doend;
2255 }
2256
Bram Moolenaard5005162014-08-22 23:05:54 +02002257#ifdef FEAT_AUTOCMD
2258 /* If this looks like an undefined user command and there are CmdUndefined
2259 * autocommands defined, trigger the matching autocommands. */
2260 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2261 && ASCII_ISUPPER(*ea.cmd)
2262 && has_cmdundefined())
2263 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002264 int ret;
2265
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002266 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002267 while (ASCII_ISALNUM(*p))
2268 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02002269 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02002270 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2271 vim_free(p);
2272 if (ret && !aborting())
2273 p = find_command(&ea, NULL);
2274 }
2275#endif
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#ifdef FEAT_USR_CMDS
2278 if (p == NULL)
2279 {
2280 if (!ea.skip)
2281 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2282 goto doend;
2283 }
2284 /* Check for wrong commands. */
2285 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2286 {
2287 errormsg = uc_fun_cmd();
2288 goto doend;
2289 }
2290#endif
2291 if (ea.cmdidx == CMD_SIZE)
2292 {
2293 if (!ea.skip)
2294 {
2295 STRCPY(IObuff, _("E492: Not an editor command"));
2296 if (!sourcing)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002297 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 errormsg = IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002299 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 goto doend;
2302 }
2303
Bram Moolenaar958636c2014-10-21 20:01:58 +02002304 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2305 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002306#ifdef HAVE_EX_SCRIPT_NI
2307 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2308#endif
2309 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
2311#ifndef FEAT_EVAL
2312 /*
2313 * When the expression evaluation is disabled, recognize the ":if" and
2314 * ":endif" commands and ignore everything in between it.
2315 */
2316 if (ea.cmdidx == CMD_if)
2317 ++if_level;
2318 if (if_level)
2319 {
2320 if (ea.cmdidx == CMD_endif)
2321 --if_level;
2322 goto doend;
2323 }
2324
2325#endif
2326
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002327 /* forced commands */
2328 if (*p == '!' && ea.cmdidx != CMD_substitute
2329 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
2331 ++p;
2332 ea.forceit = TRUE;
2333 }
2334 else
2335 ea.forceit = FALSE;
2336
2337/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002338 * 5. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002340 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002341 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
2343 if (!ea.skip)
2344 {
2345#ifdef HAVE_SANDBOX
2346 if (sandbox != 0 && !(ea.argt & SBOXOK))
2347 {
2348 /* Command not allowed in sandbox. */
2349 errormsg = (char_u *)_(e_sandbox);
2350 goto doend;
2351 }
2352#endif
2353 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2354 {
2355 /* Command not allowed in non-'modifiable' buffer */
2356 errormsg = (char_u *)_(e_modifiable);
2357 goto doend;
2358 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002359
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002360 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002361 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002363 /* Command not allowed when editing the command line. */
2364#ifdef FEAT_CMDWIN
2365 if (cmdwin_type != 0)
2366 errormsg = (char_u *)_(e_cmdwin);
2367 else
2368#endif
2369 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 goto doend;
2371 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002372#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002373 /* Disallow editing another buffer when "curbuf_lock" is set.
2374 * Do allow ":edit" (check for argument later).
2375 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002376 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002377 && ea.cmdidx != CMD_edit
2378 && ea.cmdidx != CMD_checktime
Bram Moolenaar958636c2014-10-21 20:01:58 +02002379 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002380 && curbuf_locked())
2381 goto doend;
2382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
2384 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2385 {
2386 /* no range allowed */
2387 errormsg = (char_u *)_(e_norange);
2388 goto doend;
2389 }
2390 }
2391
2392 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2393 {
2394 errormsg = (char_u *)_(e_nobang);
2395 goto doend;
2396 }
2397
2398 /*
2399 * Don't complain about the range if it is not used
2400 * (could happen if line_count is accidentally set to 0).
2401 */
2402 if (!ea.skip && !ni)
2403 {
2404 /*
2405 * If the range is backwards, ask for confirmation and, if given, swap
2406 * ea.line1 & ea.line2 so it's forwards again.
2407 * When global command is busy, don't ask, will fail below.
2408 */
2409 if (!global_busy && ea.line1 > ea.line2)
2410 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002411 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002413 if (sourcing || exmode_active)
2414 {
2415 errormsg = (char_u *)_("E493: Backwards range given");
2416 goto doend;
2417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 if (ask_yesno((char_u *)
2419 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002420 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 }
2422 lnum = ea.line1;
2423 ea.line1 = ea.line2;
2424 ea.line2 = lnum;
2425 }
2426 if ((errormsg = invalid_range(&ea)) != NULL)
2427 goto doend;
2428 }
2429
2430 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2431 ea.line2 = 1;
2432
2433 correct_range(&ea);
2434
2435#ifdef FEAT_FOLDING
2436 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2437 {
2438 /* Put the first line at the start of a closed fold, put the last line
2439 * at the end of a closed fold. */
2440 (void)hasFolding(ea.line1, &ea.line1, NULL);
2441 (void)hasFolding(ea.line2, NULL, &ea.line2);
2442 }
2443#endif
2444
2445#ifdef FEAT_QUICKFIX
2446 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002447 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 * option here, so things like % get expanded.
2449 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002450 p = replace_makeprg(&ea, p, cmdlinep);
2451 if (p == NULL)
2452 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453#endif
2454
2455 /*
2456 * Skip to start of argument.
2457 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2458 */
2459 if (ea.cmdidx == CMD_bang)
2460 ea.arg = p;
2461 else
2462 ea.arg = skipwhite(p);
2463
2464 /*
2465 * Check for "++opt=val" argument.
2466 * Must be first, allow ":w ++enc=utf8 !cmd"
2467 */
2468 if (ea.argt & ARGOPT)
2469 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2470 if (getargopt(&ea) == FAIL && !ni)
2471 {
2472 errormsg = (char_u *)_(e_invarg);
2473 goto doend;
2474 }
2475
2476 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2477 {
2478 if (*ea.arg == '>') /* append */
2479 {
2480 if (*++ea.arg != '>') /* typed wrong */
2481 {
2482 errormsg = (char_u *)_("E494: Use w or w>>");
2483 goto doend;
2484 }
2485 ea.arg = skipwhite(ea.arg + 1);
2486 ea.append = TRUE;
2487 }
2488 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2489 {
2490 ++ea.arg;
2491 ea.usefilter = TRUE;
2492 }
2493 }
2494
2495 if (ea.cmdidx == CMD_read)
2496 {
2497 if (ea.forceit)
2498 {
2499 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2500 ea.forceit = FALSE;
2501 }
2502 else if (*ea.arg == '!') /* :r !filter */
2503 {
2504 ++ea.arg;
2505 ea.usefilter = TRUE;
2506 }
2507 }
2508
2509 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2510 {
2511 ea.amount = 1;
2512 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2513 {
2514 ++ea.arg;
2515 ++ea.amount;
2516 }
2517 ea.arg = skipwhite(ea.arg);
2518 }
2519
2520 /*
2521 * Check for "+command" argument, before checking for next command.
2522 * Don't do this for ":read !cmd" and ":write !cmd".
2523 */
2524 if ((ea.argt & EDITCMD) && !ea.usefilter)
2525 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2526
2527 /*
2528 * Check for '|' to separate commands and '"' to start comments.
2529 * Don't do this for ":read !cmd" and ":write !cmd".
2530 */
2531 if ((ea.argt & TRLBAR) && !ea.usefilter)
2532 separate_nextcmd(&ea);
2533
2534 /*
2535 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002536 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2537 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002539 else if (ea.cmdidx == CMD_bang
2540 || ea.cmdidx == CMD_global
2541 || ea.cmdidx == CMD_vglobal
2542 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
2544 for (p = ea.arg; *p; ++p)
2545 {
2546 /* Remove one backslash before a newline, so that it's possible to
2547 * pass a newline to the shell and also a newline that is preceded
2548 * with a backslash. This makes it impossible to end a shell
2549 * command in a backslash, but that doesn't appear useful.
2550 * Halving the number of backslashes is incompatible with previous
2551 * versions. */
2552 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002553 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 else if (*p == '\n')
2555 {
2556 ea.nextcmd = p + 1;
2557 *p = NUL;
2558 break;
2559 }
2560 }
2561 }
2562
2563 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2564 {
2565 ea.line1 = 1;
2566 ea.line2 = curbuf->b_ml.ml_line_count;
2567 }
2568
2569 /* accept numbered register only when no count allowed (:put) */
2570 if ( (ea.argt & REGSTR)
2571 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002572 /* Do not allow register = for user commands */
2573 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2575 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002576#ifndef FEAT_CLIPBOARD
2577 /* check these explicitly for a more specific error message */
2578 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002580 errormsg = (char_u *)_(e_invalidreg);
2581 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002584 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2585 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002586 {
2587 ea.regname = *ea.arg++;
2588#ifdef FEAT_EVAL
2589 /* for '=' register: accept the rest of the line as an expression */
2590 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2591 {
2592 set_expr_line(vim_strsave(ea.arg));
2593 ea.arg += STRLEN(ea.arg);
2594 }
2595#endif
2596 ea.arg = skipwhite(ea.arg);
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 }
2599
2600 /*
2601 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2602 * count, it's a buffer name.
2603 */
2604 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2605 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2606 || vim_iswhite(*p)))
2607 {
2608 n = getdigits(&ea.arg);
2609 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002610 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
2612 errormsg = (char_u *)_(e_zerocount);
2613 goto doend;
2614 }
2615 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2616 {
2617 ea.line2 = n;
2618 if (ea.addr_count == 0)
2619 ea.addr_count = 1;
2620 }
2621 else
2622 {
2623 ea.line1 = ea.line2;
2624 ea.line2 += n - 1;
2625 ++ea.addr_count;
2626 /*
2627 * Be vi compatible: no error message for out of range.
2628 */
2629 if (ea.line2 > curbuf->b_ml.ml_line_count)
2630 ea.line2 = curbuf->b_ml.ml_line_count;
2631 }
2632 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002633
2634 /*
2635 * Check for flags: 'l', 'p' and '#'.
2636 */
2637 if (ea.argt & EXFLAGS)
2638 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002640 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002641 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 {
2643 errormsg = (char_u *)_(e_trailing);
2644 goto doend;
2645 }
2646
2647 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2648 {
2649 errormsg = (char_u *)_(e_argreq);
2650 goto doend;
2651 }
2652
2653#ifdef FEAT_EVAL
2654 /*
2655 * Skip the command when it's not going to be executed.
2656 * The commands like :if, :endif, etc. always need to be executed.
2657 * Also make an exception for commands that handle a trailing command
2658 * themselves.
2659 */
2660 if (ea.skip)
2661 {
2662 switch (ea.cmdidx)
2663 {
2664 /* commands that need evaluation */
2665 case CMD_while:
2666 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002667 case CMD_for:
2668 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 case CMD_if:
2670 case CMD_elseif:
2671 case CMD_else:
2672 case CMD_endif:
2673 case CMD_try:
2674 case CMD_catch:
2675 case CMD_finally:
2676 case CMD_endtry:
2677 case CMD_function:
2678 break;
2679
2680 /* Commands that handle '|' themselves. Check: A command should
2681 * either have the TRLBAR flag, appear in this list or appear in
2682 * the list at ":help :bar". */
2683 case CMD_aboveleft:
2684 case CMD_and:
2685 case CMD_belowright:
2686 case CMD_botright:
2687 case CMD_browse:
2688 case CMD_call:
2689 case CMD_confirm:
2690 case CMD_delfunction:
2691 case CMD_djump:
2692 case CMD_dlist:
2693 case CMD_dsearch:
2694 case CMD_dsplit:
2695 case CMD_echo:
2696 case CMD_echoerr:
2697 case CMD_echomsg:
2698 case CMD_echon:
2699 case CMD_execute:
2700 case CMD_help:
2701 case CMD_hide:
2702 case CMD_ijump:
2703 case CMD_ilist:
2704 case CMD_isearch:
2705 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002706 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 case CMD_keepjumps:
2708 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002709 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 case CMD_leftabove:
2711 case CMD_let:
2712 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002713 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002715 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002716 case CMD_noautocmd:
2717 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 case CMD_perl:
2719 case CMD_psearch:
2720 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002721 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002722 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 case CMD_return:
2724 case CMD_rightbelow:
2725 case CMD_ruby:
2726 case CMD_silent:
2727 case CMD_smagic:
2728 case CMD_snomagic:
2729 case CMD_substitute:
2730 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002731 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 case CMD_tcl:
2733 case CMD_throw:
2734 case CMD_tilde:
2735 case CMD_topleft:
2736 case CMD_unlet:
2737 case CMD_verbose:
2738 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002739 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 break;
2741
2742 default: goto doend;
2743 }
2744 }
2745#endif
2746
2747 if (ea.argt & XFILE)
2748 {
2749 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2750 goto doend;
2751 }
2752
2753#ifdef FEAT_LISTCMDS
2754 /*
2755 * Accept buffer name. Cannot be used at the same time with a buffer
2756 * number. Don't do this for a user command.
2757 */
2758 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002759 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
2761 /*
2762 * :bdelete, :bwipeout and :bunload take several arguments, separated
2763 * by spaces: find next space (skipping over escaped characters).
2764 * The others take one argument: ignore trailing spaces.
2765 */
2766 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2767 || ea.cmdidx == CMD_bunload)
2768 p = skiptowhite_esc(ea.arg);
2769 else
2770 {
2771 p = ea.arg + STRLEN(ea.arg);
2772 while (p > ea.arg && vim_iswhite(p[-1]))
2773 --p;
2774 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002775 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2776 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 if (ea.line2 < 0) /* failed */
2778 goto doend;
2779 ea.addr_count = 1;
2780 ea.arg = skipwhite(p);
2781 }
2782#endif
2783
2784/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01002785 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 *
2787 * The "ea" structure holds the arguments that can be used.
2788 */
2789 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002790 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 ea.cookie = cookie;
2792#ifdef FEAT_EVAL
2793 ea.cstack = cstack;
2794#endif
2795
2796#ifdef FEAT_USR_CMDS
Bram Moolenaar958636c2014-10-21 20:01:58 +02002797 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
2799 /*
2800 * Execute a user-defined command.
2801 */
2802 do_ucmd(&ea);
2803 }
2804 else
2805#endif
2806 {
2807 /*
2808 * Call the function to execute the command.
2809 */
2810 ea.errmsg = NULL;
2811 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2812 if (ea.errmsg != NULL)
2813 errormsg = (char_u *)_(ea.errmsg);
2814 }
2815
2816#ifdef FEAT_EVAL
2817 /*
2818 * If the command just executed called do_cmdline(), any throw or ":return"
2819 * or ":finish" encountered there must also check the cstack of the still
2820 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2821 * exception, or reanimate a returned function or finished script file and
2822 * return or finish it again.
2823 */
2824 if (need_rethrow)
2825 do_throw(cstack);
2826 else if (check_cstack)
2827 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002828 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002830 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 && current_func_returned())
2832 do_return(&ea, TRUE, FALSE, NULL);
2833 }
2834 need_rethrow = check_cstack = FALSE;
2835#endif
2836
2837doend:
2838 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2839 curwin->w_cursor.lnum = 1;
2840
2841 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2842 {
2843 if (sourcing)
2844 {
2845 if (errormsg != IObuff)
2846 {
2847 STRCPY(IObuff, errormsg);
2848 errormsg = IObuff;
2849 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002850 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852 emsg(errormsg);
2853 }
2854#ifdef FEAT_EVAL
2855 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002856 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2857 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858#endif
2859
2860 if (verbose_save >= 0)
2861 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002862#ifdef FEAT_AUTOCMD
2863 if (cmdmod.save_ei != NULL)
2864 {
2865 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002866 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2867 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002868 free_string_option(cmdmod.save_ei);
2869 }
2870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871
2872 cmdmod = save_cmdmod;
2873
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002874 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 {
2876 /* messages could be enabled for a serious error, need to check if the
2877 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002878 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002879 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 emsg_silent -= did_esilent;
2881 if (emsg_silent < 0)
2882 emsg_silent = 0;
2883 /* Restore msg_scroll, it's set by file I/O commands, even when no
2884 * message is actually displayed. */
2885 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002886
2887 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2888 * somewhere in the line. Put it back in the first column. */
2889 if (redirecting())
2890 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
2892
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002893#ifdef HAVE_SANDBOX
2894 if (did_sandbox)
2895 --sandbox;
2896#endif
2897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2899 ea.nextcmd = NULL;
2900
2901#ifdef FEAT_EVAL
2902 --ex_nesting_level;
2903#endif
2904
2905 return ea.nextcmd;
2906}
2907#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002908 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909#endif
2910
2911/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002912 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 * If there is a match advance "pp" to the argument and return TRUE.
2914 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002915 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002917 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 char *cmd; /* name of command */
2919 int len; /* required length */
2920{
2921 int i;
2922
2923 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002924 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 break;
2926 if (i >= len && !isalpha((*pp)[i]))
2927 {
2928 *pp = skipwhite(*pp + i);
2929 return TRUE;
2930 }
2931 return FALSE;
2932}
2933
2934/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002935 * Append "cmd" to the error message in IObuff.
2936 * Takes care of limiting the length and handling 0xa0, which would be
2937 * invisible otherwise.
2938 */
2939 static void
2940append_command(cmd)
2941 char_u *cmd;
2942{
2943 char_u *s = cmd;
2944 char_u *d;
2945
2946 STRCAT(IObuff, ": ");
2947 d = IObuff + STRLEN(IObuff);
2948 while (*s != NUL && d - IObuff < IOSIZE - 7)
2949 {
2950 if (
2951#ifdef FEAT_MBYTE
2952 enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) :
2953#endif
2954 *s == 0xa0)
2955 {
2956 s +=
2957#ifdef FEAT_MBYTE
2958 enc_utf8 ? 2 :
2959#endif
2960 1;
2961 STRCPY(d, "<a0>");
2962 d += 4;
2963 }
2964 else
2965 MB_COPY_CHAR(s, d);
2966 }
2967 *d = NUL;
2968}
2969
2970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002972 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002974 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 * Returns NULL for an ambiguous user command.
2976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 static char_u *
2978find_command(eap, full)
2979 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002980 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 int len;
2983 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002984 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 /*
2987 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002988 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 * - the 'k' command can directly be followed by any character.
2990 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2991 * but :sre[wind] is another command, as are :scrip[tnames],
2992 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002993 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 */
2995 p = eap->cmd;
2996 if (*p == 'k')
2997 {
2998 eap->cmdidx = CMD_k;
2999 ++p;
3000 }
3001 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003002 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
3003 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 || p[1] == 'g'
3005 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3006 || p[1] == 'I'
3007 || (p[1] == 'r' && p[2] != 'e')))
3008 {
3009 eap->cmdidx = CMD_substitute;
3010 ++p;
3011 }
3012 else
3013 {
3014 while (ASCII_ISALPHA(*p))
3015 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003016 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003017 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003018 while (ASCII_ISALNUM(*p))
3019 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003020
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 /* check for non-alpha command */
3022 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3023 ++p;
3024 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003025 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3026 {
3027 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3028 * :delete with the 'l' flag. Same for 'p'. */
3029 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003030 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003031 break;
3032 if (i == len - 1)
3033 {
3034 --len;
3035 if (p[-1] == 'l')
3036 eap->flags |= EXFLAG_LIST;
3037 else
3038 eap->flags |= EXFLAG_PRINT;
3039 }
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
3042 if (ASCII_ISLOWER(*eap->cmd))
3043 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
3044 else
3045 eap->cmdidx = cmdidxs[26];
3046
3047 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3048 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3049 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3050 (size_t)len) == 0)
3051 {
3052#ifdef FEAT_EVAL
3053 if (full != NULL
3054 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3055 *full = TRUE;
3056#endif
3057 break;
3058 }
3059
3060#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003061 /* Look for a user defined command as a last resort. Let ":Print" be
3062 * overruled by a user defined command. */
3063 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3064 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003066 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 while (ASCII_ISALNUM(*p))
3068 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003069 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003072 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 eap->cmdidx = CMD_SIZE;
3074 }
3075
3076 return p;
3077}
3078
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003079#ifdef FEAT_USR_CMDS
3080/*
3081 * Search for a user command that matches "eap->cmd".
3082 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
3083 * Return a pointer to just after the command.
3084 * Return NULL if there is no matching command.
3085 */
3086 static char_u *
3087find_ucmd(eap, p, full, xp, compl)
3088 exarg_T *eap;
3089 char_u *p; /* end of the command (possibly including count) */
3090 int *full; /* set to TRUE for a full match */
3091 expand_T *xp; /* used for completion, NULL otherwise */
3092 int *compl; /* completion flags or NULL */
3093{
3094 int len = (int)(p - eap->cmd);
3095 int j, k, matchlen = 0;
3096 ucmd_T *uc;
3097 int found = FALSE;
3098 int possible = FALSE;
3099 char_u *cp, *np; /* Point into typed cmd and test name */
3100 garray_T *gap;
3101 int amb_local = FALSE; /* Found ambiguous buffer-local command,
3102 only full match global is accepted. */
3103
3104 /*
3105 * Look for buffer-local user commands first, then global ones.
3106 */
3107 gap = &curbuf->b_ucmds;
3108 for (;;)
3109 {
3110 for (j = 0; j < gap->ga_len; ++j)
3111 {
3112 uc = USER_CMD_GA(gap, j);
3113 cp = eap->cmd;
3114 np = uc->uc_name;
3115 k = 0;
3116 while (k < len && *np != NUL && *cp++ == *np++)
3117 k++;
3118 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
3119 {
3120 /* If finding a second match, the command is ambiguous. But
3121 * not if a buffer-local command wasn't a full match and a
3122 * global command is a full match. */
3123 if (k == len && found && *np != NUL)
3124 {
3125 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003127 amb_local = TRUE;
3128 }
3129
3130 if (!found || (k == len && *np == NUL))
3131 {
3132 /* If we matched up to a digit, then there could
3133 * be another command including the digit that we
3134 * should use instead.
3135 */
3136 if (k == len)
3137 found = TRUE;
3138 else
3139 possible = TRUE;
3140
3141 if (gap == &ucmds)
3142 eap->cmdidx = CMD_USER;
3143 else
3144 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003145 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003146 eap->useridx = j;
3147
3148# ifdef FEAT_CMDL_COMPL
3149 if (compl != NULL)
3150 *compl = uc->uc_compl;
3151# ifdef FEAT_EVAL
3152 if (xp != NULL)
3153 {
3154 xp->xp_arg = uc->uc_compl_arg;
3155 xp->xp_scriptID = uc->uc_scriptID;
3156 }
3157# endif
3158# endif
3159 /* Do not search for further abbreviations
3160 * if this is an exact match. */
3161 matchlen = k;
3162 if (k == len && *np == NUL)
3163 {
3164 if (full != NULL)
3165 *full = TRUE;
3166 amb_local = FALSE;
3167 break;
3168 }
3169 }
3170 }
3171 }
3172
3173 /* Stop if we found a full match or searched all. */
3174 if (j < gap->ga_len || gap == &ucmds)
3175 break;
3176 gap = &ucmds;
3177 }
3178
3179 /* Only found ambiguous matches. */
3180 if (amb_local)
3181 {
3182 if (xp != NULL)
3183 xp->xp_context = EXPAND_UNSUCCESSFUL;
3184 return NULL;
3185 }
3186
3187 /* The match we found may be followed immediately by a number. Move "p"
3188 * back to point to it. */
3189 if (found || possible)
3190 return p + (matchlen - len);
3191 return p;
3192}
3193#endif
3194
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003196static struct cmdmod
3197{
3198 char *name;
3199 int minlen;
3200 int has_count; /* :123verbose :3tab */
3201} cmdmods[] = {
3202 {"aboveleft", 3, FALSE},
3203 {"belowright", 3, FALSE},
3204 {"botright", 2, FALSE},
3205 {"browse", 3, FALSE},
3206 {"confirm", 4, FALSE},
3207 {"hide", 3, FALSE},
3208 {"keepalt", 5, FALSE},
3209 {"keepjumps", 5, FALSE},
3210 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003211 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003212 {"leftabove", 5, FALSE},
3213 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003214 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003215 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003216 {"rightbelow", 6, FALSE},
3217 {"sandbox", 3, FALSE},
3218 {"silent", 3, FALSE},
3219 {"tab", 3, TRUE},
3220 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003221 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003222 {"verbose", 4, TRUE},
3223 {"vertical", 4, FALSE},
3224};
3225
3226/*
3227 * Return length of a command modifier (including optional count).
3228 * Return zero when it's not a modifier.
3229 */
3230 int
3231modifier_len(cmd)
3232 char_u *cmd;
3233{
3234 int i, j;
3235 char_u *p = cmd;
3236
3237 if (VIM_ISDIGIT(*cmd))
3238 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003239 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003240 {
3241 for (j = 0; p[j] != NUL; ++j)
3242 if (p[j] != cmdmods[i].name[j])
3243 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003244 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003245 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003246 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003247 }
3248 return 0;
3249}
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251/*
3252 * Return > 0 if an Ex command "name" exists.
3253 * Return 2 if there is an exact match.
3254 * Return 3 if there is an ambiguous match.
3255 */
3256 int
3257cmd_exists(name)
3258 char_u *name;
3259{
3260 exarg_T ea;
3261 int full = FALSE;
3262 int i;
3263 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003264 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
3266 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003267 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 for (j = 0; name[j] != NUL; ++j)
3270 if (name[j] != cmdmods[i].name[j])
3271 break;
3272 if (name[j] == NUL && j >= cmdmods[i].minlen)
3273 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3274 }
3275
Bram Moolenaara9587612006-05-04 21:47:50 +00003276 /* Check built-in commands and user defined commands.
3277 * For ":2match" and ":3match" we need to skip the number. */
3278 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003280 p = find_command(&ea, &full);
3281 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003283 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3284 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003285 if (*skipwhite(p) != NUL)
3286 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3288}
3289#endif
3290
3291/*
3292 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3293 * we don't need/want deleted. Maybe this could be done better if we didn't
3294 * repeat all this stuff. The only problem is that they may not stay
3295 * perfectly compatible with each other, but then the command line syntax
3296 * probably won't change that much -- webb.
3297 */
3298 char_u *
3299set_one_cmd_context(xp, buff)
3300 expand_T *xp;
3301 char_u *buff; /* buffer for command string */
3302{
3303 char_u *p;
3304 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003305 int len = 0;
3306 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3308 int compl = EXPAND_NOTHING;
3309#endif
3310#ifdef FEAT_CMDL_COMPL
3311 int delim;
3312#endif
3313 int forceit = FALSE;
3314 int usefilter = FALSE; /* filter instead of file name */
3315
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003316 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 xp->xp_pattern = buff;
3318 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
3321/*
3322 * 2. skip comment lines and leading space, colons or bars
3323 */
3324 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3325 ;
3326 xp->xp_pattern = cmd;
3327
3328 if (*cmd == NUL)
3329 return NULL;
3330 if (*cmd == '"') /* ignore comment lines */
3331 {
3332 xp->xp_context = EXPAND_NOTHING;
3333 return NULL;
3334 }
3335
3336/*
3337 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3338 */
3339 cmd = skip_range(cmd, &xp->xp_context);
3340
3341/*
3342 * 4. parse command
3343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 xp->xp_pattern = cmd;
3345 if (*cmd == NUL)
3346 return NULL;
3347 if (*cmd == '"')
3348 {
3349 xp->xp_context = EXPAND_NOTHING;
3350 return NULL;
3351 }
3352
3353 if (*cmd == '|' || *cmd == '\n')
3354 return cmd + 1; /* There's another command */
3355
3356 /*
3357 * Isolate the command and search for it in the command table.
3358 * Exceptions:
3359 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003360 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3362 */
3363 if (*cmd == 'k' && cmd[1] != 'e')
3364 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003365 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 p = cmd + 1;
3367 }
3368 else
3369 {
3370 p = cmd;
3371 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3372 ++p;
3373 /* check for non-alpha command */
3374 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3375 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003376 /* for python 3.x: ":py3*" commands completion */
3377 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003378 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003379 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003380 while (ASCII_ISALPHA(*p) || *p == '*')
3381 ++p;
3382 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003383 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003385 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 {
3387 xp->xp_context = EXPAND_UNSUCCESSFUL;
3388 return NULL;
3389 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003390 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003391 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3392 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3393 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 break;
3395
3396#ifdef FEAT_USR_CMDS
3397 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3399 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400#endif
3401 }
3402
3403 /*
3404 * If the cursor is touching the command, and it ends in an alpha-numeric
3405 * character, complete the command name.
3406 */
3407 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3408 return NULL;
3409
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003410 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
3412 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3413 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003414 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 p = cmd + 1;
3416 }
3417#ifdef FEAT_USR_CMDS
3418 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3419 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 ea.cmd = cmd;
3421 p = find_ucmd(&ea, p, NULL, xp,
3422# if defined(FEAT_CMDL_COMPL)
3423 &compl
3424# else
3425 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003428 if (p == NULL)
3429 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
3431#endif
3432 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003433 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 {
3435 /* Not still touching the command and it was an illegal one */
3436 xp->xp_context = EXPAND_UNSUCCESSFUL;
3437 return NULL;
3438 }
3439
3440 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3441
3442 if (*p == '!') /* forced commands */
3443 {
3444 forceit = TRUE;
3445 ++p;
3446 }
3447
3448/*
3449 * 5. parse arguments
3450 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003451 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003452 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453
3454 arg = skipwhite(p);
3455
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003456 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458 if (*arg == '>') /* append */
3459 {
3460 if (*++arg == '>')
3461 ++arg;
3462 arg = skipwhite(arg);
3463 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003464 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
3466 ++arg;
3467 usefilter = TRUE;
3468 }
3469 }
3470
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003471 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
3473 usefilter = forceit; /* :r! filter if forced */
3474 if (*arg == '!') /* :r !filter */
3475 {
3476 ++arg;
3477 usefilter = TRUE;
3478 }
3479 }
3480
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003481 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 {
3483 while (*arg == *cmd) /* allow any number of '>' or '<' */
3484 ++arg;
3485 arg = skipwhite(arg);
3486 }
3487
3488 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003489 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 {
3491 /* Check if we're in the +command */
3492 p = arg + 1;
3493 arg = skip_cmd_arg(arg, FALSE);
3494
3495 /* Still touching the command after '+'? */
3496 if (*arg == NUL)
3497 return p;
3498
3499 /* Skip space(s) after +command to get to the real argument */
3500 arg = skipwhite(arg);
3501 }
3502
3503 /*
3504 * Check for '|' to separate commands and '"' to start comments.
3505 * Don't do this for ":read !cmd" and ":write !cmd".
3506 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003507 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 p = arg;
3510 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 p += 2;
3513 while (*p)
3514 {
3515 if (*p == Ctrl_V)
3516 {
3517 if (p[1] != NUL)
3518 ++p;
3519 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003520 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 || *p == '|' || *p == '\n')
3522 {
3523 if (*(p - 1) != '\\')
3524 {
3525 if (*p == '|' || *p == '\n')
3526 return p + 1;
3527 return NULL; /* It's a comment */
3528 }
3529 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003530 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532 }
3533
3534 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003535 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 vim_strchr((char_u *)"|\"", *arg) == NULL)
3537 return NULL;
3538
3539 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003540 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003542 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003543 while (*p && p < buff + len)
3544 {
3545 if (*p == ' ' || *p == TAB)
3546 {
3547 /* argument starts after a space */
3548 xp->xp_pattern = ++p;
3549 }
3550 else
3551 {
3552 if (*p == '\\' && *(p + 1) != NUL)
3553 ++p; /* skip over escaped character */
3554 mb_ptr_adv(p);
3555 }
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003558 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003560 int c;
3561 int in_quote = FALSE;
3562 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 /*
3565 * Allow spaces within back-quotes to count as part of the argument
3566 * being expanded.
3567 */
3568 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003569 p = xp->xp_pattern;
3570 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003572#ifdef FEAT_MBYTE
3573 if (has_mbyte)
3574 c = mb_ptr2char(p);
3575 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003577 c = *p;
3578 if (c == '\\' && p[1] != NUL)
3579 ++p;
3580 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 if (!in_quote)
3583 {
3584 xp->xp_pattern = p;
3585 bow = p + 1;
3586 }
3587 in_quote = !in_quote;
3588 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003589 /* An argument can contain just about everything, except
3590 * characters that end the command and white space. */
3591 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003592#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003593 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003594#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003595 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003596 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003597 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003598 while (*p != NUL)
3599 {
3600#ifdef FEAT_MBYTE
3601 if (has_mbyte)
3602 c = mb_ptr2char(p);
3603 else
3604#endif
3605 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003606 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003607 break;
3608#ifdef FEAT_MBYTE
3609 if (has_mbyte)
3610 len = (*mb_ptr2len)(p);
3611 else
3612#endif
3613 len = 1;
3614 mb_ptr_adv(p);
3615 }
3616 if (in_quote)
3617 bow = p;
3618 else
3619 xp->xp_pattern = p;
3620 p -= len;
3621 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003622 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624
3625 /*
3626 * If we are still inside the quotes, and we passed a space, just
3627 * expand from there.
3628 */
3629 if (bow != NULL && in_quote)
3630 xp->xp_pattern = bow;
3631 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003632
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003633 /* For a shell command more chars need to be escaped. */
3634 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003635 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003636#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003637 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003638#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003639 /* When still after the command name expand executables. */
3640 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003641 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 /* Check for environment variable */
3645 if (*xp->xp_pattern == '$'
3646#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3647 || *xp->xp_pattern == '%'
3648#endif
3649 )
3650 {
3651 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3652 if (!vim_isIDc(*p))
3653 break;
3654 if (*p == NUL)
3655 {
3656 xp->xp_context = EXPAND_ENV_VARS;
3657 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003658#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3659 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003660 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003661 compl = EXPAND_ENV_VARS;
3662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003665#if defined(FEAT_CMDL_COMPL)
3666 /* Check for user names */
3667 if (*xp->xp_pattern == '~')
3668 {
3669 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3670 ;
3671 /* Complete ~user only if it partially matches a user name.
3672 * A full match ~user<Tab> will be replaced by user's home
3673 * directory i.e. something like ~user<Tab> -> /home/user/ */
3674 if (*p == NUL && p > xp->xp_pattern + 1
3675 && match_user(xp->xp_pattern + 1) == 1)
3676 {
3677 xp->xp_context = EXPAND_USER;
3678 ++xp->xp_pattern;
3679 }
3680 }
3681#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 }
3683
3684/*
3685 * 6. switch on command name
3686 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003687 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003689 case CMD_find:
3690 case CMD_sfind:
3691 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003692 if (xp->xp_context == EXPAND_FILES)
3693 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003694 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 case CMD_cd:
3696 case CMD_chdir:
3697 case CMD_lcd:
3698 case CMD_lchdir:
3699 if (xp->xp_context == EXPAND_FILES)
3700 xp->xp_context = EXPAND_DIRECTORIES;
3701 break;
3702 case CMD_help:
3703 xp->xp_context = EXPAND_HELP;
3704 xp->xp_pattern = arg;
3705 break;
3706
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003707 /* Command modifiers: return the argument.
3708 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003710 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 case CMD_belowright:
3712 case CMD_botright:
3713 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003714 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003716 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 case CMD_folddoclosed:
3718 case CMD_folddoopen:
3719 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003720 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 case CMD_keepjumps:
3722 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003723 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 case CMD_leftabove:
3725 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003726 case CMD_noautocmd:
3727 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003729 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003731 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003732 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 case CMD_topleft:
3734 case CMD_verbose:
3735 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003736 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 return arg;
3738
Bram Moolenaar4f688582007-07-24 12:34:30 +00003739#ifdef FEAT_CMDL_COMPL
3740# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 case CMD_match:
3742 if (*arg == NUL || !ends_excmd(*arg))
3743 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003744 /* also complete "None" */
3745 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 arg = skipwhite(skiptowhite(arg));
3747 if (*arg != NUL)
3748 {
3749 xp->xp_context = EXPAND_NOTHING;
3750 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3751 }
3752 }
3753 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756/*
3757 * All completion for the +cmdline_compl feature goes here.
3758 */
3759
3760# ifdef FEAT_USR_CMDS
3761 case CMD_command:
3762 /* Check for attributes */
3763 while (*arg == '-')
3764 {
3765 arg++; /* Skip "-" */
3766 p = skiptowhite(arg);
3767 if (*p == NUL)
3768 {
3769 /* Cursor is still in the attribute */
3770 p = vim_strchr(arg, '=');
3771 if (p == NULL)
3772 {
3773 /* No "=", so complete attribute names */
3774 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3775 xp->xp_pattern = arg;
3776 return NULL;
3777 }
3778
3779 /* For the -complete and -nargs attributes, we complete
3780 * their arguments as well.
3781 */
3782 if (STRNICMP(arg, "complete", p - arg) == 0)
3783 {
3784 xp->xp_context = EXPAND_USER_COMPLETE;
3785 xp->xp_pattern = p + 1;
3786 return NULL;
3787 }
3788 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3789 {
3790 xp->xp_context = EXPAND_USER_NARGS;
3791 xp->xp_pattern = p + 1;
3792 return NULL;
3793 }
3794 return NULL;
3795 }
3796 arg = skipwhite(p);
3797 }
3798
3799 /* After the attributes comes the new command name */
3800 p = skiptowhite(arg);
3801 if (*p == NUL)
3802 {
3803 xp->xp_context = EXPAND_USER_COMMANDS;
3804 xp->xp_pattern = arg;
3805 break;
3806 }
3807
3808 /* And finally comes a normal command */
3809 return skipwhite(p);
3810
3811 case CMD_delcommand:
3812 xp->xp_context = EXPAND_USER_COMMANDS;
3813 xp->xp_pattern = arg;
3814 break;
3815# endif
3816
3817 case CMD_global:
3818 case CMD_vglobal:
3819 delim = *arg; /* get the delimiter */
3820 if (delim)
3821 ++arg; /* skip delimiter if there is one */
3822
3823 while (arg[0] != NUL && arg[0] != delim)
3824 {
3825 if (arg[0] == '\\' && arg[1] != NUL)
3826 ++arg;
3827 ++arg;
3828 }
3829 if (arg[0] != NUL)
3830 return arg + 1;
3831 break;
3832 case CMD_and:
3833 case CMD_substitute:
3834 delim = *arg;
3835 if (delim)
3836 {
3837 /* skip "from" part */
3838 ++arg;
3839 arg = skip_regexp(arg, delim, p_magic, NULL);
3840 }
3841 /* skip "to" part */
3842 while (arg[0] != NUL && arg[0] != delim)
3843 {
3844 if (arg[0] == '\\' && arg[1] != NUL)
3845 ++arg;
3846 ++arg;
3847 }
3848 if (arg[0] != NUL) /* skip delimiter */
3849 ++arg;
3850 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3851 ++arg;
3852 if (arg[0] != NUL)
3853 return arg;
3854 break;
3855 case CMD_isearch:
3856 case CMD_dsearch:
3857 case CMD_ilist:
3858 case CMD_dlist:
3859 case CMD_ijump:
3860 case CMD_psearch:
3861 case CMD_djump:
3862 case CMD_isplit:
3863 case CMD_dsplit:
3864 arg = skipwhite(skipdigits(arg)); /* skip count */
3865 if (*arg == '/') /* Match regexp, not just whole words */
3866 {
3867 for (++arg; *arg && *arg != '/'; arg++)
3868 if (*arg == '\\' && arg[1] != NUL)
3869 arg++;
3870 if (*arg)
3871 {
3872 arg = skipwhite(arg + 1);
3873
3874 /* Check for trailing illegal characters */
3875 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3876 xp->xp_context = EXPAND_NOTHING;
3877 else
3878 return arg;
3879 }
3880 }
3881 break;
3882#ifdef FEAT_AUTOCMD
3883 case CMD_autocmd:
3884 return set_context_in_autocmd(xp, arg, FALSE);
3885
3886 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003887 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 return set_context_in_autocmd(xp, arg, TRUE);
3889#endif
3890 case CMD_set:
3891 set_context_in_set_cmd(xp, arg, 0);
3892 break;
3893 case CMD_setglobal:
3894 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3895 break;
3896 case CMD_setlocal:
3897 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3898 break;
3899 case CMD_tag:
3900 case CMD_stag:
3901 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003902 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_tselect:
3904 case CMD_stselect:
3905 case CMD_ptselect:
3906 case CMD_tjump:
3907 case CMD_stjump:
3908 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003909 if (*p_wop != NUL)
3910 xp->xp_context = EXPAND_TAGS_LISTFILES;
3911 else
3912 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 xp->xp_pattern = arg;
3914 break;
3915 case CMD_augroup:
3916 xp->xp_context = EXPAND_AUGROUP;
3917 xp->xp_pattern = arg;
3918 break;
3919#ifdef FEAT_SYN_HL
3920 case CMD_syntax:
3921 set_context_in_syntax_cmd(xp, arg);
3922 break;
3923#endif
3924#ifdef FEAT_EVAL
3925 case CMD_let:
3926 case CMD_if:
3927 case CMD_elseif:
3928 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003929 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 case CMD_echo:
3931 case CMD_echon:
3932 case CMD_execute:
3933 case CMD_echomsg:
3934 case CMD_echoerr:
3935 case CMD_call:
3936 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003937 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 break;
3939
3940 case CMD_unlet:
3941 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3942 arg = xp->xp_pattern + 1;
3943 xp->xp_context = EXPAND_USER_VARS;
3944 xp->xp_pattern = arg;
3945 break;
3946
3947 case CMD_function:
3948 case CMD_delfunction:
3949 xp->xp_context = EXPAND_USER_FUNC;
3950 xp->xp_pattern = arg;
3951 break;
3952
3953 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003954 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 break;
3956#endif
3957 case CMD_highlight:
3958 set_context_in_highlight_cmd(xp, arg);
3959 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003960#ifdef FEAT_CSCOPE
3961 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003962 case CMD_lcscope:
3963 case CMD_scscope:
3964 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003965 break;
3966#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003967#ifdef FEAT_SIGNS
3968 case CMD_sign:
3969 set_context_in_sign_cmd(xp, arg);
3970 break;
3971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972#ifdef FEAT_LISTCMDS
3973 case CMD_bdelete:
3974 case CMD_bwipeout:
3975 case CMD_bunload:
3976 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3977 arg = xp->xp_pattern + 1;
3978 /*FALLTHROUGH*/
3979 case CMD_buffer:
3980 case CMD_sbuffer:
3981 case CMD_checktime:
3982 xp->xp_context = EXPAND_BUFFERS;
3983 xp->xp_pattern = arg;
3984 break;
3985#endif
3986#ifdef FEAT_USR_CMDS
3987 case CMD_USER:
3988 case CMD_USER_BUF:
3989 if (compl != EXPAND_NOTHING)
3990 {
3991 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003992 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 {
3994# ifdef FEAT_MENU
3995 if (compl == EXPAND_MENUS)
3996 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3997# endif
3998 if (compl == EXPAND_COMMANDS)
3999 return arg;
4000 if (compl == EXPAND_MAPPINGS)
4001 return set_context_in_map_cmd(xp, (char_u *)"map",
4002 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004003 /* Find start of last argument. */
4004 p = arg;
4005 while (*p)
4006 {
4007 if (*p == ' ')
Bram Moolenaar848f8762012-07-25 17:22:23 +02004008 /* argument starts after a space */
4009 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004010 else if (*p == '\\' && *(p + 1) != NUL)
4011 ++p; /* skip over escaped character */
4012 mb_ptr_adv(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 xp->xp_pattern = arg;
4015 }
4016 xp->xp_context = compl;
4017 }
4018 break;
4019#endif
4020 case CMD_map: case CMD_noremap:
4021 case CMD_nmap: case CMD_nnoremap:
4022 case CMD_vmap: case CMD_vnoremap:
4023 case CMD_omap: case CMD_onoremap:
4024 case CMD_imap: case CMD_inoremap:
4025 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004026 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004027 case CMD_smap: case CMD_snoremap:
4028 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004030 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 case CMD_unmap:
4032 case CMD_nunmap:
4033 case CMD_vunmap:
4034 case CMD_ounmap:
4035 case CMD_iunmap:
4036 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004037 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004038 case CMD_sunmap:
4039 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004041 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 case CMD_abbreviate: case CMD_noreabbrev:
4043 case CMD_cabbrev: case CMD_cnoreabbrev:
4044 case CMD_iabbrev: case CMD_inoreabbrev:
4045 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004046 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 case CMD_unabbreviate:
4048 case CMD_cunabbrev:
4049 case CMD_iunabbrev:
4050 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004051 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052#ifdef FEAT_MENU
4053 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4054 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4055 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4056 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4057 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4058 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4059 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
4060 case CMD_tmenu: case CMD_tunmenu:
4061 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4062 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4063#endif
4064
4065 case CMD_colorscheme:
4066 xp->xp_context = EXPAND_COLORS;
4067 xp->xp_pattern = arg;
4068 break;
4069
4070 case CMD_compiler:
4071 xp->xp_context = EXPAND_COMPILER;
4072 xp->xp_pattern = arg;
4073 break;
4074
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004075 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004076 xp->xp_context = EXPAND_OWNSYNTAX;
4077 xp->xp_pattern = arg;
4078 break;
4079
4080 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004081 xp->xp_context = EXPAND_FILETYPE;
4082 xp->xp_pattern = arg;
4083 break;
4084
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4086 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4087 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004088 p = skiptowhite(arg);
4089 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 {
4091 xp->xp_context = EXPAND_LANGUAGE;
4092 xp->xp_pattern = arg;
4093 }
4094 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004095 {
4096 if ( STRNCMP(arg, "messages", p - arg) == 0
4097 || STRNCMP(arg, "ctype", p - arg) == 0
4098 || STRNCMP(arg, "time", p - arg) == 0)
4099 {
4100 xp->xp_context = EXPAND_LOCALES;
4101 xp->xp_pattern = skipwhite(p);
4102 }
4103 else
4104 xp->xp_context = EXPAND_NOTHING;
4105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 break;
4107#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004108#if defined(FEAT_PROFILE)
4109 case CMD_profile:
4110 set_context_in_profile_cmd(xp, arg);
4111 break;
4112#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004113 case CMD_behave:
4114 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004115 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004118#if defined(FEAT_CMDHIST)
4119 case CMD_history:
4120 xp->xp_context = EXPAND_HISTORY;
4121 xp->xp_pattern = arg;
4122 break;
4123#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004124#if defined(FEAT_PROFILE)
4125 case CMD_syntime:
4126 xp->xp_context = EXPAND_SYNTIME;
4127 xp->xp_pattern = arg;
4128 break;
4129#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#endif /* FEAT_CMDL_COMPL */
4132
4133 default:
4134 break;
4135 }
4136 return NULL;
4137}
4138
4139/*
4140 * skip a range specifier of the form: addr [,addr] [;addr] ..
4141 *
4142 * Backslashed delimiters after / or ? will be skipped, and commands will
4143 * not be expanded between /'s and ?'s or after "'".
4144 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004145 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 * Returns the "cmd" pointer advanced to beyond the range.
4147 */
4148 char_u *
4149skip_range(cmd, ctx)
4150 char_u *cmd;
4151 int *ctx; /* pointer to xp_context or NULL */
4152{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004153 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaardf177f62005-02-22 08:39:57 +00004155 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
4157 if (*cmd == '\'')
4158 {
4159 if (*++cmd == NUL && ctx != NULL)
4160 *ctx = EXPAND_NOTHING;
4161 }
4162 else if (*cmd == '/' || *cmd == '?')
4163 {
4164 delim = *cmd++;
4165 while (*cmd != NUL && *cmd != delim)
4166 if (*cmd++ == '\\' && *cmd != NUL)
4167 ++cmd;
4168 if (*cmd == NUL && ctx != NULL)
4169 *ctx = EXPAND_NOTHING;
4170 }
4171 if (*cmd != NUL)
4172 ++cmd;
4173 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004174
4175 /* Skip ":" and white space. */
4176 while (*cmd == ':')
4177 cmd = skipwhite(cmd + 1);
4178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return cmd;
4180}
4181
4182/*
4183 * get a single EX address
4184 *
4185 * Set ptr to the next character after the part that was interpreted.
4186 * Set ptr to NULL when an error is encountered.
4187 *
4188 * Return MAXLNUM when no Ex address was found.
4189 */
4190 static linenr_T
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004191get_address(ptr, addr_type, skip, to_other_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 char_u **ptr;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004193 int addr_type; /* flag: one of ADDR_LINES, ... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 int skip; /* only skip the address, don't use it */
4195 int to_other_file; /* flag: may jump to other file */
4196{
4197 int c;
4198 int i;
4199 long n;
4200 char_u *cmd;
4201 pos_T pos;
4202 pos_T *fp;
4203 linenr_T lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004204 win_T *wp;
4205 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
4207 cmd = skipwhite(*ptr);
4208 lnum = MAXLNUM;
4209 do
4210 {
4211 switch (*cmd)
4212 {
4213 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004214 ++cmd;
4215 switch (addr_type)
4216 {
4217 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 lnum = curwin->w_cursor.lnum;
4219 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004220 case ADDR_WINDOWS:
4221 lnum = 0;
4222 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4223 {
4224 lnum++;
4225 if (wp == curwin)
4226 break;
4227 }
4228 break;
4229 case ADDR_ARGUMENTS:
4230 lnum = curwin->w_arg_idx + 1;
4231 break;
4232 case ADDR_LOADED_BUFFERS:
4233 case ADDR_UNLOADED_BUFFERS:
4234 lnum = curbuf->b_fnum;
4235 break;
4236 case ADDR_TABS:
4237 lnum = 0;
4238 for(tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4239 {
4240 lnum++;
4241 if (tp == curtab)
4242 break;
4243 }
4244 break;
4245 }
4246 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004249 ++cmd;
4250 switch (addr_type)
4251 {
4252 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 lnum = curbuf->b_ml.ml_line_count;
4254 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004255 case ADDR_WINDOWS:
4256 lnum = 0;
4257 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4258 lnum++;
4259 break;
4260 case ADDR_ARGUMENTS:
4261 lnum = ARGCOUNT;
4262 break;
4263 case ADDR_LOADED_BUFFERS:
4264 case ADDR_UNLOADED_BUFFERS:
4265 lnum = lastbuf->b_fnum;
4266 break;
4267 case ADDR_TABS:
4268 lnum = 0;
4269 for(tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4270 lnum++;
4271 break;
4272 }
4273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
4275 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004276 if (*++cmd == NUL)
4277 {
4278 cmd = NULL;
4279 goto error;
4280 }
4281 if (addr_type != ADDR_LINES)
4282 {
4283 EMSG(_(e_invaddr));
4284 goto error;
4285 }
4286 if (skip)
4287 ++cmd;
4288 else
4289 {
4290 /* Only accept a mark in another file when it is
4291 * used by itself: ":'M". */
4292 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4293 ++cmd;
4294 if (fp == (pos_T *)-1)
4295 /* Jumped to another file. */
4296 lnum = curwin->w_cursor.lnum;
4297 else
4298 {
4299 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 {
4301 cmd = NULL;
4302 goto error;
4303 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004304 lnum = fp->lnum;
4305 }
4306 }
4307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308
4309 case '/':
4310 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004311 c = *cmd++;
4312 if (addr_type != ADDR_LINES)
4313 {
4314 EMSG(_(e_invaddr));
4315 goto error;
4316 }
4317 if (skip) /* skip "/pat/" */
4318 {
4319 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4320 if (*cmd == c)
4321 ++cmd;
4322 }
4323 else
4324 {
4325 pos = curwin->w_cursor; /* save curwin->w_cursor */
4326 /*
4327 * When '/' or '?' follows another address, start
4328 * from there.
4329 */
4330 if (lnum != MAXLNUM)
4331 curwin->w_cursor.lnum = lnum;
4332 /*
4333 * Start a forward search at the end of the line.
4334 * Start a backward search at the start of the line.
4335 * This makes sure we never match in the current
4336 * line, and can match anywhere in the
4337 * next/previous line.
4338 */
4339 if (c == '/')
4340 curwin->w_cursor.col = MAXCOL;
4341 else
4342 curwin->w_cursor.col = 0;
4343 searchcmdlen = 0;
4344 if (!do_search(NULL, c, cmd, 1L,
4345 SEARCH_HIS | SEARCH_MSG, NULL))
4346 {
4347 curwin->w_cursor = pos;
4348 cmd = NULL;
4349 goto error;
4350 }
4351 lnum = curwin->w_cursor.lnum;
4352 curwin->w_cursor = pos;
4353 /* adjust command string pointer */
4354 cmd += searchcmdlen;
4355 }
4356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
4358 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 ++cmd;
4360 if (addr_type != ADDR_LINES)
4361 {
4362 EMSG(_(e_invaddr));
4363 goto error;
4364 }
4365 if (*cmd == '&')
4366 i = RE_SUBST;
4367 else if (*cmd == '?' || *cmd == '/')
4368 i = RE_SEARCH;
4369 else
4370 {
4371 EMSG(_(e_backslash));
4372 cmd = NULL;
4373 goto error;
4374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 if (!skip)
4377 {
4378 /*
4379 * When search follows another address, start from
4380 * there.
4381 */
4382 if (lnum != MAXLNUM)
4383 pos.lnum = lnum;
4384 else
4385 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004387 /*
4388 * Start the search just like for the above
4389 * do_search().
4390 */
4391 if (*cmd != '?')
4392 pos.col = MAXCOL;
4393 else
4394 pos.col = 0;
4395 if (searchit(curwin, curbuf, &pos,
4396 *cmd == '?' ? BACKWARD : FORWARD,
4397 (char_u *)"", 1L, SEARCH_MSG,
4398 i, (linenr_T)0, NULL) != FAIL)
4399 lnum = pos.lnum;
4400 else
4401 {
4402 cmd = NULL;
4403 goto error;
4404 }
4405 }
4406 ++cmd;
4407 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408
4409 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004410 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4411 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413
4414 for (;;)
4415 {
4416 cmd = skipwhite(cmd);
4417 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4418 break;
4419
4420 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004421 {
4422 switch (addr_type)
4423 {
4424 case ADDR_LINES:
4425 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4426 break;
4427 case ADDR_WINDOWS:
4428 lnum = 0;
4429 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4430 {
4431 lnum++;
4432 if (wp == curwin)
4433 break;
4434 }
4435 break;
4436 case ADDR_ARGUMENTS:
4437 lnum = curwin->w_arg_idx + 1;
4438 break;
4439 case ADDR_LOADED_BUFFERS:
4440 case ADDR_UNLOADED_BUFFERS:
4441 lnum = curbuf->b_fnum;
4442 break;
4443 case ADDR_TABS:
4444 lnum = 0;
4445 for(tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4446 {
4447 lnum++;
4448 if (tp == curtab)
4449 break;
4450 }
4451 break;
4452 }
4453 }
4454
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 if (VIM_ISDIGIT(*cmd))
4456 i = '+'; /* "number" is same as "+number" */
4457 else
4458 i = *cmd++;
4459 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4460 n = 1;
4461 else
4462 n = getdigits(&cmd);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 if (addr_type == ADDR_LOADED_BUFFERS
4464 || addr_type == ADDR_UNLOADED_BUFFERS)
4465 lnum = compute_buffer_local_count(addr_type, lnum, n);
4466 else if (i == '-')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 lnum -= n;
4468 else
4469 lnum += n;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004470
4471 switch (addr_type)
4472 {
4473 case ADDR_LINES:
4474 break;
4475 case ADDR_ARGUMENTS:
4476 if (lnum < 0)
4477 lnum = 0;
4478 else if (lnum >= ARGCOUNT)
4479 lnum = ARGCOUNT;
4480 break;
4481 case ADDR_TABS:
4482 if (lnum < 0)
4483 {
4484 lnum = 0;
4485 break;
4486 }
4487 c = 0;
4488 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4489 c++;
4490 if (lnum >= c)
4491 lnum = c;
4492 break;
4493 case ADDR_WINDOWS:
4494 if (lnum < 0)
4495 {
4496 lnum = 0;
4497 break;
4498 }
4499 c = 0;
4500 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4501 c++;
4502 if (lnum > c)
4503 lnum = c;
4504 break;
4505 case ADDR_LOADED_BUFFERS:
4506 case ADDR_UNLOADED_BUFFERS:
4507 if (lnum < firstbuf->b_fnum)
4508 {
4509 lnum = firstbuf->b_fnum;
4510 break;
4511 }
4512 if (lnum > lastbuf->b_fnum)
4513 lnum = lastbuf->b_fnum;
4514 break;
4515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517 } while (*cmd == '/' || *cmd == '?');
4518
4519error:
4520 *ptr = cmd;
4521 return lnum;
4522}
4523
4524/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004525 * Get flags from an Ex command argument.
4526 */
4527 static void
4528get_flags(eap)
4529 exarg_T *eap;
4530{
4531 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4532 {
4533 if (*eap->arg == 'l')
4534 eap->flags |= EXFLAG_LIST;
4535 else if (*eap->arg == 'p')
4536 eap->flags |= EXFLAG_PRINT;
4537 else
4538 eap->flags |= EXFLAG_NR;
4539 eap->arg = skipwhite(eap->arg + 1);
4540 }
4541}
4542
4543/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 * Function called for command which is Not Implemented. NI!
4545 */
4546 void
4547ex_ni(eap)
4548 exarg_T *eap;
4549{
4550 if (!eap->skip)
4551 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4552}
4553
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004554#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555/*
4556 * Function called for script command which is Not Implemented. NI!
4557 * Skips over ":perl <<EOF" constructs.
4558 */
4559 static void
4560ex_script_ni(eap)
4561 exarg_T *eap;
4562{
4563 if (!eap->skip)
4564 ex_ni(eap);
4565 else
4566 vim_free(script_get(eap, eap->arg));
4567}
4568#endif
4569
4570/*
4571 * Check range in Ex command for validity.
4572 * Return NULL when valid, error message when invalid.
4573 */
4574 static char_u *
4575invalid_range(eap)
4576 exarg_T *eap;
4577{
4578 if ( eap->line1 < 0
4579 || eap->line2 < 0
4580 || eap->line1 > eap->line2
4581 || ((eap->argt & RANGE)
4582 && !(eap->argt & NOTADR)
4583 && eap->line2 > curbuf->b_ml.ml_line_count
4584#ifdef FEAT_DIFF
4585 + (eap->cmdidx == CMD_diffget)
4586#endif
4587 ))
4588 return (char_u *)_(e_invrange);
4589 return NULL;
4590}
4591
4592/*
4593 * Correct the range for zero line number, if required.
4594 */
4595 static void
4596correct_range(eap)
4597 exarg_T *eap;
4598{
4599 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4600 {
4601 if (eap->line1 == 0)
4602 eap->line1 = 1;
4603 if (eap->line2 == 0)
4604 eap->line2 = 1;
4605 }
4606}
4607
Bram Moolenaar748bf032005-02-02 23:04:36 +00004608#ifdef FEAT_QUICKFIX
4609static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4610
4611/*
4612 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4613 * pattern. Otherwise return eap->arg.
4614 */
4615 static char_u *
4616skip_grep_pat(eap)
4617 exarg_T *eap;
4618{
4619 char_u *p = eap->arg;
4620
Bram Moolenaara37420f2006-02-04 22:37:47 +00004621 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4622 || eap->cmdidx == CMD_vimgrepadd
4623 || eap->cmdidx == CMD_lvimgrepadd
4624 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004625 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004626 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004627 if (p == NULL)
4628 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004629 }
4630 return p;
4631}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004632
4633/*
4634 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4635 * in the command line, so that things like % get expanded.
4636 */
4637 static char_u *
4638replace_makeprg(eap, p, cmdlinep)
4639 exarg_T *eap;
4640 char_u *p;
4641 char_u **cmdlinep;
4642{
4643 char_u *new_cmdline;
4644 char_u *program;
4645 char_u *pos;
4646 char_u *ptr;
4647 int len;
4648 int i;
4649
4650 /*
4651 * Don't do it when ":vimgrep" is used for ":grep".
4652 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004653 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4654 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4655 || eap->cmdidx == CMD_grepadd
4656 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004657 && !grep_internal(eap->cmdidx))
4658 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004659 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4660 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004661 {
4662 if (*curbuf->b_p_gp == NUL)
4663 program = p_gp;
4664 else
4665 program = curbuf->b_p_gp;
4666 }
4667 else
4668 {
4669 if (*curbuf->b_p_mp == NUL)
4670 program = p_mp;
4671 else
4672 program = curbuf->b_p_mp;
4673 }
4674
4675 p = skipwhite(p);
4676
4677 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4678 {
4679 /* replace $* by given arguments */
4680 i = 1;
4681 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4682 ++i;
4683 len = (int)STRLEN(p);
4684 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4685 if (new_cmdline == NULL)
4686 return NULL; /* out of memory */
4687 ptr = new_cmdline;
4688 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4689 {
4690 i = (int)(pos - program);
4691 STRNCPY(ptr, program, i);
4692 STRCPY(ptr += i, p);
4693 ptr += len;
4694 program = pos + 2;
4695 }
4696 STRCPY(ptr, program);
4697 }
4698 else
4699 {
4700 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4701 if (new_cmdline == NULL)
4702 return NULL; /* out of memory */
4703 STRCPY(new_cmdline, program);
4704 STRCAT(new_cmdline, " ");
4705 STRCAT(new_cmdline, p);
4706 }
4707 msg_make(p);
4708
4709 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4710 vim_free(*cmdlinep);
4711 *cmdlinep = new_cmdline;
4712 p = new_cmdline;
4713 }
4714 return p;
4715}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004716#endif
4717
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718/*
4719 * Expand file name in Ex command argument.
4720 * Return FAIL for failure, OK otherwise.
4721 */
4722 int
4723expand_filename(eap, cmdlinep, errormsgp)
4724 exarg_T *eap;
4725 char_u **cmdlinep;
4726 char_u **errormsgp;
4727{
4728 int has_wildcards; /* need to expand wildcards */
4729 char_u *repl;
4730 int srclen;
4731 char_u *p;
4732 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004733 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
Bram Moolenaar748bf032005-02-02 23:04:36 +00004735#ifdef FEAT_QUICKFIX
4736 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4737 p = skip_grep_pat(eap);
4738#else
4739 p = eap->arg;
4740#endif
4741
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 /*
4743 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4744 * the file name contains a wildcard it should not cause expanding.
4745 * (it will be expanded anyway if there is a wildcard before replacing).
4746 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004747 has_wildcards = mch_has_wildcard(p);
4748 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004750#ifdef FEAT_EVAL
4751 /* Skip over `=expr`, wildcards in it are not expanded. */
4752 if (p[0] == '`' && p[1] == '=')
4753 {
4754 p += 2;
4755 (void)skip_expr(&p);
4756 if (*p == '`')
4757 ++p;
4758 continue;
4759 }
4760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 /*
4762 * Quick check if this cannot be the start of a special string.
4763 * Also removes backslash before '%', '#' and '<'.
4764 */
4765 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4766 {
4767 ++p;
4768 continue;
4769 }
4770
4771 /*
4772 * Try to find a match at this position.
4773 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004774 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4775 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 if (*errormsgp != NULL) /* error detected */
4777 return FAIL;
4778 if (repl == NULL) /* no match found */
4779 {
4780 p += srclen;
4781 continue;
4782 }
4783
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004784 /* Wildcards won't be expanded below, the replacement is taken
4785 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4786 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4787 {
4788 char_u *l = repl;
4789
4790 repl = expand_env_save(repl);
4791 vim_free(l);
4792 }
4793
Bram Moolenaar63b92542007-03-27 14:57:09 +00004794 /* Need to escape white space et al. with a backslash.
4795 * Don't do this for:
4796 * - replacement that already has been escaped: "##"
4797 * - shell commands (may have to use quotes instead).
4798 * - non-unix systems when there is a single argument (spaces don't
4799 * separate arguments then).
4800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004802 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 && eap->cmdidx != CMD_bang
4804 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004805 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004807 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004809 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810#ifndef UNIX
4811 && !(eap->argt & NOSPC)
4812#endif
4813 )
4814 {
4815 char_u *l;
4816#ifdef BACKSLASH_IN_FILENAME
4817 /* Don't escape a backslash here, because rem_backslash() doesn't
4818 * remove it later. */
4819 static char_u *nobslash = (char_u *)" \t\"|";
4820# define ESCAPE_CHARS nobslash
4821#else
4822# define ESCAPE_CHARS escape_chars
4823#endif
4824
4825 for (l = repl; *l; ++l)
4826 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4827 {
4828 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4829 if (l != NULL)
4830 {
4831 vim_free(repl);
4832 repl = l;
4833 }
4834 break;
4835 }
4836 }
4837
4838 /* For a shell command a '!' must be escaped. */
4839 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004840 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 char_u *l;
4843
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004844 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 if (l != NULL)
4846 {
4847 vim_free(repl);
4848 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 }
4850 }
4851
4852 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4853 vim_free(repl);
4854 if (p == NULL)
4855 return FAIL;
4856 }
4857
4858 /*
4859 * One file argument: Expand wildcards.
4860 * Don't do this with ":r !command" or ":w !command".
4861 */
4862 if ((eap->argt & NOSPC) && !eap->usefilter)
4863 {
4864 /*
4865 * May do this twice:
4866 * 1. Replace environment variables.
4867 * 2. Replace any other wildcards, remove backslashes.
4868 */
4869 for (n = 1; n <= 2; ++n)
4870 {
4871 if (n == 2)
4872 {
4873#ifdef UNIX
4874 /*
4875 * Only for Unix we check for more than one file name.
4876 * For other systems spaces are considered to be part
4877 * of the file name.
4878 * Only check here if there is no wildcard, otherwise
4879 * ExpandOne() will check for errors. This allows
4880 * ":e `ls ve*.c`" on Unix.
4881 */
4882 if (!has_wildcards)
4883 for (p = eap->arg; *p; ++p)
4884 {
4885 /* skip escaped characters */
4886 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4887 ++p;
4888 else if (vim_iswhite(*p))
4889 {
4890 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4891 return FAIL;
4892 }
4893 }
4894#endif
4895
4896 /*
4897 * Halve the number of backslashes (this is Vi compatible).
4898 * For Unix and OS/2, when wildcards are expanded, this is
4899 * done by ExpandOne() below.
4900 */
4901#if defined(UNIX) || defined(OS2)
4902 if (!has_wildcards)
4903#endif
4904 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906
4907 if (has_wildcards)
4908 {
4909 if (n == 1)
4910 {
4911 /*
4912 * First loop: May expand environment variables. This
4913 * can be done much faster with expand_env() than with
4914 * something else (e.g., calling a shell).
4915 * After expanding environment variables, check again
4916 * if there are still wildcards present.
4917 */
4918 if (vim_strchr(eap->arg, '$') != NULL
4919 || vim_strchr(eap->arg, '~') != NULL)
4920 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004921 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004922 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 has_wildcards = mch_has_wildcard(NameBuff);
4924 p = NameBuff;
4925 }
4926 else
4927 p = NULL;
4928 }
4929 else /* n == 2 */
4930 {
4931 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004932 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934 ExpandInit(&xpc);
4935 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004936 if (p_wic)
4937 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004939 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (p == NULL)
4941 return FAIL;
4942 }
4943 if (p != NULL)
4944 {
4945 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4946 p, cmdlinep);
4947 if (n == 2) /* p came from ExpandOne() */
4948 vim_free(p);
4949 }
4950 }
4951 }
4952 }
4953 return OK;
4954}
4955
4956/*
4957 * Replace part of the command line, keeping eap->cmd, eap->arg and
4958 * eap->nextcmd correct.
4959 * "src" points to the part that is to be replaced, of length "srclen".
4960 * "repl" is the replacement string.
4961 * Returns a pointer to the character after the replaced string.
4962 * Returns NULL for failure.
4963 */
4964 static char_u *
4965repl_cmdline(eap, src, srclen, repl, cmdlinep)
4966 exarg_T *eap;
4967 char_u *src;
4968 int srclen;
4969 char_u *repl;
4970 char_u **cmdlinep;
4971{
4972 int len;
4973 int i;
4974 char_u *new_cmdline;
4975
4976 /*
4977 * The new command line is build in new_cmdline[].
4978 * First allocate it.
4979 * Careful: a "+cmd" argument may have been NUL terminated.
4980 */
4981 len = (int)STRLEN(repl);
4982 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004983 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4985 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4986 return NULL; /* out of memory! */
4987
4988 /*
4989 * Copy the stuff before the expanded part.
4990 * Copy the expanded stuff.
4991 * Copy what came after the expanded part.
4992 * Copy the next commands, if there are any.
4993 */
4994 i = (int)(src - *cmdlinep); /* length of part before match */
4995 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 mch_memmove(new_cmdline + i, repl, (size_t)len);
4998 i += len; /* remember the end of the string */
4999 STRCPY(new_cmdline + i, src + srclen);
5000 src = new_cmdline + i; /* remember where to continue */
5001
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005002 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 {
5004 i = (int)STRLEN(new_cmdline) + 1;
5005 STRCPY(new_cmdline + i, eap->nextcmd);
5006 eap->nextcmd = new_cmdline + i;
5007 }
5008 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5009 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5010 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5011 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5012 vim_free(*cmdlinep);
5013 *cmdlinep = new_cmdline;
5014
5015 return src;
5016}
5017
5018/*
5019 * Check for '|' to separate commands and '"' to start comments.
5020 */
5021 void
5022separate_nextcmd(eap)
5023 exarg_T *eap;
5024{
5025 char_u *p;
5026
Bram Moolenaar86b68352004-12-27 21:59:20 +00005027#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005028 p = skip_grep_pat(eap);
5029#else
5030 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005031#endif
5032
5033 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 if (*p == Ctrl_V)
5036 {
5037 if (eap->argt & (USECTRLV | XFILE))
5038 ++p; /* skip CTRL-V and next char */
5039 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005040 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005041 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (*p == NUL) /* stop at NUL after CTRL-V */
5043 break;
5044 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005045
5046#ifdef FEAT_EVAL
5047 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005048 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005049 {
5050 p += 2;
5051 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005052 }
5053#endif
5054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 /* Check for '"': start of comment or '|': next command */
5056 /* :@" and :*" do not start a comment!
5057 * :redir @" doesn't either. */
5058 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5059 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5060 || p != eap->arg)
5061 && (eap->cmdidx != CMD_redir
5062 || p != eap->arg + 1 || p[-1] != '@'))
5063 || *p == '|' || *p == '\n')
5064 {
5065 /*
5066 * We remove the '\' before the '|', unless USECTRLV is used
5067 * AND 'b' is present in 'cpoptions'.
5068 */
5069 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5070 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5071 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005072 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 --p;
5074 }
5075 else
5076 {
5077 eap->nextcmd = check_nextcmd(p);
5078 *p = NUL;
5079 break;
5080 }
5081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5085 del_trailing_spaces(eap->arg);
5086}
5087
5088/*
5089 * get + command from ex argument
5090 */
5091 static char_u *
5092getargcmd(argp)
5093 char_u **argp;
5094{
5095 char_u *arg = *argp;
5096 char_u *command = NULL;
5097
5098 if (*arg == '+') /* +[command] */
5099 {
5100 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005101 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 command = dollar_command;
5103 else
5104 {
5105 command = arg;
5106 arg = skip_cmd_arg(command, TRUE);
5107 if (*arg != NUL)
5108 *arg++ = NUL; /* terminate command with NUL */
5109 }
5110
5111 arg = skipwhite(arg); /* skip over spaces */
5112 *argp = arg;
5113 }
5114 return command;
5115}
5116
5117/*
5118 * Find end of "+command" argument. Skip over "\ " and "\\".
5119 */
5120 static char_u *
5121skip_cmd_arg(p, rembs)
5122 char_u *p;
5123 int rembs; /* TRUE to halve the number of backslashes */
5124{
5125 while (*p && !vim_isspace(*p))
5126 {
5127 if (*p == '\\' && p[1] != NUL)
5128 {
5129 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005130 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 else
5132 ++p;
5133 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005134 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 return p;
5137}
5138
5139/*
5140 * Get "++opt=arg" argument.
5141 * Return FAIL or OK.
5142 */
5143 static int
5144getargopt(eap)
5145 exarg_T *eap;
5146{
5147 char_u *arg = eap->arg + 2;
5148 int *pp = NULL;
5149#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005150 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 char_u *p;
5152#endif
5153
5154 /* ":edit ++[no]bin[ary] file" */
5155 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5156 {
5157 if (*arg == 'n')
5158 {
5159 arg += 2;
5160 eap->force_bin = FORCE_NOBIN;
5161 }
5162 else
5163 eap->force_bin = FORCE_BIN;
5164 if (!checkforcmd(&arg, "binary", 3))
5165 return FAIL;
5166 eap->arg = skipwhite(arg);
5167 return OK;
5168 }
5169
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005170 /* ":read ++edit file" */
5171 if (STRNCMP(arg, "edit", 4) == 0)
5172 {
5173 eap->read_edit = TRUE;
5174 eap->arg = skipwhite(arg + 4);
5175 return OK;
5176 }
5177
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 if (STRNCMP(arg, "ff", 2) == 0)
5179 {
5180 arg += 2;
5181 pp = &eap->force_ff;
5182 }
5183 else if (STRNCMP(arg, "fileformat", 10) == 0)
5184 {
5185 arg += 10;
5186 pp = &eap->force_ff;
5187 }
5188#ifdef FEAT_MBYTE
5189 else if (STRNCMP(arg, "enc", 3) == 0)
5190 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005191 if (STRNCMP(arg, "encoding", 8) == 0)
5192 arg += 8;
5193 else
5194 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 pp = &eap->force_enc;
5196 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005197 else if (STRNCMP(arg, "bad", 3) == 0)
5198 {
5199 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005200 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202#endif
5203
5204 if (pp == NULL || *arg != '=')
5205 return FAIL;
5206
5207 ++arg;
5208 *pp = (int)(arg - eap->cmd);
5209 arg = skip_cmd_arg(arg, FALSE);
5210 eap->arg = skipwhite(arg);
5211 *arg = NUL;
5212
5213#ifdef FEAT_MBYTE
5214 if (pp == &eap->force_ff)
5215 {
5216#endif
5217 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5218 return FAIL;
5219#ifdef FEAT_MBYTE
5220 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005221 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
5223 /* Make 'fileencoding' lower case. */
5224 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5225 *p = TOLOWER_ASC(*p);
5226 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005227 else
5228 {
5229 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5230 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005231 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005232 if (STRICMP(p, "keep") == 0)
5233 eap->bad_char = BAD_KEEP;
5234 else if (STRICMP(p, "drop") == 0)
5235 eap->bad_char = BAD_DROP;
5236 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5237 eap->bad_char = *p;
5238 else
5239 return FAIL;
5240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241#endif
5242
5243 return OK;
5244}
5245
5246/*
5247 * ":abbreviate" and friends.
5248 */
5249 static void
5250ex_abbreviate(eap)
5251 exarg_T *eap;
5252{
5253 do_exmap(eap, TRUE); /* almost the same as mapping */
5254}
5255
5256/*
5257 * ":map" and friends.
5258 */
5259 static void
5260ex_map(eap)
5261 exarg_T *eap;
5262{
5263 /*
5264 * If we are sourcing .exrc or .vimrc in current directory we
5265 * print the mappings for security reasons.
5266 */
5267 if (secure)
5268 {
5269 secure = 2;
5270 msg_outtrans(eap->cmd);
5271 msg_putchar('\n');
5272 }
5273 do_exmap(eap, FALSE);
5274}
5275
5276/*
5277 * ":unmap" and friends.
5278 */
5279 static void
5280ex_unmap(eap)
5281 exarg_T *eap;
5282{
5283 do_exmap(eap, FALSE);
5284}
5285
5286/*
5287 * ":mapclear" and friends.
5288 */
5289 static void
5290ex_mapclear(eap)
5291 exarg_T *eap;
5292{
5293 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5294}
5295
5296/*
5297 * ":abclear" and friends.
5298 */
5299 static void
5300ex_abclear(eap)
5301 exarg_T *eap;
5302{
5303 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5304}
5305
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005306#if defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 static void
5308ex_autocmd(eap)
5309 exarg_T *eap;
5310{
5311 /*
5312 * Disallow auto commands from .exrc and .vimrc in current
5313 * directory for security reasons.
5314 */
5315 if (secure)
5316 {
5317 secure = 2;
5318 eap->errmsg = e_curdir;
5319 }
5320 else if (eap->cmdidx == CMD_autocmd)
5321 do_autocmd(eap->arg, eap->forceit);
5322 else
5323 do_augroup(eap->arg, eap->forceit);
5324}
5325
5326/*
5327 * ":doautocmd": Apply the automatic commands to the current buffer.
5328 */
5329 static void
5330ex_doautocmd(eap)
5331 exarg_T *eap;
5332{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005333 char_u *arg = eap->arg;
5334 int call_do_modelines = check_nomodeline(&arg);
5335
5336 (void)do_doautocmd(arg, TRUE);
5337 if (call_do_modelines) /* Only when there is no <nomodeline>. */
5338 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339}
5340#endif
5341
5342#ifdef FEAT_LISTCMDS
5343/*
5344 * :[N]bunload[!] [N] [bufname] unload buffer
5345 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5346 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5347 */
5348 static void
5349ex_bunload(eap)
5350 exarg_T *eap;
5351{
5352 eap->errmsg = do_bufdel(
5353 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5354 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5355 : DOBUF_UNLOAD, eap->arg,
5356 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5357}
5358
5359/*
5360 * :[N]buffer [N] to buffer N
5361 * :[N]sbuffer [N] to buffer N
5362 */
5363 static void
5364ex_buffer(eap)
5365 exarg_T *eap;
5366{
5367 if (*eap->arg)
5368 eap->errmsg = e_trailing;
5369 else
5370 {
5371 if (eap->addr_count == 0) /* default is current buffer */
5372 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5373 else
5374 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005375 if (eap->do_ecmd_cmd != NULL)
5376 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 }
5378}
5379
5380/*
5381 * :[N]bmodified [N] to next mod. buffer
5382 * :[N]sbmodified [N] to next mod. buffer
5383 */
5384 static void
5385ex_bmodified(eap)
5386 exarg_T *eap;
5387{
5388 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005389 if (eap->do_ecmd_cmd != NULL)
5390 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391}
5392
5393/*
5394 * :[N]bnext [N] to next buffer
5395 * :[N]sbnext [N] split and to next buffer
5396 */
5397 static void
5398ex_bnext(eap)
5399 exarg_T *eap;
5400{
5401 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005402 if (eap->do_ecmd_cmd != NULL)
5403 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404}
5405
5406/*
5407 * :[N]bNext [N] to previous buffer
5408 * :[N]bprevious [N] to previous buffer
5409 * :[N]sbNext [N] split and to previous buffer
5410 * :[N]sbprevious [N] split and to previous buffer
5411 */
5412 static void
5413ex_bprevious(eap)
5414 exarg_T *eap;
5415{
5416 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005417 if (eap->do_ecmd_cmd != NULL)
5418 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419}
5420
5421/*
5422 * :brewind to first buffer
5423 * :bfirst to first buffer
5424 * :sbrewind split and to first buffer
5425 * :sbfirst split and to first buffer
5426 */
5427 static void
5428ex_brewind(eap)
5429 exarg_T *eap;
5430{
5431 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005432 if (eap->do_ecmd_cmd != NULL)
5433 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434}
5435
5436/*
5437 * :blast to last buffer
5438 * :sblast split and to last buffer
5439 */
5440 static void
5441ex_blast(eap)
5442 exarg_T *eap;
5443{
5444 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005445 if (eap->do_ecmd_cmd != NULL)
5446 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447}
5448#endif
5449
5450 int
5451ends_excmd(c)
5452 int c;
5453{
5454 return (c == NUL || c == '|' || c == '"' || c == '\n');
5455}
5456
5457#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5458 || defined(PROTO)
5459/*
5460 * Return the next command, after the first '|' or '\n'.
5461 * Return NULL if not found.
5462 */
5463 char_u *
5464find_nextcmd(p)
5465 char_u *p;
5466{
5467 while (*p != '|' && *p != '\n')
5468 {
5469 if (*p == NUL)
5470 return NULL;
5471 ++p;
5472 }
5473 return (p + 1);
5474}
5475#endif
5476
5477/*
5478 * Check if *p is a separator between Ex commands.
5479 * Return NULL if it isn't, (p + 1) if it is.
5480 */
5481 char_u *
5482check_nextcmd(p)
5483 char_u *p;
5484{
5485 p = skipwhite(p);
5486 if (*p == '|' || *p == '\n')
5487 return (p + 1);
5488 else
5489 return NULL;
5490}
5491
5492/*
5493 * - if there are more files to edit
5494 * - and this is the last window
5495 * - and forceit not used
5496 * - and not repeated twice on a row
5497 * return FAIL and give error message if 'message' TRUE
5498 * return OK otherwise
5499 */
5500 static int
5501check_more(message, forceit)
5502 int message; /* when FALSE check only, no messages */
5503 int forceit;
5504{
5505 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5506
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005507 if (!forceit && only_one_window()
5508 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
5510 if (message)
5511 {
5512#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5513 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5514 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005515 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 if (n == 1)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02005518 vim_strncpy(buff,
5519 (char_u *)_("1 more file to edit. Quit anyway?"),
Bram Moolenaard9462e32011-04-11 21:35:11 +02005520 DIALOG_MSG_SIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 else
Bram Moolenaard9462e32011-04-11 21:35:11 +02005522 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 _("%d more files to edit. Quit anyway?"), n);
5524 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5525 return OK;
5526 return FAIL;
5527 }
5528#endif
5529 if (n == 1)
5530 EMSG(_("E173: 1 more file to edit"));
5531 else
5532 EMSGN(_("E173: %ld more files to edit"), n);
5533 quitmore = 2; /* next try to quit is allowed */
5534 }
5535 return FAIL;
5536 }
5537 return OK;
5538}
5539
5540#ifdef FEAT_CMDL_COMPL
5541/*
5542 * Function given to ExpandGeneric() to obtain the list of command names.
5543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 char_u *
5545get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005546 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 int idx;
5548{
5549 if (idx >= (int)CMD_SIZE)
5550# ifdef FEAT_USR_CMDS
5551 return get_user_command_name(idx);
5552# else
5553 return NULL;
5554# endif
5555 return cmdnames[idx].cmd_name;
5556}
5557#endif
5558
5559#if defined(FEAT_USR_CMDS) || defined(PROTO)
5560static 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));
5561static void uc_list __ARGS((char_u *name, size_t name_len));
5562static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5563static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5564static 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));
5565
5566 static int
5567uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5568 char_u *name;
5569 size_t name_len;
5570 char_u *rep;
5571 long argt;
5572 long def;
5573 int flags;
5574 int compl;
5575 char_u *compl_arg;
5576 int force;
5577{
5578 ucmd_T *cmd = NULL;
5579 char_u *p;
5580 int i;
5581 int cmp = 1;
5582 char_u *rep_buf = NULL;
5583 garray_T *gap;
5584
Bram Moolenaar9c102382006-05-03 21:26:49 +00005585 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 if (rep_buf == NULL)
5587 {
5588 /* Can't replace termcodes - try using the string as is */
5589 rep_buf = vim_strsave(rep);
5590
5591 /* Give up if out of memory */
5592 if (rep_buf == NULL)
5593 return FAIL;
5594 }
5595
5596 /* get address of growarray: global or in curbuf */
5597 if (flags & UC_BUFFER)
5598 {
5599 gap = &curbuf->b_ucmds;
5600 if (gap->ga_itemsize == 0)
5601 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5602 }
5603 else
5604 gap = &ucmds;
5605
5606 /* Search for the command in the already defined commands. */
5607 for (i = 0; i < gap->ga_len; ++i)
5608 {
5609 size_t len;
5610
5611 cmd = USER_CMD_GA(gap, i);
5612 len = STRLEN(cmd->uc_name);
5613 cmp = STRNCMP(name, cmd->uc_name, name_len);
5614 if (cmp == 0)
5615 {
5616 if (name_len < len)
5617 cmp = -1;
5618 else if (name_len > len)
5619 cmp = 1;
5620 }
5621
5622 if (cmp == 0)
5623 {
5624 if (!force)
5625 {
5626 EMSG(_("E174: Command already exists: add ! to replace it"));
5627 goto fail;
5628 }
5629
5630 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005631 cmd->uc_rep = NULL;
5632#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5633 vim_free(cmd->uc_compl_arg);
5634 cmd->uc_compl_arg = NULL;
5635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 break;
5637 }
5638
5639 /* Stop as soon as we pass the name to add */
5640 if (cmp < 0)
5641 break;
5642 }
5643
5644 /* Extend the array unless we're replacing an existing command */
5645 if (cmp != 0)
5646 {
5647 if (ga_grow(gap, 1) != OK)
5648 goto fail;
5649 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5650 goto fail;
5651
5652 cmd = USER_CMD_GA(gap, i);
5653 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5654
5655 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
5657 cmd->uc_name = p;
5658 }
5659
5660 cmd->uc_rep = rep_buf;
5661 cmd->uc_argt = argt;
5662 cmd->uc_def = def;
5663 cmd->uc_compl = compl;
5664#ifdef FEAT_EVAL
5665 cmd->uc_scriptID = current_SID;
5666# ifdef FEAT_CMDL_COMPL
5667 cmd->uc_compl_arg = compl_arg;
5668# endif
5669#endif
5670
5671 return OK;
5672
5673fail:
5674 vim_free(rep_buf);
5675#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5676 vim_free(compl_arg);
5677#endif
5678 return FAIL;
5679}
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005682#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683/*
5684 * List of names for completion for ":command" with the EXPAND_ flag.
5685 * Must be alphabetical for completion.
5686 */
5687static struct
5688{
5689 int expand;
5690 char *name;
5691} command_complete[] =
5692{
5693 {EXPAND_AUGROUP, "augroup"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005694 {EXPAND_BEHAVE, "behave"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {EXPAND_BUFFERS, "buffer"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005696 {EXPAND_COLORS, "color"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {EXPAND_COMMANDS, "command"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005698 {EXPAND_COMPILER, "compiler"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005699#if defined(FEAT_CSCOPE)
5700 {EXPAND_CSCOPE, "cscope"},
5701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5703 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005704 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705#endif
5706 {EXPAND_DIRECTORIES, "dir"},
5707 {EXPAND_ENV_VARS, "environment"},
5708 {EXPAND_EVENTS, "event"},
5709 {EXPAND_EXPRESSION, "expression"},
5710 {EXPAND_FILES, "file"},
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005711 {EXPAND_FILES_IN_PATH, "file_in_path"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005712 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {EXPAND_FUNCTIONS, "function"},
5714 {EXPAND_HELP, "help"},
5715 {EXPAND_HIGHLIGHT, "highlight"},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005716#if defined(FEAT_CMDHIST)
5717 {EXPAND_HISTORY, "history"},
5718#endif
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005719#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005720 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaare9edd7f2011-07-20 16:37:24 +02005721 {EXPAND_LOCALES, "locale"},
5722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 {EXPAND_MAPPINGS, "mapping"},
5724 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005725 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005726#if defined(FEAT_PROFILE)
5727 {EXPAND_SYNTIME, "syntime"},
5728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005730 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005731#if defined(FEAT_SIGNS)
5732 {EXPAND_SIGN, "sign"},
5733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 {EXPAND_TAGS, "tag"},
5735 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
Bram Moolenaar24305862012-08-15 14:05:05 +02005736 {EXPAND_USER, "user"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 {EXPAND_USER_VARS, "var"},
5738 {0, NULL}
5739};
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005742#if defined(FEAT_USR_CMDS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 static void
5744uc_list(name, name_len)
5745 char_u *name;
5746 size_t name_len;
5747{
5748 int i, j;
5749 int found = FALSE;
5750 ucmd_T *cmd;
5751 int len;
5752 long a;
5753 garray_T *gap;
5754
5755 gap = &curbuf->b_ucmds;
5756 for (;;)
5757 {
5758 for (i = 0; i < gap->ga_len; ++i)
5759 {
5760 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005761 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
5763 /* Skip commands which don't match the requested prefix */
5764 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5765 continue;
5766
5767 /* Put out the title first time */
5768 if (!found)
5769 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5770 found = TRUE;
5771 msg_putchar('\n');
5772 if (got_int)
5773 break;
5774
5775 /* Special cases */
5776 msg_putchar(a & BANG ? '!' : ' ');
5777 msg_putchar(a & REGSTR ? '"' : ' ');
5778 msg_putchar(gap != &ucmds ? 'b' : ' ');
5779 msg_putchar(' ');
5780
5781 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5782 len = (int)STRLEN(cmd->uc_name) + 4;
5783
5784 do {
5785 msg_putchar(' ');
5786 ++len;
5787 } while (len < 16);
5788
5789 len = 0;
5790
5791 /* Arguments */
5792 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5793 {
5794 case 0: IObuff[len++] = '0'; break;
5795 case (EXTRA): IObuff[len++] = '*'; break;
5796 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5797 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5798 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5799 }
5800
5801 do {
5802 IObuff[len++] = ' ';
5803 } while (len < 5);
5804
5805 /* Range */
5806 if (a & (RANGE|COUNT))
5807 {
5808 if (a & COUNT)
5809 {
5810 /* -count=N */
5811 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5812 len += (int)STRLEN(IObuff + len);
5813 }
5814 else if (a & DFLALL)
5815 IObuff[len++] = '%';
5816 else if (cmd->uc_def >= 0)
5817 {
5818 /* -range=N */
5819 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5820 len += (int)STRLEN(IObuff + len);
5821 }
5822 else
5823 IObuff[len++] = '.';
5824 }
5825
5826 do {
5827 IObuff[len++] = ' ';
5828 } while (len < 11);
5829
5830 /* Completion */
5831 for (j = 0; command_complete[j].expand != 0; ++j)
5832 if (command_complete[j].expand == cmd->uc_compl)
5833 {
5834 STRCPY(IObuff + len, command_complete[j].name);
5835 len += (int)STRLEN(IObuff + len);
5836 break;
5837 }
5838
5839 do {
5840 IObuff[len++] = ' ';
5841 } while (len < 21);
5842
5843 IObuff[len] = '\0';
5844 msg_outtrans(IObuff);
5845
5846 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005847#ifdef FEAT_EVAL
5848 if (p_verbose > 0)
5849 last_set_msg(cmd->uc_scriptID);
5850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 out_flush();
5852 ui_breakcheck();
5853 if (got_int)
5854 break;
5855 }
5856 if (gap == &ucmds || i < gap->ga_len)
5857 break;
5858 gap = &ucmds;
5859 }
5860
5861 if (!found)
5862 MSG(_("No user-defined commands found"));
5863}
5864
5865 static char_u *
5866uc_fun_cmd()
5867{
5868 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5869 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5870 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5871 0xb9, 0x7f, 0};
5872 int i;
5873
5874 for (i = 0; fcmd[i]; ++i)
5875 IObuff[i] = fcmd[i] - 0x40;
5876 IObuff[i] = 0;
5877 return IObuff;
5878}
5879
5880 static int
5881uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5882 char_u *attr;
5883 size_t len;
5884 long *argt;
5885 long *def;
5886 int *flags;
5887 int *compl;
5888 char_u **compl_arg;
5889{
5890 char_u *p;
5891
5892 if (len == 0)
5893 {
5894 EMSG(_("E175: No attribute specified"));
5895 return FAIL;
5896 }
5897
5898 /* First, try the simple attributes (no arguments) */
5899 if (STRNICMP(attr, "bang", len) == 0)
5900 *argt |= BANG;
5901 else if (STRNICMP(attr, "buffer", len) == 0)
5902 *flags |= UC_BUFFER;
5903 else if (STRNICMP(attr, "register", len) == 0)
5904 *argt |= REGSTR;
5905 else if (STRNICMP(attr, "bar", len) == 0)
5906 *argt |= TRLBAR;
5907 else
5908 {
5909 int i;
5910 char_u *val = NULL;
5911 size_t vallen = 0;
5912 size_t attrlen = len;
5913
5914 /* Look for the attribute name - which is the part before any '=' */
5915 for (i = 0; i < (int)len; ++i)
5916 {
5917 if (attr[i] == '=')
5918 {
5919 val = &attr[i + 1];
5920 vallen = len - i - 1;
5921 attrlen = i;
5922 break;
5923 }
5924 }
5925
5926 if (STRNICMP(attr, "nargs", attrlen) == 0)
5927 {
5928 if (vallen == 1)
5929 {
5930 if (*val == '0')
5931 /* Do nothing - this is the default */;
5932 else if (*val == '1')
5933 *argt |= (EXTRA | NOSPC | NEEDARG);
5934 else if (*val == '*')
5935 *argt |= EXTRA;
5936 else if (*val == '?')
5937 *argt |= (EXTRA | NOSPC);
5938 else if (*val == '+')
5939 *argt |= (EXTRA | NEEDARG);
5940 else
5941 goto wrong_nargs;
5942 }
5943 else
5944 {
5945wrong_nargs:
5946 EMSG(_("E176: Invalid number of arguments"));
5947 return FAIL;
5948 }
5949 }
5950 else if (STRNICMP(attr, "range", attrlen) == 0)
5951 {
5952 *argt |= RANGE;
5953 if (vallen == 1 && *val == '%')
5954 *argt |= DFLALL;
5955 else if (val != NULL)
5956 {
5957 p = val;
5958 if (*def >= 0)
5959 {
5960two_count:
5961 EMSG(_("E177: Count cannot be specified twice"));
5962 return FAIL;
5963 }
5964
5965 *def = getdigits(&p);
5966 *argt |= (ZEROR | NOTADR);
5967
5968 if (p != val + vallen || vallen == 0)
5969 {
5970invalid_count:
5971 EMSG(_("E178: Invalid default value for count"));
5972 return FAIL;
5973 }
5974 }
5975 }
5976 else if (STRNICMP(attr, "count", attrlen) == 0)
5977 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005978 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979
5980 if (val != NULL)
5981 {
5982 p = val;
5983 if (*def >= 0)
5984 goto two_count;
5985
5986 *def = getdigits(&p);
5987
5988 if (p != val + vallen)
5989 goto invalid_count;
5990 }
5991
5992 if (*def < 0)
5993 *def = 0;
5994 }
5995 else if (STRNICMP(attr, "complete", attrlen) == 0)
5996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 if (val == NULL)
5998 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005999 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 return FAIL;
6001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006003 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
6004 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 }
6007 else
6008 {
6009 char_u ch = attr[len];
6010 attr[len] = '\0';
6011 EMSG2(_("E181: Invalid attribute: %s"), attr);
6012 attr[len] = ch;
6013 return FAIL;
6014 }
6015 }
6016
6017 return OK;
6018}
6019
Bram Moolenaara850a712009-01-28 14:42:59 +00006020/*
6021 * ":command ..."
6022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 static void
6024ex_command(eap)
6025 exarg_T *eap;
6026{
6027 char_u *name;
6028 char_u *end;
6029 char_u *p;
6030 long argt = 0;
6031 long def = -1;
6032 int flags = 0;
6033 int compl = EXPAND_NOTHING;
6034 char_u *compl_arg = NULL;
6035 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006036 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
6038 p = eap->arg;
6039
6040 /* Check for attributes */
6041 while (*p == '-')
6042 {
6043 ++p;
6044 end = skiptowhite(p);
6045 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
6046 == FAIL)
6047 return;
6048 p = skipwhite(end);
6049 }
6050
6051 /* Get the name (if any) and skip to the following argument */
6052 name = p;
6053 if (ASCII_ISALPHA(*p))
6054 while (ASCII_ISALNUM(*p))
6055 ++p;
6056 if (!ends_excmd(*p) && !vim_iswhite(*p))
6057 {
6058 EMSG(_("E182: Invalid command name"));
6059 return;
6060 }
6061 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006062 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
6064 /* If there is nothing after the name, and no attributes were specified,
6065 * we are listing commands
6066 */
6067 p = skipwhite(end);
6068 if (!has_attr && ends_excmd(*p))
6069 {
6070 uc_list(name, end - name);
6071 }
6072 else if (!ASCII_ISUPPER(*name))
6073 {
6074 EMSG(_("E183: User defined commands must start with an uppercase letter"));
6075 return;
6076 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01006077 else if ((name_len == 1 && *name == 'X')
6078 || (name_len <= 4
6079 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
6080 {
6081 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
6082 return;
6083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 else
6085 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
6086 eap->forceit);
6087}
6088
6089/*
6090 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 * Clear all user commands, global and for current buffer.
6092 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00006093 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006095 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 uc_clear(&ucmds);
6098 uc_clear(&curbuf->b_ucmds);
6099}
6100
6101/*
6102 * Clear all user commands for "gap".
6103 */
6104 void
6105uc_clear(gap)
6106 garray_T *gap;
6107{
6108 int i;
6109 ucmd_T *cmd;
6110
6111 for (i = 0; i < gap->ga_len; ++i)
6112 {
6113 cmd = USER_CMD_GA(gap, i);
6114 vim_free(cmd->uc_name);
6115 vim_free(cmd->uc_rep);
6116# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6117 vim_free(cmd->uc_compl_arg);
6118# endif
6119 }
6120 ga_clear(gap);
6121}
6122
6123 static void
6124ex_delcommand(eap)
6125 exarg_T *eap;
6126{
6127 int i = 0;
6128 ucmd_T *cmd = NULL;
6129 int cmp = -1;
6130 garray_T *gap;
6131
6132 gap = &curbuf->b_ucmds;
6133 for (;;)
6134 {
6135 for (i = 0; i < gap->ga_len; ++i)
6136 {
6137 cmd = USER_CMD_GA(gap, i);
6138 cmp = STRCMP(eap->arg, cmd->uc_name);
6139 if (cmp <= 0)
6140 break;
6141 }
6142 if (gap == &ucmds || cmp == 0)
6143 break;
6144 gap = &ucmds;
6145 }
6146
6147 if (cmp != 0)
6148 {
6149 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
6150 return;
6151 }
6152
6153 vim_free(cmd->uc_name);
6154 vim_free(cmd->uc_rep);
6155# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6156 vim_free(cmd->uc_compl_arg);
6157# endif
6158
6159 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 if (i < gap->ga_len)
6162 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
6163}
6164
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006165/*
6166 * split and quote args for <f-args>
6167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 static char_u *
6169uc_split_args(arg, lenp)
6170 char_u *arg;
6171 size_t *lenp;
6172{
6173 char_u *buf;
6174 char_u *p;
6175 char_u *q;
6176 int len;
6177
6178 /* Precalculate length */
6179 p = arg;
6180 len = 2; /* Initial and final quotes */
6181
6182 while (*p)
6183 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006184 if (p[0] == '\\' && p[1] == '\\')
6185 {
6186 len += 2;
6187 p += 2;
6188 }
6189 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 {
6191 len += 1;
6192 p += 2;
6193 }
6194 else if (*p == '\\' || *p == '"')
6195 {
6196 len += 2;
6197 p += 1;
6198 }
6199 else if (vim_iswhite(*p))
6200 {
6201 p = skipwhite(p);
6202 if (*p == NUL)
6203 break;
6204 len += 3; /* "," */
6205 }
6206 else
6207 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006208#ifdef FEAT_MBYTE
6209 int charlen = (*mb_ptr2len)(p);
6210 len += charlen;
6211 p += charlen;
6212#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 ++len;
6214 ++p;
Bram Moolenaardfef1542012-07-10 19:25:10 +02006215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
6217 }
6218
6219 buf = alloc(len + 1);
6220 if (buf == NULL)
6221 {
6222 *lenp = 0;
6223 return buf;
6224 }
6225
6226 p = arg;
6227 q = buf;
6228 *q++ = '"';
6229 while (*p)
6230 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006231 if (p[0] == '\\' && p[1] == '\\')
6232 {
6233 *q++ = '\\';
6234 *q++ = '\\';
6235 p += 2;
6236 }
6237 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 {
6239 *q++ = p[1];
6240 p += 2;
6241 }
6242 else if (*p == '\\' || *p == '"')
6243 {
6244 *q++ = '\\';
6245 *q++ = *p++;
6246 }
6247 else if (vim_iswhite(*p))
6248 {
6249 p = skipwhite(p);
6250 if (*p == NUL)
6251 break;
6252 *q++ = '"';
6253 *q++ = ',';
6254 *q++ = '"';
6255 }
6256 else
6257 {
Bram Moolenaardfef1542012-07-10 19:25:10 +02006258 MB_COPY_CHAR(p, q);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
6260 }
6261 *q++ = '"';
6262 *q = 0;
6263
6264 *lenp = len;
6265 return buf;
6266}
6267
6268/*
6269 * Check for a <> code in a user command.
6270 * "code" points to the '<'. "len" the length of the <> (inclusive).
6271 * "buf" is where the result is to be added.
6272 * "split_buf" points to a buffer used for splitting, caller should free it.
6273 * "split_len" is the length of what "split_buf" contains.
6274 * Returns the length of the replacement, which has been added to "buf".
6275 * Returns -1 if there was no match, and only the "<" has been copied.
6276 */
6277 static size_t
6278uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
6279 char_u *code;
6280 size_t len;
6281 char_u *buf;
6282 ucmd_T *cmd; /* the user command we're expanding */
6283 exarg_T *eap; /* ex arguments */
6284 char_u **split_buf;
6285 size_t *split_len;
6286{
6287 size_t result = 0;
6288 char_u *p = code + 1;
6289 size_t l = len - 2;
6290 int quote = 0;
6291 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
6292 ct_LT, ct_NONE } type = ct_NONE;
6293
6294 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
6295 {
6296 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
6297 p += 2;
6298 l -= 2;
6299 }
6300
Bram Moolenaar371d5402006-03-20 21:47:49 +00006301 ++l;
6302 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006304 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006306 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006308 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006310 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006312 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006314 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00006316 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 type = ct_REGISTER;
6318
6319 switch (type)
6320 {
6321 case ct_ARGS:
6322 /* Simple case first */
6323 if (*eap->arg == NUL)
6324 {
6325 if (quote == 1)
6326 {
6327 result = 2;
6328 if (buf != NULL)
6329 STRCPY(buf, "''");
6330 }
6331 else
6332 result = 0;
6333 break;
6334 }
6335
6336 /* When specified there is a single argument don't split it.
6337 * Works for ":Cmd %" when % is "a b c". */
6338 if ((eap->argt & NOSPC) && quote == 2)
6339 quote = 1;
6340
6341 switch (quote)
6342 {
6343 case 0: /* No quoting, no splitting */
6344 result = STRLEN(eap->arg);
6345 if (buf != NULL)
6346 STRCPY(buf, eap->arg);
6347 break;
6348 case 1: /* Quote, but don't split */
6349 result = STRLEN(eap->arg) + 2;
6350 for (p = eap->arg; *p; ++p)
6351 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006352#ifdef FEAT_MBYTE
6353 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6354 /* DBCS can contain \ in a trail byte, skip the
6355 * double-byte character. */
6356 ++p;
6357 else
6358#endif
6359 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 ++result;
6361 }
6362
6363 if (buf != NULL)
6364 {
6365 *buf++ = '"';
6366 for (p = eap->arg; *p; ++p)
6367 {
Bram Moolenaar7d550fb2012-01-26 20:41:26 +01006368#ifdef FEAT_MBYTE
6369 if (enc_dbcs != 0 && (*mb_ptr2len)(p) == 2)
6370 /* DBCS can contain \ in a trail byte, copy the
6371 * double-byte character to avoid escaping. */
6372 *buf++ = *p++;
6373 else
6374#endif
6375 if (*p == '\\' || *p == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 *buf++ = '\\';
6377 *buf++ = *p;
6378 }
6379 *buf = '"';
6380 }
6381
6382 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00006383 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 /* This is hard, so only do it once, and cache the result */
6385 if (*split_buf == NULL)
6386 *split_buf = uc_split_args(eap->arg, split_len);
6387
6388 result = *split_len;
6389 if (buf != NULL && result != 0)
6390 STRCPY(buf, *split_buf);
6391
6392 break;
6393 }
6394 break;
6395
6396 case ct_BANG:
6397 result = eap->forceit ? 1 : 0;
6398 if (quote)
6399 result += 2;
6400 if (buf != NULL)
6401 {
6402 if (quote)
6403 *buf++ = '"';
6404 if (eap->forceit)
6405 *buf++ = '!';
6406 if (quote)
6407 *buf = '"';
6408 }
6409 break;
6410
6411 case ct_LINE1:
6412 case ct_LINE2:
6413 case ct_COUNT:
6414 {
6415 char num_buf[20];
6416 long num = (type == ct_LINE1) ? eap->line1 :
6417 (type == ct_LINE2) ? eap->line2 :
6418 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
6419 size_t num_len;
6420
6421 sprintf(num_buf, "%ld", num);
6422 num_len = STRLEN(num_buf);
6423 result = num_len;
6424
6425 if (quote)
6426 result += 2;
6427
6428 if (buf != NULL)
6429 {
6430 if (quote)
6431 *buf++ = '"';
6432 STRCPY(buf, num_buf);
6433 buf += num_len;
6434 if (quote)
6435 *buf = '"';
6436 }
6437
6438 break;
6439 }
6440
6441 case ct_REGISTER:
6442 result = eap->regname ? 1 : 0;
6443 if (quote)
6444 result += 2;
6445 if (buf != NULL)
6446 {
6447 if (quote)
6448 *buf++ = '\'';
6449 if (eap->regname)
6450 *buf++ = eap->regname;
6451 if (quote)
6452 *buf = '\'';
6453 }
6454 break;
6455
6456 case ct_LT:
6457 result = 1;
6458 if (buf != NULL)
6459 *buf = '<';
6460 break;
6461
6462 default:
6463 /* Not recognized: just copy the '<' and return -1. */
6464 result = (size_t)-1;
6465 if (buf != NULL)
6466 *buf = '<';
6467 break;
6468 }
6469
6470 return result;
6471}
6472
6473 static void
6474do_ucmd(eap)
6475 exarg_T *eap;
6476{
6477 char_u *buf;
6478 char_u *p;
6479 char_u *q;
6480
6481 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006482 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006483 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 size_t len, totlen;
6485
6486 size_t split_len = 0;
6487 char_u *split_buf = NULL;
6488 ucmd_T *cmd;
6489#ifdef FEAT_EVAL
6490 scid_T save_current_SID = current_SID;
6491#endif
6492
6493 if (eap->cmdidx == CMD_USER)
6494 cmd = USER_CMD(eap->useridx);
6495 else
6496 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6497
6498 /*
6499 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006500 * First round: "buf" is NULL, compute length, allocate "buf".
6501 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 */
6503 buf = NULL;
6504 for (;;)
6505 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006506 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006507 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006509
6510 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006512 start = vim_strchr(p, '<');
6513 if (start != NULL)
6514 end = vim_strchr(start + 1, '>');
6515 if (buf != NULL)
6516 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006517 for (ksp = p; *ksp != NUL && *ksp != K_SPECIAL; ++ksp)
6518 ;
6519 if (*ksp == K_SPECIAL
6520 && (start == NULL || ksp < start || end == NULL)
Bram Moolenaara850a712009-01-28 14:42:59 +00006521 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6522# ifdef FEAT_GUI
6523 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6524# endif
6525 ))
6526 {
Bram Moolenaarf63c49d2011-03-03 15:54:50 +01006527 /* K_SPECIAL has been put in the buffer as K_SPECIAL
Bram Moolenaara850a712009-01-28 14:42:59 +00006528 * KS_SPECIAL KE_FILLER, like for mappings, but
6529 * do_cmdline() doesn't handle that, so convert it back.
6530 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6531 len = ksp - p;
6532 if (len > 0)
6533 {
6534 mch_memmove(q, p, len);
6535 q += len;
6536 }
6537 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6538 p = ksp + 3;
6539 continue;
6540 }
6541 }
6542
6543 /* break if there no <item> is found */
6544 if (start == NULL || end == NULL)
6545 break;
6546
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 /* Include the '>' */
6548 ++end;
6549
6550 /* Take everything up to the '<' */
6551 len = start - p;
6552 if (buf == NULL)
6553 totlen += len;
6554 else
6555 {
6556 mch_memmove(q, p, len);
6557 q += len;
6558 }
6559
6560 len = uc_check_code(start, end - start, q, cmd, eap,
6561 &split_buf, &split_len);
6562 if (len == (size_t)-1)
6563 {
6564 /* no match, continue after '<' */
6565 p = start + 1;
6566 len = 1;
6567 }
6568 else
6569 p = end;
6570 if (buf == NULL)
6571 totlen += len;
6572 else
6573 q += len;
6574 }
6575 if (buf != NULL) /* second time here, finished */
6576 {
6577 STRCPY(q, p);
6578 break;
6579 }
6580
6581 totlen += STRLEN(p); /* Add on the trailing characters */
6582 buf = alloc((unsigned)(totlen + 1));
6583 if (buf == NULL)
6584 {
6585 vim_free(split_buf);
6586 return;
6587 }
6588 }
6589
6590#ifdef FEAT_EVAL
6591 current_SID = cmd->uc_scriptID;
6592#endif
6593 (void)do_cmdline(buf, eap->getline, eap->cookie,
6594 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6595#ifdef FEAT_EVAL
6596 current_SID = save_current_SID;
6597#endif
6598 vim_free(buf);
6599 vim_free(split_buf);
6600}
6601
6602# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6603 static char_u *
6604get_user_command_name(idx)
6605 int idx;
6606{
6607 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6608}
6609
6610/*
6611 * Function given to ExpandGeneric() to obtain the list of user command names.
6612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 char_u *
6614get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006615 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 int idx;
6617{
6618 if (idx < curbuf->b_ucmds.ga_len)
6619 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6620 idx -= curbuf->b_ucmds.ga_len;
6621 if (idx < ucmds.ga_len)
6622 return USER_CMD(idx)->uc_name;
6623 return NULL;
6624}
6625
6626/*
6627 * Function given to ExpandGeneric() to obtain the list of user command
6628 * attributes.
6629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 char_u *
6631get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006632 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 int idx;
6634{
6635 static char *user_cmd_flags[] =
6636 {"bang", "bar", "buffer", "complete", "count",
6637 "nargs", "range", "register"};
6638
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006639 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 return NULL;
6641 return (char_u *)user_cmd_flags[idx];
6642}
6643
6644/*
6645 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 char_u *
6648get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006649 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 int idx;
6651{
6652 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6653
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006654 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 return NULL;
6656 return (char_u *)user_cmd_nargs[idx];
6657}
6658
6659/*
6660 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6661 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 char_u *
6663get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006664 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 int idx;
6666{
6667 return (char_u *)command_complete[idx].name;
6668}
6669# endif /* FEAT_CMDL_COMPL */
6670
6671#endif /* FEAT_USR_CMDS */
6672
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006673#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6674/*
6675 * Parse a completion argument "value[vallen]".
6676 * The detected completion goes in "*complp", argument type in "*argt".
6677 * When there is an argument, for function and user defined completion, it's
6678 * copied to allocated memory and stored in "*compl_arg".
6679 * Returns FAIL if something is wrong.
6680 */
6681 int
6682parse_compl_arg(value, vallen, complp, argt, compl_arg)
6683 char_u *value;
6684 int vallen;
6685 int *complp;
6686 long *argt;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006687 char_u **compl_arg UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006688{
6689 char_u *arg = NULL;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006690# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006691 size_t arglen = 0;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006692# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006693 int i;
6694 int valend = vallen;
6695
6696 /* Look for any argument part - which is the part after any ',' */
6697 for (i = 0; i < vallen; ++i)
6698 {
6699 if (value[i] == ',')
6700 {
6701 arg = &value[i + 1];
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006702# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006703 arglen = vallen - i - 1;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006704# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006705 valend = i;
6706 break;
6707 }
6708 }
6709
6710 for (i = 0; command_complete[i].expand != 0; ++i)
6711 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006712 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006713 && STRNCMP(value, command_complete[i].name, valend) == 0)
6714 {
6715 *complp = command_complete[i].expand;
6716 if (command_complete[i].expand == EXPAND_BUFFERS)
6717 *argt |= BUFNAME;
6718 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6719 || command_complete[i].expand == EXPAND_FILES)
6720 *argt |= XFILE;
6721 break;
6722 }
6723 }
6724
6725 if (command_complete[i].expand == 0)
6726 {
6727 EMSG2(_("E180: Invalid complete value: %s"), value);
6728 return FAIL;
6729 }
6730
6731# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6732 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6733 && arg != NULL)
6734# else
6735 if (arg != NULL)
6736# endif
6737 {
6738 EMSG(_("E468: Completion argument only allowed for custom completion"));
6739 return FAIL;
6740 }
6741
6742# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6743 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6744 && arg == NULL)
6745 {
6746 EMSG(_("E467: Custom completion requires a function argument"));
6747 return FAIL;
6748 }
6749
6750 if (arg != NULL)
6751 *compl_arg = vim_strnsave(arg, (int)arglen);
6752# endif
6753 return OK;
6754}
6755#endif
6756
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 static void
6758ex_colorscheme(eap)
6759 exarg_T *eap;
6760{
Bram Moolenaare6850792010-05-14 15:28:44 +02006761 if (*eap->arg == NUL)
6762 {
6763#ifdef FEAT_EVAL
6764 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6765 char_u *p = NULL;
6766
6767 if (expr != NULL)
6768 {
6769 ++emsg_off;
6770 p = eval_to_string(expr, NULL, FALSE);
6771 --emsg_off;
6772 vim_free(expr);
6773 }
6774 if (p != NULL)
6775 {
6776 MSG(p);
6777 vim_free(p);
6778 }
6779 else
6780 MSG("default");
6781#else
6782 MSG(_("unknown"));
6783#endif
6784 }
6785 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarbe1e9e92012-09-18 16:47:07 +02006786 EMSG2(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787}
6788
6789 static void
6790ex_highlight(eap)
6791 exarg_T *eap;
6792{
6793 if (*eap->arg == NUL && eap->cmd[2] == '!')
6794 MSG(_("Greetings, Vim user!"));
6795 do_highlight(eap->arg, eap->forceit, FALSE);
6796}
6797
6798
6799/*
6800 * Call this function if we thought we were going to exit, but we won't
6801 * (because of an error). May need to restore the terminal mode.
6802 */
6803 void
6804not_exiting()
6805{
6806 exiting = FALSE;
6807 settmode(TMODE_RAW);
6808}
6809
6810/*
6811 * ":quit": quit current window, quit Vim if closed the last window.
6812 */
6813 static void
6814ex_quit(eap)
6815 exarg_T *eap;
6816{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006817 win_T *wp;
6818 buf_T *buf;
6819 int wnr;
6820
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821#ifdef FEAT_CMDWIN
6822 if (cmdwin_type != 0)
6823 {
6824 cmdwin_result = Ctrl_C;
6825 return;
6826 }
6827#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006828 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006829 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006830 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006831 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006832 return;
6833 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006834 if (eap->addr_count > 0)
6835 {
6836 wnr = eap->line2;
6837 for (wp = firstwin; --wnr > 0; )
6838 {
6839 if (wp->w_next == NULL)
6840 break;
6841 else
6842 wp = wp->w_next;
6843 }
6844 buf = wp->w_buffer;
6845 }
6846 else
6847 {
6848 wp = curwin;
6849 buf = curbuf;
6850 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006851#ifdef FEAT_AUTOCMD
Bram Moolenaar3b53dfb2012-06-06 18:03:07 +02006852 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006853 /* Refuse to quit when locked or when the buffer in the last window is
Bram Moolenaar362ce482012-06-06 19:02:45 +02006854 * being closed (can only happen in autocommands). */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006855 if (curbuf_locked() || (buf->b_nwindows == 1 && buf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006856 return;
6857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858
6859#ifdef FEAT_NETBEANS_INTG
6860 netbeansForcedQuit = eap->forceit;
6861#endif
6862
6863 /*
6864 * If there are more files or windows we won't exit.
6865 */
6866 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6867 exiting = TRUE;
6868 if ((!P_HID(curbuf)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006869 && check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6870 | (eap->forceit ? CCGD_FORCEIT : 0)
6871 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 || check_more(TRUE, eap->forceit) == FAIL
6873 || (only_one_window() && check_changed_any(eap->forceit)))
6874 {
6875 not_exiting();
6876 }
6877 else
6878 {
6879#ifdef FEAT_WINDOWS
6880 if (only_one_window()) /* quit last window */
6881#endif
6882 getout(0);
6883#ifdef FEAT_WINDOWS
6884# ifdef FEAT_GUI
6885 need_mouse_correct = TRUE;
6886# endif
6887 /* close window; may free buffer */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006888 win_close(wp, !P_HID(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889#endif
6890 }
6891}
6892
6893/*
6894 * ":cquit".
6895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 static void
6897ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006898 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
6900 getout(1); /* this does not always pass on the exit code to the Manx
6901 compiler. why? */
6902}
6903
6904/*
6905 * ":qall": try to quit all windows
6906 */
6907 static void
6908ex_quit_all(eap)
6909 exarg_T *eap;
6910{
6911# ifdef FEAT_CMDWIN
6912 if (cmdwin_type != 0)
6913 {
6914 if (eap->forceit)
6915 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6916 else
6917 cmdwin_result = K_XF2;
6918 return;
6919 }
6920# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006921
6922 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006923 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006924 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006925 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006926 return;
6927 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006928#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01006929 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
6930 /* Refuse to quit when locked or when the buffer in the last window is
6931 * being closed (can only happen in autocommands). */
6932 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006933 return;
6934#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006935
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 exiting = TRUE;
6937 if (eap->forceit || !check_changed_any(FALSE))
6938 getout(0);
6939 not_exiting();
6940}
6941
Bram Moolenaard9967712006-03-11 21:18:15 +00006942#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943/*
6944 * ":close": close current window, unless it is the last one
6945 */
6946 static void
6947ex_close(eap)
6948 exarg_T *eap;
6949{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006950 win_T *win;
6951 int winnr = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952# ifdef FEAT_CMDWIN
6953 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006954 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 else
6956# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006957 if (!text_locked()
6958#ifdef FEAT_AUTOCMD
6959 && !curbuf_locked()
6960#endif
6961 )
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006962 {
6963 if (eap->addr_count == 0)
6964 ex_win_close(eap->forceit, curwin, NULL);
6965 else {
6966 for (win = firstwin; win != NULL; win = win->w_next)
6967 {
6968 winnr++;
6969 if (winnr == eap->line2)
6970 break;
6971 }
6972 if (win == NULL)
6973 win = lastwin;
6974 ex_win_close(eap->forceit, win, NULL);
6975 }
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977}
6978
Bram Moolenaard9967712006-03-11 21:18:15 +00006979# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006980/*
6981 * ":pclose": Close any preview window.
6982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006984ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006986{
6987 win_T *win;
6988
6989 for (win = firstwin; win != NULL; win = win->w_next)
6990 if (win->w_p_pvw)
6991 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006992 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006993 break;
6994 }
6995}
Bram Moolenaard9967712006-03-11 21:18:15 +00006996# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006997
Bram Moolenaarf740b292006-02-16 22:11:02 +00006998/*
6999 * Close window "win" and take care of handling closing the last window for a
7000 * modified buffer.
7001 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007002 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00007003ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007004 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007006 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007{
7008 int need_hide;
7009 buf_T *buf = win->w_buffer;
7010
7011 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007012 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaard9967712006-03-11 21:18:15 +00007014# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 if ((p_confirm || cmdmod.confirm) && p_write)
7016 {
7017 dialog_changed(buf, FALSE);
7018 if (buf_valid(buf) && bufIsChanged(buf))
7019 return;
7020 need_hide = FALSE;
7021 }
7022 else
Bram Moolenaard9967712006-03-11 21:18:15 +00007023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {
7025 EMSG(_(e_nowrtmsg));
7026 return;
7027 }
7028 }
7029
Bram Moolenaard9967712006-03-11 21:18:15 +00007030# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00007032# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007033
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00007035 if (tp == NULL)
7036 win_close(win, !need_hide && !P_HID(buf));
7037 else
7038 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039}
7040
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007042 * ":tabclose": close current tab page, unless it is the last one.
7043 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 */
7045 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007046ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 exarg_T *eap;
7048{
Bram Moolenaarf740b292006-02-16 22:11:02 +00007049 tabpage_T *tp;
7050
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007051# ifdef FEAT_CMDWIN
7052 if (cmdwin_type != 0)
7053 cmdwin_result = K_IGNORE;
7054 else
7055# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00007056 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007057 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00007058 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007060 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007061 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00007062 tp = find_tabpage((int)eap->line2);
7063 if (tp == NULL)
7064 {
7065 beep_flush();
7066 return;
7067 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007068 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007069 {
7070 tabpage_close_other(tp, eap->forceit);
7071 return;
7072 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007073 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007074 if (!text_locked()
7075#ifdef FEAT_AUTOCMD
7076 && !curbuf_locked()
7077#endif
7078 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00007079 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007081}
7082
7083/*
7084 * ":tabonly": close all tab pages except the current one
7085 */
7086 static void
7087ex_tabonly(eap)
7088 exarg_T *eap;
7089{
7090 tabpage_T *tp;
7091 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007092
7093# ifdef FEAT_CMDWIN
7094 if (cmdwin_type != 0)
7095 cmdwin_result = K_IGNORE;
7096 else
7097# endif
7098 if (first_tabpage->tp_next == NULL)
7099 MSG(_("Already only one tab page"));
7100 else
7101 {
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007102 if (eap->addr_count > 0)
7103 goto_tabpage(eap->line2);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007104 /* Repeat this up to a 1000 times, because autocommands may mess
7105 * up the lists. */
7106 for (done = 0; done < 1000; ++done)
7107 {
7108 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7109 if (tp->tp_topframe != topframe)
7110 {
7111 tabpage_close_other(tp, eap->forceit);
7112 /* if we failed to close it quit */
7113 if (valid_tabpage(tp))
7114 done = 1000;
7115 /* start over, "tp" is now invalid */
7116 break;
7117 }
7118 if (first_tabpage->tp_next == NULL)
7119 break;
7120 }
7121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123
7124/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00007125 * Close the current tab page.
7126 */
7127 void
7128tabpage_close(forceit)
7129 int forceit;
7130{
7131 /* First close all the windows but the current one. If that worked then
7132 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007133 if (lastwin != firstwin)
7134 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007135 if (lastwin == firstwin)
7136 ex_win_close(forceit, curwin, NULL);
7137# ifdef FEAT_GUI
7138 need_mouse_correct = TRUE;
7139# endif
7140}
7141
7142/*
7143 * Close tab page "tp", which is not the current tab page.
7144 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007145 * Also takes care of the tab pages line disappearing when closing the
7146 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00007147 */
7148 void
7149tabpage_close_other(tp, forceit)
7150 tabpage_T *tp;
7151 int forceit;
7152{
7153 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007154 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007155 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007156
7157 /* Limit to 1000 windows, autocommands may add a window while we close
7158 * one. OK, so I'm paranoid... */
7159 while (++done < 1000)
7160 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007161 wp = tp->tp_firstwin;
7162 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00007163
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007164 /* Autocommands may delete the tab page under our fingers and we may
7165 * fail to close a window with a modified buffer. */
7166 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00007167 break;
7168 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007169
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007170 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00007171 if (h != tabline_height())
7172 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00007173}
7174
7175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 * ":only".
7177 */
7178 static void
7179ex_only(eap)
7180 exarg_T *eap;
7181{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007182 win_T *wp;
7183 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184# ifdef FEAT_GUI
7185 need_mouse_correct = TRUE;
7186# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007187 if (eap->addr_count > 0)
7188 {
7189 wnr = eap->line2;
7190 for (wp = firstwin; --wnr > 0; )
7191 {
7192 if (wp->w_next == NULL)
7193 break;
7194 else
7195 wp = wp->w_next;
7196 }
7197 win_goto(wp);
7198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 close_others(TRUE, eap->forceit);
7200}
7201
7202/*
7203 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00007204 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 */
Bram Moolenaard9967712006-03-11 21:18:15 +00007206 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207ex_all(eap)
7208 exarg_T *eap;
7209{
7210 if (eap->addr_count == 0)
7211 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00007212 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213}
7214#endif /* FEAT_WINDOWS */
7215
7216 static void
7217ex_hide(eap)
7218 exarg_T *eap;
7219{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007220 win_T *win;
7221 int winnr = 0;
7222
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
7224 eap->errmsg = e_invarg;
7225 else
7226 {
7227 /* ":hide" or ":hide | cmd": hide current window */
7228 eap->nextcmd = check_nextcmd(eap->arg);
7229#ifdef FEAT_WINDOWS
7230 if (!eap->skip)
7231 {
7232# ifdef FEAT_GUI
7233 need_mouse_correct = TRUE;
7234# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01007235 if (eap->addr_count == 0)
7236 win_close(curwin, FALSE); /* don't free buffer */
7237 else {
7238 for (win = firstwin; win != NULL; win = win->w_next)
7239 {
7240 winnr++;
7241 if (winnr == eap->line2)
7242 break;
7243 }
7244 if (win == NULL)
7245 win = lastwin;
7246 win_close(win, FALSE);
7247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
7249#endif
7250 }
7251}
7252
7253/*
7254 * ":stop" and ":suspend": Suspend Vim.
7255 */
7256 static void
7257ex_stop(eap)
7258 exarg_T *eap;
7259{
7260 /*
7261 * Disallow suspending for "rvim".
7262 */
7263 if (!check_restricted()
7264#ifdef WIN3264
7265 /*
7266 * Check if external commands are allowed now.
7267 */
7268 && can_end_termcap_mode(TRUE)
7269#endif
7270 )
7271 {
7272 if (!eap->forceit)
7273 autowrite_all();
7274 windgoto((int)Rows - 1, 0);
7275 out_char('\n');
7276 out_flush();
7277 stoptermcap();
7278 out_flush(); /* needed for SUN to restore xterm buffer */
7279#ifdef FEAT_TITLE
7280 mch_restore_title(3); /* restore window titles */
7281#endif
7282 ui_suspend(); /* call machine specific function */
7283#ifdef FEAT_TITLE
7284 maketitle();
7285 resettitle(); /* force updating the title */
7286#endif
7287 starttermcap();
7288 scroll_start(); /* scroll screen before redrawing */
7289 redraw_later_clear();
7290 shell_resized(); /* may have resized window */
7291 }
7292}
7293
7294/*
7295 * ":exit", ":xit" and ":wq": Write file and exit Vim.
7296 */
7297 static void
7298ex_exit(eap)
7299 exarg_T *eap;
7300{
7301#ifdef FEAT_CMDWIN
7302 if (cmdwin_type != 0)
7303 {
7304 cmdwin_result = Ctrl_C;
7305 return;
7306 }
7307#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007308 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007309 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007310 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007311 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007312 return;
7313 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007314#ifdef FEAT_AUTOCMD
Bram Moolenaar4f8301f2013-03-13 18:30:43 +01007315 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, curbuf);
7316 /* Refuse to quit when locked or when the buffer in the last window is
7317 * being closed (can only happen in autocommands). */
7318 if (curbuf_locked() || (curbuf->b_nwindows == 1 && curbuf->b_closing))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007319 return;
7320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
7322 /*
7323 * if more files or windows we won't exit
7324 */
7325 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
7326 exiting = TRUE;
7327 if ( ((eap->cmdidx == CMD_wq
7328 || curbufIsChanged())
7329 && do_write(eap) == FAIL)
7330 || check_more(TRUE, eap->forceit) == FAIL
7331 || (only_one_window() && check_changed_any(eap->forceit)))
7332 {
7333 not_exiting();
7334 }
7335 else
7336 {
7337#ifdef FEAT_WINDOWS
7338 if (only_one_window()) /* quit last window, exit Vim */
7339#endif
7340 getout(0);
7341#ifdef FEAT_WINDOWS
7342# ifdef FEAT_GUI
7343 need_mouse_correct = TRUE;
7344# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02007345 /* Quit current window, may free the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 win_close(curwin, !P_HID(curwin->w_buffer));
7347#endif
7348 }
7349}
7350
7351/*
7352 * ":print", ":list", ":number".
7353 */
7354 static void
7355ex_print(eap)
7356 exarg_T *eap;
7357{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007358 if (curbuf->b_ml.ml_flags & ML_EMPTY)
7359 EMSG(_(e_emptybuf));
7360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007362 for ( ;!got_int; ui_breakcheck())
7363 {
7364 print_line(eap->line1,
7365 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
7366 || (eap->flags & EXFLAG_NR)),
7367 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
7368 if (++eap->line1 > eap->line2)
7369 break;
7370 out_flush(); /* show one line at a time */
7371 }
7372 setpcmark();
7373 /* put cursor at last line */
7374 curwin->w_cursor.lnum = eap->line2;
7375 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 }
7377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379}
7380
7381#ifdef FEAT_BYTEOFF
7382 static void
7383ex_goto(eap)
7384 exarg_T *eap;
7385{
7386 goto_byte(eap->line2);
7387}
7388#endif
7389
7390/*
7391 * ":shell".
7392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 static void
7394ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007395 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396{
7397 do_shell(NULL, 0);
7398}
7399
7400#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
7401 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00007402 || defined(FEAT_GUI_MSWIN) \
7403 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 || defined(PROTO)
7405
7406/*
7407 * Handle a file drop. The code is here because a drop is *nearly* like an
7408 * :args command, but not quite (we have a list of exact filenames, so we
7409 * don't want to (a) parse a command line, or (b) expand wildcards. So the
7410 * code is very similar to :args and hence needs access to a lot of the static
7411 * functions in this file.
7412 *
7413 * The list should be allocated using alloc(), as should each item in the
7414 * list. This function takes over responsibility for freeing the list.
7415 *
Bram Moolenaar81870892007-11-11 18:17:28 +00007416 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 * FreeWild(), which does a series of vim_free() calls, unless the two defines
7418 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
7419 * routine _fnexplodefree() is used. This may cause problems, but as the drop
7420 * file functionality is (currently) not in EMX this is not presently a
7421 * problem.
7422 */
7423 void
7424handle_drop(filec, filev, split)
7425 int filec; /* the number of files dropped */
7426 char_u **filev; /* the list of files dropped */
7427 int split; /* force splitting the window */
7428{
7429 exarg_T ea;
7430 int save_msg_scroll = msg_scroll;
7431
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00007432 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00007433 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007435#ifdef FEAT_AUTOCMD
7436 if (curbuf_locked())
7437 return;
7438#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00007439 /* When the screen is being updated we should not change buffers and
7440 * windows structures, it may cause freed memory to be used. */
7441 if (updating_screen)
7442 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
7444 /* Check whether the current buffer is changed. If so, we will need
7445 * to split the current window or data could be lost.
7446 * We don't need to check if the 'hidden' option is set, as in this
7447 * case the buffer won't be lost.
7448 */
7449 if (!P_HID(curbuf) && !split)
7450 {
7451 ++emsg_off;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007452 split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 --emsg_off;
7454 }
7455 if (split)
7456 {
7457# ifdef FEAT_WINDOWS
7458 if (win_split(0, 0) == FAIL)
7459 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007460 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 /* When splitting the window, create a new alist. Otherwise the
7463 * existing one is overwritten. */
7464 alist_unlink(curwin->w_alist);
7465 alist_new();
7466# else
7467 return; /* can't split, always fail */
7468# endif
7469 }
7470
7471 /*
7472 * Set up the new argument list.
7473 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00007474 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476 /*
7477 * Move to the first file.
7478 */
7479 /* Fake up a minimal "next" command for do_argfile() */
7480 vim_memset(&ea, 0, sizeof(ea));
7481 ea.cmd = (char_u *)"next";
7482 do_argfile(&ea, 0);
7483
7484 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
7485 * mode that is not needed here. */
7486 need_start_insertmode = FALSE;
7487
7488 /* Restore msg_scroll, otherwise a following command may cause scrolling
7489 * unexpectedly. The screen will be redrawn by the caller, thus
7490 * msg_scroll being set by displaying a message is irrelevant. */
7491 msg_scroll = save_msg_scroll;
7492}
7493#endif
7494
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495/*
7496 * Clear an argument list: free all file names and reset it to zero entries.
7497 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007498 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499alist_clear(al)
7500 alist_T *al;
7501{
7502 while (--al->al_ga.ga_len >= 0)
7503 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
7504 ga_clear(&al->al_ga);
7505}
7506
7507/*
7508 * Init an argument list.
7509 */
7510 void
7511alist_init(al)
7512 alist_T *al;
7513{
7514 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
7515}
7516
7517#if defined(FEAT_WINDOWS) || defined(PROTO)
7518
7519/*
7520 * Remove a reference from an argument list.
7521 * Ignored when the argument list is the global one.
7522 * If the argument list is no longer used by any window, free it.
7523 */
7524 void
7525alist_unlink(al)
7526 alist_T *al;
7527{
7528 if (al != &global_alist && --al->al_refcount <= 0)
7529 {
7530 alist_clear(al);
7531 vim_free(al);
7532 }
7533}
7534
7535# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
7536/*
7537 * Create a new argument list and use it for the current window.
7538 */
7539 void
7540alist_new()
7541{
7542 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
7543 if (curwin->w_alist == NULL)
7544 {
7545 curwin->w_alist = &global_alist;
7546 ++global_alist.al_refcount;
7547 }
7548 else
7549 {
7550 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007551 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 alist_init(curwin->w_alist);
7553 }
7554}
7555# endif
7556#endif
7557
7558#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7559/*
7560 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007561 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7562 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 */
7564 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007565alist_expand(fnum_list, fnum_len)
7566 int *fnum_list;
7567 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
7569 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007570 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 char_u **new_arg_files;
7572 int new_arg_file_count;
7573 char_u *save_p_su = p_su;
7574 int i;
7575
7576 /* Don't use 'suffixes' here. This should work like the shell did the
7577 * expansion. Also, the vimrc file isn't read yet, thus the user
7578 * can't set the options. */
7579 p_su = empty_option;
7580 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7581 if (old_arg_files != NULL)
7582 {
7583 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007584 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7585 old_arg_count = GARGCOUNT;
7586 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02007588 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 && new_arg_file_count > 0)
7590 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007591 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7592 TRUE, fnum_list, fnum_len);
7593 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 p_su = save_p_su;
7597}
7598#endif
7599
7600/*
7601 * Set the argument list for the current window.
7602 * Takes over the allocated files[] and the allocated fnames in it.
7603 */
7604 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007605alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 alist_T *al;
7607 int count;
7608 char_u **files;
7609 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007610 int *fnum_list;
7611 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 int i;
7614
7615 alist_clear(al);
7616 if (ga_grow(&al->al_ga, count) == OK)
7617 {
7618 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007619 {
7620 if (got_int)
7621 {
7622 /* When adding many buffers this can take a long time. Allow
7623 * interrupting here. */
7624 while (i < count)
7625 vim_free(files[i++]);
7626 break;
7627 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007628
7629 /* May set buffer name of a buffer previously used for the
7630 * argument list, so that it's re-used by alist_add. */
7631 if (fnum_list != NULL && i < fnum_len)
7632 buf_set_name(fnum_list[i], files[i]);
7633
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007635 ui_breakcheck();
7636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 vim_free(files);
7638 }
7639 else
7640 FreeWild(count, files);
7641#ifdef FEAT_WINDOWS
7642 if (al == &global_alist)
7643#endif
7644 arg_had_last = FALSE;
7645}
7646
7647/*
7648 * Add file "fname" to argument list "al".
7649 * "fname" must have been allocated and "al" must have been checked for room.
7650 */
7651 void
7652alist_add(al, fname, set_fnum)
7653 alist_T *al;
7654 char_u *fname;
7655 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7656{
7657 if (fname == NULL) /* don't add NULL file names */
7658 return;
7659#ifdef BACKSLASH_IN_FILENAME
7660 slash_adjust(fname);
7661#endif
7662 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7663 if (set_fnum > 0)
7664 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7665 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7666 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667}
7668
7669#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7670/*
7671 * Adjust slashes in file names. Called after 'shellslash' was set.
7672 */
7673 void
7674alist_slash_adjust()
7675{
7676 int i;
7677# ifdef FEAT_WINDOWS
7678 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007679 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680# endif
7681
7682 for (i = 0; i < GARGCOUNT; ++i)
7683 if (GARGLIST[i].ae_fname != NULL)
7684 slash_adjust(GARGLIST[i].ae_fname);
7685# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007686 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 if (wp->w_alist != &global_alist)
7688 for (i = 0; i < WARGCOUNT(wp); ++i)
7689 if (WARGLIST(wp)[i].ae_fname != NULL)
7690 slash_adjust(WARGLIST(wp)[i].ae_fname);
7691# endif
7692}
7693#endif
7694
7695/*
7696 * ":preserve".
7697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 static void
7699ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007700 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007702 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 ml_preserve(curbuf, TRUE);
7704}
7705
7706/*
7707 * ":recover".
7708 */
7709 static void
7710ex_recover(eap)
7711 exarg_T *eap;
7712{
7713 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7714 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01007715 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
7716 | CCGD_MULTWIN
7717 | (eap->forceit ? CCGD_FORCEIT : 0)
7718 | CCGD_EXCMD)
7719
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 && (*eap->arg == NUL
7721 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7722 ml_recover();
7723 recoverymode = FALSE;
7724}
7725
7726/*
7727 * Command modifier used in a wrong way.
7728 */
7729 static void
7730ex_wrongmodifier(eap)
7731 exarg_T *eap;
7732{
7733 eap->errmsg = e_invcmd;
7734}
7735
7736#ifdef FEAT_WINDOWS
7737/*
7738 * :sview [+command] file split window with new file, read-only
7739 * :split [[+command] file] split window with current or new file
7740 * :vsplit [[+command] file] split window vertically with current or new file
7741 * :new [[+command] file] split window with no or new file
7742 * :vnew [[+command] file] split vertically window with no or new file
7743 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007744 *
7745 * :tabedit open new Tab page with empty window
7746 * :tabedit [+command] file open new Tab page and edit "file"
7747 * :tabnew [[+command] file] just like :tabedit
7748 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 */
7750 void
7751ex_splitview(eap)
7752 exarg_T *eap;
7753{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007754 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007755# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007757# endif
7758# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007760# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007762# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7764 {
7765 ex_ni(eap);
7766 return;
7767 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007770# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007774# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007776 * quickfix windows. But it's OK when doing ":tab split". */
7777 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {
7779 if (eap->cmdidx == CMD_split)
7780 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007781# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 if (eap->cmdidx == CMD_vsplit)
7783 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007788# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007789 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 {
7791 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7792 FNAME_MESS, TRUE, curbuf->b_ffname);
7793 if (fname == NULL)
7794 goto theend;
7795 eap->arg = fname;
7796 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007797# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007801# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007803# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 && eap->cmdidx != CMD_new)
7807 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007808# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007809 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007810# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007811 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007812# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007813 au_has_group((char_u *)"FileExplorer"))
7814 {
7815 /* No browsing supported but we do have the file explorer:
7816 * Edit the directory. */
7817 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7818 eap->arg = (char_u *)".";
7819 }
7820 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007821# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007822 {
7823 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007825 if (fname == NULL)
7826 goto theend;
7827 eap->arg = fname;
7828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007831# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007833 /*
7834 * Either open new tab page or split the window.
7835 */
7836 if (eap->cmdidx == CMD_tabedit
7837 || eap->cmdidx == CMD_tabfind
7838 || eap->cmdidx == CMD_tabnew)
7839 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007840 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7841 : eap->addr_count == 0 ? 0
7842 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007843 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007844 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007845
7846 /* set the alternate buffer for the window we came from */
7847 if (curwin != old_curwin
7848 && win_valid(old_curwin)
7849 && old_curwin->w_buffer != curbuf
7850 && !cmdmod.keepalt)
7851 old_curwin->w_alt_fnum = curbuf->b_fnum;
7852 }
7853 }
7854 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7856 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007857# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 /* Reset 'scrollbind' when editing another file, but keep it when
7859 * doing ":split" without arguments. */
7860 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007861# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007865 {
7866 RESET_BINDING(curwin);
7867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 else
7869 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007870# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 do_exedit(eap, old_curwin);
7872 }
7873
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007874# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007878# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879theend:
7880 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007881# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007883
7884/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007885 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007886 */
7887 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007888tabpage_new()
7889{
7890 exarg_T ea;
7891
7892 vim_memset(&ea, 0, sizeof(ea));
7893 ea.cmdidx = CMD_tabnew;
7894 ea.cmd = (char_u *)"tabn";
7895 ea.arg = (char_u *)"";
7896 ex_splitview(&ea);
7897}
7898
7899/*
7900 * :tabnext command
7901 */
7902 static void
7903ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007904 exarg_T *eap;
7905{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007906 switch (eap->cmdidx)
7907 {
7908 case CMD_tabfirst:
7909 case CMD_tabrewind:
7910 goto_tabpage(1);
7911 break;
7912 case CMD_tablast:
7913 goto_tabpage(9999);
7914 break;
7915 case CMD_tabprevious:
7916 case CMD_tabNext:
7917 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7918 break;
7919 default: /* CMD_tabnext */
7920 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7921 break;
7922 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007923}
7924
7925/*
7926 * :tabmove command
7927 */
7928 static void
7929ex_tabmove(eap)
7930 exarg_T *eap;
7931{
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02007932 int tab_number = 9999;
7933
7934 if (eap->arg && *eap->arg != NUL)
7935 {
7936 char_u *p = eap->arg;
7937 int relative = 0; /* argument +N/-N means: move N places to the
7938 * right/left relative to the current position. */
7939
7940 if (*eap->arg == '-')
7941 {
7942 relative = -1;
7943 p = eap->arg + 1;
7944 }
7945 else if (*eap->arg == '+')
7946 {
7947 relative = 1;
7948 p = eap->arg + 1;
7949 }
7950 else
7951 p = eap->arg;
7952
7953 if (p == skipdigits(p))
7954 {
7955 /* No numbers as argument. */
7956 eap->errmsg = e_invarg;
7957 return;
7958 }
7959
7960 tab_number = getdigits(&p);
7961 if (relative != 0)
7962 tab_number = tab_number * relative + tabpage_index(curtab) - 1;;
7963 }
7964 else if (eap->addr_count != 0)
7965 tab_number = eap->line2;
7966
7967 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007968}
7969
7970/*
7971 * :tabs command: List tabs and their contents.
7972 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007973 static void
7974ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007975 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007976{
7977 tabpage_T *tp;
7978 win_T *wp;
7979 int tabcount = 1;
7980
7981 msg_start();
7982 msg_scroll = TRUE;
7983 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7984 {
7985 msg_putchar('\n');
7986 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7987 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7988 out_flush(); /* output one line at a time */
7989 ui_breakcheck();
7990
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007991 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007992 wp = firstwin;
7993 else
7994 wp = tp->tp_firstwin;
7995 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7996 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007997 msg_putchar('\n');
7998 msg_putchar(wp == curwin ? '>' : ' ');
7999 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00008000 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
8001 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008002 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02008003 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008004 else
8005 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
8006 IObuff, IOSIZE, TRUE);
8007 msg_outtrans(IObuff);
8008 out_flush(); /* output one line at a time */
8009 ui_breakcheck();
8010 }
8011 }
8012}
8013
8014#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015
8016/*
8017 * ":mode": Set screen mode.
8018 * If no argument given, just get the screen size and redraw.
8019 */
8020 static void
8021ex_mode(eap)
8022 exarg_T *eap;
8023{
8024 if (*eap->arg == NUL)
8025 shell_resized();
8026 else
8027 mch_screenmode(eap->arg);
8028}
8029
8030#ifdef FEAT_WINDOWS
8031/*
8032 * ":resize".
8033 * set, increment or decrement current window height
8034 */
8035 static void
8036ex_resize(eap)
8037 exarg_T *eap;
8038{
8039 int n;
8040 win_T *wp = curwin;
8041
8042 if (eap->addr_count > 0)
8043 {
8044 n = eap->line2;
8045 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
8046 ;
8047 }
8048
8049#ifdef FEAT_GUI
8050 need_mouse_correct = TRUE;
8051#endif
8052 n = atol((char *)eap->arg);
8053#ifdef FEAT_VERTSPLIT
8054 if (cmdmod.split & WSP_VERT)
8055 {
8056 if (*eap->arg == '-' || *eap->arg == '+')
8057 n += W_WIDTH(curwin);
8058 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8059 n = 9999;
8060 win_setwidth_win((int)n, wp);
8061 }
8062 else
8063#endif
8064 {
8065 if (*eap->arg == '-' || *eap->arg == '+')
8066 n += curwin->w_height;
8067 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
8068 n = 9999;
8069 win_setheight_win((int)n, wp);
8070 }
8071}
8072#endif
8073
8074/*
8075 * ":find [+command] <file>" command.
8076 */
8077 static void
8078ex_find(eap)
8079 exarg_T *eap;
8080{
8081#ifdef FEAT_SEARCHPATH
8082 char_u *fname;
8083 int count;
8084
8085 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
8086 TRUE, curbuf->b_ffname);
8087 if (eap->addr_count > 0)
8088 {
8089 /* Repeat finding the file "count" times. This matters when it
8090 * appears several times in the path. */
8091 count = eap->line2;
8092 while (fname != NULL && --count > 0)
8093 {
8094 vim_free(fname);
8095 fname = find_file_in_path(NULL, 0, FNAME_MESS,
8096 FALSE, curbuf->b_ffname);
8097 }
8098 }
8099
8100 if (fname != NULL)
8101 {
8102 eap->arg = fname;
8103#endif
8104 do_exedit(eap, NULL);
8105#ifdef FEAT_SEARCHPATH
8106 vim_free(fname);
8107 }
8108#endif
8109}
8110
8111/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00008112 * ":open" simulation: for now just work like ":visual".
8113 */
8114 static void
8115ex_open(eap)
8116 exarg_T *eap;
8117{
8118 regmatch_T regmatch;
8119 char_u *p;
8120
8121 curwin->w_cursor.lnum = eap->line2;
8122 beginline(BL_SOL | BL_FIX);
8123 if (*eap->arg == '/')
8124 {
8125 /* ":open /pattern/": put cursor in column found with pattern */
8126 ++eap->arg;
8127 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8128 *p = NUL;
8129 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
8130 if (regmatch.regprog != NULL)
8131 {
8132 regmatch.rm_ic = p_ic;
8133 p = ml_get_curline();
8134 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008135 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008136 else
8137 EMSG(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02008138 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008139 }
8140 /* Move to the NUL, ignore any other arguments. */
8141 eap->arg += STRLEN(eap->arg);
8142 }
8143 check_cursor();
8144
8145 eap->cmdidx = CMD_visual;
8146 do_exedit(eap, NULL);
8147}
8148
8149/*
8150 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 */
8152 static void
8153ex_edit(eap)
8154 exarg_T *eap;
8155{
8156 do_exedit(eap, NULL);
8157}
8158
8159/*
8160 * ":edit <file>" command and alikes.
8161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 void
8163do_exedit(eap, old_curwin)
8164 exarg_T *eap;
8165 win_T *old_curwin; /* curwin before doing a split or NULL */
8166{
8167 int n;
8168#ifdef FEAT_WINDOWS
8169 int need_hide;
8170#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008171 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172
8173 /*
8174 * ":vi" command ends Ex mode.
8175 */
8176 if (exmode_active && (eap->cmdidx == CMD_visual
8177 || eap->cmdidx == CMD_view))
8178 {
8179 exmode_active = FALSE;
8180 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008181 {
8182 /* Special case: ":global/pat/visual\NLvi-commands" */
8183 if (global_busy)
8184 {
8185 int rd = RedrawingDisabled;
8186 int nwr = no_wait_return;
8187 int ms = msg_scroll;
8188#ifdef FEAT_GUI
8189 int he = hold_gui_events;
8190#endif
8191
8192 if (eap->nextcmd != NULL)
8193 {
8194 stuffReadbuff(eap->nextcmd);
8195 eap->nextcmd = NULL;
8196 }
8197
8198 if (exmode_was != EXMODE_VIM)
8199 settmode(TMODE_RAW);
8200 RedrawingDisabled = 0;
8201 no_wait_return = 0;
8202 need_wait_return = FALSE;
8203 msg_scroll = 0;
8204#ifdef FEAT_GUI
8205 hold_gui_events = 0;
8206#endif
8207 must_redraw = CLEAR;
8208
8209 main_loop(FALSE, TRUE);
8210
8211 RedrawingDisabled = rd;
8212 no_wait_return = nwr;
8213 msg_scroll = ms;
8214#ifdef FEAT_GUI
8215 hold_gui_events = he;
8216#endif
8217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 }
8221
8222 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008223 || eap->cmdidx == CMD_tabnew
8224 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225#ifdef FEAT_VERTSPLIT
8226 || eap->cmdidx == CMD_vnew
8227#endif
8228 ) && *eap->arg == NUL)
8229 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00008230 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 setpcmark();
8232 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008233 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
8234 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 }
8236 else if ((eap->cmdidx != CMD_split
8237#ifdef FEAT_VERTSPLIT
8238 && eap->cmdidx != CMD_vsplit
8239#endif
8240 )
8241 || *eap->arg != NUL
8242#ifdef FEAT_BROWSE
8243 || cmdmod.browse
8244#endif
8245 )
8246 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00008247#ifdef FEAT_AUTOCMD
8248 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
8249 * can bring us here, others are stopped earlier. */
8250 if (*eap->arg != NUL && curbuf_locked())
8251 return;
8252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 n = readonlymode;
8254 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
8255 readonlymode = TRUE;
8256 else if (eap->cmdidx == CMD_enew)
8257 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
8258 empty buffer */
8259 setpcmark();
8260 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
8261 NULL, eap,
8262 /* ":edit" goes to first line if Vi compatible */
8263 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
8264 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
8265 ? ECMD_ONE : eap->do_ecmd_lnum,
8266 (P_HID(curbuf) ? ECMD_HIDE : 0)
8267 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01008268 /* after a split we can use an existing buffer */
8269 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270#ifdef FEAT_LISTCMDS
8271 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
8272#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00008273 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
8275 /* Editing the file failed. If the window was split, close it. */
8276#ifdef FEAT_WINDOWS
8277 if (old_curwin != NULL)
8278 {
8279 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
8280 if (!need_hide || P_HID(curbuf))
8281 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008282# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8283 cleanup_T cs;
8284
8285 /* Reset the error/interrupt/exception state here so that
8286 * aborting() returns FALSE when closing a window. */
8287 enter_cleanup(&cs);
8288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289# ifdef FEAT_GUI
8290 need_mouse_correct = TRUE;
8291# endif
8292 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008293
8294# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8295 /* Restore the error/interrupt/exception state if not
8296 * discarded by a new aborting error, interrupt, or
8297 * uncaught exception. */
8298 leave_cleanup(&cs);
8299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 }
8301 }
8302#endif
8303 }
8304 else if (readonlymode && curbuf->b_nwindows == 1)
8305 {
8306 /* When editing an already visited buffer, 'readonly' won't be set
8307 * but the previous value is kept. With ":view" and ":sview" we
8308 * want the file to be readonly, except when another window is
8309 * editing the same buffer. */
8310 curbuf->b_p_ro = TRUE;
8311 }
8312 readonlymode = n;
8313 }
8314 else
8315 {
8316 if (eap->do_ecmd_cmd != NULL)
8317 do_cmdline_cmd(eap->do_ecmd_cmd);
8318#ifdef FEAT_TITLE
8319 n = curwin->w_arg_idx_invalid;
8320#endif
8321 check_arg_idx(curwin);
8322#ifdef FEAT_TITLE
8323 if (n != curwin->w_arg_idx_invalid)
8324 maketitle();
8325#endif
8326 }
8327
8328#ifdef FEAT_WINDOWS
8329 /*
8330 * if ":split file" worked, set alternate file name in old window to new
8331 * file
8332 */
8333 if (old_curwin != NULL
8334 && *eap->arg != NUL
8335 && curwin != old_curwin
8336 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00008337 && old_curwin->w_buffer != curbuf
8338 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 old_curwin->w_alt_fnum = curbuf->b_fnum;
8340#endif
8341
8342 ex_no_reprint = TRUE;
8343}
8344
8345#ifndef FEAT_GUI
8346/*
8347 * ":gui" and ":gvim" when there is no GUI.
8348 */
8349 static void
8350ex_nogui(eap)
8351 exarg_T *eap;
8352{
8353 eap->errmsg = e_nogvim;
8354}
8355#endif
8356
8357#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
8358 static void
8359ex_tearoff(eap)
8360 exarg_T *eap;
8361{
8362 gui_make_tearoff(eap->arg);
8363}
8364#endif
8365
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008366#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 static void
8368ex_popup(eap)
8369 exarg_T *eap;
8370{
Bram Moolenaar97409f12005-07-08 22:17:29 +00008371 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372}
8373#endif
8374
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 static void
8376ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008377 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
8379 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
8380 MSG(_("No swap file"));
8381 else
8382 msg(curbuf->b_ml.ml_mfp->mf_fname);
8383}
8384
8385/*
8386 * ":syncbind" forces all 'scrollbind' windows to have the same relative
8387 * offset.
8388 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
8389 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 static void
8391ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008392 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393{
8394#ifdef FEAT_SCROLLBIND
8395 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008396 win_T *save_curwin = curwin;
8397 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 long topline;
8399 long y;
8400 linenr_T old_linenr = curwin->w_cursor.lnum;
8401
8402 setpcmark();
8403
8404 /*
8405 * determine max topline
8406 */
8407 if (curwin->w_p_scb)
8408 {
8409 topline = curwin->w_topline;
8410 for (wp = firstwin; wp; wp = wp->w_next)
8411 {
8412 if (wp->w_p_scb && wp->w_buffer)
8413 {
8414 y = wp->w_buffer->b_ml.ml_line_count - p_so;
8415 if (topline > y)
8416 topline = y;
8417 }
8418 }
8419 if (topline < 1)
8420 topline = 1;
8421 }
8422 else
8423 {
8424 topline = 1;
8425 }
8426
8427
8428 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008429 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 for (curwin = firstwin; curwin; curwin = curwin->w_next)
8432 {
8433 if (curwin->w_p_scb)
8434 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008435 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 y = topline - curwin->w_topline;
8437 if (y > 0)
8438 scrollup(y, TRUE);
8439 else
8440 scrolldown(-y, TRUE);
8441 curwin->w_scbind_pos = topline;
8442 redraw_later(VALID);
8443 cursor_correct();
8444#ifdef FEAT_WINDOWS
8445 curwin->w_redr_status = TRUE;
8446#endif
8447 }
8448 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01008449 curwin = save_curwin;
8450 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 if (curwin->w_p_scb)
8452 {
8453 did_syncbind = TRUE;
8454 checkpcmark();
8455 if (old_linenr != curwin->w_cursor.lnum)
8456 {
8457 char_u ctrl_o[2];
8458
8459 ctrl_o[0] = Ctrl_O;
8460 ctrl_o[1] = 0;
8461 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
8462 }
8463 }
8464#endif
8465}
8466
8467
8468 static void
8469ex_read(eap)
8470 exarg_T *eap;
8471{
Bram Moolenaardf177f62005-02-22 08:39:57 +00008472 int i;
8473 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
8474 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
8476 if (eap->usefilter) /* :r!cmd */
8477 do_bang(1, eap, FALSE, FALSE, TRUE);
8478 else
8479 {
8480 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
8481 return;
8482
8483#ifdef FEAT_BROWSE
8484 if (cmdmod.browse)
8485 {
8486 char_u *browseFile;
8487
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008488 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 NULL, NULL, NULL, curbuf);
8490 if (browseFile != NULL)
8491 {
8492 i = readfile(browseFile, NULL,
8493 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8494 vim_free(browseFile);
8495 }
8496 else
8497 i = OK;
8498 }
8499 else
8500#endif
8501 if (*eap->arg == NUL)
8502 {
8503 if (check_fname() == FAIL) /* check for no file name */
8504 return;
8505 i = readfile(curbuf->b_ffname, curbuf->b_fname,
8506 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8507 }
8508 else
8509 {
8510 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
8511 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
8512 i = readfile(eap->arg, NULL,
8513 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
8514
8515 }
8516 if (i == FAIL)
8517 {
8518#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
8519 if (!aborting())
8520#endif
8521 EMSG2(_(e_notopen), eap->arg);
8522 }
8523 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008524 {
8525 if (empty && exmode_active)
8526 {
8527 /* Delete the empty line that remains. Historically ex does
8528 * this but vi doesn't. */
8529 if (eap->line2 == 0)
8530 lnum = curbuf->b_ml.ml_line_count;
8531 else
8532 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008533 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008534 {
8535 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008536 if (curwin->w_cursor.lnum > 1
8537 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008538 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00008539 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008540 }
8541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 }
8545}
8546
Bram Moolenaarea408852005-06-25 22:49:46 +00008547static char_u *prev_dir = NULL;
8548
8549#if defined(EXITFREE) || defined(PROTO)
8550 void
8551free_cd_dir()
8552{
8553 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00008554 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00008555
8556 vim_free(globaldir);
8557 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00008558}
8559#endif
8560
Bram Moolenaarf4258302013-06-02 18:20:17 +02008561/*
8562 * Deal with the side effects of changing the current directory.
8563 * When "local" is TRUE then this was after an ":lcd" command.
8564 */
8565 void
8566post_chdir(local)
8567 int local;
8568{
8569 vim_free(curwin->w_localdir);
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01008570 curwin->w_localdir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008571 if (local)
8572 {
8573 /* If still in global directory, need to remember current
8574 * directory as global directory. */
8575 if (globaldir == NULL && prev_dir != NULL)
8576 globaldir = vim_strsave(prev_dir);
8577 /* Remember this local directory for the window. */
8578 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8579 curwin->w_localdir = vim_strsave(NameBuff);
8580 }
8581 else
8582 {
8583 /* We are now in the global directory, no need to remember its
8584 * name. */
8585 vim_free(globaldir);
8586 globaldir = NULL;
Bram Moolenaarf4258302013-06-02 18:20:17 +02008587 }
8588
8589 shorten_fnames(TRUE);
8590}
8591
Bram Moolenaarea408852005-06-25 22:49:46 +00008592
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593/*
8594 * ":cd", ":lcd", ":chdir" and ":lchdir".
8595 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00008596 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597ex_cd(eap)
8598 exarg_T *eap;
8599{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 char_u *new_dir;
8601 char_u *tofree;
8602
8603 new_dir = eap->arg;
8604#if !defined(UNIX) && !defined(VMS)
8605 /* for non-UNIX ":cd" means: print current directory */
8606 if (*new_dir == NUL)
8607 ex_pwd(NULL);
8608 else
8609#endif
8610 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00008611#ifdef FEAT_AUTOCMD
8612 if (allbuf_locked())
8613 return;
8614#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008615 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
8616 && !eap->forceit)
8617 {
Bram Moolenaar81870892007-11-11 18:17:28 +00008618 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00008619 return;
8620 }
8621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 /* ":cd -": Change to previous directory */
8623 if (STRCMP(new_dir, "-") == 0)
8624 {
8625 if (prev_dir == NULL)
8626 {
8627 EMSG(_("E186: No previous directory"));
8628 return;
8629 }
8630 new_dir = prev_dir;
8631 }
8632
8633 /* Save current directory for next ":cd -" */
8634 tofree = prev_dir;
8635 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8636 prev_dir = vim_strsave(NameBuff);
8637 else
8638 prev_dir = NULL;
8639
8640#if defined(UNIX) || defined(VMS)
8641 /* for UNIX ":cd" means: go to home directory */
8642 if (*new_dir == NUL)
8643 {
8644 /* use NameBuff for home directory name */
8645# ifdef VMS
8646 char_u *p;
8647
8648 p = mch_getenv((char_u *)"SYS$LOGIN");
8649 if (p == NULL || *p == NUL) /* empty is the same as not set */
8650 NameBuff[0] = NUL;
8651 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008652 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653# else
8654 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8655# endif
8656 new_dir = NameBuff;
8657 }
8658#endif
8659 if (new_dir == NULL || vim_chdir(new_dir))
8660 EMSG(_(e_failed));
8661 else
8662 {
Bram Moolenaarf4258302013-06-02 18:20:17 +02008663 post_chdir(eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
8665 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008666 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 ex_pwd(eap);
8668 }
8669 vim_free(tofree);
8670 }
8671}
8672
8673/*
8674 * ":pwd".
8675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 static void
8677ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008678 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679{
8680 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8681 {
8682#ifdef BACKSLASH_IN_FILENAME
8683 slash_adjust(NameBuff);
8684#endif
8685 msg(NameBuff);
8686 }
8687 else
8688 EMSG(_("E187: Unknown"));
8689}
8690
8691/*
8692 * ":=".
8693 */
8694 static void
8695ex_equal(eap)
8696 exarg_T *eap;
8697{
8698 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008699 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700}
8701
8702 static void
8703ex_sleep(eap)
8704 exarg_T *eap;
8705{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008706 int n;
8707 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708
8709 if (cursor_valid())
8710 {
8711 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8712 if (n >= 0)
Bram Moolenaar10395d82014-02-05 22:46:52 +01008713 windgoto((int)n, W_WINCOL(curwin) + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008715
8716 len = eap->line2;
8717 switch (*eap->arg)
8718 {
8719 case 'm': break;
8720 case NUL: len *= 1000L; break;
8721 default: EMSG2(_(e_invarg2), eap->arg); return;
8722 }
8723 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724}
8725
8726/*
8727 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8728 */
8729 void
8730do_sleep(msec)
8731 long msec;
8732{
8733 long done;
8734
8735 cursor_on();
8736 out_flush();
8737 for (done = 0; !got_int && done < msec; done += 1000L)
8738 {
8739 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8740 ui_breakcheck();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02008741#ifdef FEAT_NETBEANS_INTG
8742 /* Process the netbeans messages that may have been received in the
8743 * call to ui_breakcheck() when the GUI is in use. This may occur when
8744 * running a test case. */
8745 netbeans_parse_messages();
8746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 }
8748}
8749
8750 static void
8751do_exmap(eap, isabbrev)
8752 exarg_T *eap;
8753 int isabbrev;
8754{
8755 int mode;
8756 char_u *cmdp;
8757
8758 cmdp = eap->cmd;
8759 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8760
8761 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8762 eap->arg, mode, isabbrev))
8763 {
8764 case 1: EMSG(_(e_invarg));
8765 break;
8766 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8767 break;
8768 }
8769}
8770
8771/*
8772 * ":winsize" command (obsolete).
8773 */
8774 static void
8775ex_winsize(eap)
8776 exarg_T *eap;
8777{
8778 int w, h;
8779 char_u *arg = eap->arg;
8780 char_u *p;
8781
8782 w = getdigits(&arg);
8783 arg = skipwhite(arg);
8784 p = arg;
8785 h = getdigits(&arg);
8786 if (*p != NUL && *arg == NUL)
8787 set_shellsize(w, h, TRUE);
8788 else
8789 EMSG(_("E465: :winsize requires two number arguments"));
8790}
8791
8792#ifdef FEAT_WINDOWS
8793 static void
8794ex_wincmd(eap)
8795 exarg_T *eap;
8796{
8797 int xchar = NUL;
8798 char_u *p;
8799
8800 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8801 {
8802 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8803 if (eap->arg[1] == NUL)
8804 {
8805 EMSG(_(e_invarg));
8806 return;
8807 }
8808 xchar = eap->arg[1];
8809 p = eap->arg + 2;
8810 }
8811 else
8812 p = eap->arg + 1;
8813
8814 eap->nextcmd = check_nextcmd(p);
8815 p = skipwhite(p);
8816 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8817 EMSG(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02008818 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {
8820 /* Pass flags on for ":vertical wincmd ]". */
8821 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008822 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8824 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008825 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 }
8827}
8828#endif
8829
Bram Moolenaar843ee412004-06-30 16:16:41 +00008830#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831/*
8832 * ":winpos".
8833 */
8834 static void
8835ex_winpos(eap)
8836 exarg_T *eap;
8837{
8838 int x, y;
8839 char_u *arg = eap->arg;
8840 char_u *p;
8841
8842 if (*arg == NUL)
8843 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008844# if defined(FEAT_GUI) || defined(MSWIN)
8845# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008847# else
8848 if (mch_get_winpos(&x, &y) != FAIL)
8849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 {
8851 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8852 msg(IObuff);
8853 }
8854 else
8855# endif
8856 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8857 }
8858 else
8859 {
8860 x = getdigits(&arg);
8861 arg = skipwhite(arg);
8862 p = arg;
8863 y = getdigits(&arg);
8864 if (*p == NUL || *arg != NUL)
8865 {
8866 EMSG(_("E466: :winpos requires two number arguments"));
8867 return;
8868 }
8869# ifdef FEAT_GUI
8870 if (gui.in_use)
8871 gui_mch_set_winpos(x, y);
8872 else if (gui.starting)
8873 {
8874 /* Remember the coordinates for when the window is opened. */
8875 gui_win_x = x;
8876 gui_win_y = y;
8877 }
8878# ifdef HAVE_TGETENT
8879 else
8880# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008881# else
8882# ifdef MSWIN
8883 mch_set_winpos(x, y);
8884# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885# endif
8886# ifdef HAVE_TGETENT
8887 if (*T_CWP)
8888 term_set_winpos(x, y);
8889# endif
8890 }
8891}
8892#endif
8893
8894/*
8895 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8896 */
8897 static void
8898ex_operators(eap)
8899 exarg_T *eap;
8900{
8901 oparg_T oa;
8902
8903 clear_oparg(&oa);
8904 oa.regname = eap->regname;
8905 oa.start.lnum = eap->line1;
8906 oa.end.lnum = eap->line2;
8907 oa.line_count = eap->line2 - eap->line1 + 1;
8908 oa.motion_type = MLINE;
8909#ifdef FEAT_VIRTUALEDIT
8910 virtual_op = FALSE;
8911#endif
8912 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8913 {
8914 setpcmark();
8915 curwin->w_cursor.lnum = eap->line1;
8916 beginline(BL_SOL | BL_FIX);
8917 }
8918
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008919 if (VIsual_active)
8920 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01008921
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 switch (eap->cmdidx)
8923 {
8924 case CMD_delete:
8925 oa.op_type = OP_DELETE;
8926 op_delete(&oa);
8927 break;
8928
8929 case CMD_yank:
8930 oa.op_type = OP_YANK;
8931 (void)op_yank(&oa, FALSE, TRUE);
8932 break;
8933
8934 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02008935 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02008937 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
8938#else
8939 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02008941 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 oa.op_type = OP_RSHIFT;
8943 else
8944 oa.op_type = OP_LSHIFT;
8945 op_shift(&oa, FALSE, eap->amount);
8946 break;
8947 }
8948#ifdef FEAT_VIRTUALEDIT
8949 virtual_op = MAYBE;
8950#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008951 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952}
8953
8954/*
8955 * ":put".
8956 */
8957 static void
8958ex_put(eap)
8959 exarg_T *eap;
8960{
8961 /* ":0put" works like ":1put!". */
8962 if (eap->line2 == 0)
8963 {
8964 eap->line2 = 1;
8965 eap->forceit = TRUE;
8966 }
8967 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008968 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8969 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970}
8971
8972/*
8973 * Handle ":copy" and ":move".
8974 */
8975 static void
8976ex_copymove(eap)
8977 exarg_T *eap;
8978{
8979 long n;
8980
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01008981 n = get_address(&eap->arg, eap->addr_type, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 if (eap->arg == NULL) /* error detected */
8983 {
8984 eap->nextcmd = NULL;
8985 return;
8986 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008987 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988
8989 /*
8990 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8991 */
8992 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8993 {
8994 EMSG(_(e_invaddr));
8995 return;
8996 }
8997
8998 if (eap->cmdidx == CMD_move)
8999 {
9000 if (do_move(eap->line1, eap->line2, n) == FAIL)
9001 return;
9002 }
9003 else
9004 ex_copy(eap->line1, eap->line2, n);
9005 u_clearline();
9006 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009007 ex_may_print(eap);
9008}
9009
9010/*
9011 * Print the current line if flags were given to the Ex command.
9012 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02009013 void
Bram Moolenaardf177f62005-02-22 08:39:57 +00009014ex_may_print(eap)
9015 exarg_T *eap;
9016{
9017 if (eap->flags != 0)
9018 {
9019 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
9020 (eap->flags & EXFLAG_LIST));
9021 ex_no_reprint = TRUE;
9022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
9024
9025/*
9026 * ":smagic" and ":snomagic".
9027 */
9028 static void
9029ex_submagic(eap)
9030 exarg_T *eap;
9031{
9032 int magic_save = p_magic;
9033
9034 p_magic = (eap->cmdidx == CMD_smagic);
9035 do_sub(eap);
9036 p_magic = magic_save;
9037}
9038
9039/*
9040 * ":join".
9041 */
9042 static void
9043ex_join(eap)
9044 exarg_T *eap;
9045{
9046 curwin->w_cursor.lnum = eap->line1;
9047 if (eap->line1 == eap->line2)
9048 {
9049 if (eap->addr_count >= 2) /* :2,2join does nothing */
9050 return;
9051 if (eap->line2 == curbuf->b_ml.ml_line_count)
9052 {
9053 beep_flush();
9054 return;
9055 }
9056 ++eap->line2;
9057 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009058 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009060 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061}
9062
9063/*
9064 * ":[addr]@r" or ":[addr]*r": execute register
9065 */
9066 static void
9067ex_at(eap)
9068 exarg_T *eap;
9069{
9070 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00009071 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072
9073 curwin->w_cursor.lnum = eap->line2;
9074
9075#ifdef USE_ON_FLY_SCROLL
9076 dont_scroll = TRUE; /* disallow scrolling here */
9077#endif
9078
9079 /* get the register name. No name means to use the previous one */
9080 c = *eap->arg;
9081 if (c == NUL || (c == '*' && *eap->cmd == '*'))
9082 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00009083 /* Put the register in the typeahead buffer with the "silent" flag. */
9084 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
9085 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00009086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00009088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 else
9090 {
9091 int save_efr = exec_from_reg;
9092
9093 exec_from_reg = TRUE;
9094
9095 /*
9096 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00009097 * Continue until the stuff buffer is empty and all added characters
9098 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 */
Bram Moolenaar60462872009-11-03 11:40:19 +00009100 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
9102
9103 exec_from_reg = save_efr;
9104 }
9105}
9106
9107/*
9108 * ":!".
9109 */
9110 static void
9111ex_bang(eap)
9112 exarg_T *eap;
9113{
9114 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
9115}
9116
9117/*
9118 * ":undo".
9119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 static void
9121ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009122 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
Bram Moolenaard3667a22006-03-16 21:35:52 +00009124 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02009125 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00009126 else
9127 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128}
9129
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009130#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009131 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009132ex_wundo(eap)
9133 exarg_T *eap;
9134{
9135 char_u hash[UNDO_HASH_SIZE];
9136
9137 u_compute_hash(hash);
9138 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
9139}
9140
Bram Moolenaar6df6f472010-07-18 18:04:50 +02009141 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009142ex_rundo(eap)
9143 exarg_T *eap;
9144{
9145 char_u hash[UNDO_HASH_SIZE];
9146
9147 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02009148 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02009149}
9150#endif
9151
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152/*
9153 * ":redo".
9154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 static void
9156ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009157 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158{
9159 u_redo(1);
9160}
9161
9162/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009163 * ":earlier" and ":later".
9164 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009165 static void
9166ex_later(eap)
9167 exarg_T *eap;
9168{
9169 long count = 0;
9170 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009171 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009172 char_u *p = eap->arg;
9173
9174 if (*p == NUL)
9175 count = 1;
9176 else if (isdigit(*p))
9177 {
9178 count = getdigits(&p);
9179 switch (*p)
9180 {
9181 case 's': ++p; sec = TRUE; break;
9182 case 'm': ++p; sec = TRUE; count *= 60; break;
9183 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02009184 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
9185 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009186 }
9187 }
9188
9189 if (*p != NUL)
9190 EMSG2(_(e_invarg2), eap->arg);
9191 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02009192 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
9193 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00009194}
9195
9196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 * ":redir": start/stop redirection.
9198 */
9199 static void
9200ex_redir(eap)
9201 exarg_T *eap;
9202{
9203 char *mode;
9204 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00009205 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206
9207 if (STRICMP(eap->arg, "END") == 0)
9208 close_redir();
9209 else
9210 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009211 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009213 ++arg;
9214 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009216 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 mode = "a";
9218 }
9219 else
9220 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00009221 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
9223 close_redir();
9224
9225 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00009226 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 if (fname == NULL)
9228 return;
9229#ifdef FEAT_BROWSE
9230 if (cmdmod.browse)
9231 {
9232 char_u *browseFile;
9233
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009234 browseFile = do_browse(BROWSE_SAVE,
9235 (char_u *)_("Save Redirection"),
9236 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 if (browseFile == NULL)
9238 return; /* operation cancelled */
9239 vim_free(fname);
9240 fname = browseFile;
9241 eap->forceit = TRUE; /* since dialog already asked */
9242 }
9243#endif
9244
9245 redir_fd = open_exfile(fname, eap->forceit, mode);
9246 vim_free(fname);
9247 }
9248#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00009249 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 {
9251 /* redirect to a register a-z (resp. A-Z for appending) */
9252 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00009253 ++arg;
9254 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00009256 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00009257 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00009259 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 {
Bram Moolenaarca472992005-01-21 11:46:23 +00009261 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009262 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00009263 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009264 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00009266 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00009267 if (*arg == '>')
9268 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00009269 /* Make register empty when not using @A-@Z and the
9270 * command is valid. */
9271 if (*arg == NUL && !isupper(redir_reg))
9272 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00009274 }
9275 if (*arg != NUL)
9276 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00009277 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00009278 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00009279 }
9280 }
9281 else if (*arg == '=' && arg[1] == '>')
9282 {
9283 int append;
9284
9285 /* redirect to a variable */
9286 close_redir();
9287 arg += 2;
9288
9289 if (*arg == '>')
9290 {
9291 ++arg;
9292 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 }
9294 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00009295 append = FALSE;
9296
9297 if (var_redir_start(skipwhite(arg), append) == OK)
9298 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 }
9300#endif
9301
9302 /* TODO: redirect to a buffer */
9303
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 else
Bram Moolenaarca472992005-01-21 11:46:23 +00009305 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00009307
9308 /* Make sure redirection is not off. Can happen for cmdline completion
9309 * that indirectly invokes a command to catch its output. */
9310 if (redir_fd != NULL
9311#ifdef FEAT_EVAL
9312 || redir_reg || redir_vname
9313#endif
9314 )
9315 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316}
9317
9318/*
9319 * ":redraw": force redraw
9320 */
9321 static void
9322ex_redraw(eap)
9323 exarg_T *eap;
9324{
9325 int r = RedrawingDisabled;
9326 int p = p_lz;
9327
9328 RedrawingDisabled = 0;
9329 p_lz = FALSE;
9330 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009331 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332#ifdef FEAT_TITLE
9333 if (need_maketitle)
9334 maketitle();
9335#endif
9336 RedrawingDisabled = r;
9337 p_lz = p;
9338
9339 /* Reset msg_didout, so that a message that's there is overwritten. */
9340 msg_didout = FALSE;
9341 msg_col = 0;
9342
Bram Moolenaar56667a52013-07-17 11:54:28 +02009343 /* No need to wait after an intentional redraw. */
9344 need_wait_return = FALSE;
9345
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346 out_flush();
9347}
9348
9349/*
9350 * ":redrawstatus": force redraw of status line(s)
9351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 static void
9353ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009354 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356#if defined(FEAT_WINDOWS)
9357 int r = RedrawingDisabled;
9358 int p = p_lz;
9359
9360 RedrawingDisabled = 0;
9361 p_lz = FALSE;
9362 if (eap->forceit)
9363 status_redraw_all();
9364 else
9365 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01009366 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 RedrawingDisabled = r;
9368 p_lz = p;
9369 out_flush();
9370#endif
9371}
9372
9373 static void
9374close_redir()
9375{
9376 if (redir_fd != NULL)
9377 {
9378 fclose(redir_fd);
9379 redir_fd = NULL;
9380 }
9381#ifdef FEAT_EVAL
9382 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009383 if (redir_vname)
9384 {
9385 var_redir_stop();
9386 redir_vname = 0;
9387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388#endif
9389}
9390
9391#if defined(FEAT_SESSION) && defined(USE_CRNL)
9392# define MKSESSION_NL
9393static int mksession_nl = FALSE; /* use NL only in put_eol() */
9394#endif
9395
9396/*
9397 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
9398 */
9399 static void
9400ex_mkrc(eap)
9401 exarg_T *eap;
9402{
9403 FILE *fd;
9404 int failed = FALSE;
9405 char_u *fname;
9406#ifdef FEAT_BROWSE
9407 char_u *browseFile = NULL;
9408#endif
9409#ifdef FEAT_SESSION
9410 int view_session = FALSE;
9411 int using_vdir = FALSE; /* using 'viewdir'? */
9412 char_u *viewFile = NULL;
9413 unsigned *flagp;
9414#endif
9415
9416 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
9417 {
9418#ifdef FEAT_SESSION
9419 view_session = TRUE;
9420#else
9421 ex_ni(eap);
9422 return;
9423#endif
9424 }
9425
9426#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00009427 /* Use the short file name until ":lcd" is used. We also don't use the
9428 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00009429 did_lcd = FALSE;
9430
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
9432 if (eap->cmdidx == CMD_mkview
9433 && (*eap->arg == NUL
9434 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
9435 {
9436 eap->forceit = TRUE;
9437 fname = get_view_file(*eap->arg);
9438 if (fname == NULL)
9439 return;
9440 viewFile = fname;
9441 using_vdir = TRUE;
9442 }
9443 else
9444#endif
9445 if (*eap->arg != NUL)
9446 fname = eap->arg;
9447 else if (eap->cmdidx == CMD_mkvimrc)
9448 fname = (char_u *)VIMRC_FILE;
9449#ifdef FEAT_SESSION
9450 else if (eap->cmdidx == CMD_mksession)
9451 fname = (char_u *)SESSION_FILE;
9452#endif
9453 else
9454 fname = (char_u *)EXRC_FILE;
9455
9456#ifdef FEAT_BROWSE
9457 if (cmdmod.browse)
9458 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00009459 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460# ifdef FEAT_SESSION
9461 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
9462 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
9463# endif
9464 (char_u *)_("Save Setup"),
9465 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
9466 if (browseFile == NULL)
9467 goto theend;
9468 fname = browseFile;
9469 eap->forceit = TRUE; /* since dialog already asked */
9470 }
9471#endif
9472
9473#if defined(FEAT_SESSION) && defined(vim_mkdir)
9474 /* When using 'viewdir' may have to create the directory. */
9475 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00009476 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477#endif
9478
9479 fd = open_exfile(fname, eap->forceit, WRITEBIN);
9480 if (fd != NULL)
9481 {
9482#ifdef FEAT_SESSION
9483 if (eap->cmdidx == CMD_mkview)
9484 flagp = &vop_flags;
9485 else
9486 flagp = &ssop_flags;
9487#endif
9488
9489#ifdef MKSESSION_NL
9490 /* "unix" in 'sessionoptions': use NL line separator */
9491 if (view_session && (*flagp & SSOP_UNIX))
9492 mksession_nl = TRUE;
9493#endif
9494
9495 /* Write the version command for :mkvimrc */
9496 if (eap->cmdidx == CMD_mkvimrc)
9497 (void)put_line(fd, "version 6.0");
9498
9499#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009500 if (eap->cmdidx == CMD_mksession)
9501 {
9502 if (put_line(fd, "let SessionLoad = 1") == FAIL)
9503 failed = TRUE;
9504 }
9505
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (eap->cmdidx != CMD_mkview)
9507#endif
9508 {
9509 /* Write setting 'compatible' first, because it has side effects.
9510 * For that same reason only do it when needed. */
9511 if (p_cp)
9512 (void)put_line(fd, "if !&cp | set cp | endif");
9513 else
9514 (void)put_line(fd, "if &cp | set nocp | endif");
9515 }
9516
9517#ifdef FEAT_SESSION
9518 if (!view_session
9519 || (eap->cmdidx == CMD_mksession
9520 && (*flagp & SSOP_OPTIONS)))
9521#endif
9522 failed |= (makemap(fd, NULL) == FAIL
9523 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
9524
9525#ifdef FEAT_SESSION
9526 if (!failed && view_session)
9527 {
9528 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
9529 failed = TRUE;
9530 if (eap->cmdidx == CMD_mksession)
9531 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02009532 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533
Bram Moolenaard9462e32011-04-11 21:35:11 +02009534 dirnow = alloc(MAXPATHL);
9535 if (dirnow == NULL)
9536 failed = TRUE;
9537 else
9538 {
9539 /*
9540 * Change to session file's dir.
9541 */
9542 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02009544 *dirnow = NUL;
9545 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
9546 {
9547 if (vim_chdirfile(fname) == OK)
9548 shorten_fnames(TRUE);
9549 }
9550 else if (*dirnow != NUL
9551 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
9552 {
9553 if (mch_chdir((char *)globaldir) == 0)
9554 shorten_fnames(TRUE);
9555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
Bram Moolenaard9462e32011-04-11 21:35:11 +02009557 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558
Bram Moolenaard9462e32011-04-11 21:35:11 +02009559 /* restore original dir */
9560 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02009562 {
9563 if (mch_chdir((char *)dirnow) != 0)
9564 EMSG(_(e_prev_dir));
9565 shorten_fnames(TRUE);
9566 }
9567 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568 }
9569 }
9570 else
9571 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009572 failed |= (put_view(fd, curwin, !using_vdir, flagp,
9573 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 }
9575 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
9576 == FAIL)
9577 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009578 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
9579 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00009580 if (eap->cmdidx == CMD_mksession)
9581 {
9582 if (put_line(fd, "unlet SessionLoad") == FAIL)
9583 failed = TRUE;
9584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585 }
9586#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00009587 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
9588 failed = TRUE;
9589
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 failed |= fclose(fd);
9591
9592 if (failed)
9593 EMSG(_(e_write));
9594#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
9595 else if (eap->cmdidx == CMD_mksession)
9596 {
9597 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02009598 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599
Bram Moolenaard9462e32011-04-11 21:35:11 +02009600 tbuf = alloc(MAXPATHL);
9601 if (tbuf != NULL)
9602 {
9603 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
9604 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
9605 vim_free(tbuf);
9606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 }
9608#endif
9609#ifdef MKSESSION_NL
9610 mksession_nl = FALSE;
9611#endif
9612 }
9613
9614#ifdef FEAT_BROWSE
9615theend:
9616 vim_free(browseFile);
9617#endif
9618#ifdef FEAT_SESSION
9619 vim_free(viewFile);
9620#endif
9621}
9622
Bram Moolenaardf177f62005-02-22 08:39:57 +00009623#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9624 || defined(PROTO)
9625 int
9626vim_mkdir_emsg(name, prot)
9627 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009628 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009629{
9630 if (vim_mkdir(name, prot) != 0)
9631 {
9632 EMSG2(_("E739: Cannot create directory: %s"), name);
9633 return FAIL;
9634 }
9635 return OK;
9636}
9637#endif
9638
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639/*
9640 * Open a file for writing for an Ex command, with some checks.
9641 * Return file descriptor, or NULL on failure.
9642 */
9643 FILE *
9644open_exfile(fname, forceit, mode)
9645 char_u *fname;
9646 int forceit;
9647 char *mode; /* "w" for create new file or "a" for append */
9648{
9649 FILE *fd;
9650
9651#ifdef UNIX
9652 /* with Unix it is possible to open a directory */
9653 if (mch_isdir(fname))
9654 {
9655 EMSG2(_(e_isadir2), fname);
9656 return NULL;
9657 }
9658#endif
9659 if (!forceit && *mode != 'a' && vim_fexists(fname))
9660 {
9661 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9662 return NULL;
9663 }
9664
9665 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9666 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9667
9668 return fd;
9669}
9670
9671/*
9672 * ":mark" and ":k".
9673 */
9674 static void
9675ex_mark(eap)
9676 exarg_T *eap;
9677{
9678 pos_T pos;
9679
9680 if (*eap->arg == NUL) /* No argument? */
9681 EMSG(_(e_argreq));
9682 else if (eap->arg[1] != NUL) /* more than one character? */
9683 EMSG(_(e_trailing));
9684 else
9685 {
9686 pos = curwin->w_cursor; /* save curwin->w_cursor */
9687 curwin->w_cursor.lnum = eap->line2;
9688 beginline(BL_WHITE | BL_FIX);
9689 if (setmark(*eap->arg) == FAIL) /* set mark */
9690 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9691 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9692 }
9693}
9694
9695/*
9696 * Update w_topline, w_leftcol and the cursor position.
9697 */
9698 void
9699update_topline_cursor()
9700{
9701 check_cursor(); /* put cursor on valid line */
9702 update_topline();
9703 if (!curwin->w_p_wrap)
9704 validate_cursor();
9705 update_curswant();
9706}
9707
9708#ifdef FEAT_EX_EXTRA
9709/*
9710 * ":normal[!] {commands}": Execute normal mode commands.
9711 */
9712 static void
9713ex_normal(eap)
9714 exarg_T *eap;
9715{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 int save_msg_scroll = msg_scroll;
9717 int save_restart_edit = restart_edit;
9718 int save_msg_didout = msg_didout;
9719 int save_State = State;
9720 tasave_T tabuf;
9721 int save_insertmode = p_im;
9722 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009723 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724#ifdef FEAT_MBYTE
9725 char_u *arg = NULL;
9726 int l;
9727 char_u *p;
9728#endif
9729
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009730 if (ex_normal_lock > 0)
9731 {
9732 EMSG(_(e_secure));
9733 return;
9734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 if (ex_normal_busy >= p_mmd)
9736 {
9737 EMSG(_("E192: Recursive use of :normal too deep"));
9738 return;
9739 }
9740 ++ex_normal_busy;
9741
9742 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9743 restart_edit = 0; /* don't go to Insert mode */
9744 p_im = FALSE; /* don't use 'insertmode' */
9745
9746#ifdef FEAT_MBYTE
9747 /*
9748 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9749 * this for the K_SPECIAL leading byte, otherwise special keys will not
9750 * work.
9751 */
9752 if (has_mbyte)
9753 {
9754 int len = 0;
9755
9756 /* Count the number of characters to be escaped. */
9757 for (p = eap->arg; *p != NUL; ++p)
9758 {
9759# ifdef FEAT_GUI
9760 if (*p == CSI) /* leadbyte CSI */
9761 len += 2;
9762# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009763 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9765# ifdef FEAT_GUI
9766 || *p == CSI
9767# endif
9768 )
9769 len += 2;
9770 }
9771 if (len > 0)
9772 {
9773 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9774 if (arg != NULL)
9775 {
9776 len = 0;
9777 for (p = eap->arg; *p != NUL; ++p)
9778 {
9779 arg[len++] = *p;
9780# ifdef FEAT_GUI
9781 if (*p == CSI)
9782 {
9783 arg[len++] = KS_EXTRA;
9784 arg[len++] = (int)KE_CSI;
9785 }
9786# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009787 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 {
9789 arg[len++] = *++p;
9790 if (*p == K_SPECIAL)
9791 {
9792 arg[len++] = KS_SPECIAL;
9793 arg[len++] = KE_FILLER;
9794 }
9795# ifdef FEAT_GUI
9796 else if (*p == CSI)
9797 {
9798 arg[len++] = KS_EXTRA;
9799 arg[len++] = (int)KE_CSI;
9800 }
9801# endif
9802 }
9803 arg[len] = NUL;
9804 }
9805 }
9806 }
9807 }
9808#endif
9809
9810 /*
9811 * Save the current typeahead. This is required to allow using ":normal"
9812 * from an event handler and makes sure we don't hang when the argument
9813 * ends with half a command.
9814 */
9815 save_typeahead(&tabuf);
9816 if (tabuf.typebuf_valid)
9817 {
9818 /*
9819 * Repeat the :normal command for each line in the range. When no
9820 * range given, execute it just once, without positioning the cursor
9821 * first.
9822 */
9823 do
9824 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 if (eap->addr_count != 0)
9826 {
9827 curwin->w_cursor.lnum = eap->line1++;
9828 curwin->w_cursor.col = 0;
9829 }
9830
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009831 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832#ifdef FEAT_MBYTE
9833 arg != NULL ? arg :
9834#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009835 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 }
9837 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9838 }
9839
9840 /* Might not return to the main loop when in an event handler. */
9841 update_topline_cursor();
9842
9843 /* Restore the previous typeahead. */
9844 restore_typeahead(&tabuf);
9845
9846 --ex_normal_busy;
9847 msg_scroll = save_msg_scroll;
9848 restart_edit = save_restart_edit;
9849 p_im = save_insertmode;
9850 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009851 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9853
9854 /* Restore the state (needed when called from a function executed for
Bram Moolenaareda73602014-11-05 09:53:23 +01009855 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 State = save_State;
Bram Moolenaareda73602014-11-05 09:53:23 +01009857#ifdef FEAT_MOUSE
9858 setmouse();
9859#endif
9860#ifdef CURSOR_SHAPE
9861 ui_cursor_shape(); /* may show different cursor shape */
9862#endif
9863
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864#ifdef FEAT_MBYTE
9865 vim_free(arg);
9866#endif
9867}
9868
9869/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009870 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 */
9872 static void
9873ex_startinsert(eap)
9874 exarg_T *eap;
9875{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009876 if (eap->forceit)
9877 {
9878 coladvance((colnr_T)MAXCOL);
9879 curwin->w_curswant = MAXCOL;
9880 curwin->w_set_curswant = FALSE;
9881 }
9882
Bram Moolenaara40c5002005-01-09 21:16:21 +00009883 /* Ignore the command when already in Insert mode. Inserting an
9884 * expression register that invokes a function can do this. */
9885 if (State & INSERT)
9886 return;
9887
Bram Moolenaar64969662005-12-14 21:59:55 +00009888 if (eap->cmdidx == CMD_startinsert)
9889 restart_edit = 'a';
9890 else if (eap->cmdidx == CMD_startreplace)
9891 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009893 restart_edit = 'V';
9894
9895 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009897 if (eap->cmdidx == CMD_startinsert)
9898 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 curwin->w_curswant = 0; /* avoid MAXCOL */
9900 }
9901}
9902
9903/*
9904 * ":stopinsert"
9905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 static void
9907ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009908 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909{
9910 restart_edit = 0;
9911 stop_insert_mode = TRUE;
9912}
9913#endif
9914
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009915#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9916/*
9917 * Execute normal mode command "cmd".
9918 * "remap" can be REMAP_NONE or REMAP_YES.
9919 */
9920 void
9921exec_normal_cmd(cmd, remap, silent)
9922 char_u *cmd;
9923 int remap;
9924 int silent;
9925{
9926 oparg_T oa;
9927
9928 /*
9929 * Stuff the argument into the typeahead buffer.
9930 * Execute normal_cmd() until there is no typeahead left.
9931 */
9932 clear_oparg(&oa);
9933 finish_op = FALSE;
9934 ins_typebuf(cmd, remap, 0, TRUE, silent);
9935 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9936 && !got_int)
9937 {
9938 update_topline_cursor();
Bram Moolenaar48ac02c2011-01-17 19:50:06 +01009939 normal_cmd(&oa, TRUE); /* execute a Normal mode cmd */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009940 }
9941}
9942#endif
9943
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944#ifdef FEAT_FIND_ID
9945 static void
9946ex_checkpath(eap)
9947 exarg_T *eap;
9948{
9949 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9950 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9951 (linenr_T)1, (linenr_T)MAXLNUM);
9952}
9953
9954#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9955/*
9956 * ":psearch"
9957 */
9958 static void
9959ex_psearch(eap)
9960 exarg_T *eap;
9961{
9962 g_do_tagpreview = p_pvh;
9963 ex_findpat(eap);
9964 g_do_tagpreview = 0;
9965}
9966#endif
9967
9968 static void
9969ex_findpat(eap)
9970 exarg_T *eap;
9971{
9972 int whole = TRUE;
9973 long n;
9974 char_u *p;
9975 int action;
9976
9977 switch (cmdnames[eap->cmdidx].cmd_name[2])
9978 {
9979 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9980 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9981 action = ACTION_GOTO;
9982 else
9983 action = ACTION_SHOW;
9984 break;
9985 case 'i': /* ":ilist" and ":dlist" */
9986 action = ACTION_SHOW_ALL;
9987 break;
9988 case 'u': /* ":ijump" and ":djump" */
9989 action = ACTION_GOTO;
9990 break;
9991 default: /* ":isplit" and ":dsplit" */
9992 action = ACTION_SPLIT;
9993 break;
9994 }
9995
9996 n = 1;
9997 if (vim_isdigit(*eap->arg)) /* get count */
9998 {
9999 n = getdigits(&eap->arg);
10000 eap->arg = skipwhite(eap->arg);
10001 }
10002 if (*eap->arg == '/') /* Match regexp, not just whole words */
10003 {
10004 whole = FALSE;
10005 ++eap->arg;
10006 p = skip_regexp(eap->arg, '/', p_magic, NULL);
10007 if (*p)
10008 {
10009 *p++ = NUL;
10010 p = skipwhite(p);
10011
10012 /* Check for trailing illegal characters */
10013 if (!ends_excmd(*p))
10014 eap->errmsg = e_trailing;
10015 else
10016 eap->nextcmd = check_nextcmd(p);
10017 }
10018 }
10019 if (!eap->skip)
10020 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
10021 whole, !eap->forceit,
10022 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
10023 n, action, eap->line1, eap->line2);
10024}
10025#endif
10026
10027#ifdef FEAT_WINDOWS
10028
10029# ifdef FEAT_QUICKFIX
10030/*
10031 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
10032 */
10033 static void
10034ex_ptag(eap)
10035 exarg_T *eap;
10036{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +010010037 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010038 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10039}
10040
10041/*
10042 * ":pedit"
10043 */
10044 static void
10045ex_pedit(eap)
10046 exarg_T *eap;
10047{
10048 win_T *curwin_save = curwin;
10049
10050 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +000010051 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 keep_help_flag = curwin_save->w_buffer->b_help;
10053 do_exedit(eap, NULL);
10054 keep_help_flag = FALSE;
10055 if (curwin != curwin_save && win_valid(curwin_save))
10056 {
10057 /* Return cursor to where we were */
10058 validate_cursor();
10059 redraw_later(VALID);
10060 win_enter(curwin_save, TRUE);
10061 }
10062 g_do_tagpreview = 0;
10063}
10064# endif
10065
10066/*
10067 * ":stag", ":stselect" and ":stjump".
10068 */
10069 static void
10070ex_stag(eap)
10071 exarg_T *eap;
10072{
10073 postponed_split = -1;
10074 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010075 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
10077 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +000010078 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079}
10080#endif
10081
10082/*
10083 * ":tag", ":tselect", ":tjump", ":tnext", etc.
10084 */
10085 static void
10086ex_tag(eap)
10087 exarg_T *eap;
10088{
10089 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
10090}
10091
10092 static void
10093ex_tag_cmd(eap, name)
10094 exarg_T *eap;
10095 char_u *name;
10096{
10097 int cmd;
10098
10099 switch (name[1])
10100 {
10101 case 'j': cmd = DT_JUMP; /* ":tjump" */
10102 break;
10103 case 's': cmd = DT_SELECT; /* ":tselect" */
10104 break;
10105 case 'p': cmd = DT_PREV; /* ":tprevious" */
10106 break;
10107 case 'N': cmd = DT_PREV; /* ":tNext" */
10108 break;
10109 case 'n': cmd = DT_NEXT; /* ":tnext" */
10110 break;
10111 case 'o': cmd = DT_POP; /* ":pop" */
10112 break;
10113 case 'f': /* ":tfirst" */
10114 case 'r': cmd = DT_FIRST; /* ":trewind" */
10115 break;
10116 case 'l': cmd = DT_LAST; /* ":tlast" */
10117 break;
10118 default: /* ":tag" */
10119#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +000010120 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 {
10122 do_cstag(eap);
10123 return;
10124 }
10125#endif
10126 cmd = DT_TAG;
10127 break;
10128 }
10129
Bram Moolenaarb8a7b562006-02-01 21:47:16 +000010130 if (name[0] == 'l')
10131 {
10132#ifndef FEAT_QUICKFIX
10133 ex_ni(eap);
10134 return;
10135#else
10136 cmd = DT_LTAG;
10137#endif
10138 }
10139
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
10141 eap->forceit, TRUE);
10142}
10143
10144/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010145 * Check "str" for starting with a special cmdline variable.
10146 * If found return one of the SPEC_ values and set "*usedlen" to the length of
10147 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
10148 */
10149 int
10150find_cmdline_var(src, usedlen)
10151 char_u *src;
10152 int *usedlen;
10153{
10154 int len;
10155 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +000010156 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010157 "%",
10158#define SPEC_PERC 0
10159 "#",
10160#define SPEC_HASH 1
10161 "<cword>", /* cursor word */
10162#define SPEC_CWORD 2
10163 "<cWORD>", /* cursor WORD */
10164#define SPEC_CCWORD 3
10165 "<cfile>", /* cursor path name */
10166#define SPEC_CFILE 4
10167 "<sfile>", /* ":so" file name */
10168#define SPEC_SFILE 5
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010169 "<slnum>", /* ":so" file line number */
10170#define SPEC_SLNUM 6
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010171#ifdef FEAT_AUTOCMD
10172 "<afile>", /* autocommand file name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010173# define SPEC_AFILE 7
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010174 "<abuf>", /* autocommand buffer number */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010175# define SPEC_ABUF 8
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010176 "<amatch>", /* autocommand match name */
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010177# define SPEC_AMATCH 9
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010178#endif
10179#ifdef FEAT_CLIENTSERVER
10180 "<client>"
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010181# ifdef FEAT_AUTOCMD
10182# define SPEC_CLIENT 10
10183# else
10184# define SPEC_CLIENT 7
10185# endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010186#endif
10187 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010188
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010189 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010190 {
10191 len = (int)STRLEN(spec_str[i]);
10192 if (STRNCMP(src, spec_str[i], len) == 0)
10193 {
10194 *usedlen = len;
10195 return i;
10196 }
10197 }
10198 return -1;
10199}
10200
10201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 * Evaluate cmdline variables.
10203 *
10204 * change '%' to curbuf->b_ffname
10205 * '#' to curwin->w_altfile
10206 * '<cword>' to word under the cursor
10207 * '<cWORD>' to WORD under the cursor
10208 * '<cfile>' to path name under the cursor
10209 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010210 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 * '<afile>' to file name for autocommand
10212 * '<abuf>' to buffer number for autocommand
10213 * '<amatch>' to matching name for autocommand
10214 *
10215 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
10216 * "" for error without a message) and NULL is returned.
10217 * Returns an allocated string if a valid match was found.
10218 * Returns NULL if no match was found. "usedlen" then still contains the
10219 * number of characters to skip.
10220 */
10221 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +000010222eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010224 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225 int *usedlen; /* characters after src that are used */
10226 linenr_T *lnump; /* line number for :e command, or NULL */
10227 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010228 int *escaped; /* return value has escaped white space (can
10229 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230{
10231 int i;
10232 char_u *s;
10233 char_u *result;
10234 char_u *resultbuf = NULL;
10235 int resultlen;
10236 buf_T *buf;
10237 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
10238 int spec_idx;
10239#ifdef FEAT_MODIFY_FNAME
10240 int skip_mod = FALSE;
10241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010243
10244 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010245 if (escaped != NULL)
10246 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247
10248 /*
10249 * Check if there is something to do.
10250 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000010251 spec_idx = find_cmdline_var(src, usedlen);
10252 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 {
10254 *usedlen = 1;
10255 return NULL;
10256 }
10257
10258 /*
10259 * Skip when preceded with a backslash "\%" and "\#".
10260 * Note: In "\\%" the % is also not recognized!
10261 */
10262 if (src > srcstart && src[-1] == '\\')
10263 {
10264 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +000010265 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 return NULL;
10267 }
10268
10269 /*
10270 * word or WORD under cursor
10271 */
10272 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
10273 {
10274 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
10275 (FIND_IDENT|FIND_STRING) : FIND_STRING);
10276 if (resultlen == 0)
10277 {
10278 *errormsg = (char_u *)"";
10279 return NULL;
10280 }
10281 }
10282
10283 /*
10284 * '#': Alternate file name
10285 * '%': Current file name
10286 * File name under the cursor
10287 * File name for autocommand
10288 * and following modifiers
10289 */
10290 else
10291 {
10292 switch (spec_idx)
10293 {
10294 case SPEC_PERC: /* '%': current file */
10295 if (curbuf->b_fname == NULL)
10296 {
10297 result = (char_u *)"";
10298 valid = 0; /* Must have ":p:h" to be valid */
10299 }
10300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 result = curbuf->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010302 break;
10303
10304 case SPEC_HASH: /* '#' or "#99": alternate file */
10305 if (src[1] == '#') /* "##": the argument list */
10306 {
10307 result = arg_all();
10308 resultbuf = result;
10309 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +000010310 if (escaped != NULL)
10311 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312#ifdef FEAT_MODIFY_FNAME
10313 skip_mod = TRUE;
10314#endif
10315 break;
10316 }
10317 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +000010318 if (*s == '<') /* "#<99" uses v:oldfiles */
10319 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 i = (int)getdigits(&s);
10321 *usedlen = (int)(s - src); /* length of what we expand */
10322
Bram Moolenaard812df62008-11-09 12:46:09 +000010323 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010325 if (*usedlen < 2)
10326 {
10327 /* Should we give an error message for #<text? */
10328 *usedlen = 1;
10329 return NULL;
10330 }
10331#ifdef FEAT_EVAL
10332 result = list_find_str(get_vim_var_list(VV_OLDFILES),
10333 (long)i);
10334 if (result == NULL)
10335 {
10336 *errormsg = (char_u *)"";
10337 return NULL;
10338 }
10339#else
10340 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +000010342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343 }
10344 else
Bram Moolenaard812df62008-11-09 12:46:09 +000010345 {
10346 buf = buflist_findnr(i);
10347 if (buf == NULL)
10348 {
10349 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
10350 return NULL;
10351 }
10352 if (lnump != NULL)
10353 *lnump = ECMD_LAST;
10354 if (buf->b_fname == NULL)
10355 {
10356 result = (char_u *)"";
10357 valid = 0; /* Must have ":p:h" to be valid */
10358 }
10359 else
10360 result = buf->b_fname;
10361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 break;
10363
10364#ifdef FEAT_SEARCHPATH
10365 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000010366 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 if (result == NULL)
10368 {
10369 *errormsg = (char_u *)"";
10370 return NULL;
10371 }
10372 resultbuf = result; /* remember allocated string */
10373 break;
10374#endif
10375
10376#ifdef FEAT_AUTOCMD
10377 case SPEC_AFILE: /* file name for autocommand */
10378 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +000010379 if (result != NULL && !autocmd_fname_full)
10380 {
10381 /* Still need to turn the fname into a full path. It is
10382 * postponed to avoid a delay when <afile> is not used. */
10383 autocmd_fname_full = TRUE;
10384 result = FullName_save(autocmd_fname, FALSE);
10385 vim_free(autocmd_fname);
10386 autocmd_fname = result;
10387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 if (result == NULL)
10389 {
10390 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
10391 return NULL;
10392 }
Bram Moolenaara0174af2008-01-02 20:08:25 +000010393 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 break;
10395
10396 case SPEC_ABUF: /* buffer number for autocommand */
10397 if (autocmd_bufnr <= 0)
10398 {
10399 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
10400 return NULL;
10401 }
10402 sprintf((char *)strbuf, "%d", autocmd_bufnr);
10403 result = strbuf;
10404 break;
10405
10406 case SPEC_AMATCH: /* match name for autocommand */
10407 result = autocmd_match;
10408 if (result == NULL)
10409 {
10410 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
10411 return NULL;
10412 }
10413 break;
10414
10415#endif
10416 case SPEC_SFILE: /* file name for ":so" command */
10417 result = sourcing_name;
10418 if (result == NULL)
10419 {
10420 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
10421 return NULL;
10422 }
10423 break;
Bram Moolenaar4dbbff52010-11-24 15:50:59 +010010424 case SPEC_SLNUM: /* line in file for ":so" command */
10425 if (sourcing_name == NULL || sourcing_lnum == 0)
10426 {
10427 *errormsg = (char_u *)_("E842: no line number to use for \"<slnum>\"");
10428 return NULL;
10429 }
10430 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
10431 result = strbuf;
10432 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010433#if defined(FEAT_CLIENTSERVER)
10434 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010435 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
10436 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 result = strbuf;
10438 break;
10439#endif
10440 }
10441
10442 resultlen = (int)STRLEN(result); /* length of new string */
10443 if (src[*usedlen] == '<') /* remove the file name extension */
10444 {
10445 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010446 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 resultlen = (int)(s - result);
10448 }
10449#ifdef FEAT_MODIFY_FNAME
10450 else if (!skip_mod)
10451 {
10452 valid |= modify_fname(src, usedlen, &result, &resultbuf,
10453 &resultlen);
10454 if (result == NULL)
10455 {
10456 *errormsg = (char_u *)"";
10457 return NULL;
10458 }
10459 }
10460#endif
10461 }
10462
10463 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
10464 {
10465 if (valid != VALID_HEAD + VALID_PATH)
10466 /* xgettext:no-c-format */
10467 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
10468 else
10469 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
10470 result = NULL;
10471 }
10472 else
10473 result = vim_strnsave(result, resultlen);
10474 vim_free(resultbuf);
10475 return result;
10476}
10477
10478/*
10479 * Concatenate all files in the argument list, separated by spaces, and return
10480 * it in one allocated string.
10481 * Spaces and backslashes in the file names are escaped with a backslash.
10482 * Returns NULL when out of memory.
10483 */
10484 static char_u *
10485arg_all()
10486{
10487 int len;
10488 int idx;
10489 char_u *retval = NULL;
10490 char_u *p;
10491
10492 /*
10493 * Do this loop two times:
10494 * first time: compute the total length
10495 * second time: concatenate the names
10496 */
10497 for (;;)
10498 {
10499 len = 0;
10500 for (idx = 0; idx < ARGCOUNT; ++idx)
10501 {
10502 p = alist_name(&ARGLIST[idx]);
10503 if (p != NULL)
10504 {
10505 if (len > 0)
10506 {
10507 /* insert a space in between names */
10508 if (retval != NULL)
10509 retval[len] = ' ';
10510 ++len;
10511 }
10512 for ( ; *p != NUL; ++p)
10513 {
10514 if (*p == ' ' || *p == '\\')
10515 {
10516 /* insert a backslash */
10517 if (retval != NULL)
10518 retval[len] = '\\';
10519 ++len;
10520 }
10521 if (retval != NULL)
10522 retval[len] = *p;
10523 ++len;
10524 }
10525 }
10526 }
10527
10528 /* second time: break here */
10529 if (retval != NULL)
10530 {
10531 retval[len] = NUL;
10532 break;
10533 }
10534
10535 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +000010536 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 if (retval == NULL)
10538 break;
10539 }
10540
10541 return retval;
10542}
10543
10544#if defined(FEAT_AUTOCMD) || defined(PROTO)
10545/*
10546 * Expand the <sfile> string in "arg".
10547 *
10548 * Returns an allocated string, or NULL for any error.
10549 */
10550 char_u *
10551expand_sfile(arg)
10552 char_u *arg;
10553{
10554 char_u *errormsg;
10555 int len;
10556 char_u *result;
10557 char_u *newres;
10558 char_u *repl;
10559 int srclen;
10560 char_u *p;
10561
10562 result = vim_strsave(arg);
10563 if (result == NULL)
10564 return NULL;
10565
10566 for (p = result; *p; )
10567 {
10568 if (STRNCMP(p, "<sfile>", 7) != 0)
10569 ++p;
10570 else
10571 {
10572 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +000010573 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 if (errormsg != NULL)
10575 {
10576 if (*errormsg)
10577 emsg(errormsg);
10578 vim_free(result);
10579 return NULL;
10580 }
10581 if (repl == NULL) /* no match (cannot happen) */
10582 {
10583 p += srclen;
10584 continue;
10585 }
10586 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
10587 newres = alloc(len);
10588 if (newres == NULL)
10589 {
10590 vim_free(repl);
10591 vim_free(result);
10592 return NULL;
10593 }
10594 mch_memmove(newres, result, (size_t)(p - result));
10595 STRCPY(newres + (p - result), repl);
10596 len = (int)STRLEN(newres);
10597 STRCAT(newres, p + srclen);
10598 vim_free(repl);
10599 vim_free(result);
10600 result = newres;
10601 p = newres + len; /* continue after the match */
10602 }
10603 }
10604
10605 return result;
10606}
10607#endif
10608
10609#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010610static int ses_winsizes __ARGS((FILE *fd, int restore_size,
10611 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
10613static frame_T *ses_skipframe __ARGS((frame_T *fr));
10614static int ses_do_frame __ARGS((frame_T *fr));
10615static int ses_do_win __ARGS((win_T *wp));
10616static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
10617static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
10618static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
10619
10620/*
10621 * Write openfile commands for the current buffers to an .exrc file.
10622 * Return FAIL on error, OK otherwise.
10623 */
10624 static int
10625makeopens(fd, dirnow)
10626 FILE *fd;
10627 char_u *dirnow; /* Current directory name */
10628{
10629 buf_T *buf;
10630 int only_save_windows = TRUE;
10631 int nr;
10632 int cnr = 1;
10633 int restore_size = TRUE;
10634 win_T *wp;
10635 char_u *sname;
10636 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010637 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010638 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010639 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010640 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010641 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010642 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643
10644 if (ssop_flags & SSOP_BUFFERS)
10645 only_save_windows = FALSE; /* Save ALL buffers */
10646
10647 /*
10648 * Begin by setting the this_session variable, and then other
10649 * sessionable variables.
10650 */
10651#ifdef FEAT_EVAL
10652 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10653 return FAIL;
10654 if (ssop_flags & SSOP_GLOBALS)
10655 if (store_session_globals(fd) == FAIL)
10656 return FAIL;
10657#endif
10658
10659 /*
10660 * Close all windows but one.
10661 */
10662 if (put_line(fd, "silent only") == FAIL)
10663 return FAIL;
10664
10665 /*
10666 * Now a :cd command to the session directory or the current directory
10667 */
10668 if (ssop_flags & SSOP_SESDIR)
10669 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010670 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10671 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010672 return FAIL;
10673 }
10674 else if (ssop_flags & SSOP_CURDIR)
10675 {
10676 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10677 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010678 || fputs("cd ", fd) < 0
10679 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10680 || put_eol(fd) == FAIL)
10681 {
10682 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010683 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685 vim_free(sname);
10686 }
10687
10688 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010689 * If there is an empty, unnamed buffer we will wipe it out later.
10690 * Remember the buffer number.
10691 */
10692 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10693 return FAIL;
10694 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10695 return FAIL;
10696 if (put_line(fd, "endif") == FAIL)
10697 return FAIL;
10698
10699 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 * Now save the current files, current buffer first.
10701 */
10702 if (put_line(fd, "set shortmess=aoO") == FAIL)
10703 return FAIL;
10704
10705 /* Now put the other buffers into the buffer list */
10706 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10707 {
10708 if (!(only_save_windows && buf->b_nwindows == 0)
10709 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10710 && buf->b_fname != NULL
10711 && buf->b_p_bl)
10712 {
10713 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10714 : buf->b_wininfo->wi_fpos.lnum) < 0
10715 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10716 return FAIL;
10717 }
10718 }
10719
10720 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010721 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10723 return FAIL;
10724
10725 if (ssop_flags & SSOP_RESIZE)
10726 {
10727 /* Note: after the restore we still check it worked!*/
10728 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10729 || put_eol(fd) == FAIL)
10730 return FAIL;
10731 }
10732
10733#ifdef FEAT_GUI
10734 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10735 {
10736 int x, y;
10737
10738 if (gui_mch_get_winpos(&x, &y) == OK)
10739 {
10740 /* Note: after the restore we still check it worked!*/
10741 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10742 return FAIL;
10743 }
10744 }
10745#endif
10746
10747 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010748 * When there are two or more tabpages and 'showtabline' is 1 the tabline
10749 * will be displayed when creating the next tab. That resizes the windows
10750 * in the first tab, which may cause problems. Set 'showtabline' to 2
10751 * temporarily to avoid that.
10752 */
10753 if (p_stal == 1 && first_tabpage->tp_next != NULL)
10754 {
10755 if (put_line(fd, "set stal=2") == FAIL)
10756 return FAIL;
10757 restore_stal = TRUE;
10758 }
10759
10760 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010761 * May repeat putting Windows for each tab, when "tabpages" is in
10762 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010763 * Don't use goto_tabpage(), it may change directory and trigger
10764 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010766 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010767 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010768 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010770 int need_tabnew = FALSE;
10771
Bram Moolenaar18144c82006-04-12 21:52:12 +000010772 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010774 tabpage_T *tp = find_tabpage(tabnr);
10775
10776 if (tp == NULL)
10777 break; /* done all tab pages */
10778 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010779 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010780 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010781 tab_topframe = topframe;
10782 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010783 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010784 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010785 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010786 tab_topframe = tp->tp_topframe;
10787 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010788 if (tabnr > 1)
10789 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791
Bram Moolenaar18144c82006-04-12 21:52:12 +000010792 /*
10793 * Before creating the window layout, try loading one file. If this
10794 * is aborted we don't end up with a number of useless windows.
10795 * This may have side effects! (e.g., compressed or network file).
10796 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010797 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010798 {
10799 if (ses_do_win(wp)
10800 && wp->w_buffer->b_ffname != NULL
10801 && !wp->w_buffer->b_help
10802#ifdef FEAT_QUICKFIX
10803 && !bt_nofile(wp->w_buffer)
10804#endif
10805 )
10806 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010807 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010808 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10809 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010810 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010811 if (!wp->w_arg_idx_invalid)
10812 edited_win = wp;
10813 break;
10814 }
10815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010817 /* If no file got edited create an empty tab page. */
10818 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10819 return FAIL;
10820
Bram Moolenaar18144c82006-04-12 21:52:12 +000010821 /*
10822 * Save current window layout.
10823 */
10824 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010826 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010828 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10829 return FAIL;
10830 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832
Bram Moolenaar18144c82006-04-12 21:52:12 +000010833 /*
10834 * Check if window sizes can be restored (no windows omitted).
10835 * Remember the window number of the current window after restoring.
10836 */
10837 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010838 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010839 {
10840 if (ses_do_win(wp))
10841 ++nr;
10842 else
10843 restore_size = FALSE;
10844 if (curwin == wp)
10845 cnr = nr;
10846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847
Bram Moolenaar18144c82006-04-12 21:52:12 +000010848 /* Go to the first window. */
10849 if (put_line(fd, "wincmd t") == FAIL)
10850 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010851
Bram Moolenaar18144c82006-04-12 21:52:12 +000010852 /*
10853 * If more than one window, see if sizes can be restored.
10854 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10855 * resized when moving between windows.
10856 * Do this before restoring the view, so that the topline and the
10857 * cursor can be set. This is done again below.
10858 */
10859 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10860 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010861 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010862 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863
Bram Moolenaar18144c82006-04-12 21:52:12 +000010864 /*
10865 * Restore the view of the window (options, file, cursor, etc.).
10866 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010867 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010868 {
10869 if (!ses_do_win(wp))
10870 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010871 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10872 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010873 return FAIL;
10874 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10875 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010876 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010877 }
10878
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010879 /* The argument index in the first tab page is zero, need to set it in
10880 * each window. For further tab pages it's the window where we do
10881 * "tabedit". */
10882 cur_arg_idx = next_arg_idx;
10883
Bram Moolenaar18144c82006-04-12 21:52:12 +000010884 /*
10885 * Restore cursor to the current window if it's not the first one.
10886 */
10887 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10888 || put_eol(fd) == FAIL))
10889 return FAIL;
10890
10891 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010892 * Restore window sizes again after jumping around in windows, because
10893 * the current window has a minimum size while others may not.
10894 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010895 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010896 return FAIL;
10897
Bram Moolenaar18144c82006-04-12 21:52:12 +000010898 /* Don't continue in another tab page when doing only the current one
10899 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010900 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010901 break;
10902 }
10903
10904 if (ssop_flags & SSOP_TABPAGES)
10905 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010906 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10907 || put_eol(fd) == FAIL)
10908 return FAIL;
10909 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010910 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10911 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010912
Bram Moolenaar9c102382006-05-03 21:26:49 +000010913 /*
10914 * Wipe out an empty unnamed buffer we started in.
10915 */
10916 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10917 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010918 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010919 return FAIL;
10920 if (put_line(fd, "endif") == FAIL)
10921 return FAIL;
10922 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10923 return FAIL;
10924
10925 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10926 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10927 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929
10930 /*
10931 * Lastly, execute the x.vim file if it exists.
10932 */
10933 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10934 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010935 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 || put_line(fd, "endif") == FAIL)
10937 return FAIL;
10938
10939 return OK;
10940}
10941
10942 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010943ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010944 FILE *fd;
10945 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010946 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947{
10948 int n = 0;
10949 win_T *wp;
10950
10951 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10952 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010953 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 {
10955 if (!ses_do_win(wp))
10956 continue;
10957 ++n;
10958
10959 /* restore height when not full height */
10960 if (wp->w_height + wp->w_status_height < topframe->fr_height
10961 && (fprintf(fd,
10962 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10963 n, (long)wp->w_height, Rows / 2, Rows) < 0
10964 || put_eol(fd) == FAIL))
10965 return FAIL;
10966
10967 /* restore width when not full width */
10968 if (wp->w_width < Columns && (fprintf(fd,
10969 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10970 n, (long)wp->w_width, Columns / 2, Columns) < 0
10971 || put_eol(fd) == FAIL))
10972 return FAIL;
10973 }
10974 }
10975 else
10976 {
10977 /* Just equalise window sizes */
10978 if (put_line(fd, "wincmd =") == FAIL)
10979 return FAIL;
10980 }
10981 return OK;
10982}
10983
10984/*
10985 * Write commands to "fd" to recursively create windows for frame "fr",
10986 * horizontally and vertically split.
10987 * After the commands the last window in the frame is the current window.
10988 * Returns FAIL when writing the commands to "fd" fails.
10989 */
10990 static int
10991ses_win_rec(fd, fr)
10992 FILE *fd;
10993 frame_T *fr;
10994{
10995 frame_T *frc;
10996 int count = 0;
10997
10998 if (fr->fr_layout != FR_LEAF)
10999 {
11000 /* Find first frame that's not skipped and then create a window for
11001 * each following one (first frame is already there). */
11002 frc = ses_skipframe(fr->fr_child);
11003 if (frc != NULL)
11004 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
11005 {
11006 /* Make window as big as possible so that we have lots of room
11007 * to split. */
11008 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
11009 || put_line(fd, fr->fr_layout == FR_COL
11010 ? "split" : "vsplit") == FAIL)
11011 return FAIL;
11012 ++count;
11013 }
11014
11015 /* Go back to the first window. */
11016 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
11017 ? "%dwincmd k" : "%dwincmd h", count) < 0
11018 || put_eol(fd) == FAIL))
11019 return FAIL;
11020
11021 /* Recursively create frames/windows in each window of this column or
11022 * row. */
11023 frc = ses_skipframe(fr->fr_child);
11024 while (frc != NULL)
11025 {
11026 ses_win_rec(fd, frc);
11027 frc = ses_skipframe(frc->fr_next);
11028 /* Go to next window. */
11029 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
11030 return FAIL;
11031 }
11032 }
11033 return OK;
11034}
11035
11036/*
11037 * Skip frames that don't contain windows we want to save in the Session.
11038 * Returns NULL when there none.
11039 */
11040 static frame_T *
11041ses_skipframe(fr)
11042 frame_T *fr;
11043{
11044 frame_T *frc;
11045
11046 for (frc = fr; frc != NULL; frc = frc->fr_next)
11047 if (ses_do_frame(frc))
11048 break;
11049 return frc;
11050}
11051
11052/*
11053 * Return TRUE if frame "fr" has a window somewhere that we want to save in
11054 * the Session.
11055 */
11056 static int
11057ses_do_frame(fr)
11058 frame_T *fr;
11059{
11060 frame_T *frc;
11061
11062 if (fr->fr_layout == FR_LEAF)
11063 return ses_do_win(fr->fr_win);
11064 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
11065 if (ses_do_frame(frc))
11066 return TRUE;
11067 return FALSE;
11068}
11069
11070/*
11071 * Return non-zero if window "wp" is to be stored in the Session.
11072 */
11073 static int
11074ses_do_win(wp)
11075 win_T *wp;
11076{
11077 if (wp->w_buffer->b_fname == NULL
11078#ifdef FEAT_QUICKFIX
11079 /* When 'buftype' is "nofile" can't restore the window contents. */
11080 || bt_nofile(wp->w_buffer)
11081#endif
11082 )
11083 return (ssop_flags & SSOP_BLANK);
11084 if (wp->w_buffer->b_help)
11085 return (ssop_flags & SSOP_HELP);
11086 return TRUE;
11087}
11088
11089/*
11090 * Write commands to "fd" to restore the view of a window.
11091 * Caller must make sure 'scrolloff' is zero.
11092 */
11093 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011094put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 FILE *fd;
11096 win_T *wp;
11097 int add_edit; /* add ":edit" command to view */
11098 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011099 int current_arg_idx; /* current argument index of the window, use
11100 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101{
11102 win_T *save_curwin;
11103 int f;
11104 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011105 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106
11107 /* Always restore cursor position for ":mksession". For ":mkview" only
11108 * when 'viewoptions' contains "cursor". */
11109 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
11110
11111 /*
11112 * Local argument list.
11113 */
11114 if (wp->w_alist == &global_alist)
11115 {
11116 if (put_line(fd, "argglobal") == FAIL)
11117 return FAIL;
11118 }
11119 else
11120 {
11121 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
11122 flagp == &vop_flags
11123 || !(*flagp & SSOP_CURDIR)
11124 || wp->w_localdir != NULL, flagp) == FAIL)
11125 return FAIL;
11126 }
11127
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011128 /* Only when part of a session: restore the argument index. Some
11129 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020011130 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011131 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000011133 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 || put_eol(fd) == FAIL)
11135 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011136 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137 }
11138
11139 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000011140 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141 {
11142 /*
11143 * Load the file.
11144 */
11145 if (wp->w_buffer->b_ffname != NULL
11146#ifdef FEAT_QUICKFIX
11147 && !bt_nofile(wp->w_buffer)
11148#endif
11149 )
11150 {
11151 /*
11152 * Editing a file in this buffer: use ":edit file".
11153 * This may have side effects! (e.g., compressed or network file).
11154 */
11155 if (fputs("edit ", fd) < 0
11156 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11157 return FAIL;
11158 }
11159 else
11160 {
11161 /* No file in this buffer, just make it empty. */
11162 if (put_line(fd, "enew") == FAIL)
11163 return FAIL;
11164#ifdef FEAT_QUICKFIX
11165 if (wp->w_buffer->b_ffname != NULL)
11166 {
11167 /* The buffer does have a name, but it's not a file name. */
11168 if (fputs("file ", fd) < 0
11169 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
11170 return FAIL;
11171 }
11172#endif
11173 do_cursor = FALSE;
11174 }
11175 }
11176
11177 /*
11178 * Local mappings and abbreviations.
11179 */
11180 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11181 && makemap(fd, wp->w_buffer) == FAIL)
11182 return FAIL;
11183
11184 /*
11185 * Local options. Need to go to the window temporarily.
11186 * Store only local values when using ":mkview" and when ":mksession" is
11187 * used and 'sessionoptions' doesn't include "options".
11188 * Some folding options are always stored when "folds" is included,
11189 * otherwise the folds would not be restored correctly.
11190 */
11191 save_curwin = curwin;
11192 curwin = wp;
11193 curbuf = curwin->w_buffer;
11194 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
11195 f = makeset(fd, OPT_LOCAL,
11196 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
11197#ifdef FEAT_FOLDING
11198 else if (*flagp & SSOP_FOLDS)
11199 f = makefoldset(fd);
11200#endif
11201 else
11202 f = OK;
11203 curwin = save_curwin;
11204 curbuf = curwin->w_buffer;
11205 if (f == FAIL)
11206 return FAIL;
11207
11208#ifdef FEAT_FOLDING
11209 /*
11210 * Save Folds when 'buftype' is empty and for help files.
11211 */
11212 if ((*flagp & SSOP_FOLDS)
11213 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000011214# ifdef FEAT_QUICKFIX
11215 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
11216# endif
11217 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218 {
11219 if (put_folds(fd, wp) == FAIL)
11220 return FAIL;
11221 }
11222#endif
11223
11224 /*
11225 * Set the cursor after creating folds, since that moves the cursor.
11226 */
11227 if (do_cursor)
11228 {
11229
11230 /* Restore the cursor line in the file and relatively in the
11231 * window. Don't use "G", it changes the jumplist. */
11232 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
11233 (long)wp->w_cursor.lnum,
11234 (long)(wp->w_cursor.lnum - wp->w_topline),
11235 (long)wp->w_height / 2, (long)wp->w_height) < 0
11236 || put_eol(fd) == FAIL
11237 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
11238 || put_line(fd, "exe s:l") == FAIL
11239 || put_line(fd, "normal! zt") == FAIL
11240 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
11241 || put_eol(fd) == FAIL)
11242 return FAIL;
11243 /* Restore the cursor column and left offset when not wrapping. */
11244 if (wp->w_cursor.col == 0)
11245 {
11246 if (put_line(fd, "normal! 0") == FAIL)
11247 return FAIL;
11248 }
11249 else
11250 {
11251 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
11252 {
11253 if (fprintf(fd,
11254 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011255 (long)wp->w_virtcol + 1,
11256 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 (long)wp->w_width / 2, (long)wp->w_width) < 0
11258 || put_eol(fd) == FAIL
11259 || put_line(fd, "if s:c > 0") == FAIL
11260 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011261 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
11262 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 || put_eol(fd) == FAIL
11264 || put_line(fd, "else") == FAIL
Bram Moolenaarfdf447b2013-02-26 17:21:29 +010011265 || fprintf(fd, " normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 || put_eol(fd) == FAIL
11267 || put_line(fd, "endif") == FAIL)
11268 return FAIL;
11269 }
11270 else
11271 {
Bram Moolenaar558ddad2013-02-20 19:26:29 +010011272 if (fprintf(fd, "normal! 0%d|", wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273 || put_eol(fd) == FAIL)
11274 return FAIL;
11275 }
11276 }
11277 }
11278
11279 /*
11280 * Local directory.
11281 */
11282 if (wp->w_localdir != NULL)
11283 {
11284 if (fputs("lcd ", fd) < 0
11285 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
11286 || put_eol(fd) == FAIL)
11287 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011288 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 }
11290
11291 return OK;
11292}
11293
11294/*
11295 * Write an argument list to the session file.
11296 * Returns FAIL if writing fails.
11297 */
11298 static int
11299ses_arglist(fd, cmd, gap, fullname, flagp)
11300 FILE *fd;
11301 char *cmd;
11302 garray_T *gap;
11303 int fullname; /* TRUE: use full path name */
11304 unsigned *flagp;
11305{
11306 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011307 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308 char_u *s;
11309
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011310 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
11311 return FAIL;
11312 if (put_line(fd, "silent! argdel *") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 return FAIL;
11314 for (i = 0; i < gap->ga_len; ++i)
11315 {
11316 /* NULL file names are skipped (only happens when out of memory). */
11317 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
11318 if (s != NULL)
11319 {
11320 if (fullname)
11321 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011322 buf = alloc(MAXPATHL);
11323 if (buf != NULL)
11324 {
11325 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
11326 s = buf;
11327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011329 if (fputs("argadd ", fd) < 0
11330 || ses_put_fname(fd, s, flagp) == FAIL
11331 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020011332 {
11333 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011335 }
11336 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 }
11338 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010011339 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340}
11341
11342/*
11343 * Write a buffer name to the session file.
11344 * Also ends the line.
11345 * Returns FAIL if writing fails.
11346 */
11347 static int
11348ses_fname(fd, buf, flagp)
11349 FILE *fd;
11350 buf_T *buf;
11351 unsigned *flagp;
11352{
11353 char_u *name;
11354
11355 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011356 * the session file will be sourced.
11357 * Don't do this for ":mkview", we don't know the current directory.
11358 * Don't do this after ":lcd", we don't keep track of what the current
11359 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360 if (buf->b_sfname != NULL
11361 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011362 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000011363#ifdef FEAT_AUTOCHDIR
11364 && !p_acd
11365#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000011366 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 name = buf->b_sfname;
11368 else
11369 name = buf->b_ffname;
11370 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
11371 return FAIL;
11372 return OK;
11373}
11374
11375/*
11376 * Write a file name to the session file.
11377 * Takes care of the "slash" option in 'sessionoptions' and escapes special
11378 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011379 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 */
11381 static int
11382ses_put_fname(fd, name, flagp)
11383 FILE *fd;
11384 char_u *name;
11385 unsigned *flagp;
11386{
11387 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011388 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390
11391 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011392 if (sname == NULL)
11393 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011395 if (*flagp & SSOP_SLASH)
11396 {
11397 /* change all backslashes to forward slashes */
11398 for (p = sname; *p != NUL; mb_ptr_adv(p))
11399 if (*p == '\\')
11400 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011402
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020011403 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011404 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020011406 if (p == NULL)
11407 return FAIL;
11408
11409 /* write the result */
11410 if (fputs((char *)p, fd) < 0)
11411 retval = FAIL;
11412
11413 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 return retval;
11415}
11416
11417/*
11418 * ":loadview [nr]"
11419 */
11420 static void
11421ex_loadview(eap)
11422 exarg_T *eap;
11423{
11424 char_u *fname;
11425
11426 fname = get_view_file(*eap->arg);
11427 if (fname != NULL)
11428 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011429 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 vim_free(fname);
11431 }
11432}
11433
11434/*
11435 * Get the name of the view file for the current buffer.
11436 */
11437 static char_u *
11438get_view_file(c)
11439 int c;
11440{
11441 int len = 0;
11442 char_u *p, *s;
11443 char_u *retval;
11444 char_u *sname;
11445
11446 if (curbuf->b_ffname == NULL)
11447 {
11448 EMSG(_(e_noname));
11449 return NULL;
11450 }
11451 sname = home_replace_save(NULL, curbuf->b_ffname);
11452 if (sname == NULL)
11453 return NULL;
11454
11455 /*
11456 * We want a file name without separators, because we're not going to make
11457 * a directory.
11458 * "normal" path separator -> "=+"
11459 * "=" -> "=="
11460 * ":" path separator -> "=-"
11461 */
11462 for (p = sname; *p; ++p)
11463 if (*p == '=' || vim_ispathsep(*p))
11464 ++len;
11465 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
11466 if (retval != NULL)
11467 {
11468 STRCPY(retval, p_vdir);
11469 add_pathsep(retval);
11470 s = retval + STRLEN(retval);
11471 for (p = sname; *p; ++p)
11472 {
11473 if (*p == '=')
11474 {
11475 *s++ = '=';
11476 *s++ = '=';
11477 }
11478 else if (vim_ispathsep(*p))
11479 {
11480 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020011481#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 if (*p == ':')
11483 *s++ = '-';
11484 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000011486 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011487 }
11488 else
11489 *s++ = *p;
11490 }
11491 *s++ = '=';
11492 *s++ = c;
11493 STRCPY(s, ".vim");
11494 }
11495
11496 vim_free(sname);
11497 return retval;
11498}
11499
11500#endif /* FEAT_SESSION */
11501
11502/*
11503 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
11504 * Return FAIL for a write error.
11505 */
11506 int
11507put_eol(fd)
11508 FILE *fd;
11509{
11510 if (
11511#ifdef USE_CRNL
11512 (
11513# ifdef MKSESSION_NL
11514 !mksession_nl &&
11515# endif
11516 (putc('\r', fd) < 0)) ||
11517#endif
11518 (putc('\n', fd) < 0))
11519 return FAIL;
11520 return OK;
11521}
11522
11523/*
11524 * Write a line to "fd".
11525 * Return FAIL for a write error.
11526 */
11527 int
11528put_line(fd, s)
11529 FILE *fd;
11530 char *s;
11531{
11532 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
11533 return FAIL;
11534 return OK;
11535}
11536
11537#ifdef FEAT_VIMINFO
11538/*
11539 * ":rviminfo" and ":wviminfo".
11540 */
11541 static void
11542ex_viminfo(eap)
11543 exarg_T *eap;
11544{
11545 char_u *save_viminfo;
11546
11547 save_viminfo = p_viminfo;
11548 if (*p_viminfo == NUL)
11549 p_viminfo = (char_u *)"'100";
11550 if (eap->cmdidx == CMD_rviminfo)
11551 {
Bram Moolenaard812df62008-11-09 12:46:09 +000011552 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
11553 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554 EMSG(_("E195: Cannot open viminfo file for reading"));
11555 }
11556 else
11557 write_viminfo(eap->arg, eap->forceit);
11558 p_viminfo = save_viminfo;
11559}
11560#endif
11561
11562#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011563/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020011564 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000011565 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000011566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011567 void
11568dialog_msg(buff, format, fname)
11569 char_u *buff;
11570 char *format;
11571 char_u *fname;
11572{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573 if (fname == NULL)
11574 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020011575 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576}
11577#endif
11578
11579/*
11580 * ":behave {mswin,xterm}"
11581 */
11582 static void
11583ex_behave(eap)
11584 exarg_T *eap;
11585{
11586 if (STRCMP(eap->arg, "mswin") == 0)
11587 {
11588 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
11589 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
11590 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
11591 set_option_value((char_u *)"keymodel", 0L,
11592 (char_u *)"startsel,stopsel", 0);
11593 }
11594 else if (STRCMP(eap->arg, "xterm") == 0)
11595 {
11596 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
11597 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
11598 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
11599 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
11600 }
11601 else
11602 EMSG2(_(e_invarg2), eap->arg);
11603}
11604
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010011605#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
11606/*
11607 * Function given to ExpandGeneric() to obtain the possible arguments of the
11608 * ":behave {mswin,xterm}" command.
11609 */
11610 char_u *
11611get_behave_arg(xp, idx)
11612 expand_T *xp UNUSED;
11613 int idx;
11614{
11615 if (idx == 0)
11616 return (char_u *)"mswin";
11617 if (idx == 1)
11618 return (char_u *)"xterm";
11619 return NULL;
11620}
11621#endif
11622
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623#ifdef FEAT_AUTOCMD
11624static int filetype_detect = FALSE;
11625static int filetype_plugin = FALSE;
11626static int filetype_indent = FALSE;
11627
11628/*
11629 * ":filetype [plugin] [indent] {on,off,detect}"
11630 * on: Load the filetype.vim file to install autocommands for file types.
11631 * off: Load the ftoff.vim file to remove all autocommands for file types.
11632 * plugin on: load filetype.vim and ftplugin.vim
11633 * plugin off: load ftplugof.vim
11634 * indent on: load filetype.vim and indent.vim
11635 * indent off: load indoff.vim
11636 */
11637 static void
11638ex_filetype(eap)
11639 exarg_T *eap;
11640{
11641 char_u *arg = eap->arg;
11642 int plugin = FALSE;
11643 int indent = FALSE;
11644
11645 if (*eap->arg == NUL)
11646 {
11647 /* Print current status. */
11648 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
11649 filetype_detect ? "ON" : "OFF",
11650 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
11651 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
11652 return;
11653 }
11654
11655 /* Accept "plugin" and "indent" in any order. */
11656 for (;;)
11657 {
11658 if (STRNCMP(arg, "plugin", 6) == 0)
11659 {
11660 plugin = TRUE;
11661 arg = skipwhite(arg + 6);
11662 continue;
11663 }
11664 if (STRNCMP(arg, "indent", 6) == 0)
11665 {
11666 indent = TRUE;
11667 arg = skipwhite(arg + 6);
11668 continue;
11669 }
11670 break;
11671 }
11672 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11673 {
11674 if (*arg == 'o' || !filetype_detect)
11675 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011676 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 filetype_detect = TRUE;
11678 if (plugin)
11679 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011680 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 filetype_plugin = TRUE;
11682 }
11683 if (indent)
11684 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011685 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 filetype_indent = TRUE;
11687 }
11688 }
11689 if (*arg == 'd')
11690 {
11691 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011692 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 }
11694 }
11695 else if (STRCMP(arg, "off") == 0)
11696 {
11697 if (plugin || indent)
11698 {
11699 if (plugin)
11700 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011701 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 filetype_plugin = FALSE;
11703 }
11704 if (indent)
11705 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011706 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 filetype_indent = FALSE;
11708 }
11709 }
11710 else
11711 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011712 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 filetype_detect = FALSE;
11714 }
11715 }
11716 else
11717 EMSG2(_(e_invarg2), arg);
11718}
11719
11720/*
11721 * ":setfiletype {name}"
11722 */
11723 static void
11724ex_setfiletype(eap)
11725 exarg_T *eap;
11726{
11727 if (!did_filetype)
11728 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11729}
11730#endif
11731
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732 static void
11733ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011734 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735{
11736#ifdef FEAT_DIGRAPHS
11737 if (*eap->arg != NUL)
11738 putdigraph(eap->arg);
11739 else
11740 listdigraphs();
11741#else
11742 EMSG(_("E196: No digraphs in this version"));
11743#endif
11744}
11745
11746 static void
11747ex_set(eap)
11748 exarg_T *eap;
11749{
11750 int flags = 0;
11751
11752 if (eap->cmdidx == CMD_setlocal)
11753 flags = OPT_LOCAL;
11754 else if (eap->cmdidx == CMD_setglobal)
11755 flags = OPT_GLOBAL;
11756#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11757 if (cmdmod.browse && flags == 0)
11758 ex_options(eap);
11759 else
11760#endif
11761 (void)do_set(eap->arg, flags);
11762}
11763
11764#ifdef FEAT_SEARCH_EXTRA
11765/*
11766 * ":nohlsearch"
11767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 static void
11769ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011770 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771{
Bram Moolenaar8050efa2013-11-08 04:30:20 +010011772 SET_NO_HLSEARCH(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011773 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774}
11775
11776/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011777 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 * Sets nextcmd to the start of the next command, if any. Also called when
11779 * skipping commands to find the next command.
11780 */
11781 static void
11782ex_match(eap)
11783 exarg_T *eap;
11784{
11785 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011786 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 char_u *end;
11788 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011789 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011790
11791 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011792 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011793 else
11794 {
11795 EMSG(e_invcmd);
11796 return;
11797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798
11799 /* First clear any old pattern. */
11800 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011801 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011802
11803 if (ends_excmd(*eap->arg))
11804 end = eap->arg;
11805 else if ((STRNICMP(eap->arg, "none", 4) == 0
11806 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11807 end = eap->arg + 4;
11808 else
11809 {
11810 p = skiptowhite(eap->arg);
11811 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011812 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 p = skipwhite(p);
11814 if (*p == NUL)
11815 {
11816 /* There must be two arguments. */
11817 EMSG2(_(e_invarg2), eap->arg);
11818 return;
11819 }
11820 end = skip_regexp(p + 1, *p, TRUE, NULL);
11821 if (!eap->skip)
11822 {
11823 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11824 {
11825 eap->errmsg = e_trailing;
11826 return;
11827 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011828 if (*end != *p)
11829 {
11830 EMSG2(_(e_invarg2), p);
11831 return;
11832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833
11834 c = *end;
11835 *end = NUL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011836 match_add(curwin, g, p + 1, 10, id, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011837 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011838 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011839 }
11840 }
11841 eap->nextcmd = find_nextcmd(end);
11842}
11843#endif
11844
11845#ifdef FEAT_CRYPT
11846/*
11847 * ":X": Get crypt key
11848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011849 static void
11850ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011851 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011853 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011854 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011855}
11856#endif
11857
11858#ifdef FEAT_FOLDING
11859 static void
11860ex_fold(eap)
11861 exarg_T *eap;
11862{
11863 if (foldManualAllowed(TRUE))
11864 foldCreate(eap->line1, eap->line2);
11865}
11866
11867 static void
11868ex_foldopen(eap)
11869 exarg_T *eap;
11870{
11871 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11872 eap->forceit, FALSE);
11873}
11874
11875 static void
11876ex_folddo(eap)
11877 exarg_T *eap;
11878{
11879 linenr_T lnum;
11880
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011881#ifdef FEAT_CLIPBOARD
11882 start_global_changes();
11883#endif
11884
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 /* First set the marks for all lines closed/open. */
11886 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11887 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11888 ml_setmarked(lnum);
11889
11890 /* Execute the command on the marked lines. */
11891 global_exe(eap->arg);
11892 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011893#ifdef FEAT_CLIPBOARD
11894 end_global_changes();
11895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011896}
11897#endif