blob: 1bb92cd82899ebdb658da9fc7a574a53932a523a [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
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static 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 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static char_u *find_command __ARGS((exarg_T *eap, int *full));
65
66static void ex_abbreviate __ARGS((exarg_T *eap));
67static void ex_map __ARGS((exarg_T *eap));
68static void ex_unmap __ARGS((exarg_T *eap));
69static void ex_mapclear __ARGS((exarg_T *eap));
70static void ex_abclear __ARGS((exarg_T *eap));
71#ifndef FEAT_MENU
72# define ex_emenu ex_ni
73# define ex_menu ex_ni
74# define ex_menutranslate ex_ni
75#endif
76#ifdef FEAT_AUTOCMD
77static void ex_autocmd __ARGS((exarg_T *eap));
78static void ex_doautocmd __ARGS((exarg_T *eap));
79#else
80# define ex_autocmd ex_ni
81# define ex_doautocmd ex_ni
82# define ex_doautoall ex_ni
83#endif
84#ifdef FEAT_LISTCMDS
85static void ex_bunload __ARGS((exarg_T *eap));
86static void ex_buffer __ARGS((exarg_T *eap));
87static void ex_bmodified __ARGS((exarg_T *eap));
88static void ex_bnext __ARGS((exarg_T *eap));
89static void ex_bprevious __ARGS((exarg_T *eap));
90static void ex_brewind __ARGS((exarg_T *eap));
91static void ex_blast __ARGS((exarg_T *eap));
92#else
93# define ex_bunload ex_ni
94# define ex_buffer ex_ni
95# define ex_bmodified ex_ni
96# define ex_bnext ex_ni
97# define ex_bprevious ex_ni
98# define ex_brewind ex_ni
99# define ex_blast ex_ni
100# define buflist_list ex_ni
101# define ex_checktime ex_ni
102#endif
103#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
104# define ex_buffer_all ex_ni
105#endif
106static char_u *getargcmd __ARGS((char_u **));
107static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
108static int getargopt __ARGS((exarg_T *eap));
109#ifndef FEAT_QUICKFIX
110# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000111# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_cc ex_ni
113# define ex_cnext ex_ni
114# define ex_cfile ex_ni
115# define qf_list ex_ni
116# define qf_age ex_ni
117# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
121# define ex_cclose ex_ni
122# define ex_copen ex_ni
123# define ex_cwindow ex_ni
124#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000125#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
126# define ex_cexpr ex_ni
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static int check_more __ARGS((int, int));
130static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000131static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200132#if !defined(FEAT_PERL) \
133 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
134 || !defined(FEAT_TCL) \
135 || !defined(FEAT_RUBY) \
136 || !defined(FEAT_LUA) \
137 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000138# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void ex_script_ni __ARGS((exarg_T *eap));
140#endif
141static char_u *invalid_range __ARGS((exarg_T *eap));
142static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000143#ifdef FEAT_QUICKFIX
144static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
147static void ex_highlight __ARGS((exarg_T *eap));
148static void ex_colorscheme __ARGS((exarg_T *eap));
149static void ex_quit __ARGS((exarg_T *eap));
150static void ex_cquit __ARGS((exarg_T *eap));
151static void ex_quit_all __ARGS((exarg_T *eap));
152#ifdef FEAT_WINDOWS
153static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000154static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_resize __ARGS((exarg_T *eap));
157static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000159static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000160static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#else
164# define ex_close ex_ni
165# define ex_only ex_ni
166# define ex_all ex_ni
167# define ex_resize ex_ni
168# define ex_splitview ex_ni
169# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000170# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000172# define ex_tabs ex_ni
173# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000174# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
177static void ex_pclose __ARGS((exarg_T *eap));
178static void ex_ptag __ARGS((exarg_T *eap));
179static void ex_pedit __ARGS((exarg_T *eap));
180#else
181# define ex_pclose ex_ni
182# define ex_ptag ex_ni
183# define ex_pedit ex_ni
184#endif
185static void ex_hide __ARGS((exarg_T *eap));
186static void ex_stop __ARGS((exarg_T *eap));
187static void ex_exit __ARGS((exarg_T *eap));
188static void ex_print __ARGS((exarg_T *eap));
189#ifdef FEAT_BYTEOFF
190static void ex_goto __ARGS((exarg_T *eap));
191#else
192# define ex_goto ex_ni
193#endif
194static void ex_shell __ARGS((exarg_T *eap));
195static void ex_preserve __ARGS((exarg_T *eap));
196static void ex_recover __ARGS((exarg_T *eap));
197#ifndef FEAT_LISTCMDS
198# define ex_argedit ex_ni
199# define ex_argadd ex_ni
200# define ex_argdelete ex_ni
201# define ex_listdo ex_ni
202#endif
203static void ex_mode __ARGS((exarg_T *eap));
204static void ex_wrongmodifier __ARGS((exarg_T *eap));
205static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000206static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static void ex_edit __ARGS((exarg_T *eap));
208#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
209# define ex_drop ex_ni
210#endif
211#ifndef FEAT_GUI
212# define ex_gui ex_nogui
213static void ex_nogui __ARGS((exarg_T *eap));
214#endif
215#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
216static void ex_tearoff __ARGS((exarg_T *eap));
217#else
218# define ex_tearoff ex_ni
219#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000220#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221static void ex_popup __ARGS((exarg_T *eap));
222#else
223# define ex_popup ex_ni
224#endif
225#ifndef FEAT_GUI_MSWIN
226# define ex_simalt ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define gui_mch_find_dialog ex_ni
230# define gui_mch_replace_dialog ex_ni
231#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000232#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# define ex_helpfind ex_ni
234#endif
235#ifndef FEAT_CSCOPE
236# define do_cscope ex_ni
237# define do_scscope ex_ni
238# define do_cstag ex_ni
239#endif
240#ifndef FEAT_SYN_HL
241# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200242# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000243#endif
244#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000245# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000247# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000248# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000249# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200251#ifndef FEAT_PERSISTENT_UNDO
252# define ex_rundo ex_ni
253# define ex_wundo ex_ni
254#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200255#ifndef FEAT_LUA
256# define ex_lua ex_script_ni
257# define ex_luado ex_ni
258# define ex_luafile ex_ni
259#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000260#ifndef FEAT_MZSCHEME
261# define ex_mzscheme ex_script_ni
262# define ex_mzfile ex_ni
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_PERL
265# define ex_perl ex_script_ni
266# define ex_perldo ex_ni
267#endif
268#ifndef FEAT_PYTHON
269# define ex_python ex_script_ni
270# define ex_pyfile ex_ni
271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200273# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200274# define ex_py3file ex_ni
275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#ifndef FEAT_TCL
277# define ex_tcl ex_script_ni
278# define ex_tcldo ex_ni
279# define ex_tclfile ex_ni
280#endif
281#ifndef FEAT_RUBY
282# define ex_ruby ex_script_ni
283# define ex_rubydo ex_ni
284# define ex_rubyfile ex_ni
285#endif
286#ifndef FEAT_SNIFF
287# define ex_sniff ex_ni
288#endif
289#ifndef FEAT_KEYMAP
290# define ex_loadkeymap ex_ni
291#endif
292static void ex_swapname __ARGS((exarg_T *eap));
293static void ex_syncbind __ARGS((exarg_T *eap));
294static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static void ex_pwd __ARGS((exarg_T *eap));
296static void ex_equal __ARGS((exarg_T *eap));
297static void ex_sleep __ARGS((exarg_T *eap));
298static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
299static void ex_winsize __ARGS((exarg_T *eap));
300#ifdef FEAT_WINDOWS
301static void ex_wincmd __ARGS((exarg_T *eap));
302#else
303# define ex_wincmd ex_ni
304#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000305#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static void ex_winpos __ARGS((exarg_T *eap));
307#else
308# define ex_winpos ex_ni
309#endif
310static void ex_operators __ARGS((exarg_T *eap));
311static void ex_put __ARGS((exarg_T *eap));
312static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000313static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static void ex_submagic __ARGS((exarg_T *eap));
315static void ex_join __ARGS((exarg_T *eap));
316static void ex_at __ARGS((exarg_T *eap));
317static void ex_bang __ARGS((exarg_T *eap));
318static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200319#ifdef FEAT_PERSISTENT_UNDO
320static void ex_wundo __ARGS((exarg_T *eap));
321static void ex_rundo __ARGS((exarg_T *eap));
322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000324static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static void ex_redir __ARGS((exarg_T *eap));
326static void ex_redraw __ARGS((exarg_T *eap));
327static void ex_redrawstatus __ARGS((exarg_T *eap));
328static void close_redir __ARGS((void));
329static void ex_mkrc __ARGS((exarg_T *eap));
330static void ex_mark __ARGS((exarg_T *eap));
331#ifdef FEAT_USR_CMDS
332static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000333static 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 +0000334#endif
335#ifdef FEAT_EX_EXTRA
336static void ex_normal __ARGS((exarg_T *eap));
337static void ex_startinsert __ARGS((exarg_T *eap));
338static void ex_stopinsert __ARGS((exarg_T *eap));
339#else
340# define ex_normal ex_ni
341# define ex_align ex_ni
342# define ex_retab ex_ni
343# define ex_startinsert ex_ni
344# define ex_stopinsert ex_ni
345# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000346# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
348#ifdef FEAT_FIND_ID
349static void ex_checkpath __ARGS((exarg_T *eap));
350static void ex_findpat __ARGS((exarg_T *eap));
351#else
352# define ex_findpat ex_ni
353# define ex_checkpath ex_ni
354#endif
355#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
356static void ex_psearch __ARGS((exarg_T *eap));
357#else
358# define ex_psearch ex_ni
359#endif
360static void ex_tag __ARGS((exarg_T *eap));
361static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
362#ifndef FEAT_EVAL
363# define ex_scriptnames ex_ni
364# define ex_finish ex_ni
365# define ex_echo ex_ni
366# define ex_echohl ex_ni
367# define ex_execute ex_ni
368# define ex_call ex_ni
369# define ex_if ex_ni
370# define ex_endif ex_ni
371# define ex_else ex_ni
372# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000373# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000377# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378# define ex_throw ex_ni
379# define ex_try ex_ni
380# define ex_catch ex_ni
381# define ex_finally ex_ni
382# define ex_endtry ex_ni
383# define ex_endfunction ex_ni
384# define ex_let ex_ni
385# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000386# define ex_lockvar ex_ni
387# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# define ex_function ex_ni
389# define ex_delfunction ex_ni
390# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000391# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393static char_u *arg_all __ARGS((void));
394#ifdef FEAT_SESSION
395static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000396static 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 +0000397static void ex_loadview __ARGS((exarg_T *eap));
398static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000399static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#else
401# define ex_loadview ex_ni
402#endif
403#ifndef FEAT_EVAL
404# define ex_compiler ex_ni
405#endif
406#ifdef FEAT_VIMINFO
407static void ex_viminfo __ARGS((exarg_T *eap));
408#else
409# define ex_viminfo ex_ni
410#endif
411static void ex_behave __ARGS((exarg_T *eap));
412#ifdef FEAT_AUTOCMD
413static void ex_filetype __ARGS((exarg_T *eap));
414static void ex_setfiletype __ARGS((exarg_T *eap));
415#else
416# define ex_filetype ex_ni
417# define ex_setfiletype ex_ni
418#endif
419#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000420# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421# define ex_diffpatch ex_ni
422# define ex_diffgetput ex_ni
423# define ex_diffsplit ex_ni
424# define ex_diffthis ex_ni
425# define ex_diffupdate ex_ni
426#endif
427static void ex_digraphs __ARGS((exarg_T *eap));
428static void ex_set __ARGS((exarg_T *eap));
429#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
430# define ex_options ex_ni
431#endif
432#ifdef FEAT_SEARCH_EXTRA
433static void ex_nohlsearch __ARGS((exarg_T *eap));
434static void ex_match __ARGS((exarg_T *eap));
435#else
436# define ex_nohlsearch ex_ni
437# define ex_match ex_ni
438#endif
439#ifdef FEAT_CRYPT
440static void ex_X __ARGS((exarg_T *eap));
441#else
442# define ex_X ex_ni
443#endif
444#ifdef FEAT_FOLDING
445static void ex_fold __ARGS((exarg_T *eap));
446static void ex_foldopen __ARGS((exarg_T *eap));
447static void ex_folddo __ARGS((exarg_T *eap));
448#else
449# define ex_fold ex_ni
450# define ex_foldopen ex_ni
451# define ex_folddo ex_ni
452#endif
453#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
454 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
455# define ex_language ex_ni
456#endif
457#ifndef FEAT_SIGNS
458# define ex_sign ex_ni
459#endif
460#ifndef FEAT_SUN_WORKSHOP
461# define ex_wsverb ex_ni
462#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200464# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
469#ifndef FEAT_EVAL
470# define ex_debug ex_ni
471# define ex_breakadd ex_ni
472# define ex_debuggreedy ex_ni
473# define ex_breakdel ex_ni
474# define ex_breaklist ex_ni
475#endif
476
477#ifndef FEAT_CMDHIST
478# define ex_history ex_ni
479#endif
480#ifndef FEAT_JUMPLIST
481# define ex_jumps ex_ni
482# define ex_changes ex_ni
483#endif
484
Bram Moolenaar05159a02005-02-26 23:04:13 +0000485#ifndef FEAT_PROFILE
486# define ex_profile ex_ni
487#endif
488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * Declare cmdnames[].
491 */
492#define DO_DECLARE_EXCMD
493#include "ex_cmds.h"
494
495/*
496 * Table used to quickly search for a command, based on its first character.
497 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000498static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 CMD_append,
501 CMD_buffer,
502 CMD_change,
503 CMD_delete,
504 CMD_edit,
505 CMD_file,
506 CMD_global,
507 CMD_help,
508 CMD_insert,
509 CMD_join,
510 CMD_k,
511 CMD_list,
512 CMD_move,
513 CMD_next,
514 CMD_open,
515 CMD_print,
516 CMD_quit,
517 CMD_read,
518 CMD_substitute,
519 CMD_t,
520 CMD_undo,
521 CMD_vglobal,
522 CMD_write,
523 CMD_xit,
524 CMD_yank,
525 CMD_z,
526 CMD_bang
527};
528
529static char_u dollar_command[2] = {'$', 0};
530
531
532#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000533/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534typedef struct
535{
536 char_u *line; /* command line */
537 linenr_T lnum; /* sourcing_lnum of the line */
538} wcmd_T;
539
540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 garray_T *lines_gap; /* growarray with line info */
548 int current_line; /* last read line from growarray */
549 int repeating; /* TRUE when looping a second time */
550 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
551 char_u *(*getline) __ARGS((int, void *, int));
552 void *cookie;
553};
554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000555static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
556static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000558
559/* Struct to save a few things while debugging. Used in do_cmdline() only. */
560struct dbg_stuff
561{
562 int trylevel;
563 int force_abort;
564 except_T *caught_stack;
565 char_u *vv_exception;
566 char_u *vv_throwpoint;
567 int did_emsg;
568 int got_int;
569 int did_throw;
570 int need_rethrow;
571 int check_cstack;
572 except_T *current_exception;
573};
574
575static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577
578 static void
579save_dbg_stuff(dsp)
580 struct dbg_stuff *dsp;
581{
582 dsp->trylevel = trylevel; trylevel = 0;
583 dsp->force_abort = force_abort; force_abort = FALSE;
584 dsp->caught_stack = caught_stack; caught_stack = NULL;
585 dsp->vv_exception = v_exception(NULL);
586 dsp->vv_throwpoint = v_throwpoint(NULL);
587
588 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
589 dsp->did_emsg = did_emsg; did_emsg = FALSE;
590 dsp->got_int = got_int; got_int = FALSE;
591 dsp->did_throw = did_throw; did_throw = FALSE;
592 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
593 dsp->check_cstack = check_cstack; check_cstack = FALSE;
594 dsp->current_exception = current_exception; current_exception = NULL;
595}
596
597 static void
598restore_dbg_stuff(dsp)
599 struct dbg_stuff *dsp;
600{
601 suppress_errthrow = FALSE;
602 trylevel = dsp->trylevel;
603 force_abort = dsp->force_abort;
604 caught_stack = dsp->caught_stack;
605 (void)v_exception(dsp->vv_exception);
606 (void)v_throwpoint(dsp->vv_throwpoint);
607 did_emsg = dsp->did_emsg;
608 got_int = dsp->got_int;
609 did_throw = dsp->did_throw;
610 need_rethrow = dsp->need_rethrow;
611 check_cstack = dsp->check_cstack;
612 current_exception = dsp->current_exception;
613}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615
616
617/*
618 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
619 * command is given.
620 */
621 void
622do_exmode(improved)
623 int improved; /* TRUE for "improved Ex" mode */
624{
625 int save_msg_scroll;
626 int prev_msg_row;
627 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000628 int changedtick;
629
630 if (improved)
631 exmode_active = EXMODE_VIM;
632 else
633 exmode_active = EXMODE_NORMAL;
634 State = NORMAL;
635
636 /* When using ":global /pat/ visual" and then "Q" we return to continue
637 * the :global command. */
638 if (global_busy)
639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 save_msg_scroll = msg_scroll;
642 ++RedrawingDisabled; /* don't redisplay the window */
643 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_GUI
645 /* Ignore scrollbar and mouse events in Ex mode */
646 ++hold_gui_events;
647#endif
648#ifdef FEAT_SNIFF
649 want_sniff_request = 0; /* No K_SNIFF wanted */
650#endif
651
652 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
653 while (exmode_active)
654 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000655#ifdef FEAT_EX_EXTRA
656 /* Check for a ":normal" command and no more characters left. */
657 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
658 {
659 exmode_active = FALSE;
660 break;
661 }
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 msg_scroll = TRUE;
664 need_wait_return = FALSE;
665 ex_pressedreturn = FALSE;
666 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 prev_msg_row = msg_row;
669 prev_line = curwin->w_cursor.lnum;
670#ifdef FEAT_SNIFF
671 ProcessSniffRequests();
672#endif
673 if (improved)
674 {
675 cmdline_row = msg_row;
676 do_cmdline(NULL, getexline, NULL, 0);
677 }
678 else
679 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
680 lines_left = Rows - 1;
681
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if ((prev_line != curwin->w_cursor.lnum
683 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if (curbuf->b_ml.ml_flags & ML_EMPTY)
686 EMSG(_(e_emptybuf));
687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (ex_pressedreturn)
690 {
691 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000692 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 msg_row = prev_msg_row;
694 if (prev_msg_row == Rows - 1)
695 msg_row--;
696 }
697 msg_col = 0;
698 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
699 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000702 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
703 {
704 if (curbuf->b_ml.ml_flags & ML_EMPTY)
705 EMSG(_(e_emptybuf));
706 else
707 EMSG(_("E501: At end-of-file"));
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710
711#ifdef FEAT_GUI
712 --hold_gui_events;
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 --RedrawingDisabled;
715 --no_wait_return;
716 update_screen(CLEAR);
717 need_wait_return = FALSE;
718 msg_scroll = save_msg_scroll;
719}
720
721/*
722 * Execute a simple command line. Used for translated commands like "*".
723 */
724 int
725do_cmdline_cmd(cmd)
726 char_u *cmd;
727{
728 return do_cmdline(cmd, NULL, NULL,
729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
730}
731
732/*
733 * do_cmdline(): execute one Ex command line
734 *
735 * 1. Execute "cmdline" when it is not NULL.
736 * If "cmdline" is NULL, or more lines are needed, getline() is used.
737 * 2. Split up in parts separated with '|'.
738 *
739 * This function can be called recursively!
740 *
741 * flags:
742 * DOCMD_VERBOSE - The command will be included in the error message.
743 * DOCMD_NOWAIT - Don't call wait_return() and friends.
744 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
745 * DOCMD_KEYTYPED - Don't reset KeyTyped.
746 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
747 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
748 *
749 * return FAIL if cmdline could not be executed, OK otherwise
750 */
751 int
752do_cmdline(cmdline, getline, cookie, flags)
753 char_u *cmdline;
754 char_u *(*getline) __ARGS((int, void *, int));
755 void *cookie; /* argument for getline() */
756 int flags;
757{
758 char_u *next_cmdline; /* next cmd to execute */
759 char_u *cmdline_copy = NULL; /* copy of cmd line */
760 int used_getline = FALSE; /* used "getline" to obtain command */
761 static int recursive = 0; /* recursive depth */
762 int msg_didout_before_start = 0;
763 int count = 0; /* line number count */
764 int did_inc = FALSE; /* incremented RedrawingDisabled */
765 int retval = OK;
766#ifdef FEAT_EVAL
767 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 int current_line = 0; /* active line in lines_ga */
770 char_u *fname = NULL; /* function or script name */
771 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
772 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000773 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 int initial_trylevel;
775 struct msglist **saved_msg_list = NULL;
776 struct msglist *private_msg_list;
777
778 /* "getline" and "cookie" passed to do_one_cmd() */
779 char_u *(*cmd_getline) __ARGS((int, void *, int));
780 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#else
785# define cmd_getline getline
786# define cmd_cookie cookie
787#endif
788 static int call_depth = 0; /* recursiveness */
789
790#ifdef FEAT_EVAL
791 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
792 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000793 * This ensures that the do_errthrow() call in do_one_cmd() does not
794 * combine the messages stored by an earlier invocation of do_one_cmd()
795 * with the command name of the later one. This would happen when
796 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 saved_msg_list = msg_list;
798 msg_list = &private_msg_list;
799 private_msg_list = NULL;
800#endif
801
802 /* It's possible to create an endless loop with ":execute", catch that
803 * here. The value of 200 allows nested function calls, ":source", etc. */
804 if (call_depth == 200)
805 {
806 EMSG(_("E169: Command too recursive"));
807#ifdef FEAT_EVAL
808 /* When converting to an exception, we do not include the command name
809 * since this is not an error of the specific command. */
810 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
811 msg_list = saved_msg_list;
812#endif
813 return FAIL;
814 }
815 ++call_depth;
816
817#ifdef FEAT_EVAL
818 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 cstack.cs_trylevel = 0;
821 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000822 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
824
825 real_cookie = getline_cookie(getline, cookie);
826
827 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 getline_is_func = getline_equal(getline, cookie, get_func_line);
829 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ++ex_nesting_level;
831
832 /* Get the function or script name and the address where the next breakpoint
833 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 fname = func_name(real_cookie);
837 breakpoint = func_breakpoint(real_cookie);
838 dbg_tick = func_dbg_tick(real_cookie);
839 }
840 else if (getline_equal(getline, cookie, getsourceline))
841 {
842 fname = sourcing_name;
843 breakpoint = source_breakpoint(real_cookie);
844 dbg_tick = source_dbg_tick(real_cookie);
845 }
846
847 /*
848 * Initialize "force_abort" and "suppress_errthrow" at the top level.
849 */
850 if (!recursive)
851 {
852 force_abort = FALSE;
853 suppress_errthrow = FALSE;
854 }
855
856 /*
857 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000858 * exception handling (used when debugging). Otherwise clear it to avoid
859 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000861 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000862 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000863 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200864 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 initial_trylevel = trylevel;
867
868 /*
869 * "did_throw" will be set to TRUE when an exception is being thrown.
870 */
871 did_throw = FALSE;
872#endif
873 /*
874 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * If force_abort is set, we cancel everything.
877 */
878 did_emsg = FALSE;
879
880 /*
881 * KeyTyped is only set when calling vgetc(). Reset it here when not
882 * calling vgetc() (sourced command lines).
883 */
884 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
885 KeyTyped = FALSE;
886
887 /*
888 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * - for multiple commands on one line, separated with '|'
891 * - when repeating until there are no more lines (for ":source")
892 */
893 next_cmdline = cmdline;
894 do
895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_EVAL
897 getline_is_func = getline_equal(getline, cookie, get_func_line);
898#endif
899
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (next_cmdline == NULL
902#ifdef FEAT_EVAL
903 && !force_abort
904 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 )
908 did_emsg = FALSE;
909
910 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 * 2. If no line given: Get an allocated line with getline().
913 * 3. If a line is given: Make a copy, so we can mess with it.
914 */
915
916#ifdef FEAT_EVAL
917 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 /* Each '|' separated command is stored separately in lines_ga, to
921 * be able to jump to it. Don't use next_cmdline now. */
922 vim_free(cmdline_copy);
923 cmdline_copy = NULL;
924
925 /* Check if a function has returned or, unless it has an unclosed
926 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000930 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 func_line_end(real_cookie);
932# endif
933 if (func_has_ended(real_cookie))
934 {
935 retval = FAIL;
936 break;
937 }
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 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000941 && getline_equal(getline, cookie, getsourceline))
942 script_line_end();
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /* Check if a sourced file hit a ":finish" command. */
946 if (source_finished(getline, cookie))
947 {
948 retval = FAIL;
949 break;
950 }
951
952 /* If breakpoints have been added/deleted need to check for it. */
953 if (breakpoint != NULL && dbg_tick != NULL
954 && *dbg_tick != debug_tick)
955 {
956 *breakpoint = dbg_find_breakpoint(
957 getline_equal(getline, cookie, getsourceline),
958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
961
962 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
963 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
964
965 /* Did we encounter a breakpoint? */
966 if (breakpoint != NULL && *breakpoint != 0
967 && *breakpoint <= sourcing_lnum)
968 {
969 dbg_breakpoint(fname, sourcing_lnum);
970 /* Find next breakpoint. */
971 *breakpoint = dbg_find_breakpoint(
972 getline_equal(getline, cookie, getsourceline),
973 fname, sourcing_lnum);
974 *dbg_tick = debug_tick;
975 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978 {
979 if (getline_is_func)
980 func_line_start(real_cookie);
981 else if (getline_equal(getline, cookie, getsourceline))
982 script_line_start();
983 }
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 * again. Pass a different "getline" function to do_one_cmd()
991 * below, so that it stores lines in or reads them from
992 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 * while/for loop. */
994 cmd_getline = get_loop_line;
995 cmd_cookie = (void *)&cmd_loop_cookie;
996 cmd_loop_cookie.lines_gap = &lines_ga;
997 cmd_loop_cookie.current_line = current_line;
998 cmd_loop_cookie.getline = getline;
999 cmd_loop_cookie.cookie = cookie;
1000 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
1004 cmd_getline = getline;
1005 cmd_cookie = cookie;
1006 }
1007#endif
1008
1009 /* 2. If no line given, get an allocated line with getline(). */
1010 if (next_cmdline == NULL)
1011 {
1012 /*
1013 * Need to set msg_didout for the first line after an ":if",
1014 * otherwise the ":if" will be overwritten.
1015 */
1016 if (count == 1 && getline_equal(getline, cookie, getexline))
1017 msg_didout = TRUE;
1018 if (getline == NULL || (next_cmdline = getline(':', cookie,
1019#ifdef FEAT_EVAL
1020 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1021#else
1022 0
1023#endif
1024 )) == NULL)
1025 {
1026 /* Don't call wait_return for aborted command line. The NULL
1027 * returned for the end of a sourced file or executed function
1028 * doesn't do this. */
1029 if (KeyTyped && !(flags & DOCMD_REPEAT))
1030 need_wait_return = FALSE;
1031 retval = FAIL;
1032 break;
1033 }
1034 used_getline = TRUE;
1035
1036 /*
1037 * Keep the first typed line. Clear it when more lines are typed.
1038 */
1039 if (flags & DOCMD_KEEPLINE)
1040 {
1041 vim_free(repeat_cmdline);
1042 if (count == 0)
1043 repeat_cmdline = vim_strsave(next_cmdline);
1044 else
1045 repeat_cmdline = NULL;
1046 }
1047 }
1048
1049 /* 3. Make a copy of the command so we can mess with it. */
1050 else if (cmdline_copy == NULL)
1051 {
1052 next_cmdline = vim_strsave(next_cmdline);
1053 if (next_cmdline == NULL)
1054 {
1055 EMSG(_(e_outofmem));
1056 retval = FAIL;
1057 break;
1058 }
1059 }
1060 cmdline_copy = next_cmdline;
1061
1062#ifdef FEAT_EVAL
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * Save the current line when inside a ":while" or ":for", and when
1065 * the command looks like a ":while" or ":for", because we may need it
1066 * later. When there is a '|' and another command, it is stored
1067 * separately, because we need to be able to jump back to it from an
1068 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 if (current_line == lines_ga.ga_len
1071 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 retval = FAIL;
1076 break;
1077 }
1078 }
1079 did_endif = FALSE;
1080#endif
1081
1082 if (count++ == 0)
1083 {
1084 /*
1085 * All output from the commands is put below each other, without
1086 * waiting for a return. Don't do this when executing commands
1087 * from a script or when being called recursive (e.g. for ":e
1088 * +command file").
1089 */
1090 if (!(flags & DOCMD_NOWAIT) && !recursive)
1091 {
1092 msg_didout_before_start = msg_didout;
1093 msg_didany = FALSE; /* no output yet */
1094 msg_start();
1095 msg_scroll = TRUE; /* put messages below each other */
1096 ++no_wait_return; /* dont wait for return until finished */
1097 ++RedrawingDisabled;
1098 did_inc = TRUE;
1099 }
1100 }
1101
1102 if (p_verbose >= 15 && sourcing_name != NULL)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 verbose_enter_scroll();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 smsg((char_u *)_("line %ld: %s"),
1108 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 if (msg_silent == 0)
1110 msg_puts((char_u *)"\n"); /* don't overwrite this */
1111
1112 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 --no_wait_return;
1114 }
1115
1116 /*
1117 * 2. Execute one '|' separated command.
1118 * do_one_cmd() will return NULL if there is no trailing '|'.
1119 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1120 */
1121 ++recursive;
1122 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1123#ifdef FEAT_EVAL
1124 &cstack,
1125#endif
1126 cmd_getline, cmd_cookie);
1127 --recursive;
1128
1129#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cmd_cookie == (void *)&cmd_loop_cookie)
1131 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
1136 if (next_cmdline == NULL)
1137 {
1138 vim_free(cmdline_copy);
1139 cmdline_copy = NULL;
1140#ifdef FEAT_CMDHIST
1141 /*
1142 * If the command was typed, remember it for the ':' register.
1143 * Do this AFTER executing the command to make :@: work.
1144 */
1145 if (getline_equal(getline, cookie, getexline)
1146 && new_last_cmdline != NULL)
1147 {
1148 vim_free(last_cmdline);
1149 last_cmdline = new_last_cmdline;
1150 new_last_cmdline = NULL;
1151 }
1152#endif
1153 }
1154 else
1155 {
1156 /* need to copy the command after the '|' to cmdline_copy, for the
1157 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001158 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 next_cmdline = cmdline_copy;
1160 }
1161
1162
1163#ifdef FEAT_EVAL
1164 /* reset did_emsg for a function that is not aborted by an error */
1165 if (did_emsg && !force_abort
1166 && getline_equal(getline, cookie, get_func_line)
1167 && !func_has_abort(real_cookie))
1168 did_emsg = FALSE;
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++current_line;
1173
1174 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * An ":endwhile", ":endfor" and ":continue" is handled here.
1176 * If we were executing commands, jump back to the ":while" or
1177 * ":for".
1178 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* Jump back to the matching ":while" or ":for". Be careful
1185 * not to use a cs_line[] from an entry that isn't a ":while"
1186 * or ":for": It would make "current_line" invalid and can
1187 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!did_emsg && !got_int && !did_throw
1189 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 && (cstack.cs_flags[cstack.cs_idx]
1191 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && cstack.cs_line[cstack.cs_idx] >= 0
1193 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1194 {
1195 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* remember we jumped there */
1197 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 line_breakcheck(); /* check if CTRL-C typed */
1199
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* Check for the next breakpoint at or after the ":while"
1201 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (breakpoint != NULL)
1203 {
1204 *breakpoint = dbg_find_breakpoint(
1205 getline_equal(getline, cookie, getsourceline),
1206 fname,
1207 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1208 *dbg_tick = debug_tick;
1209 }
1210 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1216 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
1219
1220 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1227 }
1228 }
1229
1230 /*
1231 * When not inside any ":while" loop, clear remembered lines.
1232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 if (lines_ga.ga_len > 0)
1236 {
1237 sourcing_lnum =
1238 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1239 free_cmdlines(&lines_ga);
1240 }
1241 current_line = 0;
1242 }
1243
1244 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1246 * being restored at the ":endtry". Reset them here and set the
1247 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1248 * This includes the case where a missing ":endif", ":endwhile" or
1249 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 cstack.cs_lflags &= ~CSL_HAD_FINA;
1254 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1255 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 did_throw ? (void *)current_exception : NULL);
1257 did_emsg = got_int = did_throw = FALSE;
1258 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1259 }
1260
1261 /* Update global "trylevel" for recursive calls to do_cmdline() from
1262 * within this loop. */
1263 trylevel = initial_trylevel + cstack.cs_trylevel;
1264
1265 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001266 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * files) is aborted because of an error, an interrupt, or an uncaught
1268 * exception, cancel everything. If it is left normally, reset
1269 * force_abort to get the non-EH compatible abortion behavior for
1270 * the rest of the script.
1271 */
1272 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1273 force_abort = FALSE;
1274
1275 /* Convert an interrupt to an exception if appropriate. */
1276 (void)do_intthrow(&cstack);
1277#endif /* FEAT_EVAL */
1278
1279 }
1280 /*
1281 * Continue executing command lines when:
1282 * - no CTRL-C typed, no aborting error, no exception thrown or try
1283 * conditionals need to be checked for executing finally clauses or
1284 * catching an interrupt exception
1285 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * looping for ":source" command or function call.
1288 */
1289 while (!((got_int
1290#ifdef FEAT_EVAL
1291 || (did_emsg && force_abort) || did_throw
1292#endif
1293 )
1294#ifdef FEAT_EVAL
1295 && cstack.cs_trylevel == 0
1296#endif
1297 )
1298 && !(did_emsg && used_getline
1299 && (getline_equal(getline, cookie, getexmodeline)
1300 || getline_equal(getline, cookie, getexline)))
1301 && (next_cmdline != NULL
1302#ifdef FEAT_EVAL
1303 || cstack.cs_idx >= 0
1304#endif
1305 || (flags & DOCMD_REPEAT)));
1306
1307 vim_free(cmdline_copy);
1308#ifdef FEAT_EVAL
1309 free_cmdlines(&lines_ga);
1310 ga_clear(&lines_ga);
1311
1312 if (cstack.cs_idx >= 0)
1313 {
1314 /*
1315 * If a sourced file or executed function ran to its end, report the
1316 * unclosed conditional.
1317 */
1318 if (!got_int && !did_throw
1319 && ((getline_equal(getline, cookie, getsourceline)
1320 && !source_finished(getline, cookie))
1321 || (getline_equal(getline, cookie, get_func_line)
1322 && !func_has_ended(real_cookie))))
1323 {
1324 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1325 EMSG(_(e_endtry));
1326 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1327 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001328 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1329 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 else
1331 EMSG(_(e_endif));
1332 }
1333
1334 /*
1335 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1336 * ":endtry" in a sourced file or executed function. If the try
1337 * conditional is in its finally clause, ignore anything pending.
1338 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001339 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 */
1341 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001342 {
1343 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1344
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001345 if (idx >= 0)
1346 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001347 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1348 &cstack.cs_looplevel);
1349 }
1350 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 trylevel = initial_trylevel;
1352 }
1353
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001354 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1355 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * exception, do this now after rewinding the cstack. */
1357 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1358 ? (char_u *)"endfunction" : (char_u *)NULL);
1359
1360 if (trylevel == 0)
1361 {
1362 /*
1363 * When an exception is being thrown out of the outermost try
1364 * conditional, discard the uncaught exception, disable the conversion
1365 * of interrupts or errors to exceptions, and ensure that no more
1366 * commands are executed.
1367 */
1368 if (did_throw)
1369 {
1370 void *p = NULL;
1371 char_u *saved_sourcing_name;
1372 int saved_sourcing_lnum;
1373 struct msglist *messages = NULL, *next;
1374
1375 /*
1376 * If the uncaught exception is a user exception, report it as an
1377 * error. If it is an error exception, display the saved error
1378 * message now. For an interrupt exception, do nothing; the
1379 * interrupt message is given elsewhere.
1380 */
1381 switch (current_exception->type)
1382 {
1383 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001384 vim_snprintf((char *)IObuff, IOSIZE,
1385 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 current_exception->value);
1387 p = vim_strsave(IObuff);
1388 break;
1389 case ET_ERROR:
1390 messages = current_exception->messages;
1391 current_exception->messages = NULL;
1392 break;
1393 case ET_INTERRUPT:
1394 break;
1395 default:
1396 p = vim_strsave((char_u *)_(e_internal));
1397 }
1398
1399 saved_sourcing_name = sourcing_name;
1400 saved_sourcing_lnum = sourcing_lnum;
1401 sourcing_name = current_exception->throw_name;
1402 sourcing_lnum = current_exception->throw_lnum;
1403 current_exception->throw_name = NULL;
1404
1405 discard_current_exception(); /* uses IObuff if 'verbose' */
1406 suppress_errthrow = TRUE;
1407 force_abort = TRUE;
1408
1409 if (messages != NULL)
1410 {
1411 do
1412 {
1413 next = messages->next;
1414 emsg(messages->msg);
1415 vim_free(messages->msg);
1416 vim_free(messages);
1417 messages = next;
1418 }
1419 while (messages != NULL);
1420 }
1421 else if (p != NULL)
1422 {
1423 emsg(p);
1424 vim_free(p);
1425 }
1426 vim_free(sourcing_name);
1427 sourcing_name = saved_sourcing_name;
1428 sourcing_lnum = saved_sourcing_lnum;
1429 }
1430
1431 /*
1432 * On an interrupt or an aborting error not converted to an exception,
1433 * disable the conversion of errors to exceptions. (Interrupts are not
1434 * converted any more, here.) This enables also the interrupt message
1435 * when force_abort is set and did_emsg unset in case of an interrupt
1436 * from a finally clause after an error.
1437 */
1438 else if (got_int || (did_emsg && force_abort))
1439 suppress_errthrow = TRUE;
1440 }
1441
1442 /*
1443 * The current cstack will be freed when do_cmdline() returns. An uncaught
1444 * exception will have to be rethrown in the previous cstack. If a function
1445 * has just returned or a script file was just finished and the previous
1446 * cstack belongs to the same function or, respectively, script file, it
1447 * will have to be checked for finally clauses to be executed due to the
1448 * ":return" or ":finish". This is done in do_one_cmd().
1449 */
1450 if (did_throw)
1451 need_rethrow = TRUE;
1452 if ((getline_equal(getline, cookie, getsourceline)
1453 && ex_nesting_level > source_level(real_cookie))
1454 || (getline_equal(getline, cookie, get_func_line)
1455 && ex_nesting_level > func_level(real_cookie) + 1))
1456 {
1457 if (!did_throw)
1458 check_cstack = TRUE;
1459 }
1460 else
1461 {
1462 /* When leaving a function, reduce nesting level. */
1463 if (getline_equal(getline, cookie, get_func_line))
1464 --ex_nesting_level;
1465 /*
1466 * Go to debug mode when returning from a function in which we are
1467 * single-stepping.
1468 */
1469 if ((getline_equal(getline, cookie, getsourceline)
1470 || getline_equal(getline, cookie, get_func_line))
1471 && ex_nesting_level + 1 <= debug_break_level)
1472 do_debug(getline_equal(getline, cookie, getsourceline)
1473 ? (char_u *)_("End of sourced file")
1474 : (char_u *)_("End of function"));
1475 }
1476
1477 /*
1478 * Restore the exception environment (done after returning from the
1479 * debugger).
1480 */
1481 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001482 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 msg_list = saved_msg_list;
1485#endif /* FEAT_EVAL */
1486
1487 /*
1488 * If there was too much output to fit on the command line, ask the user to
1489 * hit return before redrawing the screen. With the ":global" command we do
1490 * this only once after the command is finished.
1491 */
1492 if (did_inc)
1493 {
1494 --RedrawingDisabled;
1495 --no_wait_return;
1496 msg_scroll = FALSE;
1497
1498 /*
1499 * When just finished an ":if"-":else" which was typed, no need to
1500 * wait for hit-return. Also for an error situation.
1501 */
1502 if (retval == FAIL
1503#ifdef FEAT_EVAL
1504 || (did_endif && KeyTyped && !did_emsg)
1505#endif
1506 )
1507 {
1508 need_wait_return = FALSE;
1509 msg_didany = FALSE; /* don't wait when restarting edit */
1510 }
1511 else if (need_wait_return)
1512 {
1513 /*
1514 * The msg_start() above clears msg_didout. The wait_return we do
1515 * here should not overwrite the command that may be shown before
1516 * doing that.
1517 */
1518 msg_didout |= msg_didout_before_start;
1519 wait_return(FALSE);
1520 }
1521 }
1522
1523#ifndef FEAT_EVAL
1524 /*
1525 * Reset if_level, in case a sourced script file contains more ":if" than
1526 * ":endif" (could be ":if x | foo | endif").
1527 */
1528 if_level = 0;
1529#endif
1530
1531 --call_depth;
1532 return retval;
1533}
1534
1535#ifdef FEAT_EVAL
1536/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 */
1539 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int c;
1542 void *cookie;
1543 int indent;
1544{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 wcmd_T *wp;
1547 char_u *line;
1548
1549 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1550 {
1551 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if (cp->getline == NULL)
1556 line = getcmdline(c, 0L, indent);
1557 else
1558 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001559 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 ++cp->current_line;
1561
1562 return line;
1563 }
1564
1565 KeyTyped = FALSE;
1566 ++cp->current_line;
1567 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1568 sourcing_lnum = wp->lnum;
1569 return vim_strsave(wp->line);
1570}
1571
1572/*
1573 * Store a line in "gap" so that a ":while" loop can execute it again.
1574 */
1575 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001576store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 garray_T *gap;
1578 char_u *line;
1579{
1580 if (ga_grow(gap, 1) == FAIL)
1581 return FAIL;
1582 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1584 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 return OK;
1586}
1587
1588/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 static void
1592free_cmdlines(gap)
1593 garray_T *gap;
1594{
1595 while (gap->ga_len > 0)
1596 {
1597 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1598 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600}
1601#endif
1602
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1605 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001608getline_equal(fgetline, cookie, func)
1609 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001610 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 char_u *(*func) __ARGS((int, void *, int));
1612{
1613#ifdef FEAT_EVAL
1614 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001618 * function that's originally used to obtain the lines. This may be
1619 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 cp = (struct loop_cookie *)cookie;
1622 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 gp = cp->getline;
1625 cp = cp->cookie;
1626 }
1627 return gp == func;
1628#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631}
1632
1633#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1634/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * getline function. Otherwise return "cookie".
1637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001640 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643# ifdef FEAT_EVAL
1644 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001645 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001648 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001651 cp = (struct loop_cookie *)cookie;
1652 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 gp = cp->getline;
1655 cp = cp->cookie;
1656 }
1657 return cp;
1658# else
1659 return cookie;
1660# endif
1661}
1662#endif
1663
1664/*
1665 * Execute one Ex command.
1666 *
1667 * If 'sourcing' is TRUE, the command will be included in the error message.
1668 *
1669 * 1. skip comment lines and leading space
1670 * 2. handle command modifiers
1671 * 3. parse range
1672 * 4. parse command
1673 * 5. parse arguments
1674 * 6. switch on command name
1675 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001676 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 *
1678 * This function may be called recursively!
1679 */
1680#if (_MSC_VER == 1200)
1681/*
Bram Moolenaared203462004-06-16 11:19:22 +00001682 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001684 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#endif
1686 static char_u *
1687do_one_cmd(cmdlinep, sourcing,
1688#ifdef FEAT_EVAL
1689 cstack,
1690#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001691 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 char_u **cmdlinep;
1693 int sourcing;
1694#ifdef FEAT_EVAL
1695 struct condstack *cstack;
1696#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001697 char_u *(*fgetline) __ARGS((int, void *, int));
1698 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 char_u *p;
1701 linenr_T lnum;
1702 long n;
1703 char_u *errormsg = NULL; /* error message */
1704 exarg_T ea; /* Ex command arguments */
1705 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001706 int save_msg_scroll = msg_scroll;
1707 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001709#ifdef HAVE_SANDBOX
1710 int did_sandbox = FALSE;
1711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 cmdmod_T save_cmdmod;
1713 int ni; /* set when Not Implemented */
1714
1715 vim_memset(&ea, 0, sizeof(ea));
1716 ea.line1 = 1;
1717 ea.line2 = 1;
1718#ifdef FEAT_EVAL
1719 ++ex_nesting_level;
1720#endif
1721
1722 /* when not editing the last file :q has to be typed twice */
1723 if (quitmore
1724#ifdef FEAT_EVAL
1725 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001726 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
1728 )
1729 --quitmore;
1730
1731 /*
1732 * Reset browse, confirm, etc.. They are restored when returning, for
1733 * recursive calls.
1734 */
1735 save_cmdmod = cmdmod;
1736 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1737
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001738 /* "#!anything" is handled like a comment. */
1739 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1740 goto doend;
1741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /*
1743 * Repeat until no more command modifiers are found.
1744 */
1745 ea.cmd = *cmdlinep;
1746 for (;;)
1747 {
1748/*
1749 * 1. skip comment lines and leading white space and colons
1750 */
1751 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1752 ++ea.cmd;
1753
1754 /* in ex mode, an empty line works like :+ */
1755 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 && (getline_equal(fgetline, cookie, getexmodeline)
1757 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001758 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
1760 ea.cmd = (char_u *)"+";
1761 ex_pressedreturn = TRUE;
1762 }
1763
1764 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001765 if (*ea.cmd == '"')
1766 goto doend;
1767 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001768 {
1769 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773/*
1774 * 2. handle command modifiers.
1775 */
1776 p = ea.cmd;
1777 if (VIM_ISDIGIT(*ea.cmd))
1778 p = skipwhite(skipdigits(ea.cmd));
1779 switch (*p)
1780 {
1781 /* When adding an entry, also modify cmd_exists(). */
1782 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1783 break;
1784#ifdef FEAT_WINDOWS
1785 cmdmod.split |= WSP_ABOVE;
1786#endif
1787 continue;
1788
1789 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1790 {
1791#ifdef FEAT_WINDOWS
1792 cmdmod.split |= WSP_BELOW;
1793#endif
1794 continue;
1795 }
1796 if (checkforcmd(&ea.cmd, "browse", 3))
1797 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001798#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 cmdmod.browse = TRUE;
1800#endif
1801 continue;
1802 }
1803 if (!checkforcmd(&ea.cmd, "botright", 2))
1804 break;
1805#ifdef FEAT_WINDOWS
1806 cmdmod.split |= WSP_BOT;
1807#endif
1808 continue;
1809
1810 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1811 break;
1812#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1813 cmdmod.confirm = TRUE;
1814#endif
1815 continue;
1816
1817 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1818 {
1819 cmdmod.keepmarks = TRUE;
1820 continue;
1821 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001822 if (checkforcmd(&ea.cmd, "keepalt", 5))
1823 {
1824 cmdmod.keepalt = TRUE;
1825 continue;
1826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1828 break;
1829 cmdmod.keepjumps = TRUE;
1830 continue;
1831
1832 /* ":hide" and ":hide | cmd" are not modifiers */
1833 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1834 || *p == NUL || ends_excmd(*p))
1835 break;
1836 ea.cmd = p;
1837 cmdmod.hide = TRUE;
1838 continue;
1839
1840 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1841 {
1842 cmdmod.lockmarks = TRUE;
1843 continue;
1844 }
1845
1846 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1847 break;
1848#ifdef FEAT_WINDOWS
1849 cmdmod.split |= WSP_ABOVE;
1850#endif
1851 continue;
1852
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001853 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1854 break;
1855#ifdef FEAT_AUTOCMD
1856 if (cmdmod.save_ei == NULL)
1857 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001858 /* Set 'eventignore' to "all". Restore the
1859 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001860 cmdmod.save_ei = vim_strsave(p_ei);
1861 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001862 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001863 }
1864#endif
1865 continue;
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1868 break;
1869#ifdef FEAT_WINDOWS
1870 cmdmod.split |= WSP_BELOW;
1871#endif
1872 continue;
1873
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001874 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1875 {
1876#ifdef HAVE_SANDBOX
1877 if (!did_sandbox)
1878 ++sandbox;
1879 did_sandbox = TRUE;
1880#endif
1881 continue;
1882 }
1883 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001885 if (save_msg_silent == -1)
1886 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1889 {
1890 /* ":silent!", but not "silent !cmd" */
1891 ea.cmd = skipwhite(ea.cmd + 1);
1892 ++emsg_silent;
1893 ++did_esilent;
1894 }
1895 continue;
1896
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001897 case 't': if (checkforcmd(&p, "tab", 3))
1898 {
1899#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001900 if (vim_isdigit(*ea.cmd))
1901 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1902 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001903 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001904 ea.cmd = p;
1905#endif
1906 continue;
1907 }
1908 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 break;
1910#ifdef FEAT_WINDOWS
1911 cmdmod.split |= WSP_TOP;
1912#endif
1913 continue;
1914
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001915 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1916 break;
1917 if (save_msg_silent == -1)
1918 save_msg_silent = msg_silent;
1919 msg_silent = 0;
1920 continue;
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1923 {
1924#ifdef FEAT_VERTSPLIT
1925 cmdmod.split |= WSP_VERT;
1926#endif
1927 continue;
1928 }
1929 if (!checkforcmd(&p, "verbose", 4))
1930 break;
1931 if (verbose_save < 0)
1932 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001933 if (vim_isdigit(*ea.cmd))
1934 p_verbose = atoi((char *)ea.cmd);
1935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 p_verbose = 1;
1937 ea.cmd = p;
1938 continue;
1939 }
1940 break;
1941 }
1942
1943#ifdef FEAT_EVAL
1944 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1945 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1946#else
1947 ea.skip = (if_level > 0);
1948#endif
1949
1950#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001951# ifdef FEAT_PROFILE
1952 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001953 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001955 if (getline_equal(fgetline, cookie, get_func_line))
1956 func_line_exec(getline_cookie(fgetline, cookie));
1957 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001958 script_line_exec();
1959 }
1960#endif
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 /* May go to debug mode. If this happens and the ">quit" debug command is
1963 * used, throw an interrupt exception and skip the next command. */
1964 dbg_check_breakpoint(&ea);
1965 if (!ea.skip && got_int)
1966 {
1967 ea.skip = TRUE;
1968 (void)do_intthrow(cstack);
1969 }
1970#endif
1971
1972/*
1973 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1974 *
1975 * where 'addr' is:
1976 *
1977 * % (entire file)
1978 * $ [+-NUM]
1979 * 'x [+-NUM] (where x denotes a currently defined mark)
1980 * . [+-NUM]
1981 * [+-NUM]..
1982 * NUM
1983 *
1984 * The ea.cmd pointer is updated to point to the first character following the
1985 * range spec. If an initial address is found, but no second, the upper bound
1986 * is equal to the lower.
1987 */
1988
1989 /* repeat for all ',' or ';' separated addresses */
1990 for (;;)
1991 {
1992 ea.line1 = ea.line2;
1993 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1994 ea.cmd = skipwhite(ea.cmd);
1995 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1996 if (ea.cmd == NULL) /* error detected */
1997 goto doend;
1998 if (lnum == MAXLNUM)
1999 {
2000 if (*ea.cmd == '%') /* '%' - all lines */
2001 {
2002 ++ea.cmd;
2003 ea.line1 = 1;
2004 ea.line2 = curbuf->b_ml.ml_line_count;
2005 ++ea.addr_count;
2006 }
2007 /* '*' - visual area */
2008 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2009 {
2010 pos_T *fp;
2011
2012 ++ea.cmd;
2013 if (!ea.skip)
2014 {
2015 fp = getmark('<', FALSE);
2016 if (check_mark(fp) == FAIL)
2017 goto doend;
2018 ea.line1 = fp->lnum;
2019 fp = getmark('>', FALSE);
2020 if (check_mark(fp) == FAIL)
2021 goto doend;
2022 ea.line2 = fp->lnum;
2023 ++ea.addr_count;
2024 }
2025 }
2026 }
2027 else
2028 ea.line2 = lnum;
2029 ea.addr_count++;
2030
2031 if (*ea.cmd == ';')
2032 {
2033 if (!ea.skip)
2034 curwin->w_cursor.lnum = ea.line2;
2035 }
2036 else if (*ea.cmd != ',')
2037 break;
2038 ++ea.cmd;
2039 }
2040
2041 /* One address given: set start and end lines */
2042 if (ea.addr_count == 1)
2043 {
2044 ea.line1 = ea.line2;
2045 /* ... but only implicit: really no address given */
2046 if (lnum == MAXLNUM)
2047 ea.addr_count = 0;
2048 }
2049
2050 /* Don't leave the cursor on an illegal line (caused by ';') */
2051 check_cursor_lnum();
2052
2053/*
2054 * 4. parse command
2055 */
2056
2057 /*
2058 * Skip ':' and any white space
2059 */
2060 ea.cmd = skipwhite(ea.cmd);
2061 while (*ea.cmd == ':')
2062 ea.cmd = skipwhite(ea.cmd + 1);
2063
2064 /*
2065 * If we got a line, but no command, then go to the line.
2066 * If we find a '|' or '\n' we set ea.nextcmd.
2067 */
2068 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2069 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2070 {
2071 /*
2072 * strange vi behaviour:
2073 * ":3" jumps to line 3
2074 * ":3|..." prints line 3
2075 * ":|" prints current line
2076 */
2077 if (ea.skip) /* skip this if inside :if */
2078 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002079 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
2081 ea.cmdidx = CMD_print;
2082 ea.argt = RANGE+COUNT+TRLBAR;
2083 if ((errormsg = invalid_range(&ea)) == NULL)
2084 {
2085 correct_range(&ea);
2086 ex_print(&ea);
2087 }
2088 }
2089 else if (ea.addr_count != 0)
2090 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002091 if (ea.line2 > curbuf->b_ml.ml_line_count)
2092 {
2093 /* With '-' in 'cpoptions' a line number past the file is an
2094 * error, otherwise put it at the end of the file. */
2095 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2096 ea.line2 = -1;
2097 else
2098 ea.line2 = curbuf->b_ml.ml_line_count;
2099 }
2100
2101 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002102 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 else
2104 {
2105 if (ea.line2 == 0)
2106 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 else
2108 curwin->w_cursor.lnum = ea.line2;
2109 beginline(BL_SOL | BL_FIX);
2110 }
2111 }
2112 goto doend;
2113 }
2114
2115 /* Find the command and let "p" point to after it. */
2116 p = find_command(&ea, NULL);
2117
2118#ifdef FEAT_USR_CMDS
2119 if (p == NULL)
2120 {
2121 if (!ea.skip)
2122 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2123 goto doend;
2124 }
2125 /* Check for wrong commands. */
2126 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2127 {
2128 errormsg = uc_fun_cmd();
2129 goto doend;
2130 }
2131#endif
2132 if (ea.cmdidx == CMD_SIZE)
2133 {
2134 if (!ea.skip)
2135 {
2136 STRCPY(IObuff, _("E492: Not an editor command"));
2137 if (!sourcing)
2138 {
2139 STRCAT(IObuff, ": ");
2140 STRNCAT(IObuff, *cmdlinep, 40);
2141 }
2142 errormsg = IObuff;
2143 }
2144 goto doend;
2145 }
2146
2147 ni = (
2148#ifdef FEAT_USR_CMDS
2149 !USER_CMDIDX(ea.cmdidx) &&
2150#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002151 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002152#ifdef HAVE_EX_SCRIPT_NI
2153 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2154#endif
2155 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157#ifndef FEAT_EVAL
2158 /*
2159 * When the expression evaluation is disabled, recognize the ":if" and
2160 * ":endif" commands and ignore everything in between it.
2161 */
2162 if (ea.cmdidx == CMD_if)
2163 ++if_level;
2164 if (if_level)
2165 {
2166 if (ea.cmdidx == CMD_endif)
2167 --if_level;
2168 goto doend;
2169 }
2170
2171#endif
2172
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002173 /* forced commands */
2174 if (*p == '!' && ea.cmdidx != CMD_substitute
2175 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 ++p;
2178 ea.forceit = TRUE;
2179 }
2180 else
2181 ea.forceit = FALSE;
2182
2183/*
2184 * 5. parse arguments
2185 */
2186#ifdef FEAT_USR_CMDS
2187 if (!USER_CMDIDX(ea.cmdidx))
2188#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002189 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 if (!ea.skip)
2192 {
2193#ifdef HAVE_SANDBOX
2194 if (sandbox != 0 && !(ea.argt & SBOXOK))
2195 {
2196 /* Command not allowed in sandbox. */
2197 errormsg = (char_u *)_(e_sandbox);
2198 goto doend;
2199 }
2200#endif
2201 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2202 {
2203 /* Command not allowed in non-'modifiable' buffer */
2204 errormsg = (char_u *)_(e_modifiable);
2205 goto doend;
2206 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002207
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002208 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209# ifdef FEAT_USR_CMDS
2210 && !USER_CMDIDX(ea.cmdidx)
2211# endif
2212 )
2213 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002214 /* Command not allowed when editing the command line. */
2215#ifdef FEAT_CMDWIN
2216 if (cmdwin_type != 0)
2217 errormsg = (char_u *)_(e_cmdwin);
2218 else
2219#endif
2220 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 goto doend;
2222 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002223#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002224 /* Disallow editing another buffer when "curbuf_lock" is set.
2225 * Do allow ":edit" (check for argument later).
2226 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002227 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002228 && ea.cmdidx != CMD_edit
2229 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002230# ifdef FEAT_USR_CMDS
2231 && !USER_CMDIDX(ea.cmdidx)
2232# endif
2233 && curbuf_locked())
2234 goto doend;
2235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2238 {
2239 /* no range allowed */
2240 errormsg = (char_u *)_(e_norange);
2241 goto doend;
2242 }
2243 }
2244
2245 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2246 {
2247 errormsg = (char_u *)_(e_nobang);
2248 goto doend;
2249 }
2250
2251 /*
2252 * Don't complain about the range if it is not used
2253 * (could happen if line_count is accidentally set to 0).
2254 */
2255 if (!ea.skip && !ni)
2256 {
2257 /*
2258 * If the range is backwards, ask for confirmation and, if given, swap
2259 * ea.line1 & ea.line2 so it's forwards again.
2260 * When global command is busy, don't ask, will fail below.
2261 */
2262 if (!global_busy && ea.line1 > ea.line2)
2263 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002264 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002266 if (sourcing || exmode_active)
2267 {
2268 errormsg = (char_u *)_("E493: Backwards range given");
2269 goto doend;
2270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (ask_yesno((char_u *)
2272 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002273 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275 lnum = ea.line1;
2276 ea.line1 = ea.line2;
2277 ea.line2 = lnum;
2278 }
2279 if ((errormsg = invalid_range(&ea)) != NULL)
2280 goto doend;
2281 }
2282
2283 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2284 ea.line2 = 1;
2285
2286 correct_range(&ea);
2287
2288#ifdef FEAT_FOLDING
2289 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2290 {
2291 /* Put the first line at the start of a closed fold, put the last line
2292 * at the end of a closed fold. */
2293 (void)hasFolding(ea.line1, &ea.line1, NULL);
2294 (void)hasFolding(ea.line2, NULL, &ea.line2);
2295 }
2296#endif
2297
2298#ifdef FEAT_QUICKFIX
2299 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002300 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * option here, so things like % get expanded.
2302 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002303 p = replace_makeprg(&ea, p, cmdlinep);
2304 if (p == NULL)
2305 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#endif
2307
2308 /*
2309 * Skip to start of argument.
2310 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2311 */
2312 if (ea.cmdidx == CMD_bang)
2313 ea.arg = p;
2314 else
2315 ea.arg = skipwhite(p);
2316
2317 /*
2318 * Check for "++opt=val" argument.
2319 * Must be first, allow ":w ++enc=utf8 !cmd"
2320 */
2321 if (ea.argt & ARGOPT)
2322 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2323 if (getargopt(&ea) == FAIL && !ni)
2324 {
2325 errormsg = (char_u *)_(e_invarg);
2326 goto doend;
2327 }
2328
2329 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2330 {
2331 if (*ea.arg == '>') /* append */
2332 {
2333 if (*++ea.arg != '>') /* typed wrong */
2334 {
2335 errormsg = (char_u *)_("E494: Use w or w>>");
2336 goto doend;
2337 }
2338 ea.arg = skipwhite(ea.arg + 1);
2339 ea.append = TRUE;
2340 }
2341 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2342 {
2343 ++ea.arg;
2344 ea.usefilter = TRUE;
2345 }
2346 }
2347
2348 if (ea.cmdidx == CMD_read)
2349 {
2350 if (ea.forceit)
2351 {
2352 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2353 ea.forceit = FALSE;
2354 }
2355 else if (*ea.arg == '!') /* :r !filter */
2356 {
2357 ++ea.arg;
2358 ea.usefilter = TRUE;
2359 }
2360 }
2361
2362 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2363 {
2364 ea.amount = 1;
2365 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2366 {
2367 ++ea.arg;
2368 ++ea.amount;
2369 }
2370 ea.arg = skipwhite(ea.arg);
2371 }
2372
2373 /*
2374 * Check for "+command" argument, before checking for next command.
2375 * Don't do this for ":read !cmd" and ":write !cmd".
2376 */
2377 if ((ea.argt & EDITCMD) && !ea.usefilter)
2378 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2379
2380 /*
2381 * Check for '|' to separate commands and '"' to start comments.
2382 * Don't do this for ":read !cmd" and ":write !cmd".
2383 */
2384 if ((ea.argt & TRLBAR) && !ea.usefilter)
2385 separate_nextcmd(&ea);
2386
2387 /*
2388 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002389 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2390 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002392 else if (ea.cmdidx == CMD_bang
2393 || ea.cmdidx == CMD_global
2394 || ea.cmdidx == CMD_vglobal
2395 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
2397 for (p = ea.arg; *p; ++p)
2398 {
2399 /* Remove one backslash before a newline, so that it's possible to
2400 * pass a newline to the shell and also a newline that is preceded
2401 * with a backslash. This makes it impossible to end a shell
2402 * command in a backslash, but that doesn't appear useful.
2403 * Halving the number of backslashes is incompatible with previous
2404 * versions. */
2405 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002406 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 else if (*p == '\n')
2408 {
2409 ea.nextcmd = p + 1;
2410 *p = NUL;
2411 break;
2412 }
2413 }
2414 }
2415
2416 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2417 {
2418 ea.line1 = 1;
2419 ea.line2 = curbuf->b_ml.ml_line_count;
2420 }
2421
2422 /* accept numbered register only when no count allowed (:put) */
2423 if ( (ea.argt & REGSTR)
2424 && *ea.arg != NUL
2425#ifdef FEAT_USR_CMDS
2426 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2427 && USER_CMDIDX(ea.cmdidx)))
2428 /* Do not allow register = for user commands */
2429 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2430#else
2431 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2432#endif
2433 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2434 {
2435 ea.regname = *ea.arg++;
2436#ifdef FEAT_EVAL
2437 /* for '=' register: accept the rest of the line as an expression */
2438 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2439 {
2440 set_expr_line(vim_strsave(ea.arg));
2441 ea.arg += STRLEN(ea.arg);
2442 }
2443#endif
2444 ea.arg = skipwhite(ea.arg);
2445 }
2446
2447 /*
2448 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2449 * count, it's a buffer name.
2450 */
2451 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2452 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2453 || vim_iswhite(*p)))
2454 {
2455 n = getdigits(&ea.arg);
2456 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002457 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
2459 errormsg = (char_u *)_(e_zerocount);
2460 goto doend;
2461 }
2462 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2463 {
2464 ea.line2 = n;
2465 if (ea.addr_count == 0)
2466 ea.addr_count = 1;
2467 }
2468 else
2469 {
2470 ea.line1 = ea.line2;
2471 ea.line2 += n - 1;
2472 ++ea.addr_count;
2473 /*
2474 * Be vi compatible: no error message for out of range.
2475 */
2476 if (ea.line2 > curbuf->b_ml.ml_line_count)
2477 ea.line2 = curbuf->b_ml.ml_line_count;
2478 }
2479 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002480
2481 /*
2482 * Check for flags: 'l', 'p' and '#'.
2483 */
2484 if (ea.argt & EXFLAGS)
2485 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002487 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002488 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 errormsg = (char_u *)_(e_trailing);
2491 goto doend;
2492 }
2493
2494 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2495 {
2496 errormsg = (char_u *)_(e_argreq);
2497 goto doend;
2498 }
2499
2500#ifdef FEAT_EVAL
2501 /*
2502 * Skip the command when it's not going to be executed.
2503 * The commands like :if, :endif, etc. always need to be executed.
2504 * Also make an exception for commands that handle a trailing command
2505 * themselves.
2506 */
2507 if (ea.skip)
2508 {
2509 switch (ea.cmdidx)
2510 {
2511 /* commands that need evaluation */
2512 case CMD_while:
2513 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002514 case CMD_for:
2515 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 case CMD_if:
2517 case CMD_elseif:
2518 case CMD_else:
2519 case CMD_endif:
2520 case CMD_try:
2521 case CMD_catch:
2522 case CMD_finally:
2523 case CMD_endtry:
2524 case CMD_function:
2525 break;
2526
2527 /* Commands that handle '|' themselves. Check: A command should
2528 * either have the TRLBAR flag, appear in this list or appear in
2529 * the list at ":help :bar". */
2530 case CMD_aboveleft:
2531 case CMD_and:
2532 case CMD_belowright:
2533 case CMD_botright:
2534 case CMD_browse:
2535 case CMD_call:
2536 case CMD_confirm:
2537 case CMD_delfunction:
2538 case CMD_djump:
2539 case CMD_dlist:
2540 case CMD_dsearch:
2541 case CMD_dsplit:
2542 case CMD_echo:
2543 case CMD_echoerr:
2544 case CMD_echomsg:
2545 case CMD_echon:
2546 case CMD_execute:
2547 case CMD_help:
2548 case CMD_hide:
2549 case CMD_ijump:
2550 case CMD_ilist:
2551 case CMD_isearch:
2552 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002553 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 case CMD_keepjumps:
2555 case CMD_keepmarks:
2556 case CMD_leftabove:
2557 case CMD_let:
2558 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002559 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002561 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 case CMD_perl:
2563 case CMD_psearch:
2564 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002565 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002566 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 case CMD_return:
2568 case CMD_rightbelow:
2569 case CMD_ruby:
2570 case CMD_silent:
2571 case CMD_smagic:
2572 case CMD_snomagic:
2573 case CMD_substitute:
2574 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002575 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 case CMD_tcl:
2577 case CMD_throw:
2578 case CMD_tilde:
2579 case CMD_topleft:
2580 case CMD_unlet:
2581 case CMD_verbose:
2582 case CMD_vertical:
2583 break;
2584
2585 default: goto doend;
2586 }
2587 }
2588#endif
2589
2590 if (ea.argt & XFILE)
2591 {
2592 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2593 goto doend;
2594 }
2595
2596#ifdef FEAT_LISTCMDS
2597 /*
2598 * Accept buffer name. Cannot be used at the same time with a buffer
2599 * number. Don't do this for a user command.
2600 */
2601 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2602# ifdef FEAT_USR_CMDS
2603 && !USER_CMDIDX(ea.cmdidx)
2604# endif
2605 )
2606 {
2607 /*
2608 * :bdelete, :bwipeout and :bunload take several arguments, separated
2609 * by spaces: find next space (skipping over escaped characters).
2610 * The others take one argument: ignore trailing spaces.
2611 */
2612 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2613 || ea.cmdidx == CMD_bunload)
2614 p = skiptowhite_esc(ea.arg);
2615 else
2616 {
2617 p = ea.arg + STRLEN(ea.arg);
2618 while (p > ea.arg && vim_iswhite(p[-1]))
2619 --p;
2620 }
2621 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2622 if (ea.line2 < 0) /* failed */
2623 goto doend;
2624 ea.addr_count = 1;
2625 ea.arg = skipwhite(p);
2626 }
2627#endif
2628
2629/*
2630 * 6. switch on command name
2631 *
2632 * The "ea" structure holds the arguments that can be used.
2633 */
2634 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002635 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 ea.cookie = cookie;
2637#ifdef FEAT_EVAL
2638 ea.cstack = cstack;
2639#endif
2640
2641#ifdef FEAT_USR_CMDS
2642 if (USER_CMDIDX(ea.cmdidx))
2643 {
2644 /*
2645 * Execute a user-defined command.
2646 */
2647 do_ucmd(&ea);
2648 }
2649 else
2650#endif
2651 {
2652 /*
2653 * Call the function to execute the command.
2654 */
2655 ea.errmsg = NULL;
2656 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2657 if (ea.errmsg != NULL)
2658 errormsg = (char_u *)_(ea.errmsg);
2659 }
2660
2661#ifdef FEAT_EVAL
2662 /*
2663 * If the command just executed called do_cmdline(), any throw or ":return"
2664 * or ":finish" encountered there must also check the cstack of the still
2665 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2666 * exception, or reanimate a returned function or finished script file and
2667 * return or finish it again.
2668 */
2669 if (need_rethrow)
2670 do_throw(cstack);
2671 else if (check_cstack)
2672 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002673 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002675 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 && current_func_returned())
2677 do_return(&ea, TRUE, FALSE, NULL);
2678 }
2679 need_rethrow = check_cstack = FALSE;
2680#endif
2681
2682doend:
2683 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2684 curwin->w_cursor.lnum = 1;
2685
2686 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2687 {
2688 if (sourcing)
2689 {
2690 if (errormsg != IObuff)
2691 {
2692 STRCPY(IObuff, errormsg);
2693 errormsg = IObuff;
2694 }
2695 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002696 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698 emsg(errormsg);
2699 }
2700#ifdef FEAT_EVAL
2701 do_errthrow(cstack,
2702 (ea.cmdidx != CMD_SIZE
2703# ifdef FEAT_USR_CMDS
2704 && !USER_CMDIDX(ea.cmdidx)
2705# endif
2706 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2707#endif
2708
2709 if (verbose_save >= 0)
2710 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002711#ifdef FEAT_AUTOCMD
2712 if (cmdmod.save_ei != NULL)
2713 {
2714 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002715 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2716 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002717 free_string_option(cmdmod.save_ei);
2718 }
2719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 cmdmod = save_cmdmod;
2722
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002723 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 /* messages could be enabled for a serious error, need to check if the
2726 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002727 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002728 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 emsg_silent -= did_esilent;
2730 if (emsg_silent < 0)
2731 emsg_silent = 0;
2732 /* Restore msg_scroll, it's set by file I/O commands, even when no
2733 * message is actually displayed. */
2734 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002735
2736 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2737 * somewhere in the line. Put it back in the first column. */
2738 if (redirecting())
2739 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002742#ifdef HAVE_SANDBOX
2743 if (did_sandbox)
2744 --sandbox;
2745#endif
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2748 ea.nextcmd = NULL;
2749
2750#ifdef FEAT_EVAL
2751 --ex_nesting_level;
2752#endif
2753
2754 return ea.nextcmd;
2755}
2756#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002757 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
2759
2760/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002761 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 * If there is a match advance "pp" to the argument and return TRUE.
2763 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002764 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002766 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 char *cmd; /* name of command */
2768 int len; /* required length */
2769{
2770 int i;
2771
2772 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002773 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 break;
2775 if (i >= len && !isalpha((*pp)[i]))
2776 {
2777 *pp = skipwhite(*pp + i);
2778 return TRUE;
2779 }
2780 return FALSE;
2781}
2782
2783/*
2784 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002785 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002787 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 * Returns NULL for an ambiguous user command.
2789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 static char_u *
2791find_command(eap, full)
2792 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002793 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 int len;
2796 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002797 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799 /*
2800 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002801 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 * - the 'k' command can directly be followed by any character.
2803 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2804 * but :sre[wind] is another command, as are :scrip[tnames],
2805 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002806 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 */
2808 p = eap->cmd;
2809 if (*p == 'k')
2810 {
2811 eap->cmdidx = CMD_k;
2812 ++p;
2813 }
2814 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002815 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2816 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 || p[1] == 'g'
2818 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2819 || p[1] == 'I'
2820 || (p[1] == 'r' && p[2] != 'e')))
2821 {
2822 eap->cmdidx = CMD_substitute;
2823 ++p;
2824 }
2825 else
2826 {
2827 while (ASCII_ISALPHA(*p))
2828 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002829 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002830 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002831 while (ASCII_ISALNUM(*p))
2832 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 /* check for non-alpha command */
2835 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2836 ++p;
2837 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002838 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2839 {
2840 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2841 * :delete with the 'l' flag. Same for 'p'. */
2842 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002843 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002844 break;
2845 if (i == len - 1)
2846 {
2847 --len;
2848 if (p[-1] == 'l')
2849 eap->flags |= EXFLAG_LIST;
2850 else
2851 eap->flags |= EXFLAG_PRINT;
2852 }
2853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 if (ASCII_ISLOWER(*eap->cmd))
2856 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2857 else
2858 eap->cmdidx = cmdidxs[26];
2859
2860 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2861 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2862 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2863 (size_t)len) == 0)
2864 {
2865#ifdef FEAT_EVAL
2866 if (full != NULL
2867 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2868 *full = TRUE;
2869#endif
2870 break;
2871 }
2872
2873#ifdef FEAT_USR_CMDS
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01002874 /* Look for a user defined command as a last resort. Let ":Print" be
2875 * overruled by a user defined command. */
2876 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
2877 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002879 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 while (ASCII_ISALNUM(*p))
2881 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002882 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
2884#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002885 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 eap->cmdidx = CMD_SIZE;
2887 }
2888
2889 return p;
2890}
2891
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002892#ifdef FEAT_USR_CMDS
2893/*
2894 * Search for a user command that matches "eap->cmd".
2895 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2896 * Return a pointer to just after the command.
2897 * Return NULL if there is no matching command.
2898 */
2899 static char_u *
2900find_ucmd(eap, p, full, xp, compl)
2901 exarg_T *eap;
2902 char_u *p; /* end of the command (possibly including count) */
2903 int *full; /* set to TRUE for a full match */
2904 expand_T *xp; /* used for completion, NULL otherwise */
2905 int *compl; /* completion flags or NULL */
2906{
2907 int len = (int)(p - eap->cmd);
2908 int j, k, matchlen = 0;
2909 ucmd_T *uc;
2910 int found = FALSE;
2911 int possible = FALSE;
2912 char_u *cp, *np; /* Point into typed cmd and test name */
2913 garray_T *gap;
2914 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2915 only full match global is accepted. */
2916
2917 /*
2918 * Look for buffer-local user commands first, then global ones.
2919 */
2920 gap = &curbuf->b_ucmds;
2921 for (;;)
2922 {
2923 for (j = 0; j < gap->ga_len; ++j)
2924 {
2925 uc = USER_CMD_GA(gap, j);
2926 cp = eap->cmd;
2927 np = uc->uc_name;
2928 k = 0;
2929 while (k < len && *np != NUL && *cp++ == *np++)
2930 k++;
2931 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2932 {
2933 /* If finding a second match, the command is ambiguous. But
2934 * not if a buffer-local command wasn't a full match and a
2935 * global command is a full match. */
2936 if (k == len && found && *np != NUL)
2937 {
2938 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002939 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002940 amb_local = TRUE;
2941 }
2942
2943 if (!found || (k == len && *np == NUL))
2944 {
2945 /* If we matched up to a digit, then there could
2946 * be another command including the digit that we
2947 * should use instead.
2948 */
2949 if (k == len)
2950 found = TRUE;
2951 else
2952 possible = TRUE;
2953
2954 if (gap == &ucmds)
2955 eap->cmdidx = CMD_USER;
2956 else
2957 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002958 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002959 eap->useridx = j;
2960
2961# ifdef FEAT_CMDL_COMPL
2962 if (compl != NULL)
2963 *compl = uc->uc_compl;
2964# ifdef FEAT_EVAL
2965 if (xp != NULL)
2966 {
2967 xp->xp_arg = uc->uc_compl_arg;
2968 xp->xp_scriptID = uc->uc_scriptID;
2969 }
2970# endif
2971# endif
2972 /* Do not search for further abbreviations
2973 * if this is an exact match. */
2974 matchlen = k;
2975 if (k == len && *np == NUL)
2976 {
2977 if (full != NULL)
2978 *full = TRUE;
2979 amb_local = FALSE;
2980 break;
2981 }
2982 }
2983 }
2984 }
2985
2986 /* Stop if we found a full match or searched all. */
2987 if (j < gap->ga_len || gap == &ucmds)
2988 break;
2989 gap = &ucmds;
2990 }
2991
2992 /* Only found ambiguous matches. */
2993 if (amb_local)
2994 {
2995 if (xp != NULL)
2996 xp->xp_context = EXPAND_UNSUCCESSFUL;
2997 return NULL;
2998 }
2999
3000 /* The match we found may be followed immediately by a number. Move "p"
3001 * back to point to it. */
3002 if (found || possible)
3003 return p + (matchlen - len);
3004 return p;
3005}
3006#endif
3007
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003009static struct cmdmod
3010{
3011 char *name;
3012 int minlen;
3013 int has_count; /* :123verbose :3tab */
3014} cmdmods[] = {
3015 {"aboveleft", 3, FALSE},
3016 {"belowright", 3, FALSE},
3017 {"botright", 2, FALSE},
3018 {"browse", 3, FALSE},
3019 {"confirm", 4, FALSE},
3020 {"hide", 3, FALSE},
3021 {"keepalt", 5, FALSE},
3022 {"keepjumps", 5, FALSE},
3023 {"keepmarks", 3, FALSE},
3024 {"leftabove", 5, FALSE},
3025 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003026 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003027 {"rightbelow", 6, FALSE},
3028 {"sandbox", 3, FALSE},
3029 {"silent", 3, FALSE},
3030 {"tab", 3, TRUE},
3031 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003032 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003033 {"verbose", 4, TRUE},
3034 {"vertical", 4, FALSE},
3035};
3036
3037/*
3038 * Return length of a command modifier (including optional count).
3039 * Return zero when it's not a modifier.
3040 */
3041 int
3042modifier_len(cmd)
3043 char_u *cmd;
3044{
3045 int i, j;
3046 char_u *p = cmd;
3047
3048 if (VIM_ISDIGIT(*cmd))
3049 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003050 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003051 {
3052 for (j = 0; p[j] != NUL; ++j)
3053 if (p[j] != cmdmods[i].name[j])
3054 break;
3055 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3056 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003057 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003058 }
3059 return 0;
3060}
3061
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062/*
3063 * Return > 0 if an Ex command "name" exists.
3064 * Return 2 if there is an exact match.
3065 * Return 3 if there is an ambiguous match.
3066 */
3067 int
3068cmd_exists(name)
3069 char_u *name;
3070{
3071 exarg_T ea;
3072 int full = FALSE;
3073 int i;
3074 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003075 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003078 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 for (j = 0; name[j] != NUL; ++j)
3081 if (name[j] != cmdmods[i].name[j])
3082 break;
3083 if (name[j] == NUL && j >= cmdmods[i].minlen)
3084 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3085 }
3086
Bram Moolenaara9587612006-05-04 21:47:50 +00003087 /* Check built-in commands and user defined commands.
3088 * For ":2match" and ":3match" we need to skip the number. */
3089 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003091 p = find_command(&ea, &full);
3092 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003094 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3095 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003096 if (*skipwhite(p) != NUL)
3097 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3099}
3100#endif
3101
3102/*
3103 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3104 * we don't need/want deleted. Maybe this could be done better if we didn't
3105 * repeat all this stuff. The only problem is that they may not stay
3106 * perfectly compatible with each other, but then the command line syntax
3107 * probably won't change that much -- webb.
3108 */
3109 char_u *
3110set_one_cmd_context(xp, buff)
3111 expand_T *xp;
3112 char_u *buff; /* buffer for command string */
3113{
3114 char_u *p;
3115 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003116 int len = 0;
3117 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3119 int compl = EXPAND_NOTHING;
3120#endif
3121#ifdef FEAT_CMDL_COMPL
3122 int delim;
3123#endif
3124 int forceit = FALSE;
3125 int usefilter = FALSE; /* filter instead of file name */
3126
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003127 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 xp->xp_pattern = buff;
3129 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003130 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132/*
3133 * 2. skip comment lines and leading space, colons or bars
3134 */
3135 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3136 ;
3137 xp->xp_pattern = cmd;
3138
3139 if (*cmd == NUL)
3140 return NULL;
3141 if (*cmd == '"') /* ignore comment lines */
3142 {
3143 xp->xp_context = EXPAND_NOTHING;
3144 return NULL;
3145 }
3146
3147/*
3148 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3149 */
3150 cmd = skip_range(cmd, &xp->xp_context);
3151
3152/*
3153 * 4. parse command
3154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 xp->xp_pattern = cmd;
3156 if (*cmd == NUL)
3157 return NULL;
3158 if (*cmd == '"')
3159 {
3160 xp->xp_context = EXPAND_NOTHING;
3161 return NULL;
3162 }
3163
3164 if (*cmd == '|' || *cmd == '\n')
3165 return cmd + 1; /* There's another command */
3166
3167 /*
3168 * Isolate the command and search for it in the command table.
3169 * Exceptions:
3170 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003171 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3173 */
3174 if (*cmd == 'k' && cmd[1] != 'e')
3175 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003176 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 p = cmd + 1;
3178 }
3179 else
3180 {
3181 p = cmd;
3182 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3183 ++p;
3184 /* check for non-alpha command */
3185 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3186 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003187 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003189 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 xp->xp_context = EXPAND_UNSUCCESSFUL;
3192 return NULL;
3193 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003194 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003195 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3196 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3197 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 break;
3199
3200#ifdef FEAT_USR_CMDS
3201 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3203 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204#endif
3205 }
3206
3207 /*
3208 * If the cursor is touching the command, and it ends in an alpha-numeric
3209 * character, complete the command name.
3210 */
3211 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3212 return NULL;
3213
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003214 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
3216 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3217 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003218 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 p = cmd + 1;
3220 }
3221#ifdef FEAT_USR_CMDS
3222 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3223 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003224 ea.cmd = cmd;
3225 p = find_ucmd(&ea, p, NULL, xp,
3226# if defined(FEAT_CMDL_COMPL)
3227 &compl
3228# else
3229 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003231 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003232 if (p == NULL)
3233 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 }
3235#endif
3236 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003237 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 /* Not still touching the command and it was an illegal one */
3240 xp->xp_context = EXPAND_UNSUCCESSFUL;
3241 return NULL;
3242 }
3243
3244 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3245
3246 if (*p == '!') /* forced commands */
3247 {
3248 forceit = TRUE;
3249 ++p;
3250 }
3251
3252/*
3253 * 5. parse arguments
3254 */
3255#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003256 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003258 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259
3260 arg = skipwhite(p);
3261
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 {
3264 if (*arg == '>') /* append */
3265 {
3266 if (*++arg == '>')
3267 ++arg;
3268 arg = skipwhite(arg);
3269 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003270 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 {
3272 ++arg;
3273 usefilter = TRUE;
3274 }
3275 }
3276
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003277 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 usefilter = forceit; /* :r! filter if forced */
3280 if (*arg == '!') /* :r !filter */
3281 {
3282 ++arg;
3283 usefilter = TRUE;
3284 }
3285 }
3286
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003287 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 {
3289 while (*arg == *cmd) /* allow any number of '>' or '<' */
3290 ++arg;
3291 arg = skipwhite(arg);
3292 }
3293
3294 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003295 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 /* Check if we're in the +command */
3298 p = arg + 1;
3299 arg = skip_cmd_arg(arg, FALSE);
3300
3301 /* Still touching the command after '+'? */
3302 if (*arg == NUL)
3303 return p;
3304
3305 /* Skip space(s) after +command to get to the real argument */
3306 arg = skipwhite(arg);
3307 }
3308
3309 /*
3310 * Check for '|' to separate commands and '"' to start comments.
3311 * Don't do this for ":read !cmd" and ":write !cmd".
3312 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003313 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 {
3315 p = arg;
3316 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003317 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 p += 2;
3319 while (*p)
3320 {
3321 if (*p == Ctrl_V)
3322 {
3323 if (p[1] != NUL)
3324 ++p;
3325 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003326 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 || *p == '|' || *p == '\n')
3328 {
3329 if (*(p - 1) != '\\')
3330 {
3331 if (*p == '|' || *p == '\n')
3332 return p + 1;
3333 return NULL; /* It's a comment */
3334 }
3335 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003336 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 }
3338 }
3339
3340 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003341 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 vim_strchr((char_u *)"|\"", *arg) == NULL)
3343 return NULL;
3344
3345 /* Find start of last argument (argument just before cursor): */
3346 p = buff + STRLEN(buff);
3347 while (p != arg && *p != ' ' && *p != TAB)
3348 p--;
3349 if (*p == ' ' || *p == TAB)
3350 p++;
3351 xp->xp_pattern = p;
3352
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003353 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003355 int c;
3356 int in_quote = FALSE;
3357 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
3359 /*
3360 * Allow spaces within back-quotes to count as part of the argument
3361 * being expanded.
3362 */
3363 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003364 p = xp->xp_pattern;
3365 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003367#ifdef FEAT_MBYTE
3368 if (has_mbyte)
3369 c = mb_ptr2char(p);
3370 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003372 c = *p;
3373 if (c == '\\' && p[1] != NUL)
3374 ++p;
3375 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
3377 if (!in_quote)
3378 {
3379 xp->xp_pattern = p;
3380 bow = p + 1;
3381 }
3382 in_quote = !in_quote;
3383 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003384 /* An argument can contain just about everything, except
3385 * characters that end the command and white space. */
3386 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003387#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003388 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003389#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003390 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003391 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003392 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003393 while (*p != NUL)
3394 {
3395#ifdef FEAT_MBYTE
3396 if (has_mbyte)
3397 c = mb_ptr2char(p);
3398 else
3399#endif
3400 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003401 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003402 break;
3403#ifdef FEAT_MBYTE
3404 if (has_mbyte)
3405 len = (*mb_ptr2len)(p);
3406 else
3407#endif
3408 len = 1;
3409 mb_ptr_adv(p);
3410 }
3411 if (in_quote)
3412 bow = p;
3413 else
3414 xp->xp_pattern = p;
3415 p -= len;
3416 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003417 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 }
3419
3420 /*
3421 * If we are still inside the quotes, and we passed a space, just
3422 * expand from there.
3423 */
3424 if (bow != NULL && in_quote)
3425 xp->xp_pattern = bow;
3426 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003427
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003428 /* For a shell command more chars need to be escaped. */
3429 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003430 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003431#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003432 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003433#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003434 /* When still after the command name expand executables. */
3435 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003436 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
3439 /* Check for environment variable */
3440 if (*xp->xp_pattern == '$'
3441#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3442 || *xp->xp_pattern == '%'
3443#endif
3444 )
3445 {
3446 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3447 if (!vim_isIDc(*p))
3448 break;
3449 if (*p == NUL)
3450 {
3451 xp->xp_context = EXPAND_ENV_VARS;
3452 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003453#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3454 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003455 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003456 compl = EXPAND_ENV_VARS;
3457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 }
3460 }
3461
3462/*
3463 * 6. switch on command name
3464 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003465 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003467 case CMD_find:
3468 case CMD_sfind:
3469 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003470 if (xp->xp_context == EXPAND_FILES)
3471 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 case CMD_cd:
3474 case CMD_chdir:
3475 case CMD_lcd:
3476 case CMD_lchdir:
3477 if (xp->xp_context == EXPAND_FILES)
3478 xp->xp_context = EXPAND_DIRECTORIES;
3479 break;
3480 case CMD_help:
3481 xp->xp_context = EXPAND_HELP;
3482 xp->xp_pattern = arg;
3483 break;
3484
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003485 /* Command modifiers: return the argument.
3486 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003488 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 case CMD_belowright:
3490 case CMD_botright:
3491 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003492 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003494 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 case CMD_folddoclosed:
3496 case CMD_folddoopen:
3497 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003498 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 case CMD_keepjumps:
3500 case CMD_keepmarks:
3501 case CMD_leftabove:
3502 case CMD_lockmarks:
3503 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003504 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003506 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 case CMD_topleft:
3508 case CMD_verbose:
3509 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003510 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 return arg;
3512
Bram Moolenaar4f688582007-07-24 12:34:30 +00003513#ifdef FEAT_CMDL_COMPL
3514# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 case CMD_match:
3516 if (*arg == NUL || !ends_excmd(*arg))
3517 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003518 /* also complete "None" */
3519 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 arg = skipwhite(skiptowhite(arg));
3521 if (*arg != NUL)
3522 {
3523 xp->xp_context = EXPAND_NOTHING;
3524 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3525 }
3526 }
3527 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003528# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530/*
3531 * All completion for the +cmdline_compl feature goes here.
3532 */
3533
3534# ifdef FEAT_USR_CMDS
3535 case CMD_command:
3536 /* Check for attributes */
3537 while (*arg == '-')
3538 {
3539 arg++; /* Skip "-" */
3540 p = skiptowhite(arg);
3541 if (*p == NUL)
3542 {
3543 /* Cursor is still in the attribute */
3544 p = vim_strchr(arg, '=');
3545 if (p == NULL)
3546 {
3547 /* No "=", so complete attribute names */
3548 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3549 xp->xp_pattern = arg;
3550 return NULL;
3551 }
3552
3553 /* For the -complete and -nargs attributes, we complete
3554 * their arguments as well.
3555 */
3556 if (STRNICMP(arg, "complete", p - arg) == 0)
3557 {
3558 xp->xp_context = EXPAND_USER_COMPLETE;
3559 xp->xp_pattern = p + 1;
3560 return NULL;
3561 }
3562 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3563 {
3564 xp->xp_context = EXPAND_USER_NARGS;
3565 xp->xp_pattern = p + 1;
3566 return NULL;
3567 }
3568 return NULL;
3569 }
3570 arg = skipwhite(p);
3571 }
3572
3573 /* After the attributes comes the new command name */
3574 p = skiptowhite(arg);
3575 if (*p == NUL)
3576 {
3577 xp->xp_context = EXPAND_USER_COMMANDS;
3578 xp->xp_pattern = arg;
3579 break;
3580 }
3581
3582 /* And finally comes a normal command */
3583 return skipwhite(p);
3584
3585 case CMD_delcommand:
3586 xp->xp_context = EXPAND_USER_COMMANDS;
3587 xp->xp_pattern = arg;
3588 break;
3589# endif
3590
3591 case CMD_global:
3592 case CMD_vglobal:
3593 delim = *arg; /* get the delimiter */
3594 if (delim)
3595 ++arg; /* skip delimiter if there is one */
3596
3597 while (arg[0] != NUL && arg[0] != delim)
3598 {
3599 if (arg[0] == '\\' && arg[1] != NUL)
3600 ++arg;
3601 ++arg;
3602 }
3603 if (arg[0] != NUL)
3604 return arg + 1;
3605 break;
3606 case CMD_and:
3607 case CMD_substitute:
3608 delim = *arg;
3609 if (delim)
3610 {
3611 /* skip "from" part */
3612 ++arg;
3613 arg = skip_regexp(arg, delim, p_magic, NULL);
3614 }
3615 /* skip "to" part */
3616 while (arg[0] != NUL && arg[0] != delim)
3617 {
3618 if (arg[0] == '\\' && arg[1] != NUL)
3619 ++arg;
3620 ++arg;
3621 }
3622 if (arg[0] != NUL) /* skip delimiter */
3623 ++arg;
3624 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3625 ++arg;
3626 if (arg[0] != NUL)
3627 return arg;
3628 break;
3629 case CMD_isearch:
3630 case CMD_dsearch:
3631 case CMD_ilist:
3632 case CMD_dlist:
3633 case CMD_ijump:
3634 case CMD_psearch:
3635 case CMD_djump:
3636 case CMD_isplit:
3637 case CMD_dsplit:
3638 arg = skipwhite(skipdigits(arg)); /* skip count */
3639 if (*arg == '/') /* Match regexp, not just whole words */
3640 {
3641 for (++arg; *arg && *arg != '/'; arg++)
3642 if (*arg == '\\' && arg[1] != NUL)
3643 arg++;
3644 if (*arg)
3645 {
3646 arg = skipwhite(arg + 1);
3647
3648 /* Check for trailing illegal characters */
3649 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3650 xp->xp_context = EXPAND_NOTHING;
3651 else
3652 return arg;
3653 }
3654 }
3655 break;
3656#ifdef FEAT_AUTOCMD
3657 case CMD_autocmd:
3658 return set_context_in_autocmd(xp, arg, FALSE);
3659
3660 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003661 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 return set_context_in_autocmd(xp, arg, TRUE);
3663#endif
3664 case CMD_set:
3665 set_context_in_set_cmd(xp, arg, 0);
3666 break;
3667 case CMD_setglobal:
3668 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3669 break;
3670 case CMD_setlocal:
3671 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3672 break;
3673 case CMD_tag:
3674 case CMD_stag:
3675 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003676 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 case CMD_tselect:
3678 case CMD_stselect:
3679 case CMD_ptselect:
3680 case CMD_tjump:
3681 case CMD_stjump:
3682 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003683 if (*p_wop != NUL)
3684 xp->xp_context = EXPAND_TAGS_LISTFILES;
3685 else
3686 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 xp->xp_pattern = arg;
3688 break;
3689 case CMD_augroup:
3690 xp->xp_context = EXPAND_AUGROUP;
3691 xp->xp_pattern = arg;
3692 break;
3693#ifdef FEAT_SYN_HL
3694 case CMD_syntax:
3695 set_context_in_syntax_cmd(xp, arg);
3696 break;
3697#endif
3698#ifdef FEAT_EVAL
3699 case CMD_let:
3700 case CMD_if:
3701 case CMD_elseif:
3702 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003703 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 case CMD_echo:
3705 case CMD_echon:
3706 case CMD_execute:
3707 case CMD_echomsg:
3708 case CMD_echoerr:
3709 case CMD_call:
3710 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003711 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 break;
3713
3714 case CMD_unlet:
3715 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3716 arg = xp->xp_pattern + 1;
3717 xp->xp_context = EXPAND_USER_VARS;
3718 xp->xp_pattern = arg;
3719 break;
3720
3721 case CMD_function:
3722 case CMD_delfunction:
3723 xp->xp_context = EXPAND_USER_FUNC;
3724 xp->xp_pattern = arg;
3725 break;
3726
3727 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003728 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 break;
3730#endif
3731 case CMD_highlight:
3732 set_context_in_highlight_cmd(xp, arg);
3733 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003734#ifdef FEAT_CSCOPE
3735 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003736 case CMD_lcscope:
3737 case CMD_scscope:
3738 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003739 break;
3740#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003741#ifdef FEAT_SIGNS
3742 case CMD_sign:
3743 set_context_in_sign_cmd(xp, arg);
3744 break;
3745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746#ifdef FEAT_LISTCMDS
3747 case CMD_bdelete:
3748 case CMD_bwipeout:
3749 case CMD_bunload:
3750 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3751 arg = xp->xp_pattern + 1;
3752 /*FALLTHROUGH*/
3753 case CMD_buffer:
3754 case CMD_sbuffer:
3755 case CMD_checktime:
3756 xp->xp_context = EXPAND_BUFFERS;
3757 xp->xp_pattern = arg;
3758 break;
3759#endif
3760#ifdef FEAT_USR_CMDS
3761 case CMD_USER:
3762 case CMD_USER_BUF:
3763 if (compl != EXPAND_NOTHING)
3764 {
3765 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003766 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
3768# ifdef FEAT_MENU
3769 if (compl == EXPAND_MENUS)
3770 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3771# endif
3772 if (compl == EXPAND_COMMANDS)
3773 return arg;
3774 if (compl == EXPAND_MAPPINGS)
3775 return set_context_in_map_cmd(xp, (char_u *)"map",
3776 arg, forceit, FALSE, FALSE, CMD_map);
3777 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3778 arg = xp->xp_pattern + 1;
3779 xp->xp_pattern = arg;
3780 }
3781 xp->xp_context = compl;
3782 }
3783 break;
3784#endif
3785 case CMD_map: case CMD_noremap:
3786 case CMD_nmap: case CMD_nnoremap:
3787 case CMD_vmap: case CMD_vnoremap:
3788 case CMD_omap: case CMD_onoremap:
3789 case CMD_imap: case CMD_inoremap:
3790 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003791 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003793 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 case CMD_unmap:
3795 case CMD_nunmap:
3796 case CMD_vunmap:
3797 case CMD_ounmap:
3798 case CMD_iunmap:
3799 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003800 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003802 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 case CMD_abbreviate: case CMD_noreabbrev:
3804 case CMD_cabbrev: case CMD_cnoreabbrev:
3805 case CMD_iabbrev: case CMD_inoreabbrev:
3806 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003807 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 case CMD_unabbreviate:
3809 case CMD_cunabbrev:
3810 case CMD_iunabbrev:
3811 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003812 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813#ifdef FEAT_MENU
3814 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3815 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3816 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3817 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3818 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3819 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3820 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3821 case CMD_tmenu: case CMD_tunmenu:
3822 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3823 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3824#endif
3825
3826 case CMD_colorscheme:
3827 xp->xp_context = EXPAND_COLORS;
3828 xp->xp_pattern = arg;
3829 break;
3830
3831 case CMD_compiler:
3832 xp->xp_context = EXPAND_COMPILER;
3833 xp->xp_pattern = arg;
3834 break;
3835
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003836 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02003837 xp->xp_context = EXPAND_OWNSYNTAX;
3838 xp->xp_pattern = arg;
3839 break;
3840
3841 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003842 xp->xp_context = EXPAND_FILETYPE;
3843 xp->xp_pattern = arg;
3844 break;
3845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3847 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3848 case CMD_language:
3849 if (*skiptowhite(arg) == NUL)
3850 {
3851 xp->xp_context = EXPAND_LANGUAGE;
3852 xp->xp_pattern = arg;
3853 }
3854 else
3855 xp->xp_context = EXPAND_NOTHING;
3856 break;
3857#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003858#if defined(FEAT_PROFILE)
3859 case CMD_profile:
3860 set_context_in_profile_cmd(xp, arg);
3861 break;
3862#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003863 case CMD_behave:
3864 xp->xp_context = EXPAND_BEHAVE;
3865 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
3867#endif /* FEAT_CMDL_COMPL */
3868
3869 default:
3870 break;
3871 }
3872 return NULL;
3873}
3874
3875/*
3876 * skip a range specifier of the form: addr [,addr] [;addr] ..
3877 *
3878 * Backslashed delimiters after / or ? will be skipped, and commands will
3879 * not be expanded between /'s and ?'s or after "'".
3880 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003881 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 * Returns the "cmd" pointer advanced to beyond the range.
3883 */
3884 char_u *
3885skip_range(cmd, ctx)
3886 char_u *cmd;
3887 int *ctx; /* pointer to xp_context or NULL */
3888{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003889 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
Bram Moolenaardf177f62005-02-22 08:39:57 +00003891 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
3893 if (*cmd == '\'')
3894 {
3895 if (*++cmd == NUL && ctx != NULL)
3896 *ctx = EXPAND_NOTHING;
3897 }
3898 else if (*cmd == '/' || *cmd == '?')
3899 {
3900 delim = *cmd++;
3901 while (*cmd != NUL && *cmd != delim)
3902 if (*cmd++ == '\\' && *cmd != NUL)
3903 ++cmd;
3904 if (*cmd == NUL && ctx != NULL)
3905 *ctx = EXPAND_NOTHING;
3906 }
3907 if (*cmd != NUL)
3908 ++cmd;
3909 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003910
3911 /* Skip ":" and white space. */
3912 while (*cmd == ':')
3913 cmd = skipwhite(cmd + 1);
3914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 return cmd;
3916}
3917
3918/*
3919 * get a single EX address
3920 *
3921 * Set ptr to the next character after the part that was interpreted.
3922 * Set ptr to NULL when an error is encountered.
3923 *
3924 * Return MAXLNUM when no Ex address was found.
3925 */
3926 static linenr_T
3927get_address(ptr, skip, to_other_file)
3928 char_u **ptr;
3929 int skip; /* only skip the address, don't use it */
3930 int to_other_file; /* flag: may jump to other file */
3931{
3932 int c;
3933 int i;
3934 long n;
3935 char_u *cmd;
3936 pos_T pos;
3937 pos_T *fp;
3938 linenr_T lnum;
3939
3940 cmd = skipwhite(*ptr);
3941 lnum = MAXLNUM;
3942 do
3943 {
3944 switch (*cmd)
3945 {
3946 case '.': /* '.' - Cursor position */
3947 ++cmd;
3948 lnum = curwin->w_cursor.lnum;
3949 break;
3950
3951 case '$': /* '$' - last line */
3952 ++cmd;
3953 lnum = curbuf->b_ml.ml_line_count;
3954 break;
3955
3956 case '\'': /* ''' - mark */
3957 if (*++cmd == NUL)
3958 {
3959 cmd = NULL;
3960 goto error;
3961 }
3962 if (skip)
3963 ++cmd;
3964 else
3965 {
3966 /* Only accept a mark in another file when it is
3967 * used by itself: ":'M". */
3968 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3969 ++cmd;
3970 if (fp == (pos_T *)-1)
3971 /* Jumped to another file. */
3972 lnum = curwin->w_cursor.lnum;
3973 else
3974 {
3975 if (check_mark(fp) == FAIL)
3976 {
3977 cmd = NULL;
3978 goto error;
3979 }
3980 lnum = fp->lnum;
3981 }
3982 }
3983 break;
3984
3985 case '/':
3986 case '?': /* '/' or '?' - search */
3987 c = *cmd++;
3988 if (skip) /* skip "/pat/" */
3989 {
3990 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3991 if (*cmd == c)
3992 ++cmd;
3993 }
3994 else
3995 {
3996 pos = curwin->w_cursor; /* save curwin->w_cursor */
3997 /*
3998 * When '/' or '?' follows another address, start
3999 * from there.
4000 */
4001 if (lnum != MAXLNUM)
4002 curwin->w_cursor.lnum = lnum;
4003 /*
4004 * Start a forward search at the end of the line.
4005 * Start a backward search at the start of the line.
4006 * This makes sure we never match in the current
4007 * line, and can match anywhere in the
4008 * next/previous line.
4009 */
4010 if (c == '/')
4011 curwin->w_cursor.col = MAXCOL;
4012 else
4013 curwin->w_cursor.col = 0;
4014 searchcmdlen = 0;
4015 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004016 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
4018 curwin->w_cursor = pos;
4019 cmd = NULL;
4020 goto error;
4021 }
4022 lnum = curwin->w_cursor.lnum;
4023 curwin->w_cursor = pos;
4024 /* adjust command string pointer */
4025 cmd += searchcmdlen;
4026 }
4027 break;
4028
4029 case '\\': /* "\?", "\/" or "\&", repeat search */
4030 ++cmd;
4031 if (*cmd == '&')
4032 i = RE_SUBST;
4033 else if (*cmd == '?' || *cmd == '/')
4034 i = RE_SEARCH;
4035 else
4036 {
4037 EMSG(_(e_backslash));
4038 cmd = NULL;
4039 goto error;
4040 }
4041
4042 if (!skip)
4043 {
4044 /*
4045 * When search follows another address, start from
4046 * there.
4047 */
4048 if (lnum != MAXLNUM)
4049 pos.lnum = lnum;
4050 else
4051 pos.lnum = curwin->w_cursor.lnum;
4052
4053 /*
4054 * Start the search just like for the above
4055 * do_search().
4056 */
4057 if (*cmd != '?')
4058 pos.col = MAXCOL;
4059 else
4060 pos.col = 0;
4061 if (searchit(curwin, curbuf, &pos,
4062 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004063 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004064 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 lnum = pos.lnum;
4066 else
4067 {
4068 cmd = NULL;
4069 goto error;
4070 }
4071 }
4072 ++cmd;
4073 break;
4074
4075 default:
4076 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4077 lnum = getdigits(&cmd);
4078 }
4079
4080 for (;;)
4081 {
4082 cmd = skipwhite(cmd);
4083 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4084 break;
4085
4086 if (lnum == MAXLNUM)
4087 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4088 if (VIM_ISDIGIT(*cmd))
4089 i = '+'; /* "number" is same as "+number" */
4090 else
4091 i = *cmd++;
4092 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4093 n = 1;
4094 else
4095 n = getdigits(&cmd);
4096 if (i == '-')
4097 lnum -= n;
4098 else
4099 lnum += n;
4100 }
4101 } while (*cmd == '/' || *cmd == '?');
4102
4103error:
4104 *ptr = cmd;
4105 return lnum;
4106}
4107
4108/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004109 * Get flags from an Ex command argument.
4110 */
4111 static void
4112get_flags(eap)
4113 exarg_T *eap;
4114{
4115 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4116 {
4117 if (*eap->arg == 'l')
4118 eap->flags |= EXFLAG_LIST;
4119 else if (*eap->arg == 'p')
4120 eap->flags |= EXFLAG_PRINT;
4121 else
4122 eap->flags |= EXFLAG_NR;
4123 eap->arg = skipwhite(eap->arg + 1);
4124 }
4125}
4126
4127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 * Function called for command which is Not Implemented. NI!
4129 */
4130 void
4131ex_ni(eap)
4132 exarg_T *eap;
4133{
4134 if (!eap->skip)
4135 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4136}
4137
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004138#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139/*
4140 * Function called for script command which is Not Implemented. NI!
4141 * Skips over ":perl <<EOF" constructs.
4142 */
4143 static void
4144ex_script_ni(eap)
4145 exarg_T *eap;
4146{
4147 if (!eap->skip)
4148 ex_ni(eap);
4149 else
4150 vim_free(script_get(eap, eap->arg));
4151}
4152#endif
4153
4154/*
4155 * Check range in Ex command for validity.
4156 * Return NULL when valid, error message when invalid.
4157 */
4158 static char_u *
4159invalid_range(eap)
4160 exarg_T *eap;
4161{
4162 if ( eap->line1 < 0
4163 || eap->line2 < 0
4164 || eap->line1 > eap->line2
4165 || ((eap->argt & RANGE)
4166 && !(eap->argt & NOTADR)
4167 && eap->line2 > curbuf->b_ml.ml_line_count
4168#ifdef FEAT_DIFF
4169 + (eap->cmdidx == CMD_diffget)
4170#endif
4171 ))
4172 return (char_u *)_(e_invrange);
4173 return NULL;
4174}
4175
4176/*
4177 * Correct the range for zero line number, if required.
4178 */
4179 static void
4180correct_range(eap)
4181 exarg_T *eap;
4182{
4183 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4184 {
4185 if (eap->line1 == 0)
4186 eap->line1 = 1;
4187 if (eap->line2 == 0)
4188 eap->line2 = 1;
4189 }
4190}
4191
Bram Moolenaar748bf032005-02-02 23:04:36 +00004192#ifdef FEAT_QUICKFIX
4193static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4194
4195/*
4196 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4197 * pattern. Otherwise return eap->arg.
4198 */
4199 static char_u *
4200skip_grep_pat(eap)
4201 exarg_T *eap;
4202{
4203 char_u *p = eap->arg;
4204
Bram Moolenaara37420f2006-02-04 22:37:47 +00004205 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4206 || eap->cmdidx == CMD_vimgrepadd
4207 || eap->cmdidx == CMD_lvimgrepadd
4208 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004209 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004210 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004211 if (p == NULL)
4212 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004213 }
4214 return p;
4215}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004216
4217/*
4218 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4219 * in the command line, so that things like % get expanded.
4220 */
4221 static char_u *
4222replace_makeprg(eap, p, cmdlinep)
4223 exarg_T *eap;
4224 char_u *p;
4225 char_u **cmdlinep;
4226{
4227 char_u *new_cmdline;
4228 char_u *program;
4229 char_u *pos;
4230 char_u *ptr;
4231 int len;
4232 int i;
4233
4234 /*
4235 * Don't do it when ":vimgrep" is used for ":grep".
4236 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004237 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4238 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4239 || eap->cmdidx == CMD_grepadd
4240 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004241 && !grep_internal(eap->cmdidx))
4242 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004243 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4244 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004245 {
4246 if (*curbuf->b_p_gp == NUL)
4247 program = p_gp;
4248 else
4249 program = curbuf->b_p_gp;
4250 }
4251 else
4252 {
4253 if (*curbuf->b_p_mp == NUL)
4254 program = p_mp;
4255 else
4256 program = curbuf->b_p_mp;
4257 }
4258
4259 p = skipwhite(p);
4260
4261 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4262 {
4263 /* replace $* by given arguments */
4264 i = 1;
4265 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4266 ++i;
4267 len = (int)STRLEN(p);
4268 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4269 if (new_cmdline == NULL)
4270 return NULL; /* out of memory */
4271 ptr = new_cmdline;
4272 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4273 {
4274 i = (int)(pos - program);
4275 STRNCPY(ptr, program, i);
4276 STRCPY(ptr += i, p);
4277 ptr += len;
4278 program = pos + 2;
4279 }
4280 STRCPY(ptr, program);
4281 }
4282 else
4283 {
4284 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4285 if (new_cmdline == NULL)
4286 return NULL; /* out of memory */
4287 STRCPY(new_cmdline, program);
4288 STRCAT(new_cmdline, " ");
4289 STRCAT(new_cmdline, p);
4290 }
4291 msg_make(p);
4292
4293 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4294 vim_free(*cmdlinep);
4295 *cmdlinep = new_cmdline;
4296 p = new_cmdline;
4297 }
4298 return p;
4299}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004300#endif
4301
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302/*
4303 * Expand file name in Ex command argument.
4304 * Return FAIL for failure, OK otherwise.
4305 */
4306 int
4307expand_filename(eap, cmdlinep, errormsgp)
4308 exarg_T *eap;
4309 char_u **cmdlinep;
4310 char_u **errormsgp;
4311{
4312 int has_wildcards; /* need to expand wildcards */
4313 char_u *repl;
4314 int srclen;
4315 char_u *p;
4316 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004317 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
Bram Moolenaar748bf032005-02-02 23:04:36 +00004319#ifdef FEAT_QUICKFIX
4320 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4321 p = skip_grep_pat(eap);
4322#else
4323 p = eap->arg;
4324#endif
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 /*
4327 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4328 * the file name contains a wildcard it should not cause expanding.
4329 * (it will be expanded anyway if there is a wildcard before replacing).
4330 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004331 has_wildcards = mch_has_wildcard(p);
4332 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004334#ifdef FEAT_EVAL
4335 /* Skip over `=expr`, wildcards in it are not expanded. */
4336 if (p[0] == '`' && p[1] == '=')
4337 {
4338 p += 2;
4339 (void)skip_expr(&p);
4340 if (*p == '`')
4341 ++p;
4342 continue;
4343 }
4344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 /*
4346 * Quick check if this cannot be the start of a special string.
4347 * Also removes backslash before '%', '#' and '<'.
4348 */
4349 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4350 {
4351 ++p;
4352 continue;
4353 }
4354
4355 /*
4356 * Try to find a match at this position.
4357 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004358 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4359 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 if (*errormsgp != NULL) /* error detected */
4361 return FAIL;
4362 if (repl == NULL) /* no match found */
4363 {
4364 p += srclen;
4365 continue;
4366 }
4367
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004368 /* Wildcards won't be expanded below, the replacement is taken
4369 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4370 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4371 {
4372 char_u *l = repl;
4373
4374 repl = expand_env_save(repl);
4375 vim_free(l);
4376 }
4377
Bram Moolenaar63b92542007-03-27 14:57:09 +00004378 /* Need to escape white space et al. with a backslash.
4379 * Don't do this for:
4380 * - replacement that already has been escaped: "##"
4381 * - shell commands (may have to use quotes instead).
4382 * - non-unix systems when there is a single argument (spaces don't
4383 * separate arguments then).
4384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004386 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 && eap->cmdidx != CMD_bang
4388 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004389 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004391 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004393 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394#ifndef UNIX
4395 && !(eap->argt & NOSPC)
4396#endif
4397 )
4398 {
4399 char_u *l;
4400#ifdef BACKSLASH_IN_FILENAME
4401 /* Don't escape a backslash here, because rem_backslash() doesn't
4402 * remove it later. */
4403 static char_u *nobslash = (char_u *)" \t\"|";
4404# define ESCAPE_CHARS nobslash
4405#else
4406# define ESCAPE_CHARS escape_chars
4407#endif
4408
4409 for (l = repl; *l; ++l)
4410 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4411 {
4412 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4413 if (l != NULL)
4414 {
4415 vim_free(repl);
4416 repl = l;
4417 }
4418 break;
4419 }
4420 }
4421
4422 /* For a shell command a '!' must be escaped. */
4423 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004424 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
4426 char_u *l;
4427
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004428 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 if (l != NULL)
4430 {
4431 vim_free(repl);
4432 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004433 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 if (strstr((char *)p_sh, "sh") != NULL)
4435 {
4436 l = vim_strsave_escaped(repl, (char_u *)"!");
4437 if (l != NULL)
4438 {
4439 vim_free(repl);
4440 repl = l;
4441 }
4442 }
4443 }
4444 }
4445
4446 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4447 vim_free(repl);
4448 if (p == NULL)
4449 return FAIL;
4450 }
4451
4452 /*
4453 * One file argument: Expand wildcards.
4454 * Don't do this with ":r !command" or ":w !command".
4455 */
4456 if ((eap->argt & NOSPC) && !eap->usefilter)
4457 {
4458 /*
4459 * May do this twice:
4460 * 1. Replace environment variables.
4461 * 2. Replace any other wildcards, remove backslashes.
4462 */
4463 for (n = 1; n <= 2; ++n)
4464 {
4465 if (n == 2)
4466 {
4467#ifdef UNIX
4468 /*
4469 * Only for Unix we check for more than one file name.
4470 * For other systems spaces are considered to be part
4471 * of the file name.
4472 * Only check here if there is no wildcard, otherwise
4473 * ExpandOne() will check for errors. This allows
4474 * ":e `ls ve*.c`" on Unix.
4475 */
4476 if (!has_wildcards)
4477 for (p = eap->arg; *p; ++p)
4478 {
4479 /* skip escaped characters */
4480 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4481 ++p;
4482 else if (vim_iswhite(*p))
4483 {
4484 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4485 return FAIL;
4486 }
4487 }
4488#endif
4489
4490 /*
4491 * Halve the number of backslashes (this is Vi compatible).
4492 * For Unix and OS/2, when wildcards are expanded, this is
4493 * done by ExpandOne() below.
4494 */
4495#if defined(UNIX) || defined(OS2)
4496 if (!has_wildcards)
4497#endif
4498 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500
4501 if (has_wildcards)
4502 {
4503 if (n == 1)
4504 {
4505 /*
4506 * First loop: May expand environment variables. This
4507 * can be done much faster with expand_env() than with
4508 * something else (e.g., calling a shell).
4509 * After expanding environment variables, check again
4510 * if there are still wildcards present.
4511 */
4512 if (vim_strchr(eap->arg, '$') != NULL
4513 || vim_strchr(eap->arg, '~') != NULL)
4514 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004515 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004516 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 has_wildcards = mch_has_wildcard(NameBuff);
4518 p = NameBuff;
4519 }
4520 else
4521 p = NULL;
4522 }
4523 else /* n == 2 */
4524 {
4525 expand_T xpc;
4526
4527 ExpandInit(&xpc);
4528 xpc.xp_context = EXPAND_FILES;
4529 p = ExpandOne(&xpc, eap->arg, NULL,
4530 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4531 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 if (p == NULL)
4533 return FAIL;
4534 }
4535 if (p != NULL)
4536 {
4537 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4538 p, cmdlinep);
4539 if (n == 2) /* p came from ExpandOne() */
4540 vim_free(p);
4541 }
4542 }
4543 }
4544 }
4545 return OK;
4546}
4547
4548/*
4549 * Replace part of the command line, keeping eap->cmd, eap->arg and
4550 * eap->nextcmd correct.
4551 * "src" points to the part that is to be replaced, of length "srclen".
4552 * "repl" is the replacement string.
4553 * Returns a pointer to the character after the replaced string.
4554 * Returns NULL for failure.
4555 */
4556 static char_u *
4557repl_cmdline(eap, src, srclen, repl, cmdlinep)
4558 exarg_T *eap;
4559 char_u *src;
4560 int srclen;
4561 char_u *repl;
4562 char_u **cmdlinep;
4563{
4564 int len;
4565 int i;
4566 char_u *new_cmdline;
4567
4568 /*
4569 * The new command line is build in new_cmdline[].
4570 * First allocate it.
4571 * Careful: a "+cmd" argument may have been NUL terminated.
4572 */
4573 len = (int)STRLEN(repl);
4574 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004575 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4577 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4578 return NULL; /* out of memory! */
4579
4580 /*
4581 * Copy the stuff before the expanded part.
4582 * Copy the expanded stuff.
4583 * Copy what came after the expanded part.
4584 * Copy the next commands, if there are any.
4585 */
4586 i = (int)(src - *cmdlinep); /* length of part before match */
4587 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004588
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 mch_memmove(new_cmdline + i, repl, (size_t)len);
4590 i += len; /* remember the end of the string */
4591 STRCPY(new_cmdline + i, src + srclen);
4592 src = new_cmdline + i; /* remember where to continue */
4593
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004594 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {
4596 i = (int)STRLEN(new_cmdline) + 1;
4597 STRCPY(new_cmdline + i, eap->nextcmd);
4598 eap->nextcmd = new_cmdline + i;
4599 }
4600 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4601 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4602 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4603 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4604 vim_free(*cmdlinep);
4605 *cmdlinep = new_cmdline;
4606
4607 return src;
4608}
4609
4610/*
4611 * Check for '|' to separate commands and '"' to start comments.
4612 */
4613 void
4614separate_nextcmd(eap)
4615 exarg_T *eap;
4616{
4617 char_u *p;
4618
Bram Moolenaar86b68352004-12-27 21:59:20 +00004619#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004620 p = skip_grep_pat(eap);
4621#else
4622 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004623#endif
4624
4625 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
4627 if (*p == Ctrl_V)
4628 {
4629 if (eap->argt & (USECTRLV | XFILE))
4630 ++p; /* skip CTRL-V and next char */
4631 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004632 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004633 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if (*p == NUL) /* stop at NUL after CTRL-V */
4635 break;
4636 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004637
4638#ifdef FEAT_EVAL
4639 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004640 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004641 {
4642 p += 2;
4643 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004644 }
4645#endif
4646
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 /* Check for '"': start of comment or '|': next command */
4648 /* :@" and :*" do not start a comment!
4649 * :redir @" doesn't either. */
4650 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4651 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4652 || p != eap->arg)
4653 && (eap->cmdidx != CMD_redir
4654 || p != eap->arg + 1 || p[-1] != '@'))
4655 || *p == '|' || *p == '\n')
4656 {
4657 /*
4658 * We remove the '\' before the '|', unless USECTRLV is used
4659 * AND 'b' is present in 'cpoptions'.
4660 */
4661 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4662 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4663 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004664 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 --p;
4666 }
4667 else
4668 {
4669 eap->nextcmd = check_nextcmd(p);
4670 *p = NUL;
4671 break;
4672 }
4673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4677 del_trailing_spaces(eap->arg);
4678}
4679
4680/*
4681 * get + command from ex argument
4682 */
4683 static char_u *
4684getargcmd(argp)
4685 char_u **argp;
4686{
4687 char_u *arg = *argp;
4688 char_u *command = NULL;
4689
4690 if (*arg == '+') /* +[command] */
4691 {
4692 ++arg;
4693 if (vim_isspace(*arg))
4694 command = dollar_command;
4695 else
4696 {
4697 command = arg;
4698 arg = skip_cmd_arg(command, TRUE);
4699 if (*arg != NUL)
4700 *arg++ = NUL; /* terminate command with NUL */
4701 }
4702
4703 arg = skipwhite(arg); /* skip over spaces */
4704 *argp = arg;
4705 }
4706 return command;
4707}
4708
4709/*
4710 * Find end of "+command" argument. Skip over "\ " and "\\".
4711 */
4712 static char_u *
4713skip_cmd_arg(p, rembs)
4714 char_u *p;
4715 int rembs; /* TRUE to halve the number of backslashes */
4716{
4717 while (*p && !vim_isspace(*p))
4718 {
4719 if (*p == '\\' && p[1] != NUL)
4720 {
4721 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004722 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 else
4724 ++p;
4725 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004726 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
4728 return p;
4729}
4730
4731/*
4732 * Get "++opt=arg" argument.
4733 * Return FAIL or OK.
4734 */
4735 static int
4736getargopt(eap)
4737 exarg_T *eap;
4738{
4739 char_u *arg = eap->arg + 2;
4740 int *pp = NULL;
4741#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004742 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 char_u *p;
4744#endif
4745
4746 /* ":edit ++[no]bin[ary] file" */
4747 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4748 {
4749 if (*arg == 'n')
4750 {
4751 arg += 2;
4752 eap->force_bin = FORCE_NOBIN;
4753 }
4754 else
4755 eap->force_bin = FORCE_BIN;
4756 if (!checkforcmd(&arg, "binary", 3))
4757 return FAIL;
4758 eap->arg = skipwhite(arg);
4759 return OK;
4760 }
4761
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004762 /* ":read ++edit file" */
4763 if (STRNCMP(arg, "edit", 4) == 0)
4764 {
4765 eap->read_edit = TRUE;
4766 eap->arg = skipwhite(arg + 4);
4767 return OK;
4768 }
4769
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (STRNCMP(arg, "ff", 2) == 0)
4771 {
4772 arg += 2;
4773 pp = &eap->force_ff;
4774 }
4775 else if (STRNCMP(arg, "fileformat", 10) == 0)
4776 {
4777 arg += 10;
4778 pp = &eap->force_ff;
4779 }
4780#ifdef FEAT_MBYTE
4781 else if (STRNCMP(arg, "enc", 3) == 0)
4782 {
4783 arg += 3;
4784 pp = &eap->force_enc;
4785 }
4786 else if (STRNCMP(arg, "encoding", 8) == 0)
4787 {
4788 arg += 8;
4789 pp = &eap->force_enc;
4790 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004791 else if (STRNCMP(arg, "bad", 3) == 0)
4792 {
4793 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004794 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796#endif
4797
4798 if (pp == NULL || *arg != '=')
4799 return FAIL;
4800
4801 ++arg;
4802 *pp = (int)(arg - eap->cmd);
4803 arg = skip_cmd_arg(arg, FALSE);
4804 eap->arg = skipwhite(arg);
4805 *arg = NUL;
4806
4807#ifdef FEAT_MBYTE
4808 if (pp == &eap->force_ff)
4809 {
4810#endif
4811 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4812 return FAIL;
4813#ifdef FEAT_MBYTE
4814 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004815 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
4817 /* Make 'fileencoding' lower case. */
4818 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4819 *p = TOLOWER_ASC(*p);
4820 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004821 else
4822 {
4823 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4824 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004825 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004826 if (STRICMP(p, "keep") == 0)
4827 eap->bad_char = BAD_KEEP;
4828 else if (STRICMP(p, "drop") == 0)
4829 eap->bad_char = BAD_DROP;
4830 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4831 eap->bad_char = *p;
4832 else
4833 return FAIL;
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835#endif
4836
4837 return OK;
4838}
4839
4840/*
4841 * ":abbreviate" and friends.
4842 */
4843 static void
4844ex_abbreviate(eap)
4845 exarg_T *eap;
4846{
4847 do_exmap(eap, TRUE); /* almost the same as mapping */
4848}
4849
4850/*
4851 * ":map" and friends.
4852 */
4853 static void
4854ex_map(eap)
4855 exarg_T *eap;
4856{
4857 /*
4858 * If we are sourcing .exrc or .vimrc in current directory we
4859 * print the mappings for security reasons.
4860 */
4861 if (secure)
4862 {
4863 secure = 2;
4864 msg_outtrans(eap->cmd);
4865 msg_putchar('\n');
4866 }
4867 do_exmap(eap, FALSE);
4868}
4869
4870/*
4871 * ":unmap" and friends.
4872 */
4873 static void
4874ex_unmap(eap)
4875 exarg_T *eap;
4876{
4877 do_exmap(eap, FALSE);
4878}
4879
4880/*
4881 * ":mapclear" and friends.
4882 */
4883 static void
4884ex_mapclear(eap)
4885 exarg_T *eap;
4886{
4887 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4888}
4889
4890/*
4891 * ":abclear" and friends.
4892 */
4893 static void
4894ex_abclear(eap)
4895 exarg_T *eap;
4896{
4897 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4898}
4899
4900#ifdef FEAT_AUTOCMD
4901 static void
4902ex_autocmd(eap)
4903 exarg_T *eap;
4904{
4905 /*
4906 * Disallow auto commands from .exrc and .vimrc in current
4907 * directory for security reasons.
4908 */
4909 if (secure)
4910 {
4911 secure = 2;
4912 eap->errmsg = e_curdir;
4913 }
4914 else if (eap->cmdidx == CMD_autocmd)
4915 do_autocmd(eap->arg, eap->forceit);
4916 else
4917 do_augroup(eap->arg, eap->forceit);
4918}
4919
4920/*
4921 * ":doautocmd": Apply the automatic commands to the current buffer.
4922 */
4923 static void
4924ex_doautocmd(eap)
4925 exarg_T *eap;
4926{
4927 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004928 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929}
4930#endif
4931
4932#ifdef FEAT_LISTCMDS
4933/*
4934 * :[N]bunload[!] [N] [bufname] unload buffer
4935 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4936 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4937 */
4938 static void
4939ex_bunload(eap)
4940 exarg_T *eap;
4941{
4942 eap->errmsg = do_bufdel(
4943 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4944 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4945 : DOBUF_UNLOAD, eap->arg,
4946 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4947}
4948
4949/*
4950 * :[N]buffer [N] to buffer N
4951 * :[N]sbuffer [N] to buffer N
4952 */
4953 static void
4954ex_buffer(eap)
4955 exarg_T *eap;
4956{
4957 if (*eap->arg)
4958 eap->errmsg = e_trailing;
4959 else
4960 {
4961 if (eap->addr_count == 0) /* default is current buffer */
4962 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4963 else
4964 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4965 }
4966}
4967
4968/*
4969 * :[N]bmodified [N] to next mod. buffer
4970 * :[N]sbmodified [N] to next mod. buffer
4971 */
4972 static void
4973ex_bmodified(eap)
4974 exarg_T *eap;
4975{
4976 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4977}
4978
4979/*
4980 * :[N]bnext [N] to next buffer
4981 * :[N]sbnext [N] split and to next buffer
4982 */
4983 static void
4984ex_bnext(eap)
4985 exarg_T *eap;
4986{
4987 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4988}
4989
4990/*
4991 * :[N]bNext [N] to previous buffer
4992 * :[N]bprevious [N] to previous buffer
4993 * :[N]sbNext [N] split and to previous buffer
4994 * :[N]sbprevious [N] split and to previous buffer
4995 */
4996 static void
4997ex_bprevious(eap)
4998 exarg_T *eap;
4999{
5000 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
5001}
5002
5003/*
5004 * :brewind to first buffer
5005 * :bfirst to first buffer
5006 * :sbrewind split and to first buffer
5007 * :sbfirst split and to first buffer
5008 */
5009 static void
5010ex_brewind(eap)
5011 exarg_T *eap;
5012{
5013 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5014}
5015
5016/*
5017 * :blast to last buffer
5018 * :sblast split and to last buffer
5019 */
5020 static void
5021ex_blast(eap)
5022 exarg_T *eap;
5023{
5024 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5025}
5026#endif
5027
5028 int
5029ends_excmd(c)
5030 int c;
5031{
5032 return (c == NUL || c == '|' || c == '"' || c == '\n');
5033}
5034
5035#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5036 || defined(PROTO)
5037/*
5038 * Return the next command, after the first '|' or '\n'.
5039 * Return NULL if not found.
5040 */
5041 char_u *
5042find_nextcmd(p)
5043 char_u *p;
5044{
5045 while (*p != '|' && *p != '\n')
5046 {
5047 if (*p == NUL)
5048 return NULL;
5049 ++p;
5050 }
5051 return (p + 1);
5052}
5053#endif
5054
5055/*
5056 * Check if *p is a separator between Ex commands.
5057 * Return NULL if it isn't, (p + 1) if it is.
5058 */
5059 char_u *
5060check_nextcmd(p)
5061 char_u *p;
5062{
5063 p = skipwhite(p);
5064 if (*p == '|' || *p == '\n')
5065 return (p + 1);
5066 else
5067 return NULL;
5068}
5069
5070/*
5071 * - if there are more files to edit
5072 * - and this is the last window
5073 * - and forceit not used
5074 * - and not repeated twice on a row
5075 * return FAIL and give error message if 'message' TRUE
5076 * return OK otherwise
5077 */
5078 static int
5079check_more(message, forceit)
5080 int message; /* when FALSE check only, no messages */
5081 int forceit;
5082{
5083 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5084
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005085 if (!forceit && only_one_window()
5086 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
5088 if (message)
5089 {
5090#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5091 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5092 {
5093 char_u buff[IOSIZE];
5094
5095 if (n == 1)
5096 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5097 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005098 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 _("%d more files to edit. Quit anyway?"), n);
5100 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5101 return OK;
5102 return FAIL;
5103 }
5104#endif
5105 if (n == 1)
5106 EMSG(_("E173: 1 more file to edit"));
5107 else
5108 EMSGN(_("E173: %ld more files to edit"), n);
5109 quitmore = 2; /* next try to quit is allowed */
5110 }
5111 return FAIL;
5112 }
5113 return OK;
5114}
5115
5116#ifdef FEAT_CMDL_COMPL
5117/*
5118 * Function given to ExpandGeneric() to obtain the list of command names.
5119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 char_u *
5121get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005122 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 int idx;
5124{
5125 if (idx >= (int)CMD_SIZE)
5126# ifdef FEAT_USR_CMDS
5127 return get_user_command_name(idx);
5128# else
5129 return NULL;
5130# endif
5131 return cmdnames[idx].cmd_name;
5132}
5133#endif
5134
5135#if defined(FEAT_USR_CMDS) || defined(PROTO)
5136static 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));
5137static void uc_list __ARGS((char_u *name, size_t name_len));
5138static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5139static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5140static 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));
5141
5142 static int
5143uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5144 char_u *name;
5145 size_t name_len;
5146 char_u *rep;
5147 long argt;
5148 long def;
5149 int flags;
5150 int compl;
5151 char_u *compl_arg;
5152 int force;
5153{
5154 ucmd_T *cmd = NULL;
5155 char_u *p;
5156 int i;
5157 int cmp = 1;
5158 char_u *rep_buf = NULL;
5159 garray_T *gap;
5160
Bram Moolenaar9c102382006-05-03 21:26:49 +00005161 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 if (rep_buf == NULL)
5163 {
5164 /* Can't replace termcodes - try using the string as is */
5165 rep_buf = vim_strsave(rep);
5166
5167 /* Give up if out of memory */
5168 if (rep_buf == NULL)
5169 return FAIL;
5170 }
5171
5172 /* get address of growarray: global or in curbuf */
5173 if (flags & UC_BUFFER)
5174 {
5175 gap = &curbuf->b_ucmds;
5176 if (gap->ga_itemsize == 0)
5177 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5178 }
5179 else
5180 gap = &ucmds;
5181
5182 /* Search for the command in the already defined commands. */
5183 for (i = 0; i < gap->ga_len; ++i)
5184 {
5185 size_t len;
5186
5187 cmd = USER_CMD_GA(gap, i);
5188 len = STRLEN(cmd->uc_name);
5189 cmp = STRNCMP(name, cmd->uc_name, name_len);
5190 if (cmp == 0)
5191 {
5192 if (name_len < len)
5193 cmp = -1;
5194 else if (name_len > len)
5195 cmp = 1;
5196 }
5197
5198 if (cmp == 0)
5199 {
5200 if (!force)
5201 {
5202 EMSG(_("E174: Command already exists: add ! to replace it"));
5203 goto fail;
5204 }
5205
5206 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005207 cmd->uc_rep = NULL;
5208#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5209 vim_free(cmd->uc_compl_arg);
5210 cmd->uc_compl_arg = NULL;
5211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
5213 }
5214
5215 /* Stop as soon as we pass the name to add */
5216 if (cmp < 0)
5217 break;
5218 }
5219
5220 /* Extend the array unless we're replacing an existing command */
5221 if (cmp != 0)
5222 {
5223 if (ga_grow(gap, 1) != OK)
5224 goto fail;
5225 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5226 goto fail;
5227
5228 cmd = USER_CMD_GA(gap, i);
5229 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5230
5231 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
5233 cmd->uc_name = p;
5234 }
5235
5236 cmd->uc_rep = rep_buf;
5237 cmd->uc_argt = argt;
5238 cmd->uc_def = def;
5239 cmd->uc_compl = compl;
5240#ifdef FEAT_EVAL
5241 cmd->uc_scriptID = current_SID;
5242# ifdef FEAT_CMDL_COMPL
5243 cmd->uc_compl_arg = compl_arg;
5244# endif
5245#endif
5246
5247 return OK;
5248
5249fail:
5250 vim_free(rep_buf);
5251#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5252 vim_free(compl_arg);
5253#endif
5254 return FAIL;
5255}
5256
5257/*
5258 * List of names for completion for ":command" with the EXPAND_ flag.
5259 * Must be alphabetical for completion.
5260 */
5261static struct
5262{
5263 int expand;
5264 char *name;
5265} command_complete[] =
5266{
5267 {EXPAND_AUGROUP, "augroup"},
5268 {EXPAND_BUFFERS, "buffer"},
5269 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005270#if defined(FEAT_CSCOPE)
5271 {EXPAND_CSCOPE, "cscope"},
5272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5274 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005275 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#endif
5277 {EXPAND_DIRECTORIES, "dir"},
5278 {EXPAND_ENV_VARS, "environment"},
5279 {EXPAND_EVENTS, "event"},
5280 {EXPAND_EXPRESSION, "expression"},
5281 {EXPAND_FILES, "file"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005282 {EXPAND_FILETYPE, "filetype"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 {EXPAND_FUNCTIONS, "function"},
5284 {EXPAND_HELP, "help"},
5285 {EXPAND_HIGHLIGHT, "highlight"},
5286 {EXPAND_MAPPINGS, "mapping"},
5287 {EXPAND_MENUS, "menu"},
Bram Moolenaara26559b2010-07-31 14:59:19 +02005288 {EXPAND_OWNSYNTAX, "syntax"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005290 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005291#if defined(FEAT_SIGNS)
5292 {EXPAND_SIGN, "sign"},
5293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 {EXPAND_TAGS, "tag"},
5295 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5296 {EXPAND_USER_VARS, "var"},
5297 {0, NULL}
5298};
5299
5300 static void
5301uc_list(name, name_len)
5302 char_u *name;
5303 size_t name_len;
5304{
5305 int i, j;
5306 int found = FALSE;
5307 ucmd_T *cmd;
5308 int len;
5309 long a;
5310 garray_T *gap;
5311
5312 gap = &curbuf->b_ucmds;
5313 for (;;)
5314 {
5315 for (i = 0; i < gap->ga_len; ++i)
5316 {
5317 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005318 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 /* Skip commands which don't match the requested prefix */
5321 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5322 continue;
5323
5324 /* Put out the title first time */
5325 if (!found)
5326 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5327 found = TRUE;
5328 msg_putchar('\n');
5329 if (got_int)
5330 break;
5331
5332 /* Special cases */
5333 msg_putchar(a & BANG ? '!' : ' ');
5334 msg_putchar(a & REGSTR ? '"' : ' ');
5335 msg_putchar(gap != &ucmds ? 'b' : ' ');
5336 msg_putchar(' ');
5337
5338 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5339 len = (int)STRLEN(cmd->uc_name) + 4;
5340
5341 do {
5342 msg_putchar(' ');
5343 ++len;
5344 } while (len < 16);
5345
5346 len = 0;
5347
5348 /* Arguments */
5349 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5350 {
5351 case 0: IObuff[len++] = '0'; break;
5352 case (EXTRA): IObuff[len++] = '*'; break;
5353 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5354 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5355 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5356 }
5357
5358 do {
5359 IObuff[len++] = ' ';
5360 } while (len < 5);
5361
5362 /* Range */
5363 if (a & (RANGE|COUNT))
5364 {
5365 if (a & COUNT)
5366 {
5367 /* -count=N */
5368 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5369 len += (int)STRLEN(IObuff + len);
5370 }
5371 else if (a & DFLALL)
5372 IObuff[len++] = '%';
5373 else if (cmd->uc_def >= 0)
5374 {
5375 /* -range=N */
5376 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5377 len += (int)STRLEN(IObuff + len);
5378 }
5379 else
5380 IObuff[len++] = '.';
5381 }
5382
5383 do {
5384 IObuff[len++] = ' ';
5385 } while (len < 11);
5386
5387 /* Completion */
5388 for (j = 0; command_complete[j].expand != 0; ++j)
5389 if (command_complete[j].expand == cmd->uc_compl)
5390 {
5391 STRCPY(IObuff + len, command_complete[j].name);
5392 len += (int)STRLEN(IObuff + len);
5393 break;
5394 }
5395
5396 do {
5397 IObuff[len++] = ' ';
5398 } while (len < 21);
5399
5400 IObuff[len] = '\0';
5401 msg_outtrans(IObuff);
5402
5403 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005404#ifdef FEAT_EVAL
5405 if (p_verbose > 0)
5406 last_set_msg(cmd->uc_scriptID);
5407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 out_flush();
5409 ui_breakcheck();
5410 if (got_int)
5411 break;
5412 }
5413 if (gap == &ucmds || i < gap->ga_len)
5414 break;
5415 gap = &ucmds;
5416 }
5417
5418 if (!found)
5419 MSG(_("No user-defined commands found"));
5420}
5421
5422 static char_u *
5423uc_fun_cmd()
5424{
5425 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5426 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5427 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5428 0xb9, 0x7f, 0};
5429 int i;
5430
5431 for (i = 0; fcmd[i]; ++i)
5432 IObuff[i] = fcmd[i] - 0x40;
5433 IObuff[i] = 0;
5434 return IObuff;
5435}
5436
5437 static int
5438uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5439 char_u *attr;
5440 size_t len;
5441 long *argt;
5442 long *def;
5443 int *flags;
5444 int *compl;
5445 char_u **compl_arg;
5446{
5447 char_u *p;
5448
5449 if (len == 0)
5450 {
5451 EMSG(_("E175: No attribute specified"));
5452 return FAIL;
5453 }
5454
5455 /* First, try the simple attributes (no arguments) */
5456 if (STRNICMP(attr, "bang", len) == 0)
5457 *argt |= BANG;
5458 else if (STRNICMP(attr, "buffer", len) == 0)
5459 *flags |= UC_BUFFER;
5460 else if (STRNICMP(attr, "register", len) == 0)
5461 *argt |= REGSTR;
5462 else if (STRNICMP(attr, "bar", len) == 0)
5463 *argt |= TRLBAR;
5464 else
5465 {
5466 int i;
5467 char_u *val = NULL;
5468 size_t vallen = 0;
5469 size_t attrlen = len;
5470
5471 /* Look for the attribute name - which is the part before any '=' */
5472 for (i = 0; i < (int)len; ++i)
5473 {
5474 if (attr[i] == '=')
5475 {
5476 val = &attr[i + 1];
5477 vallen = len - i - 1;
5478 attrlen = i;
5479 break;
5480 }
5481 }
5482
5483 if (STRNICMP(attr, "nargs", attrlen) == 0)
5484 {
5485 if (vallen == 1)
5486 {
5487 if (*val == '0')
5488 /* Do nothing - this is the default */;
5489 else if (*val == '1')
5490 *argt |= (EXTRA | NOSPC | NEEDARG);
5491 else if (*val == '*')
5492 *argt |= EXTRA;
5493 else if (*val == '?')
5494 *argt |= (EXTRA | NOSPC);
5495 else if (*val == '+')
5496 *argt |= (EXTRA | NEEDARG);
5497 else
5498 goto wrong_nargs;
5499 }
5500 else
5501 {
5502wrong_nargs:
5503 EMSG(_("E176: Invalid number of arguments"));
5504 return FAIL;
5505 }
5506 }
5507 else if (STRNICMP(attr, "range", attrlen) == 0)
5508 {
5509 *argt |= RANGE;
5510 if (vallen == 1 && *val == '%')
5511 *argt |= DFLALL;
5512 else if (val != NULL)
5513 {
5514 p = val;
5515 if (*def >= 0)
5516 {
5517two_count:
5518 EMSG(_("E177: Count cannot be specified twice"));
5519 return FAIL;
5520 }
5521
5522 *def = getdigits(&p);
5523 *argt |= (ZEROR | NOTADR);
5524
5525 if (p != val + vallen || vallen == 0)
5526 {
5527invalid_count:
5528 EMSG(_("E178: Invalid default value for count"));
5529 return FAIL;
5530 }
5531 }
5532 }
5533 else if (STRNICMP(attr, "count", attrlen) == 0)
5534 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005535 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536
5537 if (val != NULL)
5538 {
5539 p = val;
5540 if (*def >= 0)
5541 goto two_count;
5542
5543 *def = getdigits(&p);
5544
5545 if (p != val + vallen)
5546 goto invalid_count;
5547 }
5548
5549 if (*def < 0)
5550 *def = 0;
5551 }
5552 else if (STRNICMP(attr, "complete", attrlen) == 0)
5553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 if (val == NULL)
5555 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005556 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 return FAIL;
5558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005560 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5561 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564 else
5565 {
5566 char_u ch = attr[len];
5567 attr[len] = '\0';
5568 EMSG2(_("E181: Invalid attribute: %s"), attr);
5569 attr[len] = ch;
5570 return FAIL;
5571 }
5572 }
5573
5574 return OK;
5575}
5576
Bram Moolenaara850a712009-01-28 14:42:59 +00005577/*
5578 * ":command ..."
5579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 static void
5581ex_command(eap)
5582 exarg_T *eap;
5583{
5584 char_u *name;
5585 char_u *end;
5586 char_u *p;
5587 long argt = 0;
5588 long def = -1;
5589 int flags = 0;
5590 int compl = EXPAND_NOTHING;
5591 char_u *compl_arg = NULL;
5592 int has_attr = (eap->arg[0] == '-');
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005593 int name_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
5595 p = eap->arg;
5596
5597 /* Check for attributes */
5598 while (*p == '-')
5599 {
5600 ++p;
5601 end = skiptowhite(p);
5602 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5603 == FAIL)
5604 return;
5605 p = skipwhite(end);
5606 }
5607
5608 /* Get the name (if any) and skip to the following argument */
5609 name = p;
5610 if (ASCII_ISALPHA(*p))
5611 while (ASCII_ISALNUM(*p))
5612 ++p;
5613 if (!ends_excmd(*p) && !vim_iswhite(*p))
5614 {
5615 EMSG(_("E182: Invalid command name"));
5616 return;
5617 }
5618 end = p;
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005619 name_len = (int)(end - name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
5621 /* If there is nothing after the name, and no attributes were specified,
5622 * we are listing commands
5623 */
5624 p = skipwhite(end);
5625 if (!has_attr && ends_excmd(*p))
5626 {
5627 uc_list(name, end - name);
5628 }
5629 else if (!ASCII_ISUPPER(*name))
5630 {
5631 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5632 return;
5633 }
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01005634 else if ((name_len == 1 && *name == 'X')
5635 || (name_len <= 4
5636 && STRNCMP(name, "Next", name_len > 4 ? 4 : name_len) == 0))
5637 {
5638 EMSG(_("E841: Reserved name, cannot be used for user defined command"));
5639 return;
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 else
5642 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5643 eap->forceit);
5644}
5645
5646/*
5647 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 * Clear all user commands, global and for current buffer.
5649 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005650 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005652 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 uc_clear(&ucmds);
5655 uc_clear(&curbuf->b_ucmds);
5656}
5657
5658/*
5659 * Clear all user commands for "gap".
5660 */
5661 void
5662uc_clear(gap)
5663 garray_T *gap;
5664{
5665 int i;
5666 ucmd_T *cmd;
5667
5668 for (i = 0; i < gap->ga_len; ++i)
5669 {
5670 cmd = USER_CMD_GA(gap, i);
5671 vim_free(cmd->uc_name);
5672 vim_free(cmd->uc_rep);
5673# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5674 vim_free(cmd->uc_compl_arg);
5675# endif
5676 }
5677 ga_clear(gap);
5678}
5679
5680 static void
5681ex_delcommand(eap)
5682 exarg_T *eap;
5683{
5684 int i = 0;
5685 ucmd_T *cmd = NULL;
5686 int cmp = -1;
5687 garray_T *gap;
5688
5689 gap = &curbuf->b_ucmds;
5690 for (;;)
5691 {
5692 for (i = 0; i < gap->ga_len; ++i)
5693 {
5694 cmd = USER_CMD_GA(gap, i);
5695 cmp = STRCMP(eap->arg, cmd->uc_name);
5696 if (cmp <= 0)
5697 break;
5698 }
5699 if (gap == &ucmds || cmp == 0)
5700 break;
5701 gap = &ucmds;
5702 }
5703
5704 if (cmp != 0)
5705 {
5706 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5707 return;
5708 }
5709
5710 vim_free(cmd->uc_name);
5711 vim_free(cmd->uc_rep);
5712# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5713 vim_free(cmd->uc_compl_arg);
5714# endif
5715
5716 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717
5718 if (i < gap->ga_len)
5719 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5720}
5721
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005722/*
5723 * split and quote args for <f-args>
5724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 static char_u *
5726uc_split_args(arg, lenp)
5727 char_u *arg;
5728 size_t *lenp;
5729{
5730 char_u *buf;
5731 char_u *p;
5732 char_u *q;
5733 int len;
5734
5735 /* Precalculate length */
5736 p = arg;
5737 len = 2; /* Initial and final quotes */
5738
5739 while (*p)
5740 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005741 if (p[0] == '\\' && p[1] == '\\')
5742 {
5743 len += 2;
5744 p += 2;
5745 }
5746 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 len += 1;
5749 p += 2;
5750 }
5751 else if (*p == '\\' || *p == '"')
5752 {
5753 len += 2;
5754 p += 1;
5755 }
5756 else if (vim_iswhite(*p))
5757 {
5758 p = skipwhite(p);
5759 if (*p == NUL)
5760 break;
5761 len += 3; /* "," */
5762 }
5763 else
5764 {
5765 ++len;
5766 ++p;
5767 }
5768 }
5769
5770 buf = alloc(len + 1);
5771 if (buf == NULL)
5772 {
5773 *lenp = 0;
5774 return buf;
5775 }
5776
5777 p = arg;
5778 q = buf;
5779 *q++ = '"';
5780 while (*p)
5781 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005782 if (p[0] == '\\' && p[1] == '\\')
5783 {
5784 *q++ = '\\';
5785 *q++ = '\\';
5786 p += 2;
5787 }
5788 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 {
5790 *q++ = p[1];
5791 p += 2;
5792 }
5793 else if (*p == '\\' || *p == '"')
5794 {
5795 *q++ = '\\';
5796 *q++ = *p++;
5797 }
5798 else if (vim_iswhite(*p))
5799 {
5800 p = skipwhite(p);
5801 if (*p == NUL)
5802 break;
5803 *q++ = '"';
5804 *q++ = ',';
5805 *q++ = '"';
5806 }
5807 else
5808 {
5809 *q++ = *p++;
5810 }
5811 }
5812 *q++ = '"';
5813 *q = 0;
5814
5815 *lenp = len;
5816 return buf;
5817}
5818
5819/*
5820 * Check for a <> code in a user command.
5821 * "code" points to the '<'. "len" the length of the <> (inclusive).
5822 * "buf" is where the result is to be added.
5823 * "split_buf" points to a buffer used for splitting, caller should free it.
5824 * "split_len" is the length of what "split_buf" contains.
5825 * Returns the length of the replacement, which has been added to "buf".
5826 * Returns -1 if there was no match, and only the "<" has been copied.
5827 */
5828 static size_t
5829uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5830 char_u *code;
5831 size_t len;
5832 char_u *buf;
5833 ucmd_T *cmd; /* the user command we're expanding */
5834 exarg_T *eap; /* ex arguments */
5835 char_u **split_buf;
5836 size_t *split_len;
5837{
5838 size_t result = 0;
5839 char_u *p = code + 1;
5840 size_t l = len - 2;
5841 int quote = 0;
5842 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5843 ct_LT, ct_NONE } type = ct_NONE;
5844
5845 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5846 {
5847 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5848 p += 2;
5849 l -= 2;
5850 }
5851
Bram Moolenaar371d5402006-03-20 21:47:49 +00005852 ++l;
5853 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005855 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005857 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005859 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005861 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005863 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005865 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005867 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 type = ct_REGISTER;
5869
5870 switch (type)
5871 {
5872 case ct_ARGS:
5873 /* Simple case first */
5874 if (*eap->arg == NUL)
5875 {
5876 if (quote == 1)
5877 {
5878 result = 2;
5879 if (buf != NULL)
5880 STRCPY(buf, "''");
5881 }
5882 else
5883 result = 0;
5884 break;
5885 }
5886
5887 /* When specified there is a single argument don't split it.
5888 * Works for ":Cmd %" when % is "a b c". */
5889 if ((eap->argt & NOSPC) && quote == 2)
5890 quote = 1;
5891
5892 switch (quote)
5893 {
5894 case 0: /* No quoting, no splitting */
5895 result = STRLEN(eap->arg);
5896 if (buf != NULL)
5897 STRCPY(buf, eap->arg);
5898 break;
5899 case 1: /* Quote, but don't split */
5900 result = STRLEN(eap->arg) + 2;
5901 for (p = eap->arg; *p; ++p)
5902 {
5903 if (*p == '\\' || *p == '"')
5904 ++result;
5905 }
5906
5907 if (buf != NULL)
5908 {
5909 *buf++ = '"';
5910 for (p = eap->arg; *p; ++p)
5911 {
5912 if (*p == '\\' || *p == '"')
5913 *buf++ = '\\';
5914 *buf++ = *p;
5915 }
5916 *buf = '"';
5917 }
5918
5919 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005920 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 /* This is hard, so only do it once, and cache the result */
5922 if (*split_buf == NULL)
5923 *split_buf = uc_split_args(eap->arg, split_len);
5924
5925 result = *split_len;
5926 if (buf != NULL && result != 0)
5927 STRCPY(buf, *split_buf);
5928
5929 break;
5930 }
5931 break;
5932
5933 case ct_BANG:
5934 result = eap->forceit ? 1 : 0;
5935 if (quote)
5936 result += 2;
5937 if (buf != NULL)
5938 {
5939 if (quote)
5940 *buf++ = '"';
5941 if (eap->forceit)
5942 *buf++ = '!';
5943 if (quote)
5944 *buf = '"';
5945 }
5946 break;
5947
5948 case ct_LINE1:
5949 case ct_LINE2:
5950 case ct_COUNT:
5951 {
5952 char num_buf[20];
5953 long num = (type == ct_LINE1) ? eap->line1 :
5954 (type == ct_LINE2) ? eap->line2 :
5955 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5956 size_t num_len;
5957
5958 sprintf(num_buf, "%ld", num);
5959 num_len = STRLEN(num_buf);
5960 result = num_len;
5961
5962 if (quote)
5963 result += 2;
5964
5965 if (buf != NULL)
5966 {
5967 if (quote)
5968 *buf++ = '"';
5969 STRCPY(buf, num_buf);
5970 buf += num_len;
5971 if (quote)
5972 *buf = '"';
5973 }
5974
5975 break;
5976 }
5977
5978 case ct_REGISTER:
5979 result = eap->regname ? 1 : 0;
5980 if (quote)
5981 result += 2;
5982 if (buf != NULL)
5983 {
5984 if (quote)
5985 *buf++ = '\'';
5986 if (eap->regname)
5987 *buf++ = eap->regname;
5988 if (quote)
5989 *buf = '\'';
5990 }
5991 break;
5992
5993 case ct_LT:
5994 result = 1;
5995 if (buf != NULL)
5996 *buf = '<';
5997 break;
5998
5999 default:
6000 /* Not recognized: just copy the '<' and return -1. */
6001 result = (size_t)-1;
6002 if (buf != NULL)
6003 *buf = '<';
6004 break;
6005 }
6006
6007 return result;
6008}
6009
6010 static void
6011do_ucmd(eap)
6012 exarg_T *eap;
6013{
6014 char_u *buf;
6015 char_u *p;
6016 char_u *q;
6017
6018 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006019 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006020 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 size_t len, totlen;
6022
6023 size_t split_len = 0;
6024 char_u *split_buf = NULL;
6025 ucmd_T *cmd;
6026#ifdef FEAT_EVAL
6027 scid_T save_current_SID = current_SID;
6028#endif
6029
6030 if (eap->cmdidx == CMD_USER)
6031 cmd = USER_CMD(eap->useridx);
6032 else
6033 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6034
6035 /*
6036 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006037 * First round: "buf" is NULL, compute length, allocate "buf".
6038 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 */
6040 buf = NULL;
6041 for (;;)
6042 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006043 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006044 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006046
6047 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006049 start = vim_strchr(p, '<');
6050 if (start != NULL)
6051 end = vim_strchr(start + 1, '>');
6052 if (buf != NULL)
6053 {
6054 ksp = vim_strchr(p, K_SPECIAL);
6055 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6056 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6057# ifdef FEAT_GUI
6058 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6059# endif
6060 ))
6061 {
6062 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6063 * KS_SPECIAL KE_FILLER, like for mappings, but
6064 * do_cmdline() doesn't handle that, so convert it back.
6065 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6066 len = ksp - p;
6067 if (len > 0)
6068 {
6069 mch_memmove(q, p, len);
6070 q += len;
6071 }
6072 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6073 p = ksp + 3;
6074 continue;
6075 }
6076 }
6077
6078 /* break if there no <item> is found */
6079 if (start == NULL || end == NULL)
6080 break;
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 /* Include the '>' */
6083 ++end;
6084
6085 /* Take everything up to the '<' */
6086 len = start - p;
6087 if (buf == NULL)
6088 totlen += len;
6089 else
6090 {
6091 mch_memmove(q, p, len);
6092 q += len;
6093 }
6094
6095 len = uc_check_code(start, end - start, q, cmd, eap,
6096 &split_buf, &split_len);
6097 if (len == (size_t)-1)
6098 {
6099 /* no match, continue after '<' */
6100 p = start + 1;
6101 len = 1;
6102 }
6103 else
6104 p = end;
6105 if (buf == NULL)
6106 totlen += len;
6107 else
6108 q += len;
6109 }
6110 if (buf != NULL) /* second time here, finished */
6111 {
6112 STRCPY(q, p);
6113 break;
6114 }
6115
6116 totlen += STRLEN(p); /* Add on the trailing characters */
6117 buf = alloc((unsigned)(totlen + 1));
6118 if (buf == NULL)
6119 {
6120 vim_free(split_buf);
6121 return;
6122 }
6123 }
6124
6125#ifdef FEAT_EVAL
6126 current_SID = cmd->uc_scriptID;
6127#endif
6128 (void)do_cmdline(buf, eap->getline, eap->cookie,
6129 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6130#ifdef FEAT_EVAL
6131 current_SID = save_current_SID;
6132#endif
6133 vim_free(buf);
6134 vim_free(split_buf);
6135}
6136
6137# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6138 static char_u *
6139get_user_command_name(idx)
6140 int idx;
6141{
6142 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6143}
6144
6145/*
6146 * Function given to ExpandGeneric() to obtain the list of user command names.
6147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 char_u *
6149get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006150 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 int idx;
6152{
6153 if (idx < curbuf->b_ucmds.ga_len)
6154 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6155 idx -= curbuf->b_ucmds.ga_len;
6156 if (idx < ucmds.ga_len)
6157 return USER_CMD(idx)->uc_name;
6158 return NULL;
6159}
6160
6161/*
6162 * Function given to ExpandGeneric() to obtain the list of user command
6163 * attributes.
6164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 char_u *
6166get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006167 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 int idx;
6169{
6170 static char *user_cmd_flags[] =
6171 {"bang", "bar", "buffer", "complete", "count",
6172 "nargs", "range", "register"};
6173
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006174 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 return NULL;
6176 return (char_u *)user_cmd_flags[idx];
6177}
6178
6179/*
6180 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 char_u *
6183get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006184 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 int idx;
6186{
6187 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6188
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006189 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 return NULL;
6191 return (char_u *)user_cmd_nargs[idx];
6192}
6193
6194/*
6195 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 char_u *
6198get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006199 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 int idx;
6201{
6202 return (char_u *)command_complete[idx].name;
6203}
6204# endif /* FEAT_CMDL_COMPL */
6205
6206#endif /* FEAT_USR_CMDS */
6207
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006208#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6209/*
6210 * Parse a completion argument "value[vallen]".
6211 * The detected completion goes in "*complp", argument type in "*argt".
6212 * When there is an argument, for function and user defined completion, it's
6213 * copied to allocated memory and stored in "*compl_arg".
6214 * Returns FAIL if something is wrong.
6215 */
6216 int
6217parse_compl_arg(value, vallen, complp, argt, compl_arg)
6218 char_u *value;
6219 int vallen;
6220 int *complp;
6221 long *argt;
6222 char_u **compl_arg;
6223{
6224 char_u *arg = NULL;
6225 size_t arglen = 0;
6226 int i;
6227 int valend = vallen;
6228
6229 /* Look for any argument part - which is the part after any ',' */
6230 for (i = 0; i < vallen; ++i)
6231 {
6232 if (value[i] == ',')
6233 {
6234 arg = &value[i + 1];
6235 arglen = vallen - i - 1;
6236 valend = i;
6237 break;
6238 }
6239 }
6240
6241 for (i = 0; command_complete[i].expand != 0; ++i)
6242 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006243 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006244 && STRNCMP(value, command_complete[i].name, valend) == 0)
6245 {
6246 *complp = command_complete[i].expand;
6247 if (command_complete[i].expand == EXPAND_BUFFERS)
6248 *argt |= BUFNAME;
6249 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6250 || command_complete[i].expand == EXPAND_FILES)
6251 *argt |= XFILE;
6252 break;
6253 }
6254 }
6255
6256 if (command_complete[i].expand == 0)
6257 {
6258 EMSG2(_("E180: Invalid complete value: %s"), value);
6259 return FAIL;
6260 }
6261
6262# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6263 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6264 && arg != NULL)
6265# else
6266 if (arg != NULL)
6267# endif
6268 {
6269 EMSG(_("E468: Completion argument only allowed for custom completion"));
6270 return FAIL;
6271 }
6272
6273# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6274 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6275 && arg == NULL)
6276 {
6277 EMSG(_("E467: Custom completion requires a function argument"));
6278 return FAIL;
6279 }
6280
6281 if (arg != NULL)
6282 *compl_arg = vim_strnsave(arg, (int)arglen);
6283# endif
6284 return OK;
6285}
6286#endif
6287
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 static void
6289ex_colorscheme(eap)
6290 exarg_T *eap;
6291{
Bram Moolenaare6850792010-05-14 15:28:44 +02006292 if (*eap->arg == NUL)
6293 {
6294#ifdef FEAT_EVAL
6295 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6296 char_u *p = NULL;
6297
6298 if (expr != NULL)
6299 {
6300 ++emsg_off;
6301 p = eval_to_string(expr, NULL, FALSE);
6302 --emsg_off;
6303 vim_free(expr);
6304 }
6305 if (p != NULL)
6306 {
6307 MSG(p);
6308 vim_free(p);
6309 }
6310 else
6311 MSG("default");
6312#else
6313 MSG(_("unknown"));
6314#endif
6315 }
6316 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6318}
6319
6320 static void
6321ex_highlight(eap)
6322 exarg_T *eap;
6323{
6324 if (*eap->arg == NUL && eap->cmd[2] == '!')
6325 MSG(_("Greetings, Vim user!"));
6326 do_highlight(eap->arg, eap->forceit, FALSE);
6327}
6328
6329
6330/*
6331 * Call this function if we thought we were going to exit, but we won't
6332 * (because of an error). May need to restore the terminal mode.
6333 */
6334 void
6335not_exiting()
6336{
6337 exiting = FALSE;
6338 settmode(TMODE_RAW);
6339}
6340
6341/*
6342 * ":quit": quit current window, quit Vim if closed the last window.
6343 */
6344 static void
6345ex_quit(eap)
6346 exarg_T *eap;
6347{
6348#ifdef FEAT_CMDWIN
6349 if (cmdwin_type != 0)
6350 {
6351 cmdwin_result = Ctrl_C;
6352 return;
6353 }
6354#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006355 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006356 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006357 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006358 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006359 return;
6360 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006361#ifdef FEAT_AUTOCMD
6362 if (curbuf_locked())
6363 return;
6364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366#ifdef FEAT_NETBEANS_INTG
6367 netbeansForcedQuit = eap->forceit;
6368#endif
6369
6370 /*
6371 * If there are more files or windows we won't exit.
6372 */
6373 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6374 exiting = TRUE;
6375 if ((!P_HID(curbuf)
6376 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6377 || check_more(TRUE, eap->forceit) == FAIL
6378 || (only_one_window() && check_changed_any(eap->forceit)))
6379 {
6380 not_exiting();
6381 }
6382 else
6383 {
6384#ifdef FEAT_WINDOWS
6385 if (only_one_window()) /* quit last window */
6386#endif
6387 getout(0);
6388#ifdef FEAT_WINDOWS
6389# ifdef FEAT_GUI
6390 need_mouse_correct = TRUE;
6391# endif
6392 /* close window; may free buffer */
6393 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6394#endif
6395 }
6396}
6397
6398/*
6399 * ":cquit".
6400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 static void
6402ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006403 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 getout(1); /* this does not always pass on the exit code to the Manx
6406 compiler. why? */
6407}
6408
6409/*
6410 * ":qall": try to quit all windows
6411 */
6412 static void
6413ex_quit_all(eap)
6414 exarg_T *eap;
6415{
6416# ifdef FEAT_CMDWIN
6417 if (cmdwin_type != 0)
6418 {
6419 if (eap->forceit)
6420 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6421 else
6422 cmdwin_result = K_XF2;
6423 return;
6424 }
6425# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006426
6427 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006428 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006429 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006430 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006431 return;
6432 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006433#ifdef FEAT_AUTOCMD
6434 if (curbuf_locked())
6435 return;
6436#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006437
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 exiting = TRUE;
6439 if (eap->forceit || !check_changed_any(FALSE))
6440 getout(0);
6441 not_exiting();
6442}
6443
Bram Moolenaard9967712006-03-11 21:18:15 +00006444#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445/*
6446 * ":close": close current window, unless it is the last one
6447 */
6448 static void
6449ex_close(eap)
6450 exarg_T *eap;
6451{
6452# ifdef FEAT_CMDWIN
6453 if (cmdwin_type != 0)
6454 cmdwin_result = K_IGNORE;
6455 else
6456# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006457 if (!text_locked()
6458#ifdef FEAT_AUTOCMD
6459 && !curbuf_locked()
6460#endif
6461 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006462 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463}
6464
Bram Moolenaard9967712006-03-11 21:18:15 +00006465# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466/*
6467 * ":pclose": Close any preview window.
6468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006470ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472{
6473 win_T *win;
6474
6475 for (win = firstwin; win != NULL; win = win->w_next)
6476 if (win->w_p_pvw)
6477 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006478 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479 break;
6480 }
6481}
Bram Moolenaard9967712006-03-11 21:18:15 +00006482# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006483
Bram Moolenaarf740b292006-02-16 22:11:02 +00006484/*
6485 * Close window "win" and take care of handling closing the last window for a
6486 * modified buffer.
6487 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006488 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006489ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006492 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 int need_hide;
6495 buf_T *buf = win->w_buffer;
6496
6497 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006498 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006500# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 if ((p_confirm || cmdmod.confirm) && p_write)
6502 {
6503 dialog_changed(buf, FALSE);
6504 if (buf_valid(buf) && bufIsChanged(buf))
6505 return;
6506 need_hide = FALSE;
6507 }
6508 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006509# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 {
6511 EMSG(_(e_nowrtmsg));
6512 return;
6513 }
6514 }
6515
Bram Moolenaard9967712006-03-11 21:18:15 +00006516# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006518# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006519
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006521 if (tp == NULL)
6522 win_close(win, !need_hide && !P_HID(buf));
6523 else
6524 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525}
6526
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006528 * ":tabclose": close current tab page, unless it is the last one.
6529 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 */
6531 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006532ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 exarg_T *eap;
6534{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006535 tabpage_T *tp;
6536
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006537# ifdef FEAT_CMDWIN
6538 if (cmdwin_type != 0)
6539 cmdwin_result = K_IGNORE;
6540 else
6541# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006542 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006543 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006544 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006546 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006547 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006548 tp = find_tabpage((int)eap->line2);
6549 if (tp == NULL)
6550 {
6551 beep_flush();
6552 return;
6553 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006554 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006555 {
6556 tabpage_close_other(tp, eap->forceit);
6557 return;
6558 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006559 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006560 if (!text_locked()
6561#ifdef FEAT_AUTOCMD
6562 && !curbuf_locked()
6563#endif
6564 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006565 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006567}
6568
6569/*
6570 * ":tabonly": close all tab pages except the current one
6571 */
6572 static void
6573ex_tabonly(eap)
6574 exarg_T *eap;
6575{
6576 tabpage_T *tp;
6577 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006578
6579# ifdef FEAT_CMDWIN
6580 if (cmdwin_type != 0)
6581 cmdwin_result = K_IGNORE;
6582 else
6583# endif
6584 if (first_tabpage->tp_next == NULL)
6585 MSG(_("Already only one tab page"));
6586 else
6587 {
6588 /* Repeat this up to a 1000 times, because autocommands may mess
6589 * up the lists. */
6590 for (done = 0; done < 1000; ++done)
6591 {
6592 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6593 if (tp->tp_topframe != topframe)
6594 {
6595 tabpage_close_other(tp, eap->forceit);
6596 /* if we failed to close it quit */
6597 if (valid_tabpage(tp))
6598 done = 1000;
6599 /* start over, "tp" is now invalid */
6600 break;
6601 }
6602 if (first_tabpage->tp_next == NULL)
6603 break;
6604 }
6605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607
6608/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006609 * Close the current tab page.
6610 */
6611 void
6612tabpage_close(forceit)
6613 int forceit;
6614{
6615 /* First close all the windows but the current one. If that worked then
6616 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006617 if (lastwin != firstwin)
6618 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006619 if (lastwin == firstwin)
6620 ex_win_close(forceit, curwin, NULL);
6621# ifdef FEAT_GUI
6622 need_mouse_correct = TRUE;
6623# endif
6624}
6625
6626/*
6627 * Close tab page "tp", which is not the current tab page.
6628 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006629 * Also takes care of the tab pages line disappearing when closing the
6630 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006631 */
6632 void
6633tabpage_close_other(tp, forceit)
6634 tabpage_T *tp;
6635 int forceit;
6636{
6637 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006638 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006639 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006640
6641 /* Limit to 1000 windows, autocommands may add a window while we close
6642 * one. OK, so I'm paranoid... */
6643 while (++done < 1000)
6644 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006645 wp = tp->tp_firstwin;
6646 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006647
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006648 /* Autocommands may delete the tab page under our fingers and we may
6649 * fail to close a window with a modified buffer. */
6650 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006651 break;
6652 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006653
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006654 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006655 if (h != tabline_height())
6656 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006657}
6658
6659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 * ":only".
6661 */
6662 static void
6663ex_only(eap)
6664 exarg_T *eap;
6665{
6666# ifdef FEAT_GUI
6667 need_mouse_correct = TRUE;
6668# endif
6669 close_others(TRUE, eap->forceit);
6670}
6671
6672/*
6673 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006674 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006676 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677ex_all(eap)
6678 exarg_T *eap;
6679{
6680 if (eap->addr_count == 0)
6681 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006682 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683}
6684#endif /* FEAT_WINDOWS */
6685
6686 static void
6687ex_hide(eap)
6688 exarg_T *eap;
6689{
6690 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6691 eap->errmsg = e_invarg;
6692 else
6693 {
6694 /* ":hide" or ":hide | cmd": hide current window */
6695 eap->nextcmd = check_nextcmd(eap->arg);
6696#ifdef FEAT_WINDOWS
6697 if (!eap->skip)
6698 {
6699# ifdef FEAT_GUI
6700 need_mouse_correct = TRUE;
6701# endif
6702 win_close(curwin, FALSE); /* don't free buffer */
6703 }
6704#endif
6705 }
6706}
6707
6708/*
6709 * ":stop" and ":suspend": Suspend Vim.
6710 */
6711 static void
6712ex_stop(eap)
6713 exarg_T *eap;
6714{
6715 /*
6716 * Disallow suspending for "rvim".
6717 */
6718 if (!check_restricted()
6719#ifdef WIN3264
6720 /*
6721 * Check if external commands are allowed now.
6722 */
6723 && can_end_termcap_mode(TRUE)
6724#endif
6725 )
6726 {
6727 if (!eap->forceit)
6728 autowrite_all();
6729 windgoto((int)Rows - 1, 0);
6730 out_char('\n');
6731 out_flush();
6732 stoptermcap();
6733 out_flush(); /* needed for SUN to restore xterm buffer */
6734#ifdef FEAT_TITLE
6735 mch_restore_title(3); /* restore window titles */
6736#endif
6737 ui_suspend(); /* call machine specific function */
6738#ifdef FEAT_TITLE
6739 maketitle();
6740 resettitle(); /* force updating the title */
6741#endif
6742 starttermcap();
6743 scroll_start(); /* scroll screen before redrawing */
6744 redraw_later_clear();
6745 shell_resized(); /* may have resized window */
6746 }
6747}
6748
6749/*
6750 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6751 */
6752 static void
6753ex_exit(eap)
6754 exarg_T *eap;
6755{
6756#ifdef FEAT_CMDWIN
6757 if (cmdwin_type != 0)
6758 {
6759 cmdwin_result = Ctrl_C;
6760 return;
6761 }
6762#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006763 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006764 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006765 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006766 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006767 return;
6768 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006769#ifdef FEAT_AUTOCMD
6770 if (curbuf_locked())
6771 return;
6772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
6774 /*
6775 * if more files or windows we won't exit
6776 */
6777 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6778 exiting = TRUE;
6779 if ( ((eap->cmdidx == CMD_wq
6780 || curbufIsChanged())
6781 && do_write(eap) == FAIL)
6782 || check_more(TRUE, eap->forceit) == FAIL
6783 || (only_one_window() && check_changed_any(eap->forceit)))
6784 {
6785 not_exiting();
6786 }
6787 else
6788 {
6789#ifdef FEAT_WINDOWS
6790 if (only_one_window()) /* quit last window, exit Vim */
6791#endif
6792 getout(0);
6793#ifdef FEAT_WINDOWS
6794# ifdef FEAT_GUI
6795 need_mouse_correct = TRUE;
6796# endif
6797 /* quit current window, may free buffer */
6798 win_close(curwin, !P_HID(curwin->w_buffer));
6799#endif
6800 }
6801}
6802
6803/*
6804 * ":print", ":list", ":number".
6805 */
6806 static void
6807ex_print(eap)
6808 exarg_T *eap;
6809{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006810 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6811 EMSG(_(e_emptybuf));
6812 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006814 for ( ;!got_int; ui_breakcheck())
6815 {
6816 print_line(eap->line1,
6817 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6818 || (eap->flags & EXFLAG_NR)),
6819 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6820 if (++eap->line1 > eap->line2)
6821 break;
6822 out_flush(); /* show one line at a time */
6823 }
6824 setpcmark();
6825 /* put cursor at last line */
6826 curwin->w_cursor.lnum = eap->line2;
6827 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
6829
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831}
6832
6833#ifdef FEAT_BYTEOFF
6834 static void
6835ex_goto(eap)
6836 exarg_T *eap;
6837{
6838 goto_byte(eap->line2);
6839}
6840#endif
6841
6842/*
6843 * ":shell".
6844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 static void
6846ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006847 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848{
6849 do_shell(NULL, 0);
6850}
6851
6852#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6853 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006854 || defined(FEAT_GUI_MSWIN) \
6855 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 || defined(PROTO)
6857
6858/*
6859 * Handle a file drop. The code is here because a drop is *nearly* like an
6860 * :args command, but not quite (we have a list of exact filenames, so we
6861 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6862 * code is very similar to :args and hence needs access to a lot of the static
6863 * functions in this file.
6864 *
6865 * The list should be allocated using alloc(), as should each item in the
6866 * list. This function takes over responsibility for freeing the list.
6867 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006868 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6870 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6871 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6872 * file functionality is (currently) not in EMX this is not presently a
6873 * problem.
6874 */
6875 void
6876handle_drop(filec, filev, split)
6877 int filec; /* the number of files dropped */
6878 char_u **filev; /* the list of files dropped */
6879 int split; /* force splitting the window */
6880{
6881 exarg_T ea;
6882 int save_msg_scroll = msg_scroll;
6883
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006884 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006885 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006887#ifdef FEAT_AUTOCMD
6888 if (curbuf_locked())
6889 return;
6890#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006891 /* When the screen is being updated we should not change buffers and
6892 * windows structures, it may cause freed memory to be used. */
6893 if (updating_screen)
6894 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896 /* Check whether the current buffer is changed. If so, we will need
6897 * to split the current window or data could be lost.
6898 * We don't need to check if the 'hidden' option is set, as in this
6899 * case the buffer won't be lost.
6900 */
6901 if (!P_HID(curbuf) && !split)
6902 {
6903 ++emsg_off;
6904 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6905 --emsg_off;
6906 }
6907 if (split)
6908 {
6909# ifdef FEAT_WINDOWS
6910 if (win_split(0, 0) == FAIL)
6911 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006912 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
6914 /* When splitting the window, create a new alist. Otherwise the
6915 * existing one is overwritten. */
6916 alist_unlink(curwin->w_alist);
6917 alist_new();
6918# else
6919 return; /* can't split, always fail */
6920# endif
6921 }
6922
6923 /*
6924 * Set up the new argument list.
6925 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006926 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
6928 /*
6929 * Move to the first file.
6930 */
6931 /* Fake up a minimal "next" command for do_argfile() */
6932 vim_memset(&ea, 0, sizeof(ea));
6933 ea.cmd = (char_u *)"next";
6934 do_argfile(&ea, 0);
6935
6936 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6937 * mode that is not needed here. */
6938 need_start_insertmode = FALSE;
6939
6940 /* Restore msg_scroll, otherwise a following command may cause scrolling
6941 * unexpectedly. The screen will be redrawn by the caller, thus
6942 * msg_scroll being set by displaying a message is irrelevant. */
6943 msg_scroll = save_msg_scroll;
6944}
6945#endif
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947/*
6948 * Clear an argument list: free all file names and reset it to zero entries.
6949 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006950 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951alist_clear(al)
6952 alist_T *al;
6953{
6954 while (--al->al_ga.ga_len >= 0)
6955 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6956 ga_clear(&al->al_ga);
6957}
6958
6959/*
6960 * Init an argument list.
6961 */
6962 void
6963alist_init(al)
6964 alist_T *al;
6965{
6966 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6967}
6968
6969#if defined(FEAT_WINDOWS) || defined(PROTO)
6970
6971/*
6972 * Remove a reference from an argument list.
6973 * Ignored when the argument list is the global one.
6974 * If the argument list is no longer used by any window, free it.
6975 */
6976 void
6977alist_unlink(al)
6978 alist_T *al;
6979{
6980 if (al != &global_alist && --al->al_refcount <= 0)
6981 {
6982 alist_clear(al);
6983 vim_free(al);
6984 }
6985}
6986
6987# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6988/*
6989 * Create a new argument list and use it for the current window.
6990 */
6991 void
6992alist_new()
6993{
6994 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6995 if (curwin->w_alist == NULL)
6996 {
6997 curwin->w_alist = &global_alist;
6998 ++global_alist.al_refcount;
6999 }
7000 else
7001 {
7002 curwin->w_alist->al_refcount = 1;
7003 alist_init(curwin->w_alist);
7004 }
7005}
7006# endif
7007#endif
7008
7009#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
7010/*
7011 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00007012 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
7013 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 */
7015 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007016alist_expand(fnum_list, fnum_len)
7017 int *fnum_list;
7018 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019{
7020 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007021 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 char_u **new_arg_files;
7023 int new_arg_file_count;
7024 char_u *save_p_su = p_su;
7025 int i;
7026
7027 /* Don't use 'suffixes' here. This should work like the shell did the
7028 * expansion. Also, the vimrc file isn't read yet, thus the user
7029 * can't set the options. */
7030 p_su = empty_option;
7031 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7032 if (old_arg_files != NULL)
7033 {
7034 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007035 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7036 old_arg_count = GARGCOUNT;
7037 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 &new_arg_file_count, &new_arg_files,
7039 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7040 && new_arg_file_count > 0)
7041 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007042 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7043 TRUE, fnum_list, fnum_len);
7044 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047 p_su = save_p_su;
7048}
7049#endif
7050
7051/*
7052 * Set the argument list for the current window.
7053 * Takes over the allocated files[] and the allocated fnames in it.
7054 */
7055 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007056alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 alist_T *al;
7058 int count;
7059 char_u **files;
7060 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007061 int *fnum_list;
7062 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064 int i;
7065
7066 alist_clear(al);
7067 if (ga_grow(&al->al_ga, count) == OK)
7068 {
7069 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007070 {
7071 if (got_int)
7072 {
7073 /* When adding many buffers this can take a long time. Allow
7074 * interrupting here. */
7075 while (i < count)
7076 vim_free(files[i++]);
7077 break;
7078 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007079
7080 /* May set buffer name of a buffer previously used for the
7081 * argument list, so that it's re-used by alist_add. */
7082 if (fnum_list != NULL && i < fnum_len)
7083 buf_set_name(fnum_list[i], files[i]);
7084
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007086 ui_breakcheck();
7087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 vim_free(files);
7089 }
7090 else
7091 FreeWild(count, files);
7092#ifdef FEAT_WINDOWS
7093 if (al == &global_alist)
7094#endif
7095 arg_had_last = FALSE;
7096}
7097
7098/*
7099 * Add file "fname" to argument list "al".
7100 * "fname" must have been allocated and "al" must have been checked for room.
7101 */
7102 void
7103alist_add(al, fname, set_fnum)
7104 alist_T *al;
7105 char_u *fname;
7106 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7107{
7108 if (fname == NULL) /* don't add NULL file names */
7109 return;
7110#ifdef BACKSLASH_IN_FILENAME
7111 slash_adjust(fname);
7112#endif
7113 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7114 if (set_fnum > 0)
7115 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7116 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7117 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118}
7119
7120#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7121/*
7122 * Adjust slashes in file names. Called after 'shellslash' was set.
7123 */
7124 void
7125alist_slash_adjust()
7126{
7127 int i;
7128# ifdef FEAT_WINDOWS
7129 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007130 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131# endif
7132
7133 for (i = 0; i < GARGCOUNT; ++i)
7134 if (GARGLIST[i].ae_fname != NULL)
7135 slash_adjust(GARGLIST[i].ae_fname);
7136# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007137 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 if (wp->w_alist != &global_alist)
7139 for (i = 0; i < WARGCOUNT(wp); ++i)
7140 if (WARGLIST(wp)[i].ae_fname != NULL)
7141 slash_adjust(WARGLIST(wp)[i].ae_fname);
7142# endif
7143}
7144#endif
7145
7146/*
7147 * ":preserve".
7148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 static void
7150ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007151 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007153 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 ml_preserve(curbuf, TRUE);
7155}
7156
7157/*
7158 * ":recover".
7159 */
7160 static void
7161ex_recover(eap)
7162 exarg_T *eap;
7163{
7164 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7165 recoverymode = TRUE;
7166 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7167 && (*eap->arg == NUL
7168 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7169 ml_recover();
7170 recoverymode = FALSE;
7171}
7172
7173/*
7174 * Command modifier used in a wrong way.
7175 */
7176 static void
7177ex_wrongmodifier(eap)
7178 exarg_T *eap;
7179{
7180 eap->errmsg = e_invcmd;
7181}
7182
7183#ifdef FEAT_WINDOWS
7184/*
7185 * :sview [+command] file split window with new file, read-only
7186 * :split [[+command] file] split window with current or new file
7187 * :vsplit [[+command] file] split window vertically with current or new file
7188 * :new [[+command] file] split window with no or new file
7189 * :vnew [[+command] file] split vertically window with no or new file
7190 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007191 *
7192 * :tabedit open new Tab page with empty window
7193 * :tabedit [+command] file open new Tab page and edit "file"
7194 * :tabnew [[+command] file] just like :tabedit
7195 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 */
7197 void
7198ex_splitview(eap)
7199 exarg_T *eap;
7200{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007201 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007202# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007204# endif
7205# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007209# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7211 {
7212 ex_ni(eap);
7213 return;
7214 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007217# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007221# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007223 * quickfix windows. But it's OK when doing ":tab split". */
7224 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {
7226 if (eap->cmdidx == CMD_split)
7227 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007228# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 if (eap->cmdidx == CMD_vsplit)
7230 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007233# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007235# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007236 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 {
7238 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7239 FNAME_MESS, TRUE, curbuf->b_ffname);
7240 if (fname == NULL)
7241 goto theend;
7242 eap->arg = fname;
7243 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007244# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007248# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007250# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 && eap->cmdidx != CMD_new)
7254 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007255# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007256 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007257# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007258 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007259# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007260 au_has_group((char_u *)"FileExplorer"))
7261 {
7262 /* No browsing supported but we do have the file explorer:
7263 * Edit the directory. */
7264 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7265 eap->arg = (char_u *)".";
7266 }
7267 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007268# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007269 {
7270 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007272 if (fname == NULL)
7273 goto theend;
7274 eap->arg = fname;
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007278# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007280 /*
7281 * Either open new tab page or split the window.
7282 */
7283 if (eap->cmdidx == CMD_tabedit
7284 || eap->cmdidx == CMD_tabfind
7285 || eap->cmdidx == CMD_tabnew)
7286 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007287 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7288 : eap->addr_count == 0 ? 0
7289 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007290 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007291 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007292
7293 /* set the alternate buffer for the window we came from */
7294 if (curwin != old_curwin
7295 && win_valid(old_curwin)
7296 && old_curwin->w_buffer != curbuf
7297 && !cmdmod.keepalt)
7298 old_curwin->w_alt_fnum = curbuf->b_fnum;
7299 }
7300 }
7301 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7303 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007304# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 /* Reset 'scrollbind' when editing another file, but keep it when
7306 * doing ":split" without arguments. */
7307 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007308# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007312 {
7313 RESET_BINDING(curwin);
7314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 else
7316 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 do_exedit(eap, old_curwin);
7319 }
7320
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007321# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007323# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007325# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326theend:
7327 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007328# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007330
7331/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007332 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007333 */
7334 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007335tabpage_new()
7336{
7337 exarg_T ea;
7338
7339 vim_memset(&ea, 0, sizeof(ea));
7340 ea.cmdidx = CMD_tabnew;
7341 ea.cmd = (char_u *)"tabn";
7342 ea.arg = (char_u *)"";
7343 ex_splitview(&ea);
7344}
7345
7346/*
7347 * :tabnext command
7348 */
7349 static void
7350ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007351 exarg_T *eap;
7352{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007353 switch (eap->cmdidx)
7354 {
7355 case CMD_tabfirst:
7356 case CMD_tabrewind:
7357 goto_tabpage(1);
7358 break;
7359 case CMD_tablast:
7360 goto_tabpage(9999);
7361 break;
7362 case CMD_tabprevious:
7363 case CMD_tabNext:
7364 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7365 break;
7366 default: /* CMD_tabnext */
7367 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7368 break;
7369 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007370}
7371
7372/*
7373 * :tabmove command
7374 */
7375 static void
7376ex_tabmove(eap)
7377 exarg_T *eap;
7378{
7379 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007380}
7381
7382/*
7383 * :tabs command: List tabs and their contents.
7384 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007385 static void
7386ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007387 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007388{
7389 tabpage_T *tp;
7390 win_T *wp;
7391 int tabcount = 1;
7392
7393 msg_start();
7394 msg_scroll = TRUE;
7395 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7396 {
7397 msg_putchar('\n');
7398 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7399 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7400 out_flush(); /* output one line at a time */
7401 ui_breakcheck();
7402
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007403 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007404 wp = firstwin;
7405 else
7406 wp = tp->tp_firstwin;
7407 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7408 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007409 msg_putchar('\n');
7410 msg_putchar(wp == curwin ? '>' : ' ');
7411 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007412 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7413 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007414 if (buf_spname(wp->w_buffer) != NULL)
7415 STRCPY(IObuff, buf_spname(wp->w_buffer));
7416 else
7417 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7418 IObuff, IOSIZE, TRUE);
7419 msg_outtrans(IObuff);
7420 out_flush(); /* output one line at a time */
7421 ui_breakcheck();
7422 }
7423 }
7424}
7425
7426#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427
7428/*
7429 * ":mode": Set screen mode.
7430 * If no argument given, just get the screen size and redraw.
7431 */
7432 static void
7433ex_mode(eap)
7434 exarg_T *eap;
7435{
7436 if (*eap->arg == NUL)
7437 shell_resized();
7438 else
7439 mch_screenmode(eap->arg);
7440}
7441
7442#ifdef FEAT_WINDOWS
7443/*
7444 * ":resize".
7445 * set, increment or decrement current window height
7446 */
7447 static void
7448ex_resize(eap)
7449 exarg_T *eap;
7450{
7451 int n;
7452 win_T *wp = curwin;
7453
7454 if (eap->addr_count > 0)
7455 {
7456 n = eap->line2;
7457 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7458 ;
7459 }
7460
7461#ifdef FEAT_GUI
7462 need_mouse_correct = TRUE;
7463#endif
7464 n = atol((char *)eap->arg);
7465#ifdef FEAT_VERTSPLIT
7466 if (cmdmod.split & WSP_VERT)
7467 {
7468 if (*eap->arg == '-' || *eap->arg == '+')
7469 n += W_WIDTH(curwin);
7470 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7471 n = 9999;
7472 win_setwidth_win((int)n, wp);
7473 }
7474 else
7475#endif
7476 {
7477 if (*eap->arg == '-' || *eap->arg == '+')
7478 n += curwin->w_height;
7479 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7480 n = 9999;
7481 win_setheight_win((int)n, wp);
7482 }
7483}
7484#endif
7485
7486/*
7487 * ":find [+command] <file>" command.
7488 */
7489 static void
7490ex_find(eap)
7491 exarg_T *eap;
7492{
7493#ifdef FEAT_SEARCHPATH
7494 char_u *fname;
7495 int count;
7496
7497 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7498 TRUE, curbuf->b_ffname);
7499 if (eap->addr_count > 0)
7500 {
7501 /* Repeat finding the file "count" times. This matters when it
7502 * appears several times in the path. */
7503 count = eap->line2;
7504 while (fname != NULL && --count > 0)
7505 {
7506 vim_free(fname);
7507 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7508 FALSE, curbuf->b_ffname);
7509 }
7510 }
7511
7512 if (fname != NULL)
7513 {
7514 eap->arg = fname;
7515#endif
7516 do_exedit(eap, NULL);
7517#ifdef FEAT_SEARCHPATH
7518 vim_free(fname);
7519 }
7520#endif
7521}
7522
7523/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007524 * ":open" simulation: for now just work like ":visual".
7525 */
7526 static void
7527ex_open(eap)
7528 exarg_T *eap;
7529{
7530 regmatch_T regmatch;
7531 char_u *p;
7532
7533 curwin->w_cursor.lnum = eap->line2;
7534 beginline(BL_SOL | BL_FIX);
7535 if (*eap->arg == '/')
7536 {
7537 /* ":open /pattern/": put cursor in column found with pattern */
7538 ++eap->arg;
7539 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7540 *p = NUL;
7541 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7542 if (regmatch.regprog != NULL)
7543 {
7544 regmatch.rm_ic = p_ic;
7545 p = ml_get_curline();
7546 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007547 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007548 else
7549 EMSG(_(e_nomatch));
7550 vim_free(regmatch.regprog);
7551 }
7552 /* Move to the NUL, ignore any other arguments. */
7553 eap->arg += STRLEN(eap->arg);
7554 }
7555 check_cursor();
7556
7557 eap->cmdidx = CMD_visual;
7558 do_exedit(eap, NULL);
7559}
7560
7561/*
7562 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 */
7564 static void
7565ex_edit(eap)
7566 exarg_T *eap;
7567{
7568 do_exedit(eap, NULL);
7569}
7570
7571/*
7572 * ":edit <file>" command and alikes.
7573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 void
7575do_exedit(eap, old_curwin)
7576 exarg_T *eap;
7577 win_T *old_curwin; /* curwin before doing a split or NULL */
7578{
7579 int n;
7580#ifdef FEAT_WINDOWS
7581 int need_hide;
7582#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007583 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584
7585 /*
7586 * ":vi" command ends Ex mode.
7587 */
7588 if (exmode_active && (eap->cmdidx == CMD_visual
7589 || eap->cmdidx == CMD_view))
7590 {
7591 exmode_active = FALSE;
7592 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007593 {
7594 /* Special case: ":global/pat/visual\NLvi-commands" */
7595 if (global_busy)
7596 {
7597 int rd = RedrawingDisabled;
7598 int nwr = no_wait_return;
7599 int ms = msg_scroll;
7600#ifdef FEAT_GUI
7601 int he = hold_gui_events;
7602#endif
7603
7604 if (eap->nextcmd != NULL)
7605 {
7606 stuffReadbuff(eap->nextcmd);
7607 eap->nextcmd = NULL;
7608 }
7609
7610 if (exmode_was != EXMODE_VIM)
7611 settmode(TMODE_RAW);
7612 RedrawingDisabled = 0;
7613 no_wait_return = 0;
7614 need_wait_return = FALSE;
7615 msg_scroll = 0;
7616#ifdef FEAT_GUI
7617 hold_gui_events = 0;
7618#endif
7619 must_redraw = CLEAR;
7620
7621 main_loop(FALSE, TRUE);
7622
7623 RedrawingDisabled = rd;
7624 no_wait_return = nwr;
7625 msg_scroll = ms;
7626#ifdef FEAT_GUI
7627 hold_gui_events = he;
7628#endif
7629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 }
7633
7634 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007635 || eap->cmdidx == CMD_tabnew
7636 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637#ifdef FEAT_VERTSPLIT
7638 || eap->cmdidx == CMD_vnew
7639#endif
7640 ) && *eap->arg == NUL)
7641 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007642 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 setpcmark();
7644 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007645 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7646 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 }
7648 else if ((eap->cmdidx != CMD_split
7649#ifdef FEAT_VERTSPLIT
7650 && eap->cmdidx != CMD_vsplit
7651#endif
7652 )
7653 || *eap->arg != NUL
7654#ifdef FEAT_BROWSE
7655 || cmdmod.browse
7656#endif
7657 )
7658 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007659#ifdef FEAT_AUTOCMD
7660 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7661 * can bring us here, others are stopped earlier. */
7662 if (*eap->arg != NUL && curbuf_locked())
7663 return;
7664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 n = readonlymode;
7666 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7667 readonlymode = TRUE;
7668 else if (eap->cmdidx == CMD_enew)
7669 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7670 empty buffer */
7671 setpcmark();
7672 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7673 NULL, eap,
7674 /* ":edit" goes to first line if Vi compatible */
7675 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7676 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7677 ? ECMD_ONE : eap->do_ecmd_lnum,
7678 (P_HID(curbuf) ? ECMD_HIDE : 0)
7679 + (eap->forceit ? ECMD_FORCEIT : 0)
7680#ifdef FEAT_LISTCMDS
7681 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7682#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007683 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {
7685 /* Editing the file failed. If the window was split, close it. */
7686#ifdef FEAT_WINDOWS
7687 if (old_curwin != NULL)
7688 {
7689 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7690 if (!need_hide || P_HID(curbuf))
7691 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007692# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7693 cleanup_T cs;
7694
7695 /* Reset the error/interrupt/exception state here so that
7696 * aborting() returns FALSE when closing a window. */
7697 enter_cleanup(&cs);
7698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699# ifdef FEAT_GUI
7700 need_mouse_correct = TRUE;
7701# endif
7702 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007703
7704# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7705 /* Restore the error/interrupt/exception state if not
7706 * discarded by a new aborting error, interrupt, or
7707 * uncaught exception. */
7708 leave_cleanup(&cs);
7709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 }
7711 }
7712#endif
7713 }
7714 else if (readonlymode && curbuf->b_nwindows == 1)
7715 {
7716 /* When editing an already visited buffer, 'readonly' won't be set
7717 * but the previous value is kept. With ":view" and ":sview" we
7718 * want the file to be readonly, except when another window is
7719 * editing the same buffer. */
7720 curbuf->b_p_ro = TRUE;
7721 }
7722 readonlymode = n;
7723 }
7724 else
7725 {
7726 if (eap->do_ecmd_cmd != NULL)
7727 do_cmdline_cmd(eap->do_ecmd_cmd);
7728#ifdef FEAT_TITLE
7729 n = curwin->w_arg_idx_invalid;
7730#endif
7731 check_arg_idx(curwin);
7732#ifdef FEAT_TITLE
7733 if (n != curwin->w_arg_idx_invalid)
7734 maketitle();
7735#endif
7736 }
7737
7738#ifdef FEAT_WINDOWS
7739 /*
7740 * if ":split file" worked, set alternate file name in old window to new
7741 * file
7742 */
7743 if (old_curwin != NULL
7744 && *eap->arg != NUL
7745 && curwin != old_curwin
7746 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007747 && old_curwin->w_buffer != curbuf
7748 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 old_curwin->w_alt_fnum = curbuf->b_fnum;
7750#endif
7751
7752 ex_no_reprint = TRUE;
7753}
7754
7755#ifndef FEAT_GUI
7756/*
7757 * ":gui" and ":gvim" when there is no GUI.
7758 */
7759 static void
7760ex_nogui(eap)
7761 exarg_T *eap;
7762{
7763 eap->errmsg = e_nogvim;
7764}
7765#endif
7766
7767#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7768 static void
7769ex_tearoff(eap)
7770 exarg_T *eap;
7771{
7772 gui_make_tearoff(eap->arg);
7773}
7774#endif
7775
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007776#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 static void
7778ex_popup(eap)
7779 exarg_T *eap;
7780{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007781 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782}
7783#endif
7784
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 static void
7786ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007787 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7790 MSG(_("No swap file"));
7791 else
7792 msg(curbuf->b_ml.ml_mfp->mf_fname);
7793}
7794
7795/*
7796 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7797 * offset.
7798 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 static void
7801ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007802 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804#ifdef FEAT_SCROLLBIND
7805 win_T *wp;
7806 long topline;
7807 long y;
7808 linenr_T old_linenr = curwin->w_cursor.lnum;
7809
7810 setpcmark();
7811
7812 /*
7813 * determine max topline
7814 */
7815 if (curwin->w_p_scb)
7816 {
7817 topline = curwin->w_topline;
7818 for (wp = firstwin; wp; wp = wp->w_next)
7819 {
7820 if (wp->w_p_scb && wp->w_buffer)
7821 {
7822 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7823 if (topline > y)
7824 topline = y;
7825 }
7826 }
7827 if (topline < 1)
7828 topline = 1;
7829 }
7830 else
7831 {
7832 topline = 1;
7833 }
7834
7835
7836 /*
7837 * set all scrollbind windows to the same topline
7838 */
7839 wp = curwin;
7840 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7841 {
7842 if (curwin->w_p_scb)
7843 {
7844 y = topline - curwin->w_topline;
7845 if (y > 0)
7846 scrollup(y, TRUE);
7847 else
7848 scrolldown(-y, TRUE);
7849 curwin->w_scbind_pos = topline;
7850 redraw_later(VALID);
7851 cursor_correct();
7852#ifdef FEAT_WINDOWS
7853 curwin->w_redr_status = TRUE;
7854#endif
7855 }
7856 }
7857 curwin = wp;
7858 if (curwin->w_p_scb)
7859 {
7860 did_syncbind = TRUE;
7861 checkpcmark();
7862 if (old_linenr != curwin->w_cursor.lnum)
7863 {
7864 char_u ctrl_o[2];
7865
7866 ctrl_o[0] = Ctrl_O;
7867 ctrl_o[1] = 0;
7868 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7869 }
7870 }
7871#endif
7872}
7873
7874
7875 static void
7876ex_read(eap)
7877 exarg_T *eap;
7878{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007879 int i;
7880 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7881 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882
7883 if (eap->usefilter) /* :r!cmd */
7884 do_bang(1, eap, FALSE, FALSE, TRUE);
7885 else
7886 {
7887 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7888 return;
7889
7890#ifdef FEAT_BROWSE
7891 if (cmdmod.browse)
7892 {
7893 char_u *browseFile;
7894
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007895 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 NULL, NULL, NULL, curbuf);
7897 if (browseFile != NULL)
7898 {
7899 i = readfile(browseFile, NULL,
7900 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7901 vim_free(browseFile);
7902 }
7903 else
7904 i = OK;
7905 }
7906 else
7907#endif
7908 if (*eap->arg == NUL)
7909 {
7910 if (check_fname() == FAIL) /* check for no file name */
7911 return;
7912 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7913 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7914 }
7915 else
7916 {
7917 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7918 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7919 i = readfile(eap->arg, NULL,
7920 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7921
7922 }
7923 if (i == FAIL)
7924 {
7925#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7926 if (!aborting())
7927#endif
7928 EMSG2(_(e_notopen), eap->arg);
7929 }
7930 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007931 {
7932 if (empty && exmode_active)
7933 {
7934 /* Delete the empty line that remains. Historically ex does
7935 * this but vi doesn't. */
7936 if (eap->line2 == 0)
7937 lnum = curbuf->b_ml.ml_line_count;
7938 else
7939 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007940 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007941 {
7942 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007943 if (curwin->w_cursor.lnum > 1
7944 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007945 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007946 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007947 }
7948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 }
7952}
7953
Bram Moolenaarea408852005-06-25 22:49:46 +00007954static char_u *prev_dir = NULL;
7955
7956#if defined(EXITFREE) || defined(PROTO)
7957 void
7958free_cd_dir()
7959{
7960 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007961 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007962
7963 vim_free(globaldir);
7964 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007965}
7966#endif
7967
7968
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969/*
7970 * ":cd", ":lcd", ":chdir" and ":lchdir".
7971 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007972 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973ex_cd(eap)
7974 exarg_T *eap;
7975{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 char_u *new_dir;
7977 char_u *tofree;
7978
7979 new_dir = eap->arg;
7980#if !defined(UNIX) && !defined(VMS)
7981 /* for non-UNIX ":cd" means: print current directory */
7982 if (*new_dir == NUL)
7983 ex_pwd(NULL);
7984 else
7985#endif
7986 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007987#ifdef FEAT_AUTOCMD
7988 if (allbuf_locked())
7989 return;
7990#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007991 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7992 && !eap->forceit)
7993 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007994 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007995 return;
7996 }
7997
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 /* ":cd -": Change to previous directory */
7999 if (STRCMP(new_dir, "-") == 0)
8000 {
8001 if (prev_dir == NULL)
8002 {
8003 EMSG(_("E186: No previous directory"));
8004 return;
8005 }
8006 new_dir = prev_dir;
8007 }
8008
8009 /* Save current directory for next ":cd -" */
8010 tofree = prev_dir;
8011 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8012 prev_dir = vim_strsave(NameBuff);
8013 else
8014 prev_dir = NULL;
8015
8016#if defined(UNIX) || defined(VMS)
8017 /* for UNIX ":cd" means: go to home directory */
8018 if (*new_dir == NUL)
8019 {
8020 /* use NameBuff for home directory name */
8021# ifdef VMS
8022 char_u *p;
8023
8024 p = mch_getenv((char_u *)"SYS$LOGIN");
8025 if (p == NULL || *p == NUL) /* empty is the same as not set */
8026 NameBuff[0] = NUL;
8027 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008028 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029# else
8030 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8031# endif
8032 new_dir = NameBuff;
8033 }
8034#endif
8035 if (new_dir == NULL || vim_chdir(new_dir))
8036 EMSG(_(e_failed));
8037 else
8038 {
8039 vim_free(curwin->w_localdir);
8040 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8041 {
8042 /* If still in global directory, need to remember current
8043 * directory as global directory. */
8044 if (globaldir == NULL && prev_dir != NULL)
8045 globaldir = vim_strsave(prev_dir);
8046 /* Remember this local directory for the window. */
8047 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8048 curwin->w_localdir = vim_strsave(NameBuff);
8049 }
8050 else
8051 {
8052 /* We are now in the global directory, no need to remember its
8053 * name. */
8054 vim_free(globaldir);
8055 globaldir = NULL;
8056 curwin->w_localdir = NULL;
8057 }
8058
8059 shorten_fnames(TRUE);
8060
8061 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008062 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 ex_pwd(eap);
8064 }
8065 vim_free(tofree);
8066 }
8067}
8068
8069/*
8070 * ":pwd".
8071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 static void
8073ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008074 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075{
8076 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8077 {
8078#ifdef BACKSLASH_IN_FILENAME
8079 slash_adjust(NameBuff);
8080#endif
8081 msg(NameBuff);
8082 }
8083 else
8084 EMSG(_("E187: Unknown"));
8085}
8086
8087/*
8088 * ":=".
8089 */
8090 static void
8091ex_equal(eap)
8092 exarg_T *eap;
8093{
8094 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008095 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096}
8097
8098 static void
8099ex_sleep(eap)
8100 exarg_T *eap;
8101{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008102 int n;
8103 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
8105 if (cursor_valid())
8106 {
8107 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8108 if (n >= 0)
8109 windgoto((int)n, curwin->w_wcol);
8110 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008111
8112 len = eap->line2;
8113 switch (*eap->arg)
8114 {
8115 case 'm': break;
8116 case NUL: len *= 1000L; break;
8117 default: EMSG2(_(e_invarg2), eap->arg); return;
8118 }
8119 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120}
8121
8122/*
8123 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8124 */
8125 void
8126do_sleep(msec)
8127 long msec;
8128{
8129 long done;
8130
8131 cursor_on();
8132 out_flush();
8133 for (done = 0; !got_int && done < msec; done += 1000L)
8134 {
8135 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8136 ui_breakcheck();
8137 }
8138}
8139
8140 static void
8141do_exmap(eap, isabbrev)
8142 exarg_T *eap;
8143 int isabbrev;
8144{
8145 int mode;
8146 char_u *cmdp;
8147
8148 cmdp = eap->cmd;
8149 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8150
8151 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8152 eap->arg, mode, isabbrev))
8153 {
8154 case 1: EMSG(_(e_invarg));
8155 break;
8156 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8157 break;
8158 }
8159}
8160
8161/*
8162 * ":winsize" command (obsolete).
8163 */
8164 static void
8165ex_winsize(eap)
8166 exarg_T *eap;
8167{
8168 int w, h;
8169 char_u *arg = eap->arg;
8170 char_u *p;
8171
8172 w = getdigits(&arg);
8173 arg = skipwhite(arg);
8174 p = arg;
8175 h = getdigits(&arg);
8176 if (*p != NUL && *arg == NUL)
8177 set_shellsize(w, h, TRUE);
8178 else
8179 EMSG(_("E465: :winsize requires two number arguments"));
8180}
8181
8182#ifdef FEAT_WINDOWS
8183 static void
8184ex_wincmd(eap)
8185 exarg_T *eap;
8186{
8187 int xchar = NUL;
8188 char_u *p;
8189
8190 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8191 {
8192 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8193 if (eap->arg[1] == NUL)
8194 {
8195 EMSG(_(e_invarg));
8196 return;
8197 }
8198 xchar = eap->arg[1];
8199 p = eap->arg + 2;
8200 }
8201 else
8202 p = eap->arg + 1;
8203
8204 eap->nextcmd = check_nextcmd(p);
8205 p = skipwhite(p);
8206 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8207 EMSG(_(e_invarg));
8208 else
8209 {
8210 /* Pass flags on for ":vertical wincmd ]". */
8211 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008212 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8214 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008215 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 }
8217}
8218#endif
8219
Bram Moolenaar843ee412004-06-30 16:16:41 +00008220#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221/*
8222 * ":winpos".
8223 */
8224 static void
8225ex_winpos(eap)
8226 exarg_T *eap;
8227{
8228 int x, y;
8229 char_u *arg = eap->arg;
8230 char_u *p;
8231
8232 if (*arg == NUL)
8233 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008234# if defined(FEAT_GUI) || defined(MSWIN)
8235# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008237# else
8238 if (mch_get_winpos(&x, &y) != FAIL)
8239# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {
8241 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8242 msg(IObuff);
8243 }
8244 else
8245# endif
8246 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8247 }
8248 else
8249 {
8250 x = getdigits(&arg);
8251 arg = skipwhite(arg);
8252 p = arg;
8253 y = getdigits(&arg);
8254 if (*p == NUL || *arg != NUL)
8255 {
8256 EMSG(_("E466: :winpos requires two number arguments"));
8257 return;
8258 }
8259# ifdef FEAT_GUI
8260 if (gui.in_use)
8261 gui_mch_set_winpos(x, y);
8262 else if (gui.starting)
8263 {
8264 /* Remember the coordinates for when the window is opened. */
8265 gui_win_x = x;
8266 gui_win_y = y;
8267 }
8268# ifdef HAVE_TGETENT
8269 else
8270# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008271# else
8272# ifdef MSWIN
8273 mch_set_winpos(x, y);
8274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275# endif
8276# ifdef HAVE_TGETENT
8277 if (*T_CWP)
8278 term_set_winpos(x, y);
8279# endif
8280 }
8281}
8282#endif
8283
8284/*
8285 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8286 */
8287 static void
8288ex_operators(eap)
8289 exarg_T *eap;
8290{
8291 oparg_T oa;
8292
8293 clear_oparg(&oa);
8294 oa.regname = eap->regname;
8295 oa.start.lnum = eap->line1;
8296 oa.end.lnum = eap->line2;
8297 oa.line_count = eap->line2 - eap->line1 + 1;
8298 oa.motion_type = MLINE;
8299#ifdef FEAT_VIRTUALEDIT
8300 virtual_op = FALSE;
8301#endif
8302 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8303 {
8304 setpcmark();
8305 curwin->w_cursor.lnum = eap->line1;
8306 beginline(BL_SOL | BL_FIX);
8307 }
8308
8309 switch (eap->cmdidx)
8310 {
8311 case CMD_delete:
8312 oa.op_type = OP_DELETE;
8313 op_delete(&oa);
8314 break;
8315
8316 case CMD_yank:
8317 oa.op_type = OP_YANK;
8318 (void)op_yank(&oa, FALSE, TRUE);
8319 break;
8320
8321 default: /* CMD_rshift or CMD_lshift */
8322 if ((eap->cmdidx == CMD_rshift)
8323#ifdef FEAT_RIGHTLEFT
8324 ^ curwin->w_p_rl
8325#endif
8326 )
8327 oa.op_type = OP_RSHIFT;
8328 else
8329 oa.op_type = OP_LSHIFT;
8330 op_shift(&oa, FALSE, eap->amount);
8331 break;
8332 }
8333#ifdef FEAT_VIRTUALEDIT
8334 virtual_op = MAYBE;
8335#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008336 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337}
8338
8339/*
8340 * ":put".
8341 */
8342 static void
8343ex_put(eap)
8344 exarg_T *eap;
8345{
8346 /* ":0put" works like ":1put!". */
8347 if (eap->line2 == 0)
8348 {
8349 eap->line2 = 1;
8350 eap->forceit = TRUE;
8351 }
8352 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008353 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8354 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355}
8356
8357/*
8358 * Handle ":copy" and ":move".
8359 */
8360 static void
8361ex_copymove(eap)
8362 exarg_T *eap;
8363{
8364 long n;
8365
8366 n = get_address(&eap->arg, FALSE, FALSE);
8367 if (eap->arg == NULL) /* error detected */
8368 {
8369 eap->nextcmd = NULL;
8370 return;
8371 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008372 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
8374 /*
8375 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8376 */
8377 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8378 {
8379 EMSG(_(e_invaddr));
8380 return;
8381 }
8382
8383 if (eap->cmdidx == CMD_move)
8384 {
8385 if (do_move(eap->line1, eap->line2, n) == FAIL)
8386 return;
8387 }
8388 else
8389 ex_copy(eap->line1, eap->line2, n);
8390 u_clearline();
8391 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008392 ex_may_print(eap);
8393}
8394
8395/*
8396 * Print the current line if flags were given to the Ex command.
8397 */
8398 static void
8399ex_may_print(eap)
8400 exarg_T *eap;
8401{
8402 if (eap->flags != 0)
8403 {
8404 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8405 (eap->flags & EXFLAG_LIST));
8406 ex_no_reprint = TRUE;
8407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408}
8409
8410/*
8411 * ":smagic" and ":snomagic".
8412 */
8413 static void
8414ex_submagic(eap)
8415 exarg_T *eap;
8416{
8417 int magic_save = p_magic;
8418
8419 p_magic = (eap->cmdidx == CMD_smagic);
8420 do_sub(eap);
8421 p_magic = magic_save;
8422}
8423
8424/*
8425 * ":join".
8426 */
8427 static void
8428ex_join(eap)
8429 exarg_T *eap;
8430{
8431 curwin->w_cursor.lnum = eap->line1;
8432 if (eap->line1 == eap->line2)
8433 {
8434 if (eap->addr_count >= 2) /* :2,2join does nothing */
8435 return;
8436 if (eap->line2 == curbuf->b_ml.ml_line_count)
8437 {
8438 beep_flush();
8439 return;
8440 }
8441 ++eap->line2;
8442 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008443 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008445 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446}
8447
8448/*
8449 * ":[addr]@r" or ":[addr]*r": execute register
8450 */
8451 static void
8452ex_at(eap)
8453 exarg_T *eap;
8454{
8455 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008456 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457
8458 curwin->w_cursor.lnum = eap->line2;
8459
8460#ifdef USE_ON_FLY_SCROLL
8461 dont_scroll = TRUE; /* disallow scrolling here */
8462#endif
8463
8464 /* get the register name. No name means to use the previous one */
8465 c = *eap->arg;
8466 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8467 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008468 /* Put the register in the typeahead buffer with the "silent" flag. */
8469 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8470 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008471 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 else
8475 {
8476 int save_efr = exec_from_reg;
8477
8478 exec_from_reg = TRUE;
8479
8480 /*
8481 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008482 * Continue until the stuff buffer is empty and all added characters
8483 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008485 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8487
8488 exec_from_reg = save_efr;
8489 }
8490}
8491
8492/*
8493 * ":!".
8494 */
8495 static void
8496ex_bang(eap)
8497 exarg_T *eap;
8498{
8499 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8500}
8501
8502/*
8503 * ":undo".
8504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 static void
8506ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008507 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008509 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008510 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008511 else
8512 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513}
8514
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008515#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008516 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008517ex_wundo(eap)
8518 exarg_T *eap;
8519{
8520 char_u hash[UNDO_HASH_SIZE];
8521
8522 u_compute_hash(hash);
8523 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8524}
8525
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008526 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008527ex_rundo(eap)
8528 exarg_T *eap;
8529{
8530 char_u hash[UNDO_HASH_SIZE];
8531
8532 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008533 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008534}
8535#endif
8536
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537/*
8538 * ":redo".
8539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 static void
8541ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008542 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 u_redo(1);
8545}
8546
8547/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008548 * ":earlier" and ":later".
8549 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008550 static void
8551ex_later(eap)
8552 exarg_T *eap;
8553{
8554 long count = 0;
8555 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008556 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008557 char_u *p = eap->arg;
8558
8559 if (*p == NUL)
8560 count = 1;
8561 else if (isdigit(*p))
8562 {
8563 count = getdigits(&p);
8564 switch (*p)
8565 {
8566 case 's': ++p; sec = TRUE; break;
8567 case 'm': ++p; sec = TRUE; count *= 60; break;
8568 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008569 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8570 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008571 }
8572 }
8573
8574 if (*p != NUL)
8575 EMSG2(_(e_invarg2), eap->arg);
8576 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008577 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8578 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008579}
8580
8581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 * ":redir": start/stop redirection.
8583 */
8584 static void
8585ex_redir(eap)
8586 exarg_T *eap;
8587{
8588 char *mode;
8589 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008590 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591
8592 if (STRICMP(eap->arg, "END") == 0)
8593 close_redir();
8594 else
8595 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008596 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008598 ++arg;
8599 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008601 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 mode = "a";
8603 }
8604 else
8605 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008606 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607
8608 close_redir();
8609
8610 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008611 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (fname == NULL)
8613 return;
8614#ifdef FEAT_BROWSE
8615 if (cmdmod.browse)
8616 {
8617 char_u *browseFile;
8618
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008619 browseFile = do_browse(BROWSE_SAVE,
8620 (char_u *)_("Save Redirection"),
8621 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 if (browseFile == NULL)
8623 return; /* operation cancelled */
8624 vim_free(fname);
8625 fname = browseFile;
8626 eap->forceit = TRUE; /* since dialog already asked */
8627 }
8628#endif
8629
8630 redir_fd = open_exfile(fname, eap->forceit, mode);
8631 vim_free(fname);
8632 }
8633#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008634 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {
8636 /* redirect to a register a-z (resp. A-Z for appending) */
8637 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008638 ++arg;
8639 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008641 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008642 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008644 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008646 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008647 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008648 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008649 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008651 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008652 if (*arg == '>')
8653 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008654 /* Make register empty when not using @A-@Z and the
8655 * command is valid. */
8656 if (*arg == NUL && !isupper(redir_reg))
8657 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008659 }
8660 if (*arg != NUL)
8661 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008662 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008663 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008664 }
8665 }
8666 else if (*arg == '=' && arg[1] == '>')
8667 {
8668 int append;
8669
8670 /* redirect to a variable */
8671 close_redir();
8672 arg += 2;
8673
8674 if (*arg == '>')
8675 {
8676 ++arg;
8677 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 }
8679 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008680 append = FALSE;
8681
8682 if (var_redir_start(skipwhite(arg), append) == OK)
8683 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 }
8685#endif
8686
8687 /* TODO: redirect to a buffer */
8688
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008690 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008692
8693 /* Make sure redirection is not off. Can happen for cmdline completion
8694 * that indirectly invokes a command to catch its output. */
8695 if (redir_fd != NULL
8696#ifdef FEAT_EVAL
8697 || redir_reg || redir_vname
8698#endif
8699 )
8700 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701}
8702
8703/*
8704 * ":redraw": force redraw
8705 */
8706 static void
8707ex_redraw(eap)
8708 exarg_T *eap;
8709{
8710 int r = RedrawingDisabled;
8711 int p = p_lz;
8712
8713 RedrawingDisabled = 0;
8714 p_lz = FALSE;
8715 update_topline();
8716 update_screen(eap->forceit ? CLEAR :
8717#ifdef FEAT_VISUAL
8718 VIsual_active ? INVERTED :
8719#endif
8720 0);
8721#ifdef FEAT_TITLE
8722 if (need_maketitle)
8723 maketitle();
8724#endif
8725 RedrawingDisabled = r;
8726 p_lz = p;
8727
8728 /* Reset msg_didout, so that a message that's there is overwritten. */
8729 msg_didout = FALSE;
8730 msg_col = 0;
8731
8732 out_flush();
8733}
8734
8735/*
8736 * ":redrawstatus": force redraw of status line(s)
8737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 static void
8739ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008740 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741{
8742#if defined(FEAT_WINDOWS)
8743 int r = RedrawingDisabled;
8744 int p = p_lz;
8745
8746 RedrawingDisabled = 0;
8747 p_lz = FALSE;
8748 if (eap->forceit)
8749 status_redraw_all();
8750 else
8751 status_redraw_curbuf();
8752 update_screen(
8753# ifdef FEAT_VISUAL
8754 VIsual_active ? INVERTED :
8755# endif
8756 0);
8757 RedrawingDisabled = r;
8758 p_lz = p;
8759 out_flush();
8760#endif
8761}
8762
8763 static void
8764close_redir()
8765{
8766 if (redir_fd != NULL)
8767 {
8768 fclose(redir_fd);
8769 redir_fd = NULL;
8770 }
8771#ifdef FEAT_EVAL
8772 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008773 if (redir_vname)
8774 {
8775 var_redir_stop();
8776 redir_vname = 0;
8777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778#endif
8779}
8780
8781#if defined(FEAT_SESSION) && defined(USE_CRNL)
8782# define MKSESSION_NL
8783static int mksession_nl = FALSE; /* use NL only in put_eol() */
8784#endif
8785
8786/*
8787 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8788 */
8789 static void
8790ex_mkrc(eap)
8791 exarg_T *eap;
8792{
8793 FILE *fd;
8794 int failed = FALSE;
8795 char_u *fname;
8796#ifdef FEAT_BROWSE
8797 char_u *browseFile = NULL;
8798#endif
8799#ifdef FEAT_SESSION
8800 int view_session = FALSE;
8801 int using_vdir = FALSE; /* using 'viewdir'? */
8802 char_u *viewFile = NULL;
8803 unsigned *flagp;
8804#endif
8805
8806 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8807 {
8808#ifdef FEAT_SESSION
8809 view_session = TRUE;
8810#else
8811 ex_ni(eap);
8812 return;
8813#endif
8814 }
8815
8816#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008817 /* Use the short file name until ":lcd" is used. We also don't use the
8818 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008819 did_lcd = FALSE;
8820
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8822 if (eap->cmdidx == CMD_mkview
8823 && (*eap->arg == NUL
8824 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8825 {
8826 eap->forceit = TRUE;
8827 fname = get_view_file(*eap->arg);
8828 if (fname == NULL)
8829 return;
8830 viewFile = fname;
8831 using_vdir = TRUE;
8832 }
8833 else
8834#endif
8835 if (*eap->arg != NUL)
8836 fname = eap->arg;
8837 else if (eap->cmdidx == CMD_mkvimrc)
8838 fname = (char_u *)VIMRC_FILE;
8839#ifdef FEAT_SESSION
8840 else if (eap->cmdidx == CMD_mksession)
8841 fname = (char_u *)SESSION_FILE;
8842#endif
8843 else
8844 fname = (char_u *)EXRC_FILE;
8845
8846#ifdef FEAT_BROWSE
8847 if (cmdmod.browse)
8848 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008849 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850# ifdef FEAT_SESSION
8851 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8852 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8853# endif
8854 (char_u *)_("Save Setup"),
8855 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8856 if (browseFile == NULL)
8857 goto theend;
8858 fname = browseFile;
8859 eap->forceit = TRUE; /* since dialog already asked */
8860 }
8861#endif
8862
8863#if defined(FEAT_SESSION) && defined(vim_mkdir)
8864 /* When using 'viewdir' may have to create the directory. */
8865 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008866 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867#endif
8868
8869 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8870 if (fd != NULL)
8871 {
8872#ifdef FEAT_SESSION
8873 if (eap->cmdidx == CMD_mkview)
8874 flagp = &vop_flags;
8875 else
8876 flagp = &ssop_flags;
8877#endif
8878
8879#ifdef MKSESSION_NL
8880 /* "unix" in 'sessionoptions': use NL line separator */
8881 if (view_session && (*flagp & SSOP_UNIX))
8882 mksession_nl = TRUE;
8883#endif
8884
8885 /* Write the version command for :mkvimrc */
8886 if (eap->cmdidx == CMD_mkvimrc)
8887 (void)put_line(fd, "version 6.0");
8888
8889#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008890 if (eap->cmdidx == CMD_mksession)
8891 {
8892 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8893 failed = TRUE;
8894 }
8895
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 if (eap->cmdidx != CMD_mkview)
8897#endif
8898 {
8899 /* Write setting 'compatible' first, because it has side effects.
8900 * For that same reason only do it when needed. */
8901 if (p_cp)
8902 (void)put_line(fd, "if !&cp | set cp | endif");
8903 else
8904 (void)put_line(fd, "if &cp | set nocp | endif");
8905 }
8906
8907#ifdef FEAT_SESSION
8908 if (!view_session
8909 || (eap->cmdidx == CMD_mksession
8910 && (*flagp & SSOP_OPTIONS)))
8911#endif
8912 failed |= (makemap(fd, NULL) == FAIL
8913 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8914
8915#ifdef FEAT_SESSION
8916 if (!failed && view_session)
8917 {
8918 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8919 failed = TRUE;
8920 if (eap->cmdidx == CMD_mksession)
8921 {
8922 char_u dirnow[MAXPATHL]; /* current directory */
8923
8924 /*
8925 * Change to session file's dir.
8926 */
8927 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8928 || mch_chdir((char *)dirnow) != 0)
8929 *dirnow = NUL;
8930 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8931 {
8932 if (vim_chdirfile(fname) == OK)
8933 shorten_fnames(TRUE);
8934 }
8935 else if (*dirnow != NUL
8936 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8937 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008938 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008939 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 }
8941
8942 failed |= (makeopens(fd, dirnow) == FAIL);
8943
8944 /* restore original dir */
8945 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8946 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8947 {
8948 if (mch_chdir((char *)dirnow) != 0)
8949 EMSG(_(e_prev_dir));
8950 shorten_fnames(TRUE);
8951 }
8952 }
8953 else
8954 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008955 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8956 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8959 == FAIL)
8960 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008961 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8962 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008963 if (eap->cmdidx == CMD_mksession)
8964 {
8965 if (put_line(fd, "unlet SessionLoad") == FAIL)
8966 failed = TRUE;
8967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 }
8969#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008970 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8971 failed = TRUE;
8972
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973 failed |= fclose(fd);
8974
8975 if (failed)
8976 EMSG(_(e_write));
8977#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8978 else if (eap->cmdidx == CMD_mksession)
8979 {
8980 /* successful session write - set this_session var */
8981 char_u tbuf[MAXPATHL];
8982
8983 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8984 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8985 }
8986#endif
8987#ifdef MKSESSION_NL
8988 mksession_nl = FALSE;
8989#endif
8990 }
8991
8992#ifdef FEAT_BROWSE
8993theend:
8994 vim_free(browseFile);
8995#endif
8996#ifdef FEAT_SESSION
8997 vim_free(viewFile);
8998#endif
8999}
9000
Bram Moolenaardf177f62005-02-22 08:39:57 +00009001#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
9002 || defined(PROTO)
9003 int
9004vim_mkdir_emsg(name, prot)
9005 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00009006 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00009007{
9008 if (vim_mkdir(name, prot) != 0)
9009 {
9010 EMSG2(_("E739: Cannot create directory: %s"), name);
9011 return FAIL;
9012 }
9013 return OK;
9014}
9015#endif
9016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017/*
9018 * Open a file for writing for an Ex command, with some checks.
9019 * Return file descriptor, or NULL on failure.
9020 */
9021 FILE *
9022open_exfile(fname, forceit, mode)
9023 char_u *fname;
9024 int forceit;
9025 char *mode; /* "w" for create new file or "a" for append */
9026{
9027 FILE *fd;
9028
9029#ifdef UNIX
9030 /* with Unix it is possible to open a directory */
9031 if (mch_isdir(fname))
9032 {
9033 EMSG2(_(e_isadir2), fname);
9034 return NULL;
9035 }
9036#endif
9037 if (!forceit && *mode != 'a' && vim_fexists(fname))
9038 {
9039 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9040 return NULL;
9041 }
9042
9043 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9044 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9045
9046 return fd;
9047}
9048
9049/*
9050 * ":mark" and ":k".
9051 */
9052 static void
9053ex_mark(eap)
9054 exarg_T *eap;
9055{
9056 pos_T pos;
9057
9058 if (*eap->arg == NUL) /* No argument? */
9059 EMSG(_(e_argreq));
9060 else if (eap->arg[1] != NUL) /* more than one character? */
9061 EMSG(_(e_trailing));
9062 else
9063 {
9064 pos = curwin->w_cursor; /* save curwin->w_cursor */
9065 curwin->w_cursor.lnum = eap->line2;
9066 beginline(BL_WHITE | BL_FIX);
9067 if (setmark(*eap->arg) == FAIL) /* set mark */
9068 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9069 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9070 }
9071}
9072
9073/*
9074 * Update w_topline, w_leftcol and the cursor position.
9075 */
9076 void
9077update_topline_cursor()
9078{
9079 check_cursor(); /* put cursor on valid line */
9080 update_topline();
9081 if (!curwin->w_p_wrap)
9082 validate_cursor();
9083 update_curswant();
9084}
9085
9086#ifdef FEAT_EX_EXTRA
9087/*
9088 * ":normal[!] {commands}": Execute normal mode commands.
9089 */
9090 static void
9091ex_normal(eap)
9092 exarg_T *eap;
9093{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 int save_msg_scroll = msg_scroll;
9095 int save_restart_edit = restart_edit;
9096 int save_msg_didout = msg_didout;
9097 int save_State = State;
9098 tasave_T tabuf;
9099 int save_insertmode = p_im;
9100 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009101 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102#ifdef FEAT_MBYTE
9103 char_u *arg = NULL;
9104 int l;
9105 char_u *p;
9106#endif
9107
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009108 if (ex_normal_lock > 0)
9109 {
9110 EMSG(_(e_secure));
9111 return;
9112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 if (ex_normal_busy >= p_mmd)
9114 {
9115 EMSG(_("E192: Recursive use of :normal too deep"));
9116 return;
9117 }
9118 ++ex_normal_busy;
9119
9120 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9121 restart_edit = 0; /* don't go to Insert mode */
9122 p_im = FALSE; /* don't use 'insertmode' */
9123
9124#ifdef FEAT_MBYTE
9125 /*
9126 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9127 * this for the K_SPECIAL leading byte, otherwise special keys will not
9128 * work.
9129 */
9130 if (has_mbyte)
9131 {
9132 int len = 0;
9133
9134 /* Count the number of characters to be escaped. */
9135 for (p = eap->arg; *p != NUL; ++p)
9136 {
9137# ifdef FEAT_GUI
9138 if (*p == CSI) /* leadbyte CSI */
9139 len += 2;
9140# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009141 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9143# ifdef FEAT_GUI
9144 || *p == CSI
9145# endif
9146 )
9147 len += 2;
9148 }
9149 if (len > 0)
9150 {
9151 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9152 if (arg != NULL)
9153 {
9154 len = 0;
9155 for (p = eap->arg; *p != NUL; ++p)
9156 {
9157 arg[len++] = *p;
9158# ifdef FEAT_GUI
9159 if (*p == CSI)
9160 {
9161 arg[len++] = KS_EXTRA;
9162 arg[len++] = (int)KE_CSI;
9163 }
9164# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009165 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 {
9167 arg[len++] = *++p;
9168 if (*p == K_SPECIAL)
9169 {
9170 arg[len++] = KS_SPECIAL;
9171 arg[len++] = KE_FILLER;
9172 }
9173# ifdef FEAT_GUI
9174 else if (*p == CSI)
9175 {
9176 arg[len++] = KS_EXTRA;
9177 arg[len++] = (int)KE_CSI;
9178 }
9179# endif
9180 }
9181 arg[len] = NUL;
9182 }
9183 }
9184 }
9185 }
9186#endif
9187
9188 /*
9189 * Save the current typeahead. This is required to allow using ":normal"
9190 * from an event handler and makes sure we don't hang when the argument
9191 * ends with half a command.
9192 */
9193 save_typeahead(&tabuf);
9194 if (tabuf.typebuf_valid)
9195 {
9196 /*
9197 * Repeat the :normal command for each line in the range. When no
9198 * range given, execute it just once, without positioning the cursor
9199 * first.
9200 */
9201 do
9202 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 if (eap->addr_count != 0)
9204 {
9205 curwin->w_cursor.lnum = eap->line1++;
9206 curwin->w_cursor.col = 0;
9207 }
9208
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009209 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210#ifdef FEAT_MBYTE
9211 arg != NULL ? arg :
9212#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009213 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 }
9215 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9216 }
9217
9218 /* Might not return to the main loop when in an event handler. */
9219 update_topline_cursor();
9220
9221 /* Restore the previous typeahead. */
9222 restore_typeahead(&tabuf);
9223
9224 --ex_normal_busy;
9225 msg_scroll = save_msg_scroll;
9226 restart_edit = save_restart_edit;
9227 p_im = save_insertmode;
9228 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009229 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9231
9232 /* Restore the state (needed when called from a function executed for
9233 * 'indentexpr'). */
9234 State = save_State;
9235#ifdef FEAT_MBYTE
9236 vim_free(arg);
9237#endif
9238}
9239
9240/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009241 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 */
9243 static void
9244ex_startinsert(eap)
9245 exarg_T *eap;
9246{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009247 if (eap->forceit)
9248 {
9249 coladvance((colnr_T)MAXCOL);
9250 curwin->w_curswant = MAXCOL;
9251 curwin->w_set_curswant = FALSE;
9252 }
9253
Bram Moolenaara40c5002005-01-09 21:16:21 +00009254 /* Ignore the command when already in Insert mode. Inserting an
9255 * expression register that invokes a function can do this. */
9256 if (State & INSERT)
9257 return;
9258
Bram Moolenaar64969662005-12-14 21:59:55 +00009259 if (eap->cmdidx == CMD_startinsert)
9260 restart_edit = 'a';
9261 else if (eap->cmdidx == CMD_startreplace)
9262 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009264 restart_edit = 'V';
9265
9266 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009268 if (eap->cmdidx == CMD_startinsert)
9269 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 curwin->w_curswant = 0; /* avoid MAXCOL */
9271 }
9272}
9273
9274/*
9275 * ":stopinsert"
9276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 static void
9278ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009279 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
9281 restart_edit = 0;
9282 stop_insert_mode = TRUE;
9283}
9284#endif
9285
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009286#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9287/*
9288 * Execute normal mode command "cmd".
9289 * "remap" can be REMAP_NONE or REMAP_YES.
9290 */
9291 void
9292exec_normal_cmd(cmd, remap, silent)
9293 char_u *cmd;
9294 int remap;
9295 int silent;
9296{
9297 oparg_T oa;
9298
9299 /*
9300 * Stuff the argument into the typeahead buffer.
9301 * Execute normal_cmd() until there is no typeahead left.
9302 */
9303 clear_oparg(&oa);
9304 finish_op = FALSE;
9305 ins_typebuf(cmd, remap, 0, TRUE, silent);
9306 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9307 && !got_int)
9308 {
9309 update_topline_cursor();
9310 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9311 }
9312}
9313#endif
9314
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315#ifdef FEAT_FIND_ID
9316 static void
9317ex_checkpath(eap)
9318 exarg_T *eap;
9319{
9320 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9321 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9322 (linenr_T)1, (linenr_T)MAXLNUM);
9323}
9324
9325#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9326/*
9327 * ":psearch"
9328 */
9329 static void
9330ex_psearch(eap)
9331 exarg_T *eap;
9332{
9333 g_do_tagpreview = p_pvh;
9334 ex_findpat(eap);
9335 g_do_tagpreview = 0;
9336}
9337#endif
9338
9339 static void
9340ex_findpat(eap)
9341 exarg_T *eap;
9342{
9343 int whole = TRUE;
9344 long n;
9345 char_u *p;
9346 int action;
9347
9348 switch (cmdnames[eap->cmdidx].cmd_name[2])
9349 {
9350 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9351 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9352 action = ACTION_GOTO;
9353 else
9354 action = ACTION_SHOW;
9355 break;
9356 case 'i': /* ":ilist" and ":dlist" */
9357 action = ACTION_SHOW_ALL;
9358 break;
9359 case 'u': /* ":ijump" and ":djump" */
9360 action = ACTION_GOTO;
9361 break;
9362 default: /* ":isplit" and ":dsplit" */
9363 action = ACTION_SPLIT;
9364 break;
9365 }
9366
9367 n = 1;
9368 if (vim_isdigit(*eap->arg)) /* get count */
9369 {
9370 n = getdigits(&eap->arg);
9371 eap->arg = skipwhite(eap->arg);
9372 }
9373 if (*eap->arg == '/') /* Match regexp, not just whole words */
9374 {
9375 whole = FALSE;
9376 ++eap->arg;
9377 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9378 if (*p)
9379 {
9380 *p++ = NUL;
9381 p = skipwhite(p);
9382
9383 /* Check for trailing illegal characters */
9384 if (!ends_excmd(*p))
9385 eap->errmsg = e_trailing;
9386 else
9387 eap->nextcmd = check_nextcmd(p);
9388 }
9389 }
9390 if (!eap->skip)
9391 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9392 whole, !eap->forceit,
9393 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9394 n, action, eap->line1, eap->line2);
9395}
9396#endif
9397
9398#ifdef FEAT_WINDOWS
9399
9400# ifdef FEAT_QUICKFIX
9401/*
9402 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9403 */
9404 static void
9405ex_ptag(eap)
9406 exarg_T *eap;
9407{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009408 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9410}
9411
9412/*
9413 * ":pedit"
9414 */
9415 static void
9416ex_pedit(eap)
9417 exarg_T *eap;
9418{
9419 win_T *curwin_save = curwin;
9420
9421 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009422 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 keep_help_flag = curwin_save->w_buffer->b_help;
9424 do_exedit(eap, NULL);
9425 keep_help_flag = FALSE;
9426 if (curwin != curwin_save && win_valid(curwin_save))
9427 {
9428 /* Return cursor to where we were */
9429 validate_cursor();
9430 redraw_later(VALID);
9431 win_enter(curwin_save, TRUE);
9432 }
9433 g_do_tagpreview = 0;
9434}
9435# endif
9436
9437/*
9438 * ":stag", ":stselect" and ":stjump".
9439 */
9440 static void
9441ex_stag(eap)
9442 exarg_T *eap;
9443{
9444 postponed_split = -1;
9445 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009446 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9448 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009449 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450}
9451#endif
9452
9453/*
9454 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9455 */
9456 static void
9457ex_tag(eap)
9458 exarg_T *eap;
9459{
9460 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9461}
9462
9463 static void
9464ex_tag_cmd(eap, name)
9465 exarg_T *eap;
9466 char_u *name;
9467{
9468 int cmd;
9469
9470 switch (name[1])
9471 {
9472 case 'j': cmd = DT_JUMP; /* ":tjump" */
9473 break;
9474 case 's': cmd = DT_SELECT; /* ":tselect" */
9475 break;
9476 case 'p': cmd = DT_PREV; /* ":tprevious" */
9477 break;
9478 case 'N': cmd = DT_PREV; /* ":tNext" */
9479 break;
9480 case 'n': cmd = DT_NEXT; /* ":tnext" */
9481 break;
9482 case 'o': cmd = DT_POP; /* ":pop" */
9483 break;
9484 case 'f': /* ":tfirst" */
9485 case 'r': cmd = DT_FIRST; /* ":trewind" */
9486 break;
9487 case 'l': cmd = DT_LAST; /* ":tlast" */
9488 break;
9489 default: /* ":tag" */
9490#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009491 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492 {
9493 do_cstag(eap);
9494 return;
9495 }
9496#endif
9497 cmd = DT_TAG;
9498 break;
9499 }
9500
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009501 if (name[0] == 'l')
9502 {
9503#ifndef FEAT_QUICKFIX
9504 ex_ni(eap);
9505 return;
9506#else
9507 cmd = DT_LTAG;
9508#endif
9509 }
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9512 eap->forceit, TRUE);
9513}
9514
9515/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009516 * Check "str" for starting with a special cmdline variable.
9517 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9518 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9519 */
9520 int
9521find_cmdline_var(src, usedlen)
9522 char_u *src;
9523 int *usedlen;
9524{
9525 int len;
9526 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009527 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009528 "%",
9529#define SPEC_PERC 0
9530 "#",
9531#define SPEC_HASH 1
9532 "<cword>", /* cursor word */
9533#define SPEC_CWORD 2
9534 "<cWORD>", /* cursor WORD */
9535#define SPEC_CCWORD 3
9536 "<cfile>", /* cursor path name */
9537#define SPEC_CFILE 4
9538 "<sfile>", /* ":so" file name */
9539#define SPEC_SFILE 5
9540#ifdef FEAT_AUTOCMD
9541 "<afile>", /* autocommand file name */
9542# define SPEC_AFILE 6
9543 "<abuf>", /* autocommand buffer number */
9544# define SPEC_ABUF 7
9545 "<amatch>", /* autocommand match name */
9546# define SPEC_AMATCH 8
9547#endif
9548#ifdef FEAT_CLIENTSERVER
9549 "<client>"
9550# define SPEC_CLIENT 9
9551#endif
9552 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009553
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009554 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009555 {
9556 len = (int)STRLEN(spec_str[i]);
9557 if (STRNCMP(src, spec_str[i], len) == 0)
9558 {
9559 *usedlen = len;
9560 return i;
9561 }
9562 }
9563 return -1;
9564}
9565
9566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 * Evaluate cmdline variables.
9568 *
9569 * change '%' to curbuf->b_ffname
9570 * '#' to curwin->w_altfile
9571 * '<cword>' to word under the cursor
9572 * '<cWORD>' to WORD under the cursor
9573 * '<cfile>' to path name under the cursor
9574 * '<sfile>' to sourced file name
9575 * '<afile>' to file name for autocommand
9576 * '<abuf>' to buffer number for autocommand
9577 * '<amatch>' to matching name for autocommand
9578 *
9579 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9580 * "" for error without a message) and NULL is returned.
9581 * Returns an allocated string if a valid match was found.
9582 * Returns NULL if no match was found. "usedlen" then still contains the
9583 * number of characters to skip.
9584 */
9585 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009586eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009588 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 int *usedlen; /* characters after src that are used */
9590 linenr_T *lnump; /* line number for :e command, or NULL */
9591 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009592 int *escaped; /* return value has escaped white space (can
9593 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594{
9595 int i;
9596 char_u *s;
9597 char_u *result;
9598 char_u *resultbuf = NULL;
9599 int resultlen;
9600 buf_T *buf;
9601 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9602 int spec_idx;
9603#ifdef FEAT_MODIFY_FNAME
9604 int skip_mod = FALSE;
9605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
9607#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9608 char_u strbuf[30];
9609#endif
9610
9611 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009612 if (escaped != NULL)
9613 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614
9615 /*
9616 * Check if there is something to do.
9617 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009618 spec_idx = find_cmdline_var(src, usedlen);
9619 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620 {
9621 *usedlen = 1;
9622 return NULL;
9623 }
9624
9625 /*
9626 * Skip when preceded with a backslash "\%" and "\#".
9627 * Note: In "\\%" the % is also not recognized!
9628 */
9629 if (src > srcstart && src[-1] == '\\')
9630 {
9631 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009632 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 return NULL;
9634 }
9635
9636 /*
9637 * word or WORD under cursor
9638 */
9639 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9640 {
9641 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9642 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9643 if (resultlen == 0)
9644 {
9645 *errormsg = (char_u *)"";
9646 return NULL;
9647 }
9648 }
9649
9650 /*
9651 * '#': Alternate file name
9652 * '%': Current file name
9653 * File name under the cursor
9654 * File name for autocommand
9655 * and following modifiers
9656 */
9657 else
9658 {
9659 switch (spec_idx)
9660 {
9661 case SPEC_PERC: /* '%': current file */
9662 if (curbuf->b_fname == NULL)
9663 {
9664 result = (char_u *)"";
9665 valid = 0; /* Must have ":p:h" to be valid */
9666 }
9667 else
9668#ifdef RISCOS
9669 /* Always use the full path for RISC OS if possible. */
9670 result = curbuf->b_ffname;
9671 if (result == NULL)
9672 result = curbuf->b_fname;
9673#else
9674 result = curbuf->b_fname;
9675#endif
9676 break;
9677
9678 case SPEC_HASH: /* '#' or "#99": alternate file */
9679 if (src[1] == '#') /* "##": the argument list */
9680 {
9681 result = arg_all();
9682 resultbuf = result;
9683 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009684 if (escaped != NULL)
9685 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686#ifdef FEAT_MODIFY_FNAME
9687 skip_mod = TRUE;
9688#endif
9689 break;
9690 }
9691 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009692 if (*s == '<') /* "#<99" uses v:oldfiles */
9693 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 i = (int)getdigits(&s);
9695 *usedlen = (int)(s - src); /* length of what we expand */
9696
Bram Moolenaard812df62008-11-09 12:46:09 +00009697 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009699 if (*usedlen < 2)
9700 {
9701 /* Should we give an error message for #<text? */
9702 *usedlen = 1;
9703 return NULL;
9704 }
9705#ifdef FEAT_EVAL
9706 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9707 (long)i);
9708 if (result == NULL)
9709 {
9710 *errormsg = (char_u *)"";
9711 return NULL;
9712 }
9713#else
9714 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 }
9718 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009719 {
9720 buf = buflist_findnr(i);
9721 if (buf == NULL)
9722 {
9723 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9724 return NULL;
9725 }
9726 if (lnump != NULL)
9727 *lnump = ECMD_LAST;
9728 if (buf->b_fname == NULL)
9729 {
9730 result = (char_u *)"";
9731 valid = 0; /* Must have ":p:h" to be valid */
9732 }
9733 else
9734 result = buf->b_fname;
9735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 break;
9737
9738#ifdef FEAT_SEARCHPATH
9739 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009740 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (result == NULL)
9742 {
9743 *errormsg = (char_u *)"";
9744 return NULL;
9745 }
9746 resultbuf = result; /* remember allocated string */
9747 break;
9748#endif
9749
9750#ifdef FEAT_AUTOCMD
9751 case SPEC_AFILE: /* file name for autocommand */
9752 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009753 if (result != NULL && !autocmd_fname_full)
9754 {
9755 /* Still need to turn the fname into a full path. It is
9756 * postponed to avoid a delay when <afile> is not used. */
9757 autocmd_fname_full = TRUE;
9758 result = FullName_save(autocmd_fname, FALSE);
9759 vim_free(autocmd_fname);
9760 autocmd_fname = result;
9761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 if (result == NULL)
9763 {
9764 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9765 return NULL;
9766 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009767 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 break;
9769
9770 case SPEC_ABUF: /* buffer number for autocommand */
9771 if (autocmd_bufnr <= 0)
9772 {
9773 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9774 return NULL;
9775 }
9776 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9777 result = strbuf;
9778 break;
9779
9780 case SPEC_AMATCH: /* match name for autocommand */
9781 result = autocmd_match;
9782 if (result == NULL)
9783 {
9784 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9785 return NULL;
9786 }
9787 break;
9788
9789#endif
9790 case SPEC_SFILE: /* file name for ":so" command */
9791 result = sourcing_name;
9792 if (result == NULL)
9793 {
9794 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9795 return NULL;
9796 }
9797 break;
9798#if defined(FEAT_CLIENTSERVER)
9799 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009800 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9801 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 result = strbuf;
9803 break;
9804#endif
9805 }
9806
9807 resultlen = (int)STRLEN(result); /* length of new string */
9808 if (src[*usedlen] == '<') /* remove the file name extension */
9809 {
9810 ++*usedlen;
9811#ifdef RISCOS
9812 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9813#else
9814 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9815#endif
9816 resultlen = (int)(s - result);
9817 }
9818#ifdef FEAT_MODIFY_FNAME
9819 else if (!skip_mod)
9820 {
9821 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9822 &resultlen);
9823 if (result == NULL)
9824 {
9825 *errormsg = (char_u *)"";
9826 return NULL;
9827 }
9828 }
9829#endif
9830 }
9831
9832 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9833 {
9834 if (valid != VALID_HEAD + VALID_PATH)
9835 /* xgettext:no-c-format */
9836 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9837 else
9838 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9839 result = NULL;
9840 }
9841 else
9842 result = vim_strnsave(result, resultlen);
9843 vim_free(resultbuf);
9844 return result;
9845}
9846
9847/*
9848 * Concatenate all files in the argument list, separated by spaces, and return
9849 * it in one allocated string.
9850 * Spaces and backslashes in the file names are escaped with a backslash.
9851 * Returns NULL when out of memory.
9852 */
9853 static char_u *
9854arg_all()
9855{
9856 int len;
9857 int idx;
9858 char_u *retval = NULL;
9859 char_u *p;
9860
9861 /*
9862 * Do this loop two times:
9863 * first time: compute the total length
9864 * second time: concatenate the names
9865 */
9866 for (;;)
9867 {
9868 len = 0;
9869 for (idx = 0; idx < ARGCOUNT; ++idx)
9870 {
9871 p = alist_name(&ARGLIST[idx]);
9872 if (p != NULL)
9873 {
9874 if (len > 0)
9875 {
9876 /* insert a space in between names */
9877 if (retval != NULL)
9878 retval[len] = ' ';
9879 ++len;
9880 }
9881 for ( ; *p != NUL; ++p)
9882 {
9883 if (*p == ' ' || *p == '\\')
9884 {
9885 /* insert a backslash */
9886 if (retval != NULL)
9887 retval[len] = '\\';
9888 ++len;
9889 }
9890 if (retval != NULL)
9891 retval[len] = *p;
9892 ++len;
9893 }
9894 }
9895 }
9896
9897 /* second time: break here */
9898 if (retval != NULL)
9899 {
9900 retval[len] = NUL;
9901 break;
9902 }
9903
9904 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009905 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 if (retval == NULL)
9907 break;
9908 }
9909
9910 return retval;
9911}
9912
9913#if defined(FEAT_AUTOCMD) || defined(PROTO)
9914/*
9915 * Expand the <sfile> string in "arg".
9916 *
9917 * Returns an allocated string, or NULL for any error.
9918 */
9919 char_u *
9920expand_sfile(arg)
9921 char_u *arg;
9922{
9923 char_u *errormsg;
9924 int len;
9925 char_u *result;
9926 char_u *newres;
9927 char_u *repl;
9928 int srclen;
9929 char_u *p;
9930
9931 result = vim_strsave(arg);
9932 if (result == NULL)
9933 return NULL;
9934
9935 for (p = result; *p; )
9936 {
9937 if (STRNCMP(p, "<sfile>", 7) != 0)
9938 ++p;
9939 else
9940 {
9941 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009942 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 if (errormsg != NULL)
9944 {
9945 if (*errormsg)
9946 emsg(errormsg);
9947 vim_free(result);
9948 return NULL;
9949 }
9950 if (repl == NULL) /* no match (cannot happen) */
9951 {
9952 p += srclen;
9953 continue;
9954 }
9955 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9956 newres = alloc(len);
9957 if (newres == NULL)
9958 {
9959 vim_free(repl);
9960 vim_free(result);
9961 return NULL;
9962 }
9963 mch_memmove(newres, result, (size_t)(p - result));
9964 STRCPY(newres + (p - result), repl);
9965 len = (int)STRLEN(newres);
9966 STRCAT(newres, p + srclen);
9967 vim_free(repl);
9968 vim_free(result);
9969 result = newres;
9970 p = newres + len; /* continue after the match */
9971 }
9972 }
9973
9974 return result;
9975}
9976#endif
9977
9978#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009979static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9980 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9982static frame_T *ses_skipframe __ARGS((frame_T *fr));
9983static int ses_do_frame __ARGS((frame_T *fr));
9984static int ses_do_win __ARGS((win_T *wp));
9985static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9986static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9987static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9988
9989/*
9990 * Write openfile commands for the current buffers to an .exrc file.
9991 * Return FAIL on error, OK otherwise.
9992 */
9993 static int
9994makeopens(fd, dirnow)
9995 FILE *fd;
9996 char_u *dirnow; /* Current directory name */
9997{
9998 buf_T *buf;
9999 int only_save_windows = TRUE;
10000 int nr;
10001 int cnr = 1;
10002 int restore_size = TRUE;
10003 win_T *wp;
10004 char_u *sname;
10005 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010006 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010007 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010008 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010009 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +000010010 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011
10012 if (ssop_flags & SSOP_BUFFERS)
10013 only_save_windows = FALSE; /* Save ALL buffers */
10014
10015 /*
10016 * Begin by setting the this_session variable, and then other
10017 * sessionable variables.
10018 */
10019#ifdef FEAT_EVAL
10020 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10021 return FAIL;
10022 if (ssop_flags & SSOP_GLOBALS)
10023 if (store_session_globals(fd) == FAIL)
10024 return FAIL;
10025#endif
10026
10027 /*
10028 * Close all windows but one.
10029 */
10030 if (put_line(fd, "silent only") == FAIL)
10031 return FAIL;
10032
10033 /*
10034 * Now a :cd command to the session directory or the current directory
10035 */
10036 if (ssop_flags & SSOP_SESDIR)
10037 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010038 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10039 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 return FAIL;
10041 }
10042 else if (ssop_flags & SSOP_CURDIR)
10043 {
10044 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10045 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010046 || fputs("cd ", fd) < 0
10047 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10048 || put_eol(fd) == FAIL)
10049 {
10050 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053 vim_free(sname);
10054 }
10055
10056 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010057 * If there is an empty, unnamed buffer we will wipe it out later.
10058 * Remember the buffer number.
10059 */
10060 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10061 return FAIL;
10062 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10063 return FAIL;
10064 if (put_line(fd, "endif") == FAIL)
10065 return FAIL;
10066
10067 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 * Now save the current files, current buffer first.
10069 */
10070 if (put_line(fd, "set shortmess=aoO") == FAIL)
10071 return FAIL;
10072
10073 /* Now put the other buffers into the buffer list */
10074 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10075 {
10076 if (!(only_save_windows && buf->b_nwindows == 0)
10077 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10078 && buf->b_fname != NULL
10079 && buf->b_p_bl)
10080 {
10081 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10082 : buf->b_wininfo->wi_fpos.lnum) < 0
10083 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10084 return FAIL;
10085 }
10086 }
10087
10088 /* the global argument list */
10089 if (ses_arglist(fd, "args", &global_alist.al_ga,
10090 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10091 return FAIL;
10092
10093 if (ssop_flags & SSOP_RESIZE)
10094 {
10095 /* Note: after the restore we still check it worked!*/
10096 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10097 || put_eol(fd) == FAIL)
10098 return FAIL;
10099 }
10100
10101#ifdef FEAT_GUI
10102 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10103 {
10104 int x, y;
10105
10106 if (gui_mch_get_winpos(&x, &y) == OK)
10107 {
10108 /* Note: after the restore we still check it worked!*/
10109 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10110 return FAIL;
10111 }
10112 }
10113#endif
10114
10115 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010116 * May repeat putting Windows for each tab, when "tabpages" is in
10117 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010118 * Don't use goto_tabpage(), it may change directory and trigger
10119 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010121 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010122 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010123 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010125 int need_tabnew = FALSE;
10126
Bram Moolenaar18144c82006-04-12 21:52:12 +000010127 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010129 tabpage_T *tp = find_tabpage(tabnr);
10130
10131 if (tp == NULL)
10132 break; /* done all tab pages */
10133 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010134 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010135 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010136 tab_topframe = topframe;
10137 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010138 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010139 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010140 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010141 tab_topframe = tp->tp_topframe;
10142 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010143 if (tabnr > 1)
10144 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
Bram Moolenaar18144c82006-04-12 21:52:12 +000010147 /*
10148 * Before creating the window layout, try loading one file. If this
10149 * is aborted we don't end up with a number of useless windows.
10150 * This may have side effects! (e.g., compressed or network file).
10151 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010152 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010153 {
10154 if (ses_do_win(wp)
10155 && wp->w_buffer->b_ffname != NULL
10156 && !wp->w_buffer->b_help
10157#ifdef FEAT_QUICKFIX
10158 && !bt_nofile(wp->w_buffer)
10159#endif
10160 )
10161 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010162 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010163 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10164 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010165 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010166 if (!wp->w_arg_idx_invalid)
10167 edited_win = wp;
10168 break;
10169 }
10170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010172 /* If no file got edited create an empty tab page. */
10173 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10174 return FAIL;
10175
Bram Moolenaar18144c82006-04-12 21:52:12 +000010176 /*
10177 * Save current window layout.
10178 */
10179 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010181 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010183 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10184 return FAIL;
10185 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10186 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010187
Bram Moolenaar18144c82006-04-12 21:52:12 +000010188 /*
10189 * Check if window sizes can be restored (no windows omitted).
10190 * Remember the window number of the current window after restoring.
10191 */
10192 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010193 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010194 {
10195 if (ses_do_win(wp))
10196 ++nr;
10197 else
10198 restore_size = FALSE;
10199 if (curwin == wp)
10200 cnr = nr;
10201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202
Bram Moolenaar18144c82006-04-12 21:52:12 +000010203 /* Go to the first window. */
10204 if (put_line(fd, "wincmd t") == FAIL)
10205 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010206
Bram Moolenaar18144c82006-04-12 21:52:12 +000010207 /*
10208 * If more than one window, see if sizes can be restored.
10209 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10210 * resized when moving between windows.
10211 * Do this before restoring the view, so that the topline and the
10212 * cursor can be set. This is done again below.
10213 */
10214 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10215 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010216 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010217 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
Bram Moolenaar18144c82006-04-12 21:52:12 +000010219 /*
10220 * Restore the view of the window (options, file, cursor, etc.).
10221 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010222 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010223 {
10224 if (!ses_do_win(wp))
10225 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010226 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10227 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010228 return FAIL;
10229 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10230 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010231 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010232 }
10233
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010234 /* The argument index in the first tab page is zero, need to set it in
10235 * each window. For further tab pages it's the window where we do
10236 * "tabedit". */
10237 cur_arg_idx = next_arg_idx;
10238
Bram Moolenaar18144c82006-04-12 21:52:12 +000010239 /*
10240 * Restore cursor to the current window if it's not the first one.
10241 */
10242 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10243 || put_eol(fd) == FAIL))
10244 return FAIL;
10245
10246 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010247 * Restore window sizes again after jumping around in windows, because
10248 * the current window has a minimum size while others may not.
10249 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010250 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010251 return FAIL;
10252
Bram Moolenaar18144c82006-04-12 21:52:12 +000010253 /* Don't continue in another tab page when doing only the current one
10254 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010255 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010256 break;
10257 }
10258
10259 if (ssop_flags & SSOP_TABPAGES)
10260 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010261 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10262 || put_eol(fd) == FAIL)
10263 return FAIL;
10264 }
10265
Bram Moolenaar9c102382006-05-03 21:26:49 +000010266 /*
10267 * Wipe out an empty unnamed buffer we started in.
10268 */
10269 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10270 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010271 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010272 return FAIL;
10273 if (put_line(fd, "endif") == FAIL)
10274 return FAIL;
10275 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10276 return FAIL;
10277
10278 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10279 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10280 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10281 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282
10283 /*
10284 * Lastly, execute the x.vim file if it exists.
10285 */
10286 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10287 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010288 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 || put_line(fd, "endif") == FAIL)
10290 return FAIL;
10291
10292 return OK;
10293}
10294
10295 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010296ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 FILE *fd;
10298 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010299 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300{
10301 int n = 0;
10302 win_T *wp;
10303
10304 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10305 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010306 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 {
10308 if (!ses_do_win(wp))
10309 continue;
10310 ++n;
10311
10312 /* restore height when not full height */
10313 if (wp->w_height + wp->w_status_height < topframe->fr_height
10314 && (fprintf(fd,
10315 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10316 n, (long)wp->w_height, Rows / 2, Rows) < 0
10317 || put_eol(fd) == FAIL))
10318 return FAIL;
10319
10320 /* restore width when not full width */
10321 if (wp->w_width < Columns && (fprintf(fd,
10322 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10323 n, (long)wp->w_width, Columns / 2, Columns) < 0
10324 || put_eol(fd) == FAIL))
10325 return FAIL;
10326 }
10327 }
10328 else
10329 {
10330 /* Just equalise window sizes */
10331 if (put_line(fd, "wincmd =") == FAIL)
10332 return FAIL;
10333 }
10334 return OK;
10335}
10336
10337/*
10338 * Write commands to "fd" to recursively create windows for frame "fr",
10339 * horizontally and vertically split.
10340 * After the commands the last window in the frame is the current window.
10341 * Returns FAIL when writing the commands to "fd" fails.
10342 */
10343 static int
10344ses_win_rec(fd, fr)
10345 FILE *fd;
10346 frame_T *fr;
10347{
10348 frame_T *frc;
10349 int count = 0;
10350
10351 if (fr->fr_layout != FR_LEAF)
10352 {
10353 /* Find first frame that's not skipped and then create a window for
10354 * each following one (first frame is already there). */
10355 frc = ses_skipframe(fr->fr_child);
10356 if (frc != NULL)
10357 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10358 {
10359 /* Make window as big as possible so that we have lots of room
10360 * to split. */
10361 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10362 || put_line(fd, fr->fr_layout == FR_COL
10363 ? "split" : "vsplit") == FAIL)
10364 return FAIL;
10365 ++count;
10366 }
10367
10368 /* Go back to the first window. */
10369 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10370 ? "%dwincmd k" : "%dwincmd h", count) < 0
10371 || put_eol(fd) == FAIL))
10372 return FAIL;
10373
10374 /* Recursively create frames/windows in each window of this column or
10375 * row. */
10376 frc = ses_skipframe(fr->fr_child);
10377 while (frc != NULL)
10378 {
10379 ses_win_rec(fd, frc);
10380 frc = ses_skipframe(frc->fr_next);
10381 /* Go to next window. */
10382 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10383 return FAIL;
10384 }
10385 }
10386 return OK;
10387}
10388
10389/*
10390 * Skip frames that don't contain windows we want to save in the Session.
10391 * Returns NULL when there none.
10392 */
10393 static frame_T *
10394ses_skipframe(fr)
10395 frame_T *fr;
10396{
10397 frame_T *frc;
10398
10399 for (frc = fr; frc != NULL; frc = frc->fr_next)
10400 if (ses_do_frame(frc))
10401 break;
10402 return frc;
10403}
10404
10405/*
10406 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10407 * the Session.
10408 */
10409 static int
10410ses_do_frame(fr)
10411 frame_T *fr;
10412{
10413 frame_T *frc;
10414
10415 if (fr->fr_layout == FR_LEAF)
10416 return ses_do_win(fr->fr_win);
10417 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10418 if (ses_do_frame(frc))
10419 return TRUE;
10420 return FALSE;
10421}
10422
10423/*
10424 * Return non-zero if window "wp" is to be stored in the Session.
10425 */
10426 static int
10427ses_do_win(wp)
10428 win_T *wp;
10429{
10430 if (wp->w_buffer->b_fname == NULL
10431#ifdef FEAT_QUICKFIX
10432 /* When 'buftype' is "nofile" can't restore the window contents. */
10433 || bt_nofile(wp->w_buffer)
10434#endif
10435 )
10436 return (ssop_flags & SSOP_BLANK);
10437 if (wp->w_buffer->b_help)
10438 return (ssop_flags & SSOP_HELP);
10439 return TRUE;
10440}
10441
10442/*
10443 * Write commands to "fd" to restore the view of a window.
10444 * Caller must make sure 'scrolloff' is zero.
10445 */
10446 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010447put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 FILE *fd;
10449 win_T *wp;
10450 int add_edit; /* add ":edit" command to view */
10451 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010452 int current_arg_idx; /* current argument index of the window, use
10453 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454{
10455 win_T *save_curwin;
10456 int f;
10457 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010458 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
10460 /* Always restore cursor position for ":mksession". For ":mkview" only
10461 * when 'viewoptions' contains "cursor". */
10462 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10463
10464 /*
10465 * Local argument list.
10466 */
10467 if (wp->w_alist == &global_alist)
10468 {
10469 if (put_line(fd, "argglobal") == FAIL)
10470 return FAIL;
10471 }
10472 else
10473 {
10474 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10475 flagp == &vop_flags
10476 || !(*flagp & SSOP_CURDIR)
10477 || wp->w_localdir != NULL, flagp) == FAIL)
10478 return FAIL;
10479 }
10480
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010481 /* Only when part of a session: restore the argument index. Some
10482 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010483 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010484 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010486 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 || put_eol(fd) == FAIL)
10488 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010489 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 }
10491
10492 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010493 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 {
10495 /*
10496 * Load the file.
10497 */
10498 if (wp->w_buffer->b_ffname != NULL
10499#ifdef FEAT_QUICKFIX
10500 && !bt_nofile(wp->w_buffer)
10501#endif
10502 )
10503 {
10504 /*
10505 * Editing a file in this buffer: use ":edit file".
10506 * This may have side effects! (e.g., compressed or network file).
10507 */
10508 if (fputs("edit ", fd) < 0
10509 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10510 return FAIL;
10511 }
10512 else
10513 {
10514 /* No file in this buffer, just make it empty. */
10515 if (put_line(fd, "enew") == FAIL)
10516 return FAIL;
10517#ifdef FEAT_QUICKFIX
10518 if (wp->w_buffer->b_ffname != NULL)
10519 {
10520 /* The buffer does have a name, but it's not a file name. */
10521 if (fputs("file ", fd) < 0
10522 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10523 return FAIL;
10524 }
10525#endif
10526 do_cursor = FALSE;
10527 }
10528 }
10529
10530 /*
10531 * Local mappings and abbreviations.
10532 */
10533 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10534 && makemap(fd, wp->w_buffer) == FAIL)
10535 return FAIL;
10536
10537 /*
10538 * Local options. Need to go to the window temporarily.
10539 * Store only local values when using ":mkview" and when ":mksession" is
10540 * used and 'sessionoptions' doesn't include "options".
10541 * Some folding options are always stored when "folds" is included,
10542 * otherwise the folds would not be restored correctly.
10543 */
10544 save_curwin = curwin;
10545 curwin = wp;
10546 curbuf = curwin->w_buffer;
10547 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10548 f = makeset(fd, OPT_LOCAL,
10549 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10550#ifdef FEAT_FOLDING
10551 else if (*flagp & SSOP_FOLDS)
10552 f = makefoldset(fd);
10553#endif
10554 else
10555 f = OK;
10556 curwin = save_curwin;
10557 curbuf = curwin->w_buffer;
10558 if (f == FAIL)
10559 return FAIL;
10560
10561#ifdef FEAT_FOLDING
10562 /*
10563 * Save Folds when 'buftype' is empty and for help files.
10564 */
10565 if ((*flagp & SSOP_FOLDS)
10566 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010567# ifdef FEAT_QUICKFIX
10568 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10569# endif
10570 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 {
10572 if (put_folds(fd, wp) == FAIL)
10573 return FAIL;
10574 }
10575#endif
10576
10577 /*
10578 * Set the cursor after creating folds, since that moves the cursor.
10579 */
10580 if (do_cursor)
10581 {
10582
10583 /* Restore the cursor line in the file and relatively in the
10584 * window. Don't use "G", it changes the jumplist. */
10585 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10586 (long)wp->w_cursor.lnum,
10587 (long)(wp->w_cursor.lnum - wp->w_topline),
10588 (long)wp->w_height / 2, (long)wp->w_height) < 0
10589 || put_eol(fd) == FAIL
10590 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10591 || put_line(fd, "exe s:l") == FAIL
10592 || put_line(fd, "normal! zt") == FAIL
10593 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10594 || put_eol(fd) == FAIL)
10595 return FAIL;
10596 /* Restore the cursor column and left offset when not wrapping. */
10597 if (wp->w_cursor.col == 0)
10598 {
10599 if (put_line(fd, "normal! 0") == FAIL)
10600 return FAIL;
10601 }
10602 else
10603 {
10604 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10605 {
10606 if (fprintf(fd,
10607 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10608 (long)wp->w_cursor.col,
10609 (long)(wp->w_cursor.col - wp->w_leftcol),
10610 (long)wp->w_width / 2, (long)wp->w_width) < 0
10611 || put_eol(fd) == FAIL
10612 || put_line(fd, "if s:c > 0") == FAIL
10613 || fprintf(fd,
10614 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10615 (long)wp->w_cursor.col) < 0
10616 || put_eol(fd) == FAIL
10617 || put_line(fd, "else") == FAIL
10618 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10619 || put_eol(fd) == FAIL
10620 || put_line(fd, "endif") == FAIL)
10621 return FAIL;
10622 }
10623 else
10624 {
10625 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10626 || put_eol(fd) == FAIL)
10627 return FAIL;
10628 }
10629 }
10630 }
10631
10632 /*
10633 * Local directory.
10634 */
10635 if (wp->w_localdir != NULL)
10636 {
10637 if (fputs("lcd ", fd) < 0
10638 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10639 || put_eol(fd) == FAIL)
10640 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010641 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 }
10643
10644 return OK;
10645}
10646
10647/*
10648 * Write an argument list to the session file.
10649 * Returns FAIL if writing fails.
10650 */
10651 static int
10652ses_arglist(fd, cmd, gap, fullname, flagp)
10653 FILE *fd;
10654 char *cmd;
10655 garray_T *gap;
10656 int fullname; /* TRUE: use full path name */
10657 unsigned *flagp;
10658{
10659 int i;
10660 char_u buf[MAXPATHL];
10661 char_u *s;
10662
10663 if (gap->ga_len == 0)
10664 return put_line(fd, "silent! argdel *");
10665 if (fputs(cmd, fd) < 0)
10666 return FAIL;
10667 for (i = 0; i < gap->ga_len; ++i)
10668 {
10669 /* NULL file names are skipped (only happens when out of memory). */
10670 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10671 if (s != NULL)
10672 {
10673 if (fullname)
10674 {
10675 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10676 s = buf;
10677 }
10678 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10679 return FAIL;
10680 }
10681 }
10682 return put_eol(fd);
10683}
10684
10685/*
10686 * Write a buffer name to the session file.
10687 * Also ends the line.
10688 * Returns FAIL if writing fails.
10689 */
10690 static int
10691ses_fname(fd, buf, flagp)
10692 FILE *fd;
10693 buf_T *buf;
10694 unsigned *flagp;
10695{
10696 char_u *name;
10697
10698 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010699 * the session file will be sourced.
10700 * Don't do this for ":mkview", we don't know the current directory.
10701 * Don't do this after ":lcd", we don't keep track of what the current
10702 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 if (buf->b_sfname != NULL
10704 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010705 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010706#ifdef FEAT_AUTOCHDIR
10707 && !p_acd
10708#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010709 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 name = buf->b_sfname;
10711 else
10712 name = buf->b_ffname;
10713 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10714 return FAIL;
10715 return OK;
10716}
10717
10718/*
10719 * Write a file name to the session file.
10720 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10721 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010722 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 */
10724 static int
10725ses_put_fname(fd, name, flagp)
10726 FILE *fd;
10727 char_u *name;
10728 unsigned *flagp;
10729{
10730 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010731 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733
10734 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010735 if (sname == NULL)
10736 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010738 if (*flagp & SSOP_SLASH)
10739 {
10740 /* change all backslashes to forward slashes */
10741 for (p = sname; *p != NUL; mb_ptr_adv(p))
10742 if (*p == '\\')
10743 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010745
10746 /* escapse special characters */
10747 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010749 if (p == NULL)
10750 return FAIL;
10751
10752 /* write the result */
10753 if (fputs((char *)p, fd) < 0)
10754 retval = FAIL;
10755
10756 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 return retval;
10758}
10759
10760/*
10761 * ":loadview [nr]"
10762 */
10763 static void
10764ex_loadview(eap)
10765 exarg_T *eap;
10766{
10767 char_u *fname;
10768
10769 fname = get_view_file(*eap->arg);
10770 if (fname != NULL)
10771 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010772 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 vim_free(fname);
10774 }
10775}
10776
10777/*
10778 * Get the name of the view file for the current buffer.
10779 */
10780 static char_u *
10781get_view_file(c)
10782 int c;
10783{
10784 int len = 0;
10785 char_u *p, *s;
10786 char_u *retval;
10787 char_u *sname;
10788
10789 if (curbuf->b_ffname == NULL)
10790 {
10791 EMSG(_(e_noname));
10792 return NULL;
10793 }
10794 sname = home_replace_save(NULL, curbuf->b_ffname);
10795 if (sname == NULL)
10796 return NULL;
10797
10798 /*
10799 * We want a file name without separators, because we're not going to make
10800 * a directory.
10801 * "normal" path separator -> "=+"
10802 * "=" -> "=="
10803 * ":" path separator -> "=-"
10804 */
10805 for (p = sname; *p; ++p)
10806 if (*p == '=' || vim_ispathsep(*p))
10807 ++len;
10808 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10809 if (retval != NULL)
10810 {
10811 STRCPY(retval, p_vdir);
10812 add_pathsep(retval);
10813 s = retval + STRLEN(retval);
10814 for (p = sname; *p; ++p)
10815 {
10816 if (*p == '=')
10817 {
10818 *s++ = '=';
10819 *s++ = '=';
10820 }
10821 else if (vim_ispathsep(*p))
10822 {
10823 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010824#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 || defined(VMS)
10826 if (*p == ':')
10827 *s++ = '-';
10828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010830 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 }
10832 else
10833 *s++ = *p;
10834 }
10835 *s++ = '=';
10836 *s++ = c;
10837 STRCPY(s, ".vim");
10838 }
10839
10840 vim_free(sname);
10841 return retval;
10842}
10843
10844#endif /* FEAT_SESSION */
10845
10846/*
10847 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10848 * Return FAIL for a write error.
10849 */
10850 int
10851put_eol(fd)
10852 FILE *fd;
10853{
10854 if (
10855#ifdef USE_CRNL
10856 (
10857# ifdef MKSESSION_NL
10858 !mksession_nl &&
10859# endif
10860 (putc('\r', fd) < 0)) ||
10861#endif
10862 (putc('\n', fd) < 0))
10863 return FAIL;
10864 return OK;
10865}
10866
10867/*
10868 * Write a line to "fd".
10869 * Return FAIL for a write error.
10870 */
10871 int
10872put_line(fd, s)
10873 FILE *fd;
10874 char *s;
10875{
10876 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10877 return FAIL;
10878 return OK;
10879}
10880
10881#ifdef FEAT_VIMINFO
10882/*
10883 * ":rviminfo" and ":wviminfo".
10884 */
10885 static void
10886ex_viminfo(eap)
10887 exarg_T *eap;
10888{
10889 char_u *save_viminfo;
10890
10891 save_viminfo = p_viminfo;
10892 if (*p_viminfo == NUL)
10893 p_viminfo = (char_u *)"'100";
10894 if (eap->cmdidx == CMD_rviminfo)
10895 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010896 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10897 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 EMSG(_("E195: Cannot open viminfo file for reading"));
10899 }
10900 else
10901 write_viminfo(eap->arg, eap->forceit);
10902 p_viminfo = save_viminfo;
10903}
10904#endif
10905
10906#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010907/*
10908 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010909 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 void
10912dialog_msg(buff, format, fname)
10913 char_u *buff;
10914 char *format;
10915 char_u *fname;
10916{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 if (fname == NULL)
10918 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010919 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920}
10921#endif
10922
10923/*
10924 * ":behave {mswin,xterm}"
10925 */
10926 static void
10927ex_behave(eap)
10928 exarg_T *eap;
10929{
10930 if (STRCMP(eap->arg, "mswin") == 0)
10931 {
10932 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10933 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10934 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10935 set_option_value((char_u *)"keymodel", 0L,
10936 (char_u *)"startsel,stopsel", 0);
10937 }
10938 else if (STRCMP(eap->arg, "xterm") == 0)
10939 {
10940 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10941 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10942 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10943 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10944 }
10945 else
10946 EMSG2(_(e_invarg2), eap->arg);
10947}
10948
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010949#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10950/*
10951 * Function given to ExpandGeneric() to obtain the possible arguments of the
10952 * ":behave {mswin,xterm}" command.
10953 */
10954 char_u *
10955get_behave_arg(xp, idx)
10956 expand_T *xp UNUSED;
10957 int idx;
10958{
10959 if (idx == 0)
10960 return (char_u *)"mswin";
10961 if (idx == 1)
10962 return (char_u *)"xterm";
10963 return NULL;
10964}
10965#endif
10966
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967#ifdef FEAT_AUTOCMD
10968static int filetype_detect = FALSE;
10969static int filetype_plugin = FALSE;
10970static int filetype_indent = FALSE;
10971
10972/*
10973 * ":filetype [plugin] [indent] {on,off,detect}"
10974 * on: Load the filetype.vim file to install autocommands for file types.
10975 * off: Load the ftoff.vim file to remove all autocommands for file types.
10976 * plugin on: load filetype.vim and ftplugin.vim
10977 * plugin off: load ftplugof.vim
10978 * indent on: load filetype.vim and indent.vim
10979 * indent off: load indoff.vim
10980 */
10981 static void
10982ex_filetype(eap)
10983 exarg_T *eap;
10984{
10985 char_u *arg = eap->arg;
10986 int plugin = FALSE;
10987 int indent = FALSE;
10988
10989 if (*eap->arg == NUL)
10990 {
10991 /* Print current status. */
10992 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10993 filetype_detect ? "ON" : "OFF",
10994 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10995 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10996 return;
10997 }
10998
10999 /* Accept "plugin" and "indent" in any order. */
11000 for (;;)
11001 {
11002 if (STRNCMP(arg, "plugin", 6) == 0)
11003 {
11004 plugin = TRUE;
11005 arg = skipwhite(arg + 6);
11006 continue;
11007 }
11008 if (STRNCMP(arg, "indent", 6) == 0)
11009 {
11010 indent = TRUE;
11011 arg = skipwhite(arg + 6);
11012 continue;
11013 }
11014 break;
11015 }
11016 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11017 {
11018 if (*arg == 'o' || !filetype_detect)
11019 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011020 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 filetype_detect = TRUE;
11022 if (plugin)
11023 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011024 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 filetype_plugin = TRUE;
11026 }
11027 if (indent)
11028 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011029 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 filetype_indent = TRUE;
11031 }
11032 }
11033 if (*arg == 'd')
11034 {
11035 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011036 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 }
11038 }
11039 else if (STRCMP(arg, "off") == 0)
11040 {
11041 if (plugin || indent)
11042 {
11043 if (plugin)
11044 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011045 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 filetype_plugin = FALSE;
11047 }
11048 if (indent)
11049 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011050 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 filetype_indent = FALSE;
11052 }
11053 }
11054 else
11055 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011056 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 filetype_detect = FALSE;
11058 }
11059 }
11060 else
11061 EMSG2(_(e_invarg2), arg);
11062}
11063
11064/*
11065 * ":setfiletype {name}"
11066 */
11067 static void
11068ex_setfiletype(eap)
11069 exarg_T *eap;
11070{
11071 if (!did_filetype)
11072 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11073}
11074#endif
11075
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 static void
11077ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011078 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079{
11080#ifdef FEAT_DIGRAPHS
11081 if (*eap->arg != NUL)
11082 putdigraph(eap->arg);
11083 else
11084 listdigraphs();
11085#else
11086 EMSG(_("E196: No digraphs in this version"));
11087#endif
11088}
11089
11090 static void
11091ex_set(eap)
11092 exarg_T *eap;
11093{
11094 int flags = 0;
11095
11096 if (eap->cmdidx == CMD_setlocal)
11097 flags = OPT_LOCAL;
11098 else if (eap->cmdidx == CMD_setglobal)
11099 flags = OPT_GLOBAL;
11100#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11101 if (cmdmod.browse && flags == 0)
11102 ex_options(eap);
11103 else
11104#endif
11105 (void)do_set(eap->arg, flags);
11106}
11107
11108#ifdef FEAT_SEARCH_EXTRA
11109/*
11110 * ":nohlsearch"
11111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 static void
11113ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011114 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115{
11116 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011117 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118}
11119
11120/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011121 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 * Sets nextcmd to the start of the next command, if any. Also called when
11123 * skipping commands to find the next command.
11124 */
11125 static void
11126ex_match(eap)
11127 exarg_T *eap;
11128{
11129 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011130 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 char_u *end;
11132 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011133 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011134
11135 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011136 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011137 else
11138 {
11139 EMSG(e_invcmd);
11140 return;
11141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
11143 /* First clear any old pattern. */
11144 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011145 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146
11147 if (ends_excmd(*eap->arg))
11148 end = eap->arg;
11149 else if ((STRNICMP(eap->arg, "none", 4) == 0
11150 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11151 end = eap->arg + 4;
11152 else
11153 {
11154 p = skiptowhite(eap->arg);
11155 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011156 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 p = skipwhite(p);
11158 if (*p == NUL)
11159 {
11160 /* There must be two arguments. */
11161 EMSG2(_(e_invarg2), eap->arg);
11162 return;
11163 }
11164 end = skip_regexp(p + 1, *p, TRUE, NULL);
11165 if (!eap->skip)
11166 {
11167 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11168 {
11169 eap->errmsg = e_trailing;
11170 return;
11171 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011172 if (*end != *p)
11173 {
11174 EMSG2(_(e_invarg2), p);
11175 return;
11176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177
11178 c = *end;
11179 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011180 match_add(curwin, g, p + 1, 10, id);
11181 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011182 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 }
11184 }
11185 eap->nextcmd = find_nextcmd(end);
11186}
11187#endif
11188
11189#ifdef FEAT_CRYPT
11190/*
11191 * ":X": Get crypt key
11192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 static void
11194ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011195 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011197 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011198 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199}
11200#endif
11201
11202#ifdef FEAT_FOLDING
11203 static void
11204ex_fold(eap)
11205 exarg_T *eap;
11206{
11207 if (foldManualAllowed(TRUE))
11208 foldCreate(eap->line1, eap->line2);
11209}
11210
11211 static void
11212ex_foldopen(eap)
11213 exarg_T *eap;
11214{
11215 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11216 eap->forceit, FALSE);
11217}
11218
11219 static void
11220ex_folddo(eap)
11221 exarg_T *eap;
11222{
11223 linenr_T lnum;
11224
11225 /* First set the marks for all lines closed/open. */
11226 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11227 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11228 ml_setmarked(lnum);
11229
11230 /* Execute the command on the marked lines. */
11231 global_exe(eap->arg);
11232 ml_clearmarked(); /* clear rest of the marks */
11233}
11234#endif