blob: b56fb670b15d175cd7af60a21e6cf4ae73194d22 [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 Moolenaar071d4272004-06-13 20:20:40 +00002566 case CMD_return:
2567 case CMD_rightbelow:
2568 case CMD_ruby:
2569 case CMD_silent:
2570 case CMD_smagic:
2571 case CMD_snomagic:
2572 case CMD_substitute:
2573 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002574 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 case CMD_tcl:
2576 case CMD_throw:
2577 case CMD_tilde:
2578 case CMD_topleft:
2579 case CMD_unlet:
2580 case CMD_verbose:
2581 case CMD_vertical:
2582 break;
2583
2584 default: goto doend;
2585 }
2586 }
2587#endif
2588
2589 if (ea.argt & XFILE)
2590 {
2591 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2592 goto doend;
2593 }
2594
2595#ifdef FEAT_LISTCMDS
2596 /*
2597 * Accept buffer name. Cannot be used at the same time with a buffer
2598 * number. Don't do this for a user command.
2599 */
2600 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2601# ifdef FEAT_USR_CMDS
2602 && !USER_CMDIDX(ea.cmdidx)
2603# endif
2604 )
2605 {
2606 /*
2607 * :bdelete, :bwipeout and :bunload take several arguments, separated
2608 * by spaces: find next space (skipping over escaped characters).
2609 * The others take one argument: ignore trailing spaces.
2610 */
2611 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2612 || ea.cmdidx == CMD_bunload)
2613 p = skiptowhite_esc(ea.arg);
2614 else
2615 {
2616 p = ea.arg + STRLEN(ea.arg);
2617 while (p > ea.arg && vim_iswhite(p[-1]))
2618 --p;
2619 }
2620 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2621 if (ea.line2 < 0) /* failed */
2622 goto doend;
2623 ea.addr_count = 1;
2624 ea.arg = skipwhite(p);
2625 }
2626#endif
2627
2628/*
2629 * 6. switch on command name
2630 *
2631 * The "ea" structure holds the arguments that can be used.
2632 */
2633 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002634 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 ea.cookie = cookie;
2636#ifdef FEAT_EVAL
2637 ea.cstack = cstack;
2638#endif
2639
2640#ifdef FEAT_USR_CMDS
2641 if (USER_CMDIDX(ea.cmdidx))
2642 {
2643 /*
2644 * Execute a user-defined command.
2645 */
2646 do_ucmd(&ea);
2647 }
2648 else
2649#endif
2650 {
2651 /*
2652 * Call the function to execute the command.
2653 */
2654 ea.errmsg = NULL;
2655 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2656 if (ea.errmsg != NULL)
2657 errormsg = (char_u *)_(ea.errmsg);
2658 }
2659
2660#ifdef FEAT_EVAL
2661 /*
2662 * If the command just executed called do_cmdline(), any throw or ":return"
2663 * or ":finish" encountered there must also check the cstack of the still
2664 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2665 * exception, or reanimate a returned function or finished script file and
2666 * return or finish it again.
2667 */
2668 if (need_rethrow)
2669 do_throw(cstack);
2670 else if (check_cstack)
2671 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002672 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002674 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 && current_func_returned())
2676 do_return(&ea, TRUE, FALSE, NULL);
2677 }
2678 need_rethrow = check_cstack = FALSE;
2679#endif
2680
2681doend:
2682 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2683 curwin->w_cursor.lnum = 1;
2684
2685 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2686 {
2687 if (sourcing)
2688 {
2689 if (errormsg != IObuff)
2690 {
2691 STRCPY(IObuff, errormsg);
2692 errormsg = IObuff;
2693 }
2694 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002695 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697 emsg(errormsg);
2698 }
2699#ifdef FEAT_EVAL
2700 do_errthrow(cstack,
2701 (ea.cmdidx != CMD_SIZE
2702# ifdef FEAT_USR_CMDS
2703 && !USER_CMDIDX(ea.cmdidx)
2704# endif
2705 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2706#endif
2707
2708 if (verbose_save >= 0)
2709 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002710#ifdef FEAT_AUTOCMD
2711 if (cmdmod.save_ei != NULL)
2712 {
2713 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002714 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2715 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002716 free_string_option(cmdmod.save_ei);
2717 }
2718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 cmdmod = save_cmdmod;
2721
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002722 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
2724 /* messages could be enabled for a serious error, need to check if the
2725 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002726 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002727 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 emsg_silent -= did_esilent;
2729 if (emsg_silent < 0)
2730 emsg_silent = 0;
2731 /* Restore msg_scroll, it's set by file I/O commands, even when no
2732 * message is actually displayed. */
2733 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002734
2735 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2736 * somewhere in the line. Put it back in the first column. */
2737 if (redirecting())
2738 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 }
2740
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002741#ifdef HAVE_SANDBOX
2742 if (did_sandbox)
2743 --sandbox;
2744#endif
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2747 ea.nextcmd = NULL;
2748
2749#ifdef FEAT_EVAL
2750 --ex_nesting_level;
2751#endif
2752
2753 return ea.nextcmd;
2754}
2755#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002756 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757#endif
2758
2759/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002760 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 * If there is a match advance "pp" to the argument and return TRUE.
2762 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002763 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002765 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 char *cmd; /* name of command */
2767 int len; /* required length */
2768{
2769 int i;
2770
2771 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002772 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 break;
2774 if (i >= len && !isalpha((*pp)[i]))
2775 {
2776 *pp = skipwhite(*pp + i);
2777 return TRUE;
2778 }
2779 return FALSE;
2780}
2781
2782/*
2783 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002784 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002786 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 * Returns NULL for an ambiguous user command.
2788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 static char_u *
2790find_command(eap, full)
2791 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002792 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 int len;
2795 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002796 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
2798 /*
2799 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002800 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 * - the 'k' command can directly be followed by any character.
2802 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2803 * but :sre[wind] is another command, as are :scrip[tnames],
2804 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002805 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 */
2807 p = eap->cmd;
2808 if (*p == 'k')
2809 {
2810 eap->cmdidx = CMD_k;
2811 ++p;
2812 }
2813 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002814 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2815 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 || p[1] == 'g'
2817 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2818 || p[1] == 'I'
2819 || (p[1] == 'r' && p[2] != 'e')))
2820 {
2821 eap->cmdidx = CMD_substitute;
2822 ++p;
2823 }
2824 else
2825 {
2826 while (ASCII_ISALPHA(*p))
2827 ++p;
Bram Moolenaar368373e2010-07-19 20:46:22 +02002828 /* for python 3.x support ":py3" (and ":py4" :-) */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002829 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002830 p = skipdigits(p);
2831
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 /* check for non-alpha command */
2833 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2834 ++p;
2835 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002836 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2837 {
2838 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2839 * :delete with the 'l' flag. Same for 'p'. */
2840 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002841 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002842 break;
2843 if (i == len - 1)
2844 {
2845 --len;
2846 if (p[-1] == 'l')
2847 eap->flags |= EXFLAG_LIST;
2848 else
2849 eap->flags |= EXFLAG_PRINT;
2850 }
2851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
2853 if (ASCII_ISLOWER(*eap->cmd))
2854 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2855 else
2856 eap->cmdidx = cmdidxs[26];
2857
2858 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2859 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2860 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2861 (size_t)len) == 0)
2862 {
2863#ifdef FEAT_EVAL
2864 if (full != NULL
2865 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2866 *full = TRUE;
2867#endif
2868 break;
2869 }
2870
2871#ifdef FEAT_USR_CMDS
2872 /* Look for a user defined command as a last resort */
2873 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2874 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002875 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 while (ASCII_ISALNUM(*p))
2877 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002878 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
2880#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002881 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 eap->cmdidx = CMD_SIZE;
2883 }
2884
2885 return p;
2886}
2887
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002888#ifdef FEAT_USR_CMDS
2889/*
2890 * Search for a user command that matches "eap->cmd".
2891 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2892 * Return a pointer to just after the command.
2893 * Return NULL if there is no matching command.
2894 */
2895 static char_u *
2896find_ucmd(eap, p, full, xp, compl)
2897 exarg_T *eap;
2898 char_u *p; /* end of the command (possibly including count) */
2899 int *full; /* set to TRUE for a full match */
2900 expand_T *xp; /* used for completion, NULL otherwise */
2901 int *compl; /* completion flags or NULL */
2902{
2903 int len = (int)(p - eap->cmd);
2904 int j, k, matchlen = 0;
2905 ucmd_T *uc;
2906 int found = FALSE;
2907 int possible = FALSE;
2908 char_u *cp, *np; /* Point into typed cmd and test name */
2909 garray_T *gap;
2910 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2911 only full match global is accepted. */
2912
2913 /*
2914 * Look for buffer-local user commands first, then global ones.
2915 */
2916 gap = &curbuf->b_ucmds;
2917 for (;;)
2918 {
2919 for (j = 0; j < gap->ga_len; ++j)
2920 {
2921 uc = USER_CMD_GA(gap, j);
2922 cp = eap->cmd;
2923 np = uc->uc_name;
2924 k = 0;
2925 while (k < len && *np != NUL && *cp++ == *np++)
2926 k++;
2927 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2928 {
2929 /* If finding a second match, the command is ambiguous. But
2930 * not if a buffer-local command wasn't a full match and a
2931 * global command is a full match. */
2932 if (k == len && found && *np != NUL)
2933 {
2934 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002935 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002936 amb_local = TRUE;
2937 }
2938
2939 if (!found || (k == len && *np == NUL))
2940 {
2941 /* If we matched up to a digit, then there could
2942 * be another command including the digit that we
2943 * should use instead.
2944 */
2945 if (k == len)
2946 found = TRUE;
2947 else
2948 possible = TRUE;
2949
2950 if (gap == &ucmds)
2951 eap->cmdidx = CMD_USER;
2952 else
2953 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002954 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002955 eap->useridx = j;
2956
2957# ifdef FEAT_CMDL_COMPL
2958 if (compl != NULL)
2959 *compl = uc->uc_compl;
2960# ifdef FEAT_EVAL
2961 if (xp != NULL)
2962 {
2963 xp->xp_arg = uc->uc_compl_arg;
2964 xp->xp_scriptID = uc->uc_scriptID;
2965 }
2966# endif
2967# endif
2968 /* Do not search for further abbreviations
2969 * if this is an exact match. */
2970 matchlen = k;
2971 if (k == len && *np == NUL)
2972 {
2973 if (full != NULL)
2974 *full = TRUE;
2975 amb_local = FALSE;
2976 break;
2977 }
2978 }
2979 }
2980 }
2981
2982 /* Stop if we found a full match or searched all. */
2983 if (j < gap->ga_len || gap == &ucmds)
2984 break;
2985 gap = &ucmds;
2986 }
2987
2988 /* Only found ambiguous matches. */
2989 if (amb_local)
2990 {
2991 if (xp != NULL)
2992 xp->xp_context = EXPAND_UNSUCCESSFUL;
2993 return NULL;
2994 }
2995
2996 /* The match we found may be followed immediately by a number. Move "p"
2997 * back to point to it. */
2998 if (found || possible)
2999 return p + (matchlen - len);
3000 return p;
3001}
3002#endif
3003
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003005static struct cmdmod
3006{
3007 char *name;
3008 int minlen;
3009 int has_count; /* :123verbose :3tab */
3010} cmdmods[] = {
3011 {"aboveleft", 3, FALSE},
3012 {"belowright", 3, FALSE},
3013 {"botright", 2, FALSE},
3014 {"browse", 3, FALSE},
3015 {"confirm", 4, FALSE},
3016 {"hide", 3, FALSE},
3017 {"keepalt", 5, FALSE},
3018 {"keepjumps", 5, FALSE},
3019 {"keepmarks", 3, FALSE},
3020 {"leftabove", 5, FALSE},
3021 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003022 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003023 {"rightbelow", 6, FALSE},
3024 {"sandbox", 3, FALSE},
3025 {"silent", 3, FALSE},
3026 {"tab", 3, TRUE},
3027 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003028 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003029 {"verbose", 4, TRUE},
3030 {"vertical", 4, FALSE},
3031};
3032
3033/*
3034 * Return length of a command modifier (including optional count).
3035 * Return zero when it's not a modifier.
3036 */
3037 int
3038modifier_len(cmd)
3039 char_u *cmd;
3040{
3041 int i, j;
3042 char_u *p = cmd;
3043
3044 if (VIM_ISDIGIT(*cmd))
3045 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003046 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003047 {
3048 for (j = 0; p[j] != NUL; ++j)
3049 if (p[j] != cmdmods[i].name[j])
3050 break;
3051 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3052 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003053 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003054 }
3055 return 0;
3056}
3057
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058/*
3059 * Return > 0 if an Ex command "name" exists.
3060 * Return 2 if there is an exact match.
3061 * Return 3 if there is an ambiguous match.
3062 */
3063 int
3064cmd_exists(name)
3065 char_u *name;
3066{
3067 exarg_T ea;
3068 int full = FALSE;
3069 int i;
3070 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003071 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
3073 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003074 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
3076 for (j = 0; name[j] != NUL; ++j)
3077 if (name[j] != cmdmods[i].name[j])
3078 break;
3079 if (name[j] == NUL && j >= cmdmods[i].minlen)
3080 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3081 }
3082
Bram Moolenaara9587612006-05-04 21:47:50 +00003083 /* Check built-in commands and user defined commands.
3084 * For ":2match" and ":3match" we need to skip the number. */
3085 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003087 p = find_command(&ea, &full);
3088 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003090 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3091 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003092 if (*skipwhite(p) != NUL)
3093 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3095}
3096#endif
3097
3098/*
3099 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3100 * we don't need/want deleted. Maybe this could be done better if we didn't
3101 * repeat all this stuff. The only problem is that they may not stay
3102 * perfectly compatible with each other, but then the command line syntax
3103 * probably won't change that much -- webb.
3104 */
3105 char_u *
3106set_one_cmd_context(xp, buff)
3107 expand_T *xp;
3108 char_u *buff; /* buffer for command string */
3109{
3110 char_u *p;
3111 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003112 int len = 0;
3113 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3115 int compl = EXPAND_NOTHING;
3116#endif
3117#ifdef FEAT_CMDL_COMPL
3118 int delim;
3119#endif
3120 int forceit = FALSE;
3121 int usefilter = FALSE; /* filter instead of file name */
3122
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003123 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 xp->xp_pattern = buff;
3125 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003126 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128/*
3129 * 2. skip comment lines and leading space, colons or bars
3130 */
3131 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3132 ;
3133 xp->xp_pattern = cmd;
3134
3135 if (*cmd == NUL)
3136 return NULL;
3137 if (*cmd == '"') /* ignore comment lines */
3138 {
3139 xp->xp_context = EXPAND_NOTHING;
3140 return NULL;
3141 }
3142
3143/*
3144 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3145 */
3146 cmd = skip_range(cmd, &xp->xp_context);
3147
3148/*
3149 * 4. parse command
3150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 xp->xp_pattern = cmd;
3152 if (*cmd == NUL)
3153 return NULL;
3154 if (*cmd == '"')
3155 {
3156 xp->xp_context = EXPAND_NOTHING;
3157 return NULL;
3158 }
3159
3160 if (*cmd == '|' || *cmd == '\n')
3161 return cmd + 1; /* There's another command */
3162
3163 /*
3164 * Isolate the command and search for it in the command table.
3165 * Exceptions:
3166 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003167 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3169 */
3170 if (*cmd == 'k' && cmd[1] != 'e')
3171 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003172 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 p = cmd + 1;
3174 }
3175 else
3176 {
3177 p = cmd;
3178 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3179 ++p;
3180 /* check for non-alpha command */
3181 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3182 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003183 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003185 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
3187 xp->xp_context = EXPAND_UNSUCCESSFUL;
3188 return NULL;
3189 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003190 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003191 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3192 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3193 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 break;
3195
3196#ifdef FEAT_USR_CMDS
3197 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3199 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200#endif
3201 }
3202
3203 /*
3204 * If the cursor is touching the command, and it ends in an alpha-numeric
3205 * character, complete the command name.
3206 */
3207 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3208 return NULL;
3209
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003210 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
3212 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3213 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003214 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 p = cmd + 1;
3216 }
3217#ifdef FEAT_USR_CMDS
3218 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3219 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 ea.cmd = cmd;
3221 p = find_ucmd(&ea, p, NULL, xp,
3222# if defined(FEAT_CMDL_COMPL)
3223 &compl
3224# else
3225 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003227 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003228 if (p == NULL)
3229 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 }
3231#endif
3232 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003233 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 {
3235 /* Not still touching the command and it was an illegal one */
3236 xp->xp_context = EXPAND_UNSUCCESSFUL;
3237 return NULL;
3238 }
3239
3240 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3241
3242 if (*p == '!') /* forced commands */
3243 {
3244 forceit = TRUE;
3245 ++p;
3246 }
3247
3248/*
3249 * 5. parse arguments
3250 */
3251#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003252 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003254 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255
3256 arg = skipwhite(p);
3257
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003258 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 {
3260 if (*arg == '>') /* append */
3261 {
3262 if (*++arg == '>')
3263 ++arg;
3264 arg = skipwhite(arg);
3265 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003266 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268 ++arg;
3269 usefilter = TRUE;
3270 }
3271 }
3272
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003273 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 {
3275 usefilter = forceit; /* :r! filter if forced */
3276 if (*arg == '!') /* :r !filter */
3277 {
3278 ++arg;
3279 usefilter = TRUE;
3280 }
3281 }
3282
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003283 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 {
3285 while (*arg == *cmd) /* allow any number of '>' or '<' */
3286 ++arg;
3287 arg = skipwhite(arg);
3288 }
3289
3290 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003291 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 /* Check if we're in the +command */
3294 p = arg + 1;
3295 arg = skip_cmd_arg(arg, FALSE);
3296
3297 /* Still touching the command after '+'? */
3298 if (*arg == NUL)
3299 return p;
3300
3301 /* Skip space(s) after +command to get to the real argument */
3302 arg = skipwhite(arg);
3303 }
3304
3305 /*
3306 * Check for '|' to separate commands and '"' to start comments.
3307 * Don't do this for ":read !cmd" and ":write !cmd".
3308 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003309 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 p = arg;
3312 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003313 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 p += 2;
3315 while (*p)
3316 {
3317 if (*p == Ctrl_V)
3318 {
3319 if (p[1] != NUL)
3320 ++p;
3321 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003322 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 || *p == '|' || *p == '\n')
3324 {
3325 if (*(p - 1) != '\\')
3326 {
3327 if (*p == '|' || *p == '\n')
3328 return p + 1;
3329 return NULL; /* It's a comment */
3330 }
3331 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003332 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 }
3335
3336 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003337 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 vim_strchr((char_u *)"|\"", *arg) == NULL)
3339 return NULL;
3340
3341 /* Find start of last argument (argument just before cursor): */
3342 p = buff + STRLEN(buff);
3343 while (p != arg && *p != ' ' && *p != TAB)
3344 p--;
3345 if (*p == ' ' || *p == TAB)
3346 p++;
3347 xp->xp_pattern = p;
3348
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003349 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003351 int c;
3352 int in_quote = FALSE;
3353 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355 /*
3356 * Allow spaces within back-quotes to count as part of the argument
3357 * being expanded.
3358 */
3359 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003360 p = xp->xp_pattern;
3361 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003363#ifdef FEAT_MBYTE
3364 if (has_mbyte)
3365 c = mb_ptr2char(p);
3366 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003368 c = *p;
3369 if (c == '\\' && p[1] != NUL)
3370 ++p;
3371 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
3373 if (!in_quote)
3374 {
3375 xp->xp_pattern = p;
3376 bow = p + 1;
3377 }
3378 in_quote = !in_quote;
3379 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003380 /* An argument can contain just about everything, except
3381 * characters that end the command and white space. */
3382 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003383#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003384 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003385#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003386 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003387 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003388 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003389 while (*p != NUL)
3390 {
3391#ifdef FEAT_MBYTE
3392 if (has_mbyte)
3393 c = mb_ptr2char(p);
3394 else
3395#endif
3396 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003397 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003398 break;
3399#ifdef FEAT_MBYTE
3400 if (has_mbyte)
3401 len = (*mb_ptr2len)(p);
3402 else
3403#endif
3404 len = 1;
3405 mb_ptr_adv(p);
3406 }
3407 if (in_quote)
3408 bow = p;
3409 else
3410 xp->xp_pattern = p;
3411 p -= len;
3412 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003413 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
3415
3416 /*
3417 * If we are still inside the quotes, and we passed a space, just
3418 * expand from there.
3419 */
3420 if (bow != NULL && in_quote)
3421 xp->xp_pattern = bow;
3422 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003423
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003424#ifndef BACKSLASH_IN_FILENAME
3425 /* For a shell command more chars need to be escaped. */
3426 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003427 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003428 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003429
3430 /* When still after the command name expand executables. */
3431 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003432 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003433 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
3436 /* Check for environment variable */
3437 if (*xp->xp_pattern == '$'
3438#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3439 || *xp->xp_pattern == '%'
3440#endif
3441 )
3442 {
3443 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3444 if (!vim_isIDc(*p))
3445 break;
3446 if (*p == NUL)
3447 {
3448 xp->xp_context = EXPAND_ENV_VARS;
3449 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003450#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3451 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003452 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003453 compl = EXPAND_ENV_VARS;
3454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 }
3456 }
3457 }
3458
3459/*
3460 * 6. switch on command name
3461 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003462 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003464 case CMD_find:
3465 case CMD_sfind:
3466 case CMD_tabfind:
3467 xp->xp_context = EXPAND_FILES_IN_PATH;
3468 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 case CMD_cd:
3470 case CMD_chdir:
3471 case CMD_lcd:
3472 case CMD_lchdir:
3473 if (xp->xp_context == EXPAND_FILES)
3474 xp->xp_context = EXPAND_DIRECTORIES;
3475 break;
3476 case CMD_help:
3477 xp->xp_context = EXPAND_HELP;
3478 xp->xp_pattern = arg;
3479 break;
3480
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003481 /* Command modifiers: return the argument.
3482 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003484 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 case CMD_belowright:
3486 case CMD_botright:
3487 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003488 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003490 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 case CMD_folddoclosed:
3492 case CMD_folddoopen:
3493 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003494 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 case CMD_keepjumps:
3496 case CMD_keepmarks:
3497 case CMD_leftabove:
3498 case CMD_lockmarks:
3499 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003500 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003502 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 case CMD_topleft:
3504 case CMD_verbose:
3505 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003506 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 return arg;
3508
Bram Moolenaar4f688582007-07-24 12:34:30 +00003509#ifdef FEAT_CMDL_COMPL
3510# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 case CMD_match:
3512 if (*arg == NUL || !ends_excmd(*arg))
3513 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003514 /* also complete "None" */
3515 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 arg = skipwhite(skiptowhite(arg));
3517 if (*arg != NUL)
3518 {
3519 xp->xp_context = EXPAND_NOTHING;
3520 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3521 }
3522 }
3523 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003524# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526/*
3527 * All completion for the +cmdline_compl feature goes here.
3528 */
3529
3530# ifdef FEAT_USR_CMDS
3531 case CMD_command:
3532 /* Check for attributes */
3533 while (*arg == '-')
3534 {
3535 arg++; /* Skip "-" */
3536 p = skiptowhite(arg);
3537 if (*p == NUL)
3538 {
3539 /* Cursor is still in the attribute */
3540 p = vim_strchr(arg, '=');
3541 if (p == NULL)
3542 {
3543 /* No "=", so complete attribute names */
3544 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3545 xp->xp_pattern = arg;
3546 return NULL;
3547 }
3548
3549 /* For the -complete and -nargs attributes, we complete
3550 * their arguments as well.
3551 */
3552 if (STRNICMP(arg, "complete", p - arg) == 0)
3553 {
3554 xp->xp_context = EXPAND_USER_COMPLETE;
3555 xp->xp_pattern = p + 1;
3556 return NULL;
3557 }
3558 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3559 {
3560 xp->xp_context = EXPAND_USER_NARGS;
3561 xp->xp_pattern = p + 1;
3562 return NULL;
3563 }
3564 return NULL;
3565 }
3566 arg = skipwhite(p);
3567 }
3568
3569 /* After the attributes comes the new command name */
3570 p = skiptowhite(arg);
3571 if (*p == NUL)
3572 {
3573 xp->xp_context = EXPAND_USER_COMMANDS;
3574 xp->xp_pattern = arg;
3575 break;
3576 }
3577
3578 /* And finally comes a normal command */
3579 return skipwhite(p);
3580
3581 case CMD_delcommand:
3582 xp->xp_context = EXPAND_USER_COMMANDS;
3583 xp->xp_pattern = arg;
3584 break;
3585# endif
3586
3587 case CMD_global:
3588 case CMD_vglobal:
3589 delim = *arg; /* get the delimiter */
3590 if (delim)
3591 ++arg; /* skip delimiter if there is one */
3592
3593 while (arg[0] != NUL && arg[0] != delim)
3594 {
3595 if (arg[0] == '\\' && arg[1] != NUL)
3596 ++arg;
3597 ++arg;
3598 }
3599 if (arg[0] != NUL)
3600 return arg + 1;
3601 break;
3602 case CMD_and:
3603 case CMD_substitute:
3604 delim = *arg;
3605 if (delim)
3606 {
3607 /* skip "from" part */
3608 ++arg;
3609 arg = skip_regexp(arg, delim, p_magic, NULL);
3610 }
3611 /* skip "to" part */
3612 while (arg[0] != NUL && arg[0] != delim)
3613 {
3614 if (arg[0] == '\\' && arg[1] != NUL)
3615 ++arg;
3616 ++arg;
3617 }
3618 if (arg[0] != NUL) /* skip delimiter */
3619 ++arg;
3620 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3621 ++arg;
3622 if (arg[0] != NUL)
3623 return arg;
3624 break;
3625 case CMD_isearch:
3626 case CMD_dsearch:
3627 case CMD_ilist:
3628 case CMD_dlist:
3629 case CMD_ijump:
3630 case CMD_psearch:
3631 case CMD_djump:
3632 case CMD_isplit:
3633 case CMD_dsplit:
3634 arg = skipwhite(skipdigits(arg)); /* skip count */
3635 if (*arg == '/') /* Match regexp, not just whole words */
3636 {
3637 for (++arg; *arg && *arg != '/'; arg++)
3638 if (*arg == '\\' && arg[1] != NUL)
3639 arg++;
3640 if (*arg)
3641 {
3642 arg = skipwhite(arg + 1);
3643
3644 /* Check for trailing illegal characters */
3645 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3646 xp->xp_context = EXPAND_NOTHING;
3647 else
3648 return arg;
3649 }
3650 }
3651 break;
3652#ifdef FEAT_AUTOCMD
3653 case CMD_autocmd:
3654 return set_context_in_autocmd(xp, arg, FALSE);
3655
3656 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003657 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 return set_context_in_autocmd(xp, arg, TRUE);
3659#endif
3660 case CMD_set:
3661 set_context_in_set_cmd(xp, arg, 0);
3662 break;
3663 case CMD_setglobal:
3664 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3665 break;
3666 case CMD_setlocal:
3667 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3668 break;
3669 case CMD_tag:
3670 case CMD_stag:
3671 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003672 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 case CMD_tselect:
3674 case CMD_stselect:
3675 case CMD_ptselect:
3676 case CMD_tjump:
3677 case CMD_stjump:
3678 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003679 if (*p_wop != NUL)
3680 xp->xp_context = EXPAND_TAGS_LISTFILES;
3681 else
3682 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 xp->xp_pattern = arg;
3684 break;
3685 case CMD_augroup:
3686 xp->xp_context = EXPAND_AUGROUP;
3687 xp->xp_pattern = arg;
3688 break;
3689#ifdef FEAT_SYN_HL
3690 case CMD_syntax:
3691 set_context_in_syntax_cmd(xp, arg);
3692 break;
3693#endif
3694#ifdef FEAT_EVAL
3695 case CMD_let:
3696 case CMD_if:
3697 case CMD_elseif:
3698 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003699 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 case CMD_echo:
3701 case CMD_echon:
3702 case CMD_execute:
3703 case CMD_echomsg:
3704 case CMD_echoerr:
3705 case CMD_call:
3706 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003707 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 break;
3709
3710 case CMD_unlet:
3711 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3712 arg = xp->xp_pattern + 1;
3713 xp->xp_context = EXPAND_USER_VARS;
3714 xp->xp_pattern = arg;
3715 break;
3716
3717 case CMD_function:
3718 case CMD_delfunction:
3719 xp->xp_context = EXPAND_USER_FUNC;
3720 xp->xp_pattern = arg;
3721 break;
3722
3723 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003724 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 break;
3726#endif
3727 case CMD_highlight:
3728 set_context_in_highlight_cmd(xp, arg);
3729 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003730#ifdef FEAT_CSCOPE
3731 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003732 case CMD_lcscope:
3733 case CMD_scscope:
3734 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003735 break;
3736#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003737#ifdef FEAT_SIGNS
3738 case CMD_sign:
3739 set_context_in_sign_cmd(xp, arg);
3740 break;
3741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742#ifdef FEAT_LISTCMDS
3743 case CMD_bdelete:
3744 case CMD_bwipeout:
3745 case CMD_bunload:
3746 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3747 arg = xp->xp_pattern + 1;
3748 /*FALLTHROUGH*/
3749 case CMD_buffer:
3750 case CMD_sbuffer:
3751 case CMD_checktime:
3752 xp->xp_context = EXPAND_BUFFERS;
3753 xp->xp_pattern = arg;
3754 break;
3755#endif
3756#ifdef FEAT_USR_CMDS
3757 case CMD_USER:
3758 case CMD_USER_BUF:
3759 if (compl != EXPAND_NOTHING)
3760 {
3761 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003762 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 {
3764# ifdef FEAT_MENU
3765 if (compl == EXPAND_MENUS)
3766 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3767# endif
3768 if (compl == EXPAND_COMMANDS)
3769 return arg;
3770 if (compl == EXPAND_MAPPINGS)
3771 return set_context_in_map_cmd(xp, (char_u *)"map",
3772 arg, forceit, FALSE, FALSE, CMD_map);
3773 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3774 arg = xp->xp_pattern + 1;
3775 xp->xp_pattern = arg;
3776 }
3777 xp->xp_context = compl;
3778 }
3779 break;
3780#endif
3781 case CMD_map: case CMD_noremap:
3782 case CMD_nmap: case CMD_nnoremap:
3783 case CMD_vmap: case CMD_vnoremap:
3784 case CMD_omap: case CMD_onoremap:
3785 case CMD_imap: case CMD_inoremap:
3786 case CMD_cmap: case CMD_cnoremap:
3787 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003788 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 case CMD_unmap:
3790 case CMD_nunmap:
3791 case CMD_vunmap:
3792 case CMD_ounmap:
3793 case CMD_iunmap:
3794 case CMD_cunmap:
3795 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003796 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 case CMD_abbreviate: case CMD_noreabbrev:
3798 case CMD_cabbrev: case CMD_cnoreabbrev:
3799 case CMD_iabbrev: case CMD_inoreabbrev:
3800 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003801 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 case CMD_unabbreviate:
3803 case CMD_cunabbrev:
3804 case CMD_iunabbrev:
3805 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003806 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807#ifdef FEAT_MENU
3808 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3809 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3810 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3811 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3812 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3813 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3814 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3815 case CMD_tmenu: case CMD_tunmenu:
3816 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3817 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3818#endif
3819
3820 case CMD_colorscheme:
3821 xp->xp_context = EXPAND_COLORS;
3822 xp->xp_pattern = arg;
3823 break;
3824
3825 case CMD_compiler:
3826 xp->xp_context = EXPAND_COMPILER;
3827 xp->xp_pattern = arg;
3828 break;
3829
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003830 case CMD_ownsyntax:
3831 xp->xp_context = EXPAND_FILETYPE;
3832 xp->xp_pattern = arg;
3833 break;
3834
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3836 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3837 case CMD_language:
3838 if (*skiptowhite(arg) == NUL)
3839 {
3840 xp->xp_context = EXPAND_LANGUAGE;
3841 xp->xp_pattern = arg;
3842 }
3843 else
3844 xp->xp_context = EXPAND_NOTHING;
3845 break;
3846#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003847#if defined(FEAT_PROFILE)
3848 case CMD_profile:
3849 set_context_in_profile_cmd(xp, arg);
3850 break;
3851#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003852 case CMD_behave:
3853 xp->xp_context = EXPAND_BEHAVE;
3854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856#endif /* FEAT_CMDL_COMPL */
3857
3858 default:
3859 break;
3860 }
3861 return NULL;
3862}
3863
3864/*
3865 * skip a range specifier of the form: addr [,addr] [;addr] ..
3866 *
3867 * Backslashed delimiters after / or ? will be skipped, and commands will
3868 * not be expanded between /'s and ?'s or after "'".
3869 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003870 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 * Returns the "cmd" pointer advanced to beyond the range.
3872 */
3873 char_u *
3874skip_range(cmd, ctx)
3875 char_u *cmd;
3876 int *ctx; /* pointer to xp_context or NULL */
3877{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003878 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
Bram Moolenaardf177f62005-02-22 08:39:57 +00003880 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 {
3882 if (*cmd == '\'')
3883 {
3884 if (*++cmd == NUL && ctx != NULL)
3885 *ctx = EXPAND_NOTHING;
3886 }
3887 else if (*cmd == '/' || *cmd == '?')
3888 {
3889 delim = *cmd++;
3890 while (*cmd != NUL && *cmd != delim)
3891 if (*cmd++ == '\\' && *cmd != NUL)
3892 ++cmd;
3893 if (*cmd == NUL && ctx != NULL)
3894 *ctx = EXPAND_NOTHING;
3895 }
3896 if (*cmd != NUL)
3897 ++cmd;
3898 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003899
3900 /* Skip ":" and white space. */
3901 while (*cmd == ':')
3902 cmd = skipwhite(cmd + 1);
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 return cmd;
3905}
3906
3907/*
3908 * get a single EX address
3909 *
3910 * Set ptr to the next character after the part that was interpreted.
3911 * Set ptr to NULL when an error is encountered.
3912 *
3913 * Return MAXLNUM when no Ex address was found.
3914 */
3915 static linenr_T
3916get_address(ptr, skip, to_other_file)
3917 char_u **ptr;
3918 int skip; /* only skip the address, don't use it */
3919 int to_other_file; /* flag: may jump to other file */
3920{
3921 int c;
3922 int i;
3923 long n;
3924 char_u *cmd;
3925 pos_T pos;
3926 pos_T *fp;
3927 linenr_T lnum;
3928
3929 cmd = skipwhite(*ptr);
3930 lnum = MAXLNUM;
3931 do
3932 {
3933 switch (*cmd)
3934 {
3935 case '.': /* '.' - Cursor position */
3936 ++cmd;
3937 lnum = curwin->w_cursor.lnum;
3938 break;
3939
3940 case '$': /* '$' - last line */
3941 ++cmd;
3942 lnum = curbuf->b_ml.ml_line_count;
3943 break;
3944
3945 case '\'': /* ''' - mark */
3946 if (*++cmd == NUL)
3947 {
3948 cmd = NULL;
3949 goto error;
3950 }
3951 if (skip)
3952 ++cmd;
3953 else
3954 {
3955 /* Only accept a mark in another file when it is
3956 * used by itself: ":'M". */
3957 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3958 ++cmd;
3959 if (fp == (pos_T *)-1)
3960 /* Jumped to another file. */
3961 lnum = curwin->w_cursor.lnum;
3962 else
3963 {
3964 if (check_mark(fp) == FAIL)
3965 {
3966 cmd = NULL;
3967 goto error;
3968 }
3969 lnum = fp->lnum;
3970 }
3971 }
3972 break;
3973
3974 case '/':
3975 case '?': /* '/' or '?' - search */
3976 c = *cmd++;
3977 if (skip) /* skip "/pat/" */
3978 {
3979 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3980 if (*cmd == c)
3981 ++cmd;
3982 }
3983 else
3984 {
3985 pos = curwin->w_cursor; /* save curwin->w_cursor */
3986 /*
3987 * When '/' or '?' follows another address, start
3988 * from there.
3989 */
3990 if (lnum != MAXLNUM)
3991 curwin->w_cursor.lnum = lnum;
3992 /*
3993 * Start a forward search at the end of the line.
3994 * Start a backward search at the start of the line.
3995 * This makes sure we never match in the current
3996 * line, and can match anywhere in the
3997 * next/previous line.
3998 */
3999 if (c == '/')
4000 curwin->w_cursor.col = MAXCOL;
4001 else
4002 curwin->w_cursor.col = 0;
4003 searchcmdlen = 0;
4004 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004005 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
4007 curwin->w_cursor = pos;
4008 cmd = NULL;
4009 goto error;
4010 }
4011 lnum = curwin->w_cursor.lnum;
4012 curwin->w_cursor = pos;
4013 /* adjust command string pointer */
4014 cmd += searchcmdlen;
4015 }
4016 break;
4017
4018 case '\\': /* "\?", "\/" or "\&", repeat search */
4019 ++cmd;
4020 if (*cmd == '&')
4021 i = RE_SUBST;
4022 else if (*cmd == '?' || *cmd == '/')
4023 i = RE_SEARCH;
4024 else
4025 {
4026 EMSG(_(e_backslash));
4027 cmd = NULL;
4028 goto error;
4029 }
4030
4031 if (!skip)
4032 {
4033 /*
4034 * When search follows another address, start from
4035 * there.
4036 */
4037 if (lnum != MAXLNUM)
4038 pos.lnum = lnum;
4039 else
4040 pos.lnum = curwin->w_cursor.lnum;
4041
4042 /*
4043 * Start the search just like for the above
4044 * do_search().
4045 */
4046 if (*cmd != '?')
4047 pos.col = MAXCOL;
4048 else
4049 pos.col = 0;
4050 if (searchit(curwin, curbuf, &pos,
4051 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004052 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004053 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 lnum = pos.lnum;
4055 else
4056 {
4057 cmd = NULL;
4058 goto error;
4059 }
4060 }
4061 ++cmd;
4062 break;
4063
4064 default:
4065 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4066 lnum = getdigits(&cmd);
4067 }
4068
4069 for (;;)
4070 {
4071 cmd = skipwhite(cmd);
4072 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4073 break;
4074
4075 if (lnum == MAXLNUM)
4076 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4077 if (VIM_ISDIGIT(*cmd))
4078 i = '+'; /* "number" is same as "+number" */
4079 else
4080 i = *cmd++;
4081 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4082 n = 1;
4083 else
4084 n = getdigits(&cmd);
4085 if (i == '-')
4086 lnum -= n;
4087 else
4088 lnum += n;
4089 }
4090 } while (*cmd == '/' || *cmd == '?');
4091
4092error:
4093 *ptr = cmd;
4094 return lnum;
4095}
4096
4097/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004098 * Get flags from an Ex command argument.
4099 */
4100 static void
4101get_flags(eap)
4102 exarg_T *eap;
4103{
4104 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4105 {
4106 if (*eap->arg == 'l')
4107 eap->flags |= EXFLAG_LIST;
4108 else if (*eap->arg == 'p')
4109 eap->flags |= EXFLAG_PRINT;
4110 else
4111 eap->flags |= EXFLAG_NR;
4112 eap->arg = skipwhite(eap->arg + 1);
4113 }
4114}
4115
4116/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 * Function called for command which is Not Implemented. NI!
4118 */
4119 void
4120ex_ni(eap)
4121 exarg_T *eap;
4122{
4123 if (!eap->skip)
4124 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4125}
4126
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004127#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128/*
4129 * Function called for script command which is Not Implemented. NI!
4130 * Skips over ":perl <<EOF" constructs.
4131 */
4132 static void
4133ex_script_ni(eap)
4134 exarg_T *eap;
4135{
4136 if (!eap->skip)
4137 ex_ni(eap);
4138 else
4139 vim_free(script_get(eap, eap->arg));
4140}
4141#endif
4142
4143/*
4144 * Check range in Ex command for validity.
4145 * Return NULL when valid, error message when invalid.
4146 */
4147 static char_u *
4148invalid_range(eap)
4149 exarg_T *eap;
4150{
4151 if ( eap->line1 < 0
4152 || eap->line2 < 0
4153 || eap->line1 > eap->line2
4154 || ((eap->argt & RANGE)
4155 && !(eap->argt & NOTADR)
4156 && eap->line2 > curbuf->b_ml.ml_line_count
4157#ifdef FEAT_DIFF
4158 + (eap->cmdidx == CMD_diffget)
4159#endif
4160 ))
4161 return (char_u *)_(e_invrange);
4162 return NULL;
4163}
4164
4165/*
4166 * Correct the range for zero line number, if required.
4167 */
4168 static void
4169correct_range(eap)
4170 exarg_T *eap;
4171{
4172 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4173 {
4174 if (eap->line1 == 0)
4175 eap->line1 = 1;
4176 if (eap->line2 == 0)
4177 eap->line2 = 1;
4178 }
4179}
4180
Bram Moolenaar748bf032005-02-02 23:04:36 +00004181#ifdef FEAT_QUICKFIX
4182static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4183
4184/*
4185 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4186 * pattern. Otherwise return eap->arg.
4187 */
4188 static char_u *
4189skip_grep_pat(eap)
4190 exarg_T *eap;
4191{
4192 char_u *p = eap->arg;
4193
Bram Moolenaara37420f2006-02-04 22:37:47 +00004194 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4195 || eap->cmdidx == CMD_vimgrepadd
4196 || eap->cmdidx == CMD_lvimgrepadd
4197 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004198 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004199 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004200 if (p == NULL)
4201 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004202 }
4203 return p;
4204}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004205
4206/*
4207 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4208 * in the command line, so that things like % get expanded.
4209 */
4210 static char_u *
4211replace_makeprg(eap, p, cmdlinep)
4212 exarg_T *eap;
4213 char_u *p;
4214 char_u **cmdlinep;
4215{
4216 char_u *new_cmdline;
4217 char_u *program;
4218 char_u *pos;
4219 char_u *ptr;
4220 int len;
4221 int i;
4222
4223 /*
4224 * Don't do it when ":vimgrep" is used for ":grep".
4225 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004226 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4227 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4228 || eap->cmdidx == CMD_grepadd
4229 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004230 && !grep_internal(eap->cmdidx))
4231 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004232 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4233 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004234 {
4235 if (*curbuf->b_p_gp == NUL)
4236 program = p_gp;
4237 else
4238 program = curbuf->b_p_gp;
4239 }
4240 else
4241 {
4242 if (*curbuf->b_p_mp == NUL)
4243 program = p_mp;
4244 else
4245 program = curbuf->b_p_mp;
4246 }
4247
4248 p = skipwhite(p);
4249
4250 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4251 {
4252 /* replace $* by given arguments */
4253 i = 1;
4254 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4255 ++i;
4256 len = (int)STRLEN(p);
4257 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4258 if (new_cmdline == NULL)
4259 return NULL; /* out of memory */
4260 ptr = new_cmdline;
4261 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4262 {
4263 i = (int)(pos - program);
4264 STRNCPY(ptr, program, i);
4265 STRCPY(ptr += i, p);
4266 ptr += len;
4267 program = pos + 2;
4268 }
4269 STRCPY(ptr, program);
4270 }
4271 else
4272 {
4273 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4274 if (new_cmdline == NULL)
4275 return NULL; /* out of memory */
4276 STRCPY(new_cmdline, program);
4277 STRCAT(new_cmdline, " ");
4278 STRCAT(new_cmdline, p);
4279 }
4280 msg_make(p);
4281
4282 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4283 vim_free(*cmdlinep);
4284 *cmdlinep = new_cmdline;
4285 p = new_cmdline;
4286 }
4287 return p;
4288}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004289#endif
4290
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291/*
4292 * Expand file name in Ex command argument.
4293 * Return FAIL for failure, OK otherwise.
4294 */
4295 int
4296expand_filename(eap, cmdlinep, errormsgp)
4297 exarg_T *eap;
4298 char_u **cmdlinep;
4299 char_u **errormsgp;
4300{
4301 int has_wildcards; /* need to expand wildcards */
4302 char_u *repl;
4303 int srclen;
4304 char_u *p;
4305 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004306 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
Bram Moolenaar748bf032005-02-02 23:04:36 +00004308#ifdef FEAT_QUICKFIX
4309 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4310 p = skip_grep_pat(eap);
4311#else
4312 p = eap->arg;
4313#endif
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 /*
4316 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4317 * the file name contains a wildcard it should not cause expanding.
4318 * (it will be expanded anyway if there is a wildcard before replacing).
4319 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004320 has_wildcards = mch_has_wildcard(p);
4321 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004323#ifdef FEAT_EVAL
4324 /* Skip over `=expr`, wildcards in it are not expanded. */
4325 if (p[0] == '`' && p[1] == '=')
4326 {
4327 p += 2;
4328 (void)skip_expr(&p);
4329 if (*p == '`')
4330 ++p;
4331 continue;
4332 }
4333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /*
4335 * Quick check if this cannot be the start of a special string.
4336 * Also removes backslash before '%', '#' and '<'.
4337 */
4338 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4339 {
4340 ++p;
4341 continue;
4342 }
4343
4344 /*
4345 * Try to find a match at this position.
4346 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004347 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4348 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 if (*errormsgp != NULL) /* error detected */
4350 return FAIL;
4351 if (repl == NULL) /* no match found */
4352 {
4353 p += srclen;
4354 continue;
4355 }
4356
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004357 /* Wildcards won't be expanded below, the replacement is taken
4358 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4359 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4360 {
4361 char_u *l = repl;
4362
4363 repl = expand_env_save(repl);
4364 vim_free(l);
4365 }
4366
Bram Moolenaar63b92542007-03-27 14:57:09 +00004367 /* Need to escape white space et al. with a backslash.
4368 * Don't do this for:
4369 * - replacement that already has been escaped: "##"
4370 * - shell commands (may have to use quotes instead).
4371 * - non-unix systems when there is a single argument (spaces don't
4372 * separate arguments then).
4373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004375 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 && eap->cmdidx != CMD_bang
4377 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004378 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004380 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004382 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#ifndef UNIX
4384 && !(eap->argt & NOSPC)
4385#endif
4386 )
4387 {
4388 char_u *l;
4389#ifdef BACKSLASH_IN_FILENAME
4390 /* Don't escape a backslash here, because rem_backslash() doesn't
4391 * remove it later. */
4392 static char_u *nobslash = (char_u *)" \t\"|";
4393# define ESCAPE_CHARS nobslash
4394#else
4395# define ESCAPE_CHARS escape_chars
4396#endif
4397
4398 for (l = repl; *l; ++l)
4399 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4400 {
4401 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4402 if (l != NULL)
4403 {
4404 vim_free(repl);
4405 repl = l;
4406 }
4407 break;
4408 }
4409 }
4410
4411 /* For a shell command a '!' must be escaped. */
4412 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004413 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
4415 char_u *l;
4416
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004417 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 if (l != NULL)
4419 {
4420 vim_free(repl);
4421 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004422 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (strstr((char *)p_sh, "sh") != NULL)
4424 {
4425 l = vim_strsave_escaped(repl, (char_u *)"!");
4426 if (l != NULL)
4427 {
4428 vim_free(repl);
4429 repl = l;
4430 }
4431 }
4432 }
4433 }
4434
4435 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4436 vim_free(repl);
4437 if (p == NULL)
4438 return FAIL;
4439 }
4440
4441 /*
4442 * One file argument: Expand wildcards.
4443 * Don't do this with ":r !command" or ":w !command".
4444 */
4445 if ((eap->argt & NOSPC) && !eap->usefilter)
4446 {
4447 /*
4448 * May do this twice:
4449 * 1. Replace environment variables.
4450 * 2. Replace any other wildcards, remove backslashes.
4451 */
4452 for (n = 1; n <= 2; ++n)
4453 {
4454 if (n == 2)
4455 {
4456#ifdef UNIX
4457 /*
4458 * Only for Unix we check for more than one file name.
4459 * For other systems spaces are considered to be part
4460 * of the file name.
4461 * Only check here if there is no wildcard, otherwise
4462 * ExpandOne() will check for errors. This allows
4463 * ":e `ls ve*.c`" on Unix.
4464 */
4465 if (!has_wildcards)
4466 for (p = eap->arg; *p; ++p)
4467 {
4468 /* skip escaped characters */
4469 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4470 ++p;
4471 else if (vim_iswhite(*p))
4472 {
4473 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4474 return FAIL;
4475 }
4476 }
4477#endif
4478
4479 /*
4480 * Halve the number of backslashes (this is Vi compatible).
4481 * For Unix and OS/2, when wildcards are expanded, this is
4482 * done by ExpandOne() below.
4483 */
4484#if defined(UNIX) || defined(OS2)
4485 if (!has_wildcards)
4486#endif
4487 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489
4490 if (has_wildcards)
4491 {
4492 if (n == 1)
4493 {
4494 /*
4495 * First loop: May expand environment variables. This
4496 * can be done much faster with expand_env() than with
4497 * something else (e.g., calling a shell).
4498 * After expanding environment variables, check again
4499 * if there are still wildcards present.
4500 */
4501 if (vim_strchr(eap->arg, '$') != NULL
4502 || vim_strchr(eap->arg, '~') != NULL)
4503 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004504 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004505 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 has_wildcards = mch_has_wildcard(NameBuff);
4507 p = NameBuff;
4508 }
4509 else
4510 p = NULL;
4511 }
4512 else /* n == 2 */
4513 {
4514 expand_T xpc;
4515
4516 ExpandInit(&xpc);
4517 xpc.xp_context = EXPAND_FILES;
4518 p = ExpandOne(&xpc, eap->arg, NULL,
4519 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4520 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 if (p == NULL)
4522 return FAIL;
4523 }
4524 if (p != NULL)
4525 {
4526 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4527 p, cmdlinep);
4528 if (n == 2) /* p came from ExpandOne() */
4529 vim_free(p);
4530 }
4531 }
4532 }
4533 }
4534 return OK;
4535}
4536
4537/*
4538 * Replace part of the command line, keeping eap->cmd, eap->arg and
4539 * eap->nextcmd correct.
4540 * "src" points to the part that is to be replaced, of length "srclen".
4541 * "repl" is the replacement string.
4542 * Returns a pointer to the character after the replaced string.
4543 * Returns NULL for failure.
4544 */
4545 static char_u *
4546repl_cmdline(eap, src, srclen, repl, cmdlinep)
4547 exarg_T *eap;
4548 char_u *src;
4549 int srclen;
4550 char_u *repl;
4551 char_u **cmdlinep;
4552{
4553 int len;
4554 int i;
4555 char_u *new_cmdline;
4556
4557 /*
4558 * The new command line is build in new_cmdline[].
4559 * First allocate it.
4560 * Careful: a "+cmd" argument may have been NUL terminated.
4561 */
4562 len = (int)STRLEN(repl);
4563 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004564 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4566 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4567 return NULL; /* out of memory! */
4568
4569 /*
4570 * Copy the stuff before the expanded part.
4571 * Copy the expanded stuff.
4572 * Copy what came after the expanded part.
4573 * Copy the next commands, if there are any.
4574 */
4575 i = (int)(src - *cmdlinep); /* length of part before match */
4576 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004577
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 mch_memmove(new_cmdline + i, repl, (size_t)len);
4579 i += len; /* remember the end of the string */
4580 STRCPY(new_cmdline + i, src + srclen);
4581 src = new_cmdline + i; /* remember where to continue */
4582
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004583 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 i = (int)STRLEN(new_cmdline) + 1;
4586 STRCPY(new_cmdline + i, eap->nextcmd);
4587 eap->nextcmd = new_cmdline + i;
4588 }
4589 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4590 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4591 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4592 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4593 vim_free(*cmdlinep);
4594 *cmdlinep = new_cmdline;
4595
4596 return src;
4597}
4598
4599/*
4600 * Check for '|' to separate commands and '"' to start comments.
4601 */
4602 void
4603separate_nextcmd(eap)
4604 exarg_T *eap;
4605{
4606 char_u *p;
4607
Bram Moolenaar86b68352004-12-27 21:59:20 +00004608#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004609 p = skip_grep_pat(eap);
4610#else
4611 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004612#endif
4613
4614 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 if (*p == Ctrl_V)
4617 {
4618 if (eap->argt & (USECTRLV | XFILE))
4619 ++p; /* skip CTRL-V and next char */
4620 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004621 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004622 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (*p == NUL) /* stop at NUL after CTRL-V */
4624 break;
4625 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004626
4627#ifdef FEAT_EVAL
4628 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004629 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004630 {
4631 p += 2;
4632 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004633 }
4634#endif
4635
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 /* Check for '"': start of comment or '|': next command */
4637 /* :@" and :*" do not start a comment!
4638 * :redir @" doesn't either. */
4639 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4640 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4641 || p != eap->arg)
4642 && (eap->cmdidx != CMD_redir
4643 || p != eap->arg + 1 || p[-1] != '@'))
4644 || *p == '|' || *p == '\n')
4645 {
4646 /*
4647 * We remove the '\' before the '|', unless USECTRLV is used
4648 * AND 'b' is present in 'cpoptions'.
4649 */
4650 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4651 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4652 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004653 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 --p;
4655 }
4656 else
4657 {
4658 eap->nextcmd = check_nextcmd(p);
4659 *p = NUL;
4660 break;
4661 }
4662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4666 del_trailing_spaces(eap->arg);
4667}
4668
4669/*
4670 * get + command from ex argument
4671 */
4672 static char_u *
4673getargcmd(argp)
4674 char_u **argp;
4675{
4676 char_u *arg = *argp;
4677 char_u *command = NULL;
4678
4679 if (*arg == '+') /* +[command] */
4680 {
4681 ++arg;
4682 if (vim_isspace(*arg))
4683 command = dollar_command;
4684 else
4685 {
4686 command = arg;
4687 arg = skip_cmd_arg(command, TRUE);
4688 if (*arg != NUL)
4689 *arg++ = NUL; /* terminate command with NUL */
4690 }
4691
4692 arg = skipwhite(arg); /* skip over spaces */
4693 *argp = arg;
4694 }
4695 return command;
4696}
4697
4698/*
4699 * Find end of "+command" argument. Skip over "\ " and "\\".
4700 */
4701 static char_u *
4702skip_cmd_arg(p, rembs)
4703 char_u *p;
4704 int rembs; /* TRUE to halve the number of backslashes */
4705{
4706 while (*p && !vim_isspace(*p))
4707 {
4708 if (*p == '\\' && p[1] != NUL)
4709 {
4710 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004711 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 else
4713 ++p;
4714 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004715 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717 return p;
4718}
4719
4720/*
4721 * Get "++opt=arg" argument.
4722 * Return FAIL or OK.
4723 */
4724 static int
4725getargopt(eap)
4726 exarg_T *eap;
4727{
4728 char_u *arg = eap->arg + 2;
4729 int *pp = NULL;
4730#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004731 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 char_u *p;
4733#endif
4734
4735 /* ":edit ++[no]bin[ary] file" */
4736 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4737 {
4738 if (*arg == 'n')
4739 {
4740 arg += 2;
4741 eap->force_bin = FORCE_NOBIN;
4742 }
4743 else
4744 eap->force_bin = FORCE_BIN;
4745 if (!checkforcmd(&arg, "binary", 3))
4746 return FAIL;
4747 eap->arg = skipwhite(arg);
4748 return OK;
4749 }
4750
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004751 /* ":read ++edit file" */
4752 if (STRNCMP(arg, "edit", 4) == 0)
4753 {
4754 eap->read_edit = TRUE;
4755 eap->arg = skipwhite(arg + 4);
4756 return OK;
4757 }
4758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (STRNCMP(arg, "ff", 2) == 0)
4760 {
4761 arg += 2;
4762 pp = &eap->force_ff;
4763 }
4764 else if (STRNCMP(arg, "fileformat", 10) == 0)
4765 {
4766 arg += 10;
4767 pp = &eap->force_ff;
4768 }
4769#ifdef FEAT_MBYTE
4770 else if (STRNCMP(arg, "enc", 3) == 0)
4771 {
4772 arg += 3;
4773 pp = &eap->force_enc;
4774 }
4775 else if (STRNCMP(arg, "encoding", 8) == 0)
4776 {
4777 arg += 8;
4778 pp = &eap->force_enc;
4779 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004780 else if (STRNCMP(arg, "bad", 3) == 0)
4781 {
4782 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004783 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785#endif
4786
4787 if (pp == NULL || *arg != '=')
4788 return FAIL;
4789
4790 ++arg;
4791 *pp = (int)(arg - eap->cmd);
4792 arg = skip_cmd_arg(arg, FALSE);
4793 eap->arg = skipwhite(arg);
4794 *arg = NUL;
4795
4796#ifdef FEAT_MBYTE
4797 if (pp == &eap->force_ff)
4798 {
4799#endif
4800 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4801 return FAIL;
4802#ifdef FEAT_MBYTE
4803 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004804 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
4806 /* Make 'fileencoding' lower case. */
4807 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4808 *p = TOLOWER_ASC(*p);
4809 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004810 else
4811 {
4812 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4813 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004814 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004815 if (STRICMP(p, "keep") == 0)
4816 eap->bad_char = BAD_KEEP;
4817 else if (STRICMP(p, "drop") == 0)
4818 eap->bad_char = BAD_DROP;
4819 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4820 eap->bad_char = *p;
4821 else
4822 return FAIL;
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#endif
4825
4826 return OK;
4827}
4828
4829/*
4830 * ":abbreviate" and friends.
4831 */
4832 static void
4833ex_abbreviate(eap)
4834 exarg_T *eap;
4835{
4836 do_exmap(eap, TRUE); /* almost the same as mapping */
4837}
4838
4839/*
4840 * ":map" and friends.
4841 */
4842 static void
4843ex_map(eap)
4844 exarg_T *eap;
4845{
4846 /*
4847 * If we are sourcing .exrc or .vimrc in current directory we
4848 * print the mappings for security reasons.
4849 */
4850 if (secure)
4851 {
4852 secure = 2;
4853 msg_outtrans(eap->cmd);
4854 msg_putchar('\n');
4855 }
4856 do_exmap(eap, FALSE);
4857}
4858
4859/*
4860 * ":unmap" and friends.
4861 */
4862 static void
4863ex_unmap(eap)
4864 exarg_T *eap;
4865{
4866 do_exmap(eap, FALSE);
4867}
4868
4869/*
4870 * ":mapclear" and friends.
4871 */
4872 static void
4873ex_mapclear(eap)
4874 exarg_T *eap;
4875{
4876 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4877}
4878
4879/*
4880 * ":abclear" and friends.
4881 */
4882 static void
4883ex_abclear(eap)
4884 exarg_T *eap;
4885{
4886 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4887}
4888
4889#ifdef FEAT_AUTOCMD
4890 static void
4891ex_autocmd(eap)
4892 exarg_T *eap;
4893{
4894 /*
4895 * Disallow auto commands from .exrc and .vimrc in current
4896 * directory for security reasons.
4897 */
4898 if (secure)
4899 {
4900 secure = 2;
4901 eap->errmsg = e_curdir;
4902 }
4903 else if (eap->cmdidx == CMD_autocmd)
4904 do_autocmd(eap->arg, eap->forceit);
4905 else
4906 do_augroup(eap->arg, eap->forceit);
4907}
4908
4909/*
4910 * ":doautocmd": Apply the automatic commands to the current buffer.
4911 */
4912 static void
4913ex_doautocmd(eap)
4914 exarg_T *eap;
4915{
4916 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004917 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918}
4919#endif
4920
4921#ifdef FEAT_LISTCMDS
4922/*
4923 * :[N]bunload[!] [N] [bufname] unload buffer
4924 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4925 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4926 */
4927 static void
4928ex_bunload(eap)
4929 exarg_T *eap;
4930{
4931 eap->errmsg = do_bufdel(
4932 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4933 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4934 : DOBUF_UNLOAD, eap->arg,
4935 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4936}
4937
4938/*
4939 * :[N]buffer [N] to buffer N
4940 * :[N]sbuffer [N] to buffer N
4941 */
4942 static void
4943ex_buffer(eap)
4944 exarg_T *eap;
4945{
4946 if (*eap->arg)
4947 eap->errmsg = e_trailing;
4948 else
4949 {
4950 if (eap->addr_count == 0) /* default is current buffer */
4951 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4952 else
4953 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4954 }
4955}
4956
4957/*
4958 * :[N]bmodified [N] to next mod. buffer
4959 * :[N]sbmodified [N] to next mod. buffer
4960 */
4961 static void
4962ex_bmodified(eap)
4963 exarg_T *eap;
4964{
4965 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4966}
4967
4968/*
4969 * :[N]bnext [N] to next buffer
4970 * :[N]sbnext [N] split and to next buffer
4971 */
4972 static void
4973ex_bnext(eap)
4974 exarg_T *eap;
4975{
4976 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4977}
4978
4979/*
4980 * :[N]bNext [N] to previous buffer
4981 * :[N]bprevious [N] to previous buffer
4982 * :[N]sbNext [N] split and to previous buffer
4983 * :[N]sbprevious [N] split and to previous buffer
4984 */
4985 static void
4986ex_bprevious(eap)
4987 exarg_T *eap;
4988{
4989 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4990}
4991
4992/*
4993 * :brewind to first buffer
4994 * :bfirst to first buffer
4995 * :sbrewind split and to first buffer
4996 * :sbfirst split and to first buffer
4997 */
4998 static void
4999ex_brewind(eap)
5000 exarg_T *eap;
5001{
5002 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5003}
5004
5005/*
5006 * :blast to last buffer
5007 * :sblast split and to last buffer
5008 */
5009 static void
5010ex_blast(eap)
5011 exarg_T *eap;
5012{
5013 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5014}
5015#endif
5016
5017 int
5018ends_excmd(c)
5019 int c;
5020{
5021 return (c == NUL || c == '|' || c == '"' || c == '\n');
5022}
5023
5024#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5025 || defined(PROTO)
5026/*
5027 * Return the next command, after the first '|' or '\n'.
5028 * Return NULL if not found.
5029 */
5030 char_u *
5031find_nextcmd(p)
5032 char_u *p;
5033{
5034 while (*p != '|' && *p != '\n')
5035 {
5036 if (*p == NUL)
5037 return NULL;
5038 ++p;
5039 }
5040 return (p + 1);
5041}
5042#endif
5043
5044/*
5045 * Check if *p is a separator between Ex commands.
5046 * Return NULL if it isn't, (p + 1) if it is.
5047 */
5048 char_u *
5049check_nextcmd(p)
5050 char_u *p;
5051{
5052 p = skipwhite(p);
5053 if (*p == '|' || *p == '\n')
5054 return (p + 1);
5055 else
5056 return NULL;
5057}
5058
5059/*
5060 * - if there are more files to edit
5061 * - and this is the last window
5062 * - and forceit not used
5063 * - and not repeated twice on a row
5064 * return FAIL and give error message if 'message' TRUE
5065 * return OK otherwise
5066 */
5067 static int
5068check_more(message, forceit)
5069 int message; /* when FALSE check only, no messages */
5070 int forceit;
5071{
5072 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5073
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005074 if (!forceit && only_one_window()
5075 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 {
5077 if (message)
5078 {
5079#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5080 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5081 {
5082 char_u buff[IOSIZE];
5083
5084 if (n == 1)
5085 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5086 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005087 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 _("%d more files to edit. Quit anyway?"), n);
5089 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5090 return OK;
5091 return FAIL;
5092 }
5093#endif
5094 if (n == 1)
5095 EMSG(_("E173: 1 more file to edit"));
5096 else
5097 EMSGN(_("E173: %ld more files to edit"), n);
5098 quitmore = 2; /* next try to quit is allowed */
5099 }
5100 return FAIL;
5101 }
5102 return OK;
5103}
5104
5105#ifdef FEAT_CMDL_COMPL
5106/*
5107 * Function given to ExpandGeneric() to obtain the list of command names.
5108 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 char_u *
5110get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005111 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 int idx;
5113{
5114 if (idx >= (int)CMD_SIZE)
5115# ifdef FEAT_USR_CMDS
5116 return get_user_command_name(idx);
5117# else
5118 return NULL;
5119# endif
5120 return cmdnames[idx].cmd_name;
5121}
5122#endif
5123
5124#if defined(FEAT_USR_CMDS) || defined(PROTO)
5125static 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));
5126static void uc_list __ARGS((char_u *name, size_t name_len));
5127static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5128static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5129static 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));
5130
5131 static int
5132uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5133 char_u *name;
5134 size_t name_len;
5135 char_u *rep;
5136 long argt;
5137 long def;
5138 int flags;
5139 int compl;
5140 char_u *compl_arg;
5141 int force;
5142{
5143 ucmd_T *cmd = NULL;
5144 char_u *p;
5145 int i;
5146 int cmp = 1;
5147 char_u *rep_buf = NULL;
5148 garray_T *gap;
5149
Bram Moolenaar9c102382006-05-03 21:26:49 +00005150 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (rep_buf == NULL)
5152 {
5153 /* Can't replace termcodes - try using the string as is */
5154 rep_buf = vim_strsave(rep);
5155
5156 /* Give up if out of memory */
5157 if (rep_buf == NULL)
5158 return FAIL;
5159 }
5160
5161 /* get address of growarray: global or in curbuf */
5162 if (flags & UC_BUFFER)
5163 {
5164 gap = &curbuf->b_ucmds;
5165 if (gap->ga_itemsize == 0)
5166 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5167 }
5168 else
5169 gap = &ucmds;
5170
5171 /* Search for the command in the already defined commands. */
5172 for (i = 0; i < gap->ga_len; ++i)
5173 {
5174 size_t len;
5175
5176 cmd = USER_CMD_GA(gap, i);
5177 len = STRLEN(cmd->uc_name);
5178 cmp = STRNCMP(name, cmd->uc_name, name_len);
5179 if (cmp == 0)
5180 {
5181 if (name_len < len)
5182 cmp = -1;
5183 else if (name_len > len)
5184 cmp = 1;
5185 }
5186
5187 if (cmp == 0)
5188 {
5189 if (!force)
5190 {
5191 EMSG(_("E174: Command already exists: add ! to replace it"));
5192 goto fail;
5193 }
5194
5195 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005196 cmd->uc_rep = NULL;
5197#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5198 vim_free(cmd->uc_compl_arg);
5199 cmd->uc_compl_arg = NULL;
5200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 break;
5202 }
5203
5204 /* Stop as soon as we pass the name to add */
5205 if (cmp < 0)
5206 break;
5207 }
5208
5209 /* Extend the array unless we're replacing an existing command */
5210 if (cmp != 0)
5211 {
5212 if (ga_grow(gap, 1) != OK)
5213 goto fail;
5214 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5215 goto fail;
5216
5217 cmd = USER_CMD_GA(gap, i);
5218 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5219
5220 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222 cmd->uc_name = p;
5223 }
5224
5225 cmd->uc_rep = rep_buf;
5226 cmd->uc_argt = argt;
5227 cmd->uc_def = def;
5228 cmd->uc_compl = compl;
5229#ifdef FEAT_EVAL
5230 cmd->uc_scriptID = current_SID;
5231# ifdef FEAT_CMDL_COMPL
5232 cmd->uc_compl_arg = compl_arg;
5233# endif
5234#endif
5235
5236 return OK;
5237
5238fail:
5239 vim_free(rep_buf);
5240#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5241 vim_free(compl_arg);
5242#endif
5243 return FAIL;
5244}
5245
5246/*
5247 * List of names for completion for ":command" with the EXPAND_ flag.
5248 * Must be alphabetical for completion.
5249 */
5250static struct
5251{
5252 int expand;
5253 char *name;
5254} command_complete[] =
5255{
5256 {EXPAND_AUGROUP, "augroup"},
5257 {EXPAND_BUFFERS, "buffer"},
5258 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005259#if defined(FEAT_CSCOPE)
5260 {EXPAND_CSCOPE, "cscope"},
5261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5263 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005264 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265#endif
5266 {EXPAND_DIRECTORIES, "dir"},
5267 {EXPAND_ENV_VARS, "environment"},
5268 {EXPAND_EVENTS, "event"},
5269 {EXPAND_EXPRESSION, "expression"},
5270 {EXPAND_FILES, "file"},
5271 {EXPAND_FUNCTIONS, "function"},
5272 {EXPAND_HELP, "help"},
5273 {EXPAND_HIGHLIGHT, "highlight"},
5274 {EXPAND_MAPPINGS, "mapping"},
5275 {EXPAND_MENUS, "menu"},
5276 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005277 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005278#if defined(FEAT_SIGNS)
5279 {EXPAND_SIGN, "sign"},
5280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 {EXPAND_TAGS, "tag"},
5282 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5283 {EXPAND_USER_VARS, "var"},
5284 {0, NULL}
5285};
5286
5287 static void
5288uc_list(name, name_len)
5289 char_u *name;
5290 size_t name_len;
5291{
5292 int i, j;
5293 int found = FALSE;
5294 ucmd_T *cmd;
5295 int len;
5296 long a;
5297 garray_T *gap;
5298
5299 gap = &curbuf->b_ucmds;
5300 for (;;)
5301 {
5302 for (i = 0; i < gap->ga_len; ++i)
5303 {
5304 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005305 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
5307 /* Skip commands which don't match the requested prefix */
5308 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5309 continue;
5310
5311 /* Put out the title first time */
5312 if (!found)
5313 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5314 found = TRUE;
5315 msg_putchar('\n');
5316 if (got_int)
5317 break;
5318
5319 /* Special cases */
5320 msg_putchar(a & BANG ? '!' : ' ');
5321 msg_putchar(a & REGSTR ? '"' : ' ');
5322 msg_putchar(gap != &ucmds ? 'b' : ' ');
5323 msg_putchar(' ');
5324
5325 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5326 len = (int)STRLEN(cmd->uc_name) + 4;
5327
5328 do {
5329 msg_putchar(' ');
5330 ++len;
5331 } while (len < 16);
5332
5333 len = 0;
5334
5335 /* Arguments */
5336 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5337 {
5338 case 0: IObuff[len++] = '0'; break;
5339 case (EXTRA): IObuff[len++] = '*'; break;
5340 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5341 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5342 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5343 }
5344
5345 do {
5346 IObuff[len++] = ' ';
5347 } while (len < 5);
5348
5349 /* Range */
5350 if (a & (RANGE|COUNT))
5351 {
5352 if (a & COUNT)
5353 {
5354 /* -count=N */
5355 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5356 len += (int)STRLEN(IObuff + len);
5357 }
5358 else if (a & DFLALL)
5359 IObuff[len++] = '%';
5360 else if (cmd->uc_def >= 0)
5361 {
5362 /* -range=N */
5363 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5364 len += (int)STRLEN(IObuff + len);
5365 }
5366 else
5367 IObuff[len++] = '.';
5368 }
5369
5370 do {
5371 IObuff[len++] = ' ';
5372 } while (len < 11);
5373
5374 /* Completion */
5375 for (j = 0; command_complete[j].expand != 0; ++j)
5376 if (command_complete[j].expand == cmd->uc_compl)
5377 {
5378 STRCPY(IObuff + len, command_complete[j].name);
5379 len += (int)STRLEN(IObuff + len);
5380 break;
5381 }
5382
5383 do {
5384 IObuff[len++] = ' ';
5385 } while (len < 21);
5386
5387 IObuff[len] = '\0';
5388 msg_outtrans(IObuff);
5389
5390 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005391#ifdef FEAT_EVAL
5392 if (p_verbose > 0)
5393 last_set_msg(cmd->uc_scriptID);
5394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 out_flush();
5396 ui_breakcheck();
5397 if (got_int)
5398 break;
5399 }
5400 if (gap == &ucmds || i < gap->ga_len)
5401 break;
5402 gap = &ucmds;
5403 }
5404
5405 if (!found)
5406 MSG(_("No user-defined commands found"));
5407}
5408
5409 static char_u *
5410uc_fun_cmd()
5411{
5412 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5413 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5414 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5415 0xb9, 0x7f, 0};
5416 int i;
5417
5418 for (i = 0; fcmd[i]; ++i)
5419 IObuff[i] = fcmd[i] - 0x40;
5420 IObuff[i] = 0;
5421 return IObuff;
5422}
5423
5424 static int
5425uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5426 char_u *attr;
5427 size_t len;
5428 long *argt;
5429 long *def;
5430 int *flags;
5431 int *compl;
5432 char_u **compl_arg;
5433{
5434 char_u *p;
5435
5436 if (len == 0)
5437 {
5438 EMSG(_("E175: No attribute specified"));
5439 return FAIL;
5440 }
5441
5442 /* First, try the simple attributes (no arguments) */
5443 if (STRNICMP(attr, "bang", len) == 0)
5444 *argt |= BANG;
5445 else if (STRNICMP(attr, "buffer", len) == 0)
5446 *flags |= UC_BUFFER;
5447 else if (STRNICMP(attr, "register", len) == 0)
5448 *argt |= REGSTR;
5449 else if (STRNICMP(attr, "bar", len) == 0)
5450 *argt |= TRLBAR;
5451 else
5452 {
5453 int i;
5454 char_u *val = NULL;
5455 size_t vallen = 0;
5456 size_t attrlen = len;
5457
5458 /* Look for the attribute name - which is the part before any '=' */
5459 for (i = 0; i < (int)len; ++i)
5460 {
5461 if (attr[i] == '=')
5462 {
5463 val = &attr[i + 1];
5464 vallen = len - i - 1;
5465 attrlen = i;
5466 break;
5467 }
5468 }
5469
5470 if (STRNICMP(attr, "nargs", attrlen) == 0)
5471 {
5472 if (vallen == 1)
5473 {
5474 if (*val == '0')
5475 /* Do nothing - this is the default */;
5476 else if (*val == '1')
5477 *argt |= (EXTRA | NOSPC | NEEDARG);
5478 else if (*val == '*')
5479 *argt |= EXTRA;
5480 else if (*val == '?')
5481 *argt |= (EXTRA | NOSPC);
5482 else if (*val == '+')
5483 *argt |= (EXTRA | NEEDARG);
5484 else
5485 goto wrong_nargs;
5486 }
5487 else
5488 {
5489wrong_nargs:
5490 EMSG(_("E176: Invalid number of arguments"));
5491 return FAIL;
5492 }
5493 }
5494 else if (STRNICMP(attr, "range", attrlen) == 0)
5495 {
5496 *argt |= RANGE;
5497 if (vallen == 1 && *val == '%')
5498 *argt |= DFLALL;
5499 else if (val != NULL)
5500 {
5501 p = val;
5502 if (*def >= 0)
5503 {
5504two_count:
5505 EMSG(_("E177: Count cannot be specified twice"));
5506 return FAIL;
5507 }
5508
5509 *def = getdigits(&p);
5510 *argt |= (ZEROR | NOTADR);
5511
5512 if (p != val + vallen || vallen == 0)
5513 {
5514invalid_count:
5515 EMSG(_("E178: Invalid default value for count"));
5516 return FAIL;
5517 }
5518 }
5519 }
5520 else if (STRNICMP(attr, "count", attrlen) == 0)
5521 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005522 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524 if (val != NULL)
5525 {
5526 p = val;
5527 if (*def >= 0)
5528 goto two_count;
5529
5530 *def = getdigits(&p);
5531
5532 if (p != val + vallen)
5533 goto invalid_count;
5534 }
5535
5536 if (*def < 0)
5537 *def = 0;
5538 }
5539 else if (STRNICMP(attr, "complete", attrlen) == 0)
5540 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 if (val == NULL)
5542 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005543 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 return FAIL;
5545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005547 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5548 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 }
5551 else
5552 {
5553 char_u ch = attr[len];
5554 attr[len] = '\0';
5555 EMSG2(_("E181: Invalid attribute: %s"), attr);
5556 attr[len] = ch;
5557 return FAIL;
5558 }
5559 }
5560
5561 return OK;
5562}
5563
Bram Moolenaara850a712009-01-28 14:42:59 +00005564/*
5565 * ":command ..."
5566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 static void
5568ex_command(eap)
5569 exarg_T *eap;
5570{
5571 char_u *name;
5572 char_u *end;
5573 char_u *p;
5574 long argt = 0;
5575 long def = -1;
5576 int flags = 0;
5577 int compl = EXPAND_NOTHING;
5578 char_u *compl_arg = NULL;
5579 int has_attr = (eap->arg[0] == '-');
5580
5581 p = eap->arg;
5582
5583 /* Check for attributes */
5584 while (*p == '-')
5585 {
5586 ++p;
5587 end = skiptowhite(p);
5588 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5589 == FAIL)
5590 return;
5591 p = skipwhite(end);
5592 }
5593
5594 /* Get the name (if any) and skip to the following argument */
5595 name = p;
5596 if (ASCII_ISALPHA(*p))
5597 while (ASCII_ISALNUM(*p))
5598 ++p;
5599 if (!ends_excmd(*p) && !vim_iswhite(*p))
5600 {
5601 EMSG(_("E182: Invalid command name"));
5602 return;
5603 }
5604 end = p;
5605
5606 /* If there is nothing after the name, and no attributes were specified,
5607 * we are listing commands
5608 */
5609 p = skipwhite(end);
5610 if (!has_attr && ends_excmd(*p))
5611 {
5612 uc_list(name, end - name);
5613 }
5614 else if (!ASCII_ISUPPER(*name))
5615 {
5616 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5617 return;
5618 }
5619 else
5620 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5621 eap->forceit);
5622}
5623
5624/*
5625 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 * Clear all user commands, global and for current buffer.
5627 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005628 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005630 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 uc_clear(&ucmds);
5633 uc_clear(&curbuf->b_ucmds);
5634}
5635
5636/*
5637 * Clear all user commands for "gap".
5638 */
5639 void
5640uc_clear(gap)
5641 garray_T *gap;
5642{
5643 int i;
5644 ucmd_T *cmd;
5645
5646 for (i = 0; i < gap->ga_len; ++i)
5647 {
5648 cmd = USER_CMD_GA(gap, i);
5649 vim_free(cmd->uc_name);
5650 vim_free(cmd->uc_rep);
5651# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5652 vim_free(cmd->uc_compl_arg);
5653# endif
5654 }
5655 ga_clear(gap);
5656}
5657
5658 static void
5659ex_delcommand(eap)
5660 exarg_T *eap;
5661{
5662 int i = 0;
5663 ucmd_T *cmd = NULL;
5664 int cmp = -1;
5665 garray_T *gap;
5666
5667 gap = &curbuf->b_ucmds;
5668 for (;;)
5669 {
5670 for (i = 0; i < gap->ga_len; ++i)
5671 {
5672 cmd = USER_CMD_GA(gap, i);
5673 cmp = STRCMP(eap->arg, cmd->uc_name);
5674 if (cmp <= 0)
5675 break;
5676 }
5677 if (gap == &ucmds || cmp == 0)
5678 break;
5679 gap = &ucmds;
5680 }
5681
5682 if (cmp != 0)
5683 {
5684 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5685 return;
5686 }
5687
5688 vim_free(cmd->uc_name);
5689 vim_free(cmd->uc_rep);
5690# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5691 vim_free(cmd->uc_compl_arg);
5692# endif
5693
5694 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695
5696 if (i < gap->ga_len)
5697 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5698}
5699
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005700/*
5701 * split and quote args for <f-args>
5702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 static char_u *
5704uc_split_args(arg, lenp)
5705 char_u *arg;
5706 size_t *lenp;
5707{
5708 char_u *buf;
5709 char_u *p;
5710 char_u *q;
5711 int len;
5712
5713 /* Precalculate length */
5714 p = arg;
5715 len = 2; /* Initial and final quotes */
5716
5717 while (*p)
5718 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005719 if (p[0] == '\\' && p[1] == '\\')
5720 {
5721 len += 2;
5722 p += 2;
5723 }
5724 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 len += 1;
5727 p += 2;
5728 }
5729 else if (*p == '\\' || *p == '"')
5730 {
5731 len += 2;
5732 p += 1;
5733 }
5734 else if (vim_iswhite(*p))
5735 {
5736 p = skipwhite(p);
5737 if (*p == NUL)
5738 break;
5739 len += 3; /* "," */
5740 }
5741 else
5742 {
5743 ++len;
5744 ++p;
5745 }
5746 }
5747
5748 buf = alloc(len + 1);
5749 if (buf == NULL)
5750 {
5751 *lenp = 0;
5752 return buf;
5753 }
5754
5755 p = arg;
5756 q = buf;
5757 *q++ = '"';
5758 while (*p)
5759 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005760 if (p[0] == '\\' && p[1] == '\\')
5761 {
5762 *q++ = '\\';
5763 *q++ = '\\';
5764 p += 2;
5765 }
5766 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
5768 *q++ = p[1];
5769 p += 2;
5770 }
5771 else if (*p == '\\' || *p == '"')
5772 {
5773 *q++ = '\\';
5774 *q++ = *p++;
5775 }
5776 else if (vim_iswhite(*p))
5777 {
5778 p = skipwhite(p);
5779 if (*p == NUL)
5780 break;
5781 *q++ = '"';
5782 *q++ = ',';
5783 *q++ = '"';
5784 }
5785 else
5786 {
5787 *q++ = *p++;
5788 }
5789 }
5790 *q++ = '"';
5791 *q = 0;
5792
5793 *lenp = len;
5794 return buf;
5795}
5796
5797/*
5798 * Check for a <> code in a user command.
5799 * "code" points to the '<'. "len" the length of the <> (inclusive).
5800 * "buf" is where the result is to be added.
5801 * "split_buf" points to a buffer used for splitting, caller should free it.
5802 * "split_len" is the length of what "split_buf" contains.
5803 * Returns the length of the replacement, which has been added to "buf".
5804 * Returns -1 if there was no match, and only the "<" has been copied.
5805 */
5806 static size_t
5807uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5808 char_u *code;
5809 size_t len;
5810 char_u *buf;
5811 ucmd_T *cmd; /* the user command we're expanding */
5812 exarg_T *eap; /* ex arguments */
5813 char_u **split_buf;
5814 size_t *split_len;
5815{
5816 size_t result = 0;
5817 char_u *p = code + 1;
5818 size_t l = len - 2;
5819 int quote = 0;
5820 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5821 ct_LT, ct_NONE } type = ct_NONE;
5822
5823 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5824 {
5825 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5826 p += 2;
5827 l -= 2;
5828 }
5829
Bram Moolenaar371d5402006-03-20 21:47:49 +00005830 ++l;
5831 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005833 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005835 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005837 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005839 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005841 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005843 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005845 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 type = ct_REGISTER;
5847
5848 switch (type)
5849 {
5850 case ct_ARGS:
5851 /* Simple case first */
5852 if (*eap->arg == NUL)
5853 {
5854 if (quote == 1)
5855 {
5856 result = 2;
5857 if (buf != NULL)
5858 STRCPY(buf, "''");
5859 }
5860 else
5861 result = 0;
5862 break;
5863 }
5864
5865 /* When specified there is a single argument don't split it.
5866 * Works for ":Cmd %" when % is "a b c". */
5867 if ((eap->argt & NOSPC) && quote == 2)
5868 quote = 1;
5869
5870 switch (quote)
5871 {
5872 case 0: /* No quoting, no splitting */
5873 result = STRLEN(eap->arg);
5874 if (buf != NULL)
5875 STRCPY(buf, eap->arg);
5876 break;
5877 case 1: /* Quote, but don't split */
5878 result = STRLEN(eap->arg) + 2;
5879 for (p = eap->arg; *p; ++p)
5880 {
5881 if (*p == '\\' || *p == '"')
5882 ++result;
5883 }
5884
5885 if (buf != NULL)
5886 {
5887 *buf++ = '"';
5888 for (p = eap->arg; *p; ++p)
5889 {
5890 if (*p == '\\' || *p == '"')
5891 *buf++ = '\\';
5892 *buf++ = *p;
5893 }
5894 *buf = '"';
5895 }
5896
5897 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005898 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 /* This is hard, so only do it once, and cache the result */
5900 if (*split_buf == NULL)
5901 *split_buf = uc_split_args(eap->arg, split_len);
5902
5903 result = *split_len;
5904 if (buf != NULL && result != 0)
5905 STRCPY(buf, *split_buf);
5906
5907 break;
5908 }
5909 break;
5910
5911 case ct_BANG:
5912 result = eap->forceit ? 1 : 0;
5913 if (quote)
5914 result += 2;
5915 if (buf != NULL)
5916 {
5917 if (quote)
5918 *buf++ = '"';
5919 if (eap->forceit)
5920 *buf++ = '!';
5921 if (quote)
5922 *buf = '"';
5923 }
5924 break;
5925
5926 case ct_LINE1:
5927 case ct_LINE2:
5928 case ct_COUNT:
5929 {
5930 char num_buf[20];
5931 long num = (type == ct_LINE1) ? eap->line1 :
5932 (type == ct_LINE2) ? eap->line2 :
5933 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5934 size_t num_len;
5935
5936 sprintf(num_buf, "%ld", num);
5937 num_len = STRLEN(num_buf);
5938 result = num_len;
5939
5940 if (quote)
5941 result += 2;
5942
5943 if (buf != NULL)
5944 {
5945 if (quote)
5946 *buf++ = '"';
5947 STRCPY(buf, num_buf);
5948 buf += num_len;
5949 if (quote)
5950 *buf = '"';
5951 }
5952
5953 break;
5954 }
5955
5956 case ct_REGISTER:
5957 result = eap->regname ? 1 : 0;
5958 if (quote)
5959 result += 2;
5960 if (buf != NULL)
5961 {
5962 if (quote)
5963 *buf++ = '\'';
5964 if (eap->regname)
5965 *buf++ = eap->regname;
5966 if (quote)
5967 *buf = '\'';
5968 }
5969 break;
5970
5971 case ct_LT:
5972 result = 1;
5973 if (buf != NULL)
5974 *buf = '<';
5975 break;
5976
5977 default:
5978 /* Not recognized: just copy the '<' and return -1. */
5979 result = (size_t)-1;
5980 if (buf != NULL)
5981 *buf = '<';
5982 break;
5983 }
5984
5985 return result;
5986}
5987
5988 static void
5989do_ucmd(eap)
5990 exarg_T *eap;
5991{
5992 char_u *buf;
5993 char_u *p;
5994 char_u *q;
5995
5996 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00005997 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00005998 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 size_t len, totlen;
6000
6001 size_t split_len = 0;
6002 char_u *split_buf = NULL;
6003 ucmd_T *cmd;
6004#ifdef FEAT_EVAL
6005 scid_T save_current_SID = current_SID;
6006#endif
6007
6008 if (eap->cmdidx == CMD_USER)
6009 cmd = USER_CMD(eap->useridx);
6010 else
6011 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6012
6013 /*
6014 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006015 * First round: "buf" is NULL, compute length, allocate "buf".
6016 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 */
6018 buf = NULL;
6019 for (;;)
6020 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006021 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006022 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006024
6025 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006027 start = vim_strchr(p, '<');
6028 if (start != NULL)
6029 end = vim_strchr(start + 1, '>');
6030 if (buf != NULL)
6031 {
6032 ksp = vim_strchr(p, K_SPECIAL);
6033 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6034 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6035# ifdef FEAT_GUI
6036 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6037# endif
6038 ))
6039 {
6040 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6041 * KS_SPECIAL KE_FILLER, like for mappings, but
6042 * do_cmdline() doesn't handle that, so convert it back.
6043 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6044 len = ksp - p;
6045 if (len > 0)
6046 {
6047 mch_memmove(q, p, len);
6048 q += len;
6049 }
6050 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6051 p = ksp + 3;
6052 continue;
6053 }
6054 }
6055
6056 /* break if there no <item> is found */
6057 if (start == NULL || end == NULL)
6058 break;
6059
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 /* Include the '>' */
6061 ++end;
6062
6063 /* Take everything up to the '<' */
6064 len = start - p;
6065 if (buf == NULL)
6066 totlen += len;
6067 else
6068 {
6069 mch_memmove(q, p, len);
6070 q += len;
6071 }
6072
6073 len = uc_check_code(start, end - start, q, cmd, eap,
6074 &split_buf, &split_len);
6075 if (len == (size_t)-1)
6076 {
6077 /* no match, continue after '<' */
6078 p = start + 1;
6079 len = 1;
6080 }
6081 else
6082 p = end;
6083 if (buf == NULL)
6084 totlen += len;
6085 else
6086 q += len;
6087 }
6088 if (buf != NULL) /* second time here, finished */
6089 {
6090 STRCPY(q, p);
6091 break;
6092 }
6093
6094 totlen += STRLEN(p); /* Add on the trailing characters */
6095 buf = alloc((unsigned)(totlen + 1));
6096 if (buf == NULL)
6097 {
6098 vim_free(split_buf);
6099 return;
6100 }
6101 }
6102
6103#ifdef FEAT_EVAL
6104 current_SID = cmd->uc_scriptID;
6105#endif
6106 (void)do_cmdline(buf, eap->getline, eap->cookie,
6107 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6108#ifdef FEAT_EVAL
6109 current_SID = save_current_SID;
6110#endif
6111 vim_free(buf);
6112 vim_free(split_buf);
6113}
6114
6115# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6116 static char_u *
6117get_user_command_name(idx)
6118 int idx;
6119{
6120 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6121}
6122
6123/*
6124 * Function given to ExpandGeneric() to obtain the list of user command names.
6125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 char_u *
6127get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006128 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 int idx;
6130{
6131 if (idx < curbuf->b_ucmds.ga_len)
6132 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6133 idx -= curbuf->b_ucmds.ga_len;
6134 if (idx < ucmds.ga_len)
6135 return USER_CMD(idx)->uc_name;
6136 return NULL;
6137}
6138
6139/*
6140 * Function given to ExpandGeneric() to obtain the list of user command
6141 * attributes.
6142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 char_u *
6144get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006145 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 int idx;
6147{
6148 static char *user_cmd_flags[] =
6149 {"bang", "bar", "buffer", "complete", "count",
6150 "nargs", "range", "register"};
6151
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006152 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 return NULL;
6154 return (char_u *)user_cmd_flags[idx];
6155}
6156
6157/*
6158 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 char_u *
6161get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006162 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 int idx;
6164{
6165 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6166
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006167 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 return NULL;
6169 return (char_u *)user_cmd_nargs[idx];
6170}
6171
6172/*
6173 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 char_u *
6176get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006177 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 int idx;
6179{
6180 return (char_u *)command_complete[idx].name;
6181}
6182# endif /* FEAT_CMDL_COMPL */
6183
6184#endif /* FEAT_USR_CMDS */
6185
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006186#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6187/*
6188 * Parse a completion argument "value[vallen]".
6189 * The detected completion goes in "*complp", argument type in "*argt".
6190 * When there is an argument, for function and user defined completion, it's
6191 * copied to allocated memory and stored in "*compl_arg".
6192 * Returns FAIL if something is wrong.
6193 */
6194 int
6195parse_compl_arg(value, vallen, complp, argt, compl_arg)
6196 char_u *value;
6197 int vallen;
6198 int *complp;
6199 long *argt;
6200 char_u **compl_arg;
6201{
6202 char_u *arg = NULL;
6203 size_t arglen = 0;
6204 int i;
6205 int valend = vallen;
6206
6207 /* Look for any argument part - which is the part after any ',' */
6208 for (i = 0; i < vallen; ++i)
6209 {
6210 if (value[i] == ',')
6211 {
6212 arg = &value[i + 1];
6213 arglen = vallen - i - 1;
6214 valend = i;
6215 break;
6216 }
6217 }
6218
6219 for (i = 0; command_complete[i].expand != 0; ++i)
6220 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006221 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006222 && STRNCMP(value, command_complete[i].name, valend) == 0)
6223 {
6224 *complp = command_complete[i].expand;
6225 if (command_complete[i].expand == EXPAND_BUFFERS)
6226 *argt |= BUFNAME;
6227 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6228 || command_complete[i].expand == EXPAND_FILES)
6229 *argt |= XFILE;
6230 break;
6231 }
6232 }
6233
6234 if (command_complete[i].expand == 0)
6235 {
6236 EMSG2(_("E180: Invalid complete value: %s"), value);
6237 return FAIL;
6238 }
6239
6240# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6241 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6242 && arg != NULL)
6243# else
6244 if (arg != NULL)
6245# endif
6246 {
6247 EMSG(_("E468: Completion argument only allowed for custom completion"));
6248 return FAIL;
6249 }
6250
6251# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6252 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6253 && arg == NULL)
6254 {
6255 EMSG(_("E467: Custom completion requires a function argument"));
6256 return FAIL;
6257 }
6258
6259 if (arg != NULL)
6260 *compl_arg = vim_strnsave(arg, (int)arglen);
6261# endif
6262 return OK;
6263}
6264#endif
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 static void
6267ex_colorscheme(eap)
6268 exarg_T *eap;
6269{
Bram Moolenaare6850792010-05-14 15:28:44 +02006270 if (*eap->arg == NUL)
6271 {
6272#ifdef FEAT_EVAL
6273 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6274 char_u *p = NULL;
6275
6276 if (expr != NULL)
6277 {
6278 ++emsg_off;
6279 p = eval_to_string(expr, NULL, FALSE);
6280 --emsg_off;
6281 vim_free(expr);
6282 }
6283 if (p != NULL)
6284 {
6285 MSG(p);
6286 vim_free(p);
6287 }
6288 else
6289 MSG("default");
6290#else
6291 MSG(_("unknown"));
6292#endif
6293 }
6294 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6296}
6297
6298 static void
6299ex_highlight(eap)
6300 exarg_T *eap;
6301{
6302 if (*eap->arg == NUL && eap->cmd[2] == '!')
6303 MSG(_("Greetings, Vim user!"));
6304 do_highlight(eap->arg, eap->forceit, FALSE);
6305}
6306
6307
6308/*
6309 * Call this function if we thought we were going to exit, but we won't
6310 * (because of an error). May need to restore the terminal mode.
6311 */
6312 void
6313not_exiting()
6314{
6315 exiting = FALSE;
6316 settmode(TMODE_RAW);
6317}
6318
6319/*
6320 * ":quit": quit current window, quit Vim if closed the last window.
6321 */
6322 static void
6323ex_quit(eap)
6324 exarg_T *eap;
6325{
6326#ifdef FEAT_CMDWIN
6327 if (cmdwin_type != 0)
6328 {
6329 cmdwin_result = Ctrl_C;
6330 return;
6331 }
6332#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006333 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006334 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006335 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006336 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006337 return;
6338 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006339#ifdef FEAT_AUTOCMD
6340 if (curbuf_locked())
6341 return;
6342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343
6344#ifdef FEAT_NETBEANS_INTG
6345 netbeansForcedQuit = eap->forceit;
6346#endif
6347
6348 /*
6349 * If there are more files or windows we won't exit.
6350 */
6351 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6352 exiting = TRUE;
6353 if ((!P_HID(curbuf)
6354 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6355 || check_more(TRUE, eap->forceit) == FAIL
6356 || (only_one_window() && check_changed_any(eap->forceit)))
6357 {
6358 not_exiting();
6359 }
6360 else
6361 {
6362#ifdef FEAT_WINDOWS
6363 if (only_one_window()) /* quit last window */
6364#endif
6365 getout(0);
6366#ifdef FEAT_WINDOWS
6367# ifdef FEAT_GUI
6368 need_mouse_correct = TRUE;
6369# endif
6370 /* close window; may free buffer */
6371 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6372#endif
6373 }
6374}
6375
6376/*
6377 * ":cquit".
6378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 static void
6380ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006381 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 getout(1); /* this does not always pass on the exit code to the Manx
6384 compiler. why? */
6385}
6386
6387/*
6388 * ":qall": try to quit all windows
6389 */
6390 static void
6391ex_quit_all(eap)
6392 exarg_T *eap;
6393{
6394# ifdef FEAT_CMDWIN
6395 if (cmdwin_type != 0)
6396 {
6397 if (eap->forceit)
6398 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6399 else
6400 cmdwin_result = K_XF2;
6401 return;
6402 }
6403# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006404
6405 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006406 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006407 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006408 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006409 return;
6410 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006411#ifdef FEAT_AUTOCMD
6412 if (curbuf_locked())
6413 return;
6414#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 exiting = TRUE;
6417 if (eap->forceit || !check_changed_any(FALSE))
6418 getout(0);
6419 not_exiting();
6420}
6421
Bram Moolenaard9967712006-03-11 21:18:15 +00006422#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423/*
6424 * ":close": close current window, unless it is the last one
6425 */
6426 static void
6427ex_close(eap)
6428 exarg_T *eap;
6429{
6430# ifdef FEAT_CMDWIN
6431 if (cmdwin_type != 0)
6432 cmdwin_result = K_IGNORE;
6433 else
6434# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006435 if (!text_locked()
6436#ifdef FEAT_AUTOCMD
6437 && !curbuf_locked()
6438#endif
6439 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006440 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441}
6442
Bram Moolenaard9967712006-03-11 21:18:15 +00006443# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006444/*
6445 * ":pclose": Close any preview window.
6446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006448ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006450{
6451 win_T *win;
6452
6453 for (win = firstwin; win != NULL; win = win->w_next)
6454 if (win->w_p_pvw)
6455 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006456 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006457 break;
6458 }
6459}
Bram Moolenaard9967712006-03-11 21:18:15 +00006460# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006461
Bram Moolenaarf740b292006-02-16 22:11:02 +00006462/*
6463 * Close window "win" and take care of handling closing the last window for a
6464 * modified buffer.
6465 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006467ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006468 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006470 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
6472 int need_hide;
6473 buf_T *buf = win->w_buffer;
6474
6475 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006476 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006478# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if ((p_confirm || cmdmod.confirm) && p_write)
6480 {
6481 dialog_changed(buf, FALSE);
6482 if (buf_valid(buf) && bufIsChanged(buf))
6483 return;
6484 need_hide = FALSE;
6485 }
6486 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006487# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
6489 EMSG(_(e_nowrtmsg));
6490 return;
6491 }
6492 }
6493
Bram Moolenaard9967712006-03-11 21:18:15 +00006494# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006496# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006497
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006499 if (tp == NULL)
6500 win_close(win, !need_hide && !P_HID(buf));
6501 else
6502 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503}
6504
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006506 * ":tabclose": close current tab page, unless it is the last one.
6507 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 */
6509 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006510ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 exarg_T *eap;
6512{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006513 tabpage_T *tp;
6514
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515# ifdef FEAT_CMDWIN
6516 if (cmdwin_type != 0)
6517 cmdwin_result = K_IGNORE;
6518 else
6519# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006520 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006521 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006524 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006525 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006526 tp = find_tabpage((int)eap->line2);
6527 if (tp == NULL)
6528 {
6529 beep_flush();
6530 return;
6531 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006532 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006533 {
6534 tabpage_close_other(tp, eap->forceit);
6535 return;
6536 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006537 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006538 if (!text_locked()
6539#ifdef FEAT_AUTOCMD
6540 && !curbuf_locked()
6541#endif
6542 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006543 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006545}
6546
6547/*
6548 * ":tabonly": close all tab pages except the current one
6549 */
6550 static void
6551ex_tabonly(eap)
6552 exarg_T *eap;
6553{
6554 tabpage_T *tp;
6555 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006556
6557# ifdef FEAT_CMDWIN
6558 if (cmdwin_type != 0)
6559 cmdwin_result = K_IGNORE;
6560 else
6561# endif
6562 if (first_tabpage->tp_next == NULL)
6563 MSG(_("Already only one tab page"));
6564 else
6565 {
6566 /* Repeat this up to a 1000 times, because autocommands may mess
6567 * up the lists. */
6568 for (done = 0; done < 1000; ++done)
6569 {
6570 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6571 if (tp->tp_topframe != topframe)
6572 {
6573 tabpage_close_other(tp, eap->forceit);
6574 /* if we failed to close it quit */
6575 if (valid_tabpage(tp))
6576 done = 1000;
6577 /* start over, "tp" is now invalid */
6578 break;
6579 }
6580 if (first_tabpage->tp_next == NULL)
6581 break;
6582 }
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006587 * Close the current tab page.
6588 */
6589 void
6590tabpage_close(forceit)
6591 int forceit;
6592{
6593 /* First close all the windows but the current one. If that worked then
6594 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006595 if (lastwin != firstwin)
6596 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006597 if (lastwin == firstwin)
6598 ex_win_close(forceit, curwin, NULL);
6599# ifdef FEAT_GUI
6600 need_mouse_correct = TRUE;
6601# endif
6602}
6603
6604/*
6605 * Close tab page "tp", which is not the current tab page.
6606 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006607 * Also takes care of the tab pages line disappearing when closing the
6608 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006609 */
6610 void
6611tabpage_close_other(tp, forceit)
6612 tabpage_T *tp;
6613 int forceit;
6614{
6615 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006616 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006617 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006618
6619 /* Limit to 1000 windows, autocommands may add a window while we close
6620 * one. OK, so I'm paranoid... */
6621 while (++done < 1000)
6622 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006623 wp = tp->tp_firstwin;
6624 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006625
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006626 /* Autocommands may delete the tab page under our fingers and we may
6627 * fail to close a window with a modified buffer. */
6628 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006629 break;
6630 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006631
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006632 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006633 if (h != tabline_height())
6634 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006635}
6636
6637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 * ":only".
6639 */
6640 static void
6641ex_only(eap)
6642 exarg_T *eap;
6643{
6644# ifdef FEAT_GUI
6645 need_mouse_correct = TRUE;
6646# endif
6647 close_others(TRUE, eap->forceit);
6648}
6649
6650/*
6651 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006652 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006654 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655ex_all(eap)
6656 exarg_T *eap;
6657{
6658 if (eap->addr_count == 0)
6659 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006660 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661}
6662#endif /* FEAT_WINDOWS */
6663
6664 static void
6665ex_hide(eap)
6666 exarg_T *eap;
6667{
6668 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6669 eap->errmsg = e_invarg;
6670 else
6671 {
6672 /* ":hide" or ":hide | cmd": hide current window */
6673 eap->nextcmd = check_nextcmd(eap->arg);
6674#ifdef FEAT_WINDOWS
6675 if (!eap->skip)
6676 {
6677# ifdef FEAT_GUI
6678 need_mouse_correct = TRUE;
6679# endif
6680 win_close(curwin, FALSE); /* don't free buffer */
6681 }
6682#endif
6683 }
6684}
6685
6686/*
6687 * ":stop" and ":suspend": Suspend Vim.
6688 */
6689 static void
6690ex_stop(eap)
6691 exarg_T *eap;
6692{
6693 /*
6694 * Disallow suspending for "rvim".
6695 */
6696 if (!check_restricted()
6697#ifdef WIN3264
6698 /*
6699 * Check if external commands are allowed now.
6700 */
6701 && can_end_termcap_mode(TRUE)
6702#endif
6703 )
6704 {
6705 if (!eap->forceit)
6706 autowrite_all();
6707 windgoto((int)Rows - 1, 0);
6708 out_char('\n');
6709 out_flush();
6710 stoptermcap();
6711 out_flush(); /* needed for SUN to restore xterm buffer */
6712#ifdef FEAT_TITLE
6713 mch_restore_title(3); /* restore window titles */
6714#endif
6715 ui_suspend(); /* call machine specific function */
6716#ifdef FEAT_TITLE
6717 maketitle();
6718 resettitle(); /* force updating the title */
6719#endif
6720 starttermcap();
6721 scroll_start(); /* scroll screen before redrawing */
6722 redraw_later_clear();
6723 shell_resized(); /* may have resized window */
6724 }
6725}
6726
6727/*
6728 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6729 */
6730 static void
6731ex_exit(eap)
6732 exarg_T *eap;
6733{
6734#ifdef FEAT_CMDWIN
6735 if (cmdwin_type != 0)
6736 {
6737 cmdwin_result = Ctrl_C;
6738 return;
6739 }
6740#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006741 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006742 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006743 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006744 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006745 return;
6746 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006747#ifdef FEAT_AUTOCMD
6748 if (curbuf_locked())
6749 return;
6750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
6752 /*
6753 * if more files or windows we won't exit
6754 */
6755 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6756 exiting = TRUE;
6757 if ( ((eap->cmdidx == CMD_wq
6758 || curbufIsChanged())
6759 && do_write(eap) == FAIL)
6760 || check_more(TRUE, eap->forceit) == FAIL
6761 || (only_one_window() && check_changed_any(eap->forceit)))
6762 {
6763 not_exiting();
6764 }
6765 else
6766 {
6767#ifdef FEAT_WINDOWS
6768 if (only_one_window()) /* quit last window, exit Vim */
6769#endif
6770 getout(0);
6771#ifdef FEAT_WINDOWS
6772# ifdef FEAT_GUI
6773 need_mouse_correct = TRUE;
6774# endif
6775 /* quit current window, may free buffer */
6776 win_close(curwin, !P_HID(curwin->w_buffer));
6777#endif
6778 }
6779}
6780
6781/*
6782 * ":print", ":list", ":number".
6783 */
6784 static void
6785ex_print(eap)
6786 exarg_T *eap;
6787{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006788 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6789 EMSG(_(e_emptybuf));
6790 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006792 for ( ;!got_int; ui_breakcheck())
6793 {
6794 print_line(eap->line1,
6795 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6796 || (eap->flags & EXFLAG_NR)),
6797 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6798 if (++eap->line1 > eap->line2)
6799 break;
6800 out_flush(); /* show one line at a time */
6801 }
6802 setpcmark();
6803 /* put cursor at last line */
6804 curwin->w_cursor.lnum = eap->line2;
6805 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 }
6807
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809}
6810
6811#ifdef FEAT_BYTEOFF
6812 static void
6813ex_goto(eap)
6814 exarg_T *eap;
6815{
6816 goto_byte(eap->line2);
6817}
6818#endif
6819
6820/*
6821 * ":shell".
6822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 static void
6824ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006825 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826{
6827 do_shell(NULL, 0);
6828}
6829
6830#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6831 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006832 || defined(FEAT_GUI_MSWIN) \
6833 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 || defined(PROTO)
6835
6836/*
6837 * Handle a file drop. The code is here because a drop is *nearly* like an
6838 * :args command, but not quite (we have a list of exact filenames, so we
6839 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6840 * code is very similar to :args and hence needs access to a lot of the static
6841 * functions in this file.
6842 *
6843 * The list should be allocated using alloc(), as should each item in the
6844 * list. This function takes over responsibility for freeing the list.
6845 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006846 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6848 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6849 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6850 * file functionality is (currently) not in EMX this is not presently a
6851 * problem.
6852 */
6853 void
6854handle_drop(filec, filev, split)
6855 int filec; /* the number of files dropped */
6856 char_u **filev; /* the list of files dropped */
6857 int split; /* force splitting the window */
6858{
6859 exarg_T ea;
6860 int save_msg_scroll = msg_scroll;
6861
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006862 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006863 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006865#ifdef FEAT_AUTOCMD
6866 if (curbuf_locked())
6867 return;
6868#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006869 /* When the screen is being updated we should not change buffers and
6870 * windows structures, it may cause freed memory to be used. */
6871 if (updating_screen)
6872 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873
6874 /* Check whether the current buffer is changed. If so, we will need
6875 * to split the current window or data could be lost.
6876 * We don't need to check if the 'hidden' option is set, as in this
6877 * case the buffer won't be lost.
6878 */
6879 if (!P_HID(curbuf) && !split)
6880 {
6881 ++emsg_off;
6882 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6883 --emsg_off;
6884 }
6885 if (split)
6886 {
6887# ifdef FEAT_WINDOWS
6888 if (win_split(0, 0) == FAIL)
6889 return;
6890# ifdef FEAT_SCROLLBIND
6891 curwin->w_p_scb = FALSE;
6892# endif
6893
6894 /* When splitting the window, create a new alist. Otherwise the
6895 * existing one is overwritten. */
6896 alist_unlink(curwin->w_alist);
6897 alist_new();
6898# else
6899 return; /* can't split, always fail */
6900# endif
6901 }
6902
6903 /*
6904 * Set up the new argument list.
6905 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006906 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907
6908 /*
6909 * Move to the first file.
6910 */
6911 /* Fake up a minimal "next" command for do_argfile() */
6912 vim_memset(&ea, 0, sizeof(ea));
6913 ea.cmd = (char_u *)"next";
6914 do_argfile(&ea, 0);
6915
6916 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6917 * mode that is not needed here. */
6918 need_start_insertmode = FALSE;
6919
6920 /* Restore msg_scroll, otherwise a following command may cause scrolling
6921 * unexpectedly. The screen will be redrawn by the caller, thus
6922 * msg_scroll being set by displaying a message is irrelevant. */
6923 msg_scroll = save_msg_scroll;
6924}
6925#endif
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927/*
6928 * Clear an argument list: free all file names and reset it to zero entries.
6929 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006930 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931alist_clear(al)
6932 alist_T *al;
6933{
6934 while (--al->al_ga.ga_len >= 0)
6935 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6936 ga_clear(&al->al_ga);
6937}
6938
6939/*
6940 * Init an argument list.
6941 */
6942 void
6943alist_init(al)
6944 alist_T *al;
6945{
6946 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6947}
6948
6949#if defined(FEAT_WINDOWS) || defined(PROTO)
6950
6951/*
6952 * Remove a reference from an argument list.
6953 * Ignored when the argument list is the global one.
6954 * If the argument list is no longer used by any window, free it.
6955 */
6956 void
6957alist_unlink(al)
6958 alist_T *al;
6959{
6960 if (al != &global_alist && --al->al_refcount <= 0)
6961 {
6962 alist_clear(al);
6963 vim_free(al);
6964 }
6965}
6966
6967# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6968/*
6969 * Create a new argument list and use it for the current window.
6970 */
6971 void
6972alist_new()
6973{
6974 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6975 if (curwin->w_alist == NULL)
6976 {
6977 curwin->w_alist = &global_alist;
6978 ++global_alist.al_refcount;
6979 }
6980 else
6981 {
6982 curwin->w_alist->al_refcount = 1;
6983 alist_init(curwin->w_alist);
6984 }
6985}
6986# endif
6987#endif
6988
6989#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6990/*
6991 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006992 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6993 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 */
6995 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00006996alist_expand(fnum_list, fnum_len)
6997 int *fnum_list;
6998 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999{
7000 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007001 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 char_u **new_arg_files;
7003 int new_arg_file_count;
7004 char_u *save_p_su = p_su;
7005 int i;
7006
7007 /* Don't use 'suffixes' here. This should work like the shell did the
7008 * expansion. Also, the vimrc file isn't read yet, thus the user
7009 * can't set the options. */
7010 p_su = empty_option;
7011 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7012 if (old_arg_files != NULL)
7013 {
7014 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007015 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7016 old_arg_count = GARGCOUNT;
7017 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 &new_arg_file_count, &new_arg_files,
7019 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7020 && new_arg_file_count > 0)
7021 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007022 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7023 TRUE, fnum_list, fnum_len);
7024 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
7027 p_su = save_p_su;
7028}
7029#endif
7030
7031/*
7032 * Set the argument list for the current window.
7033 * Takes over the allocated files[] and the allocated fnames in it.
7034 */
7035 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007036alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 alist_T *al;
7038 int count;
7039 char_u **files;
7040 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007041 int *fnum_list;
7042 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
7044 int i;
7045
7046 alist_clear(al);
7047 if (ga_grow(&al->al_ga, count) == OK)
7048 {
7049 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007050 {
7051 if (got_int)
7052 {
7053 /* When adding many buffers this can take a long time. Allow
7054 * interrupting here. */
7055 while (i < count)
7056 vim_free(files[i++]);
7057 break;
7058 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007059
7060 /* May set buffer name of a buffer previously used for the
7061 * argument list, so that it's re-used by alist_add. */
7062 if (fnum_list != NULL && i < fnum_len)
7063 buf_set_name(fnum_list[i], files[i]);
7064
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007066 ui_breakcheck();
7067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 vim_free(files);
7069 }
7070 else
7071 FreeWild(count, files);
7072#ifdef FEAT_WINDOWS
7073 if (al == &global_alist)
7074#endif
7075 arg_had_last = FALSE;
7076}
7077
7078/*
7079 * Add file "fname" to argument list "al".
7080 * "fname" must have been allocated and "al" must have been checked for room.
7081 */
7082 void
7083alist_add(al, fname, set_fnum)
7084 alist_T *al;
7085 char_u *fname;
7086 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7087{
7088 if (fname == NULL) /* don't add NULL file names */
7089 return;
7090#ifdef BACKSLASH_IN_FILENAME
7091 slash_adjust(fname);
7092#endif
7093 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7094 if (set_fnum > 0)
7095 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7096 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7097 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098}
7099
7100#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7101/*
7102 * Adjust slashes in file names. Called after 'shellslash' was set.
7103 */
7104 void
7105alist_slash_adjust()
7106{
7107 int i;
7108# ifdef FEAT_WINDOWS
7109 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007110 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111# endif
7112
7113 for (i = 0; i < GARGCOUNT; ++i)
7114 if (GARGLIST[i].ae_fname != NULL)
7115 slash_adjust(GARGLIST[i].ae_fname);
7116# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007117 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 if (wp->w_alist != &global_alist)
7119 for (i = 0; i < WARGCOUNT(wp); ++i)
7120 if (WARGLIST(wp)[i].ae_fname != NULL)
7121 slash_adjust(WARGLIST(wp)[i].ae_fname);
7122# endif
7123}
7124#endif
7125
7126/*
7127 * ":preserve".
7128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 static void
7130ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007131 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007133 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 ml_preserve(curbuf, TRUE);
7135}
7136
7137/*
7138 * ":recover".
7139 */
7140 static void
7141ex_recover(eap)
7142 exarg_T *eap;
7143{
7144 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7145 recoverymode = TRUE;
7146 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7147 && (*eap->arg == NUL
7148 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7149 ml_recover();
7150 recoverymode = FALSE;
7151}
7152
7153/*
7154 * Command modifier used in a wrong way.
7155 */
7156 static void
7157ex_wrongmodifier(eap)
7158 exarg_T *eap;
7159{
7160 eap->errmsg = e_invcmd;
7161}
7162
7163#ifdef FEAT_WINDOWS
7164/*
7165 * :sview [+command] file split window with new file, read-only
7166 * :split [[+command] file] split window with current or new file
7167 * :vsplit [[+command] file] split window vertically with current or new file
7168 * :new [[+command] file] split window with no or new file
7169 * :vnew [[+command] file] split vertically window with no or new file
7170 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007171 *
7172 * :tabedit open new Tab page with empty window
7173 * :tabedit [+command] file open new Tab page and edit "file"
7174 * :tabnew [[+command] file] just like :tabedit
7175 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 */
7177 void
7178ex_splitview(eap)
7179 exarg_T *eap;
7180{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007181 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007182# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007184# endif
7185# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007189# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7191 {
7192 ex_ni(eap);
7193 return;
7194 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007197# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007201# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007203 * quickfix windows. But it's OK when doing ":tab split". */
7204 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
7206 if (eap->cmdidx == CMD_split)
7207 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007208# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (eap->cmdidx == CMD_vsplit)
7210 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007211# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007213# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007215# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007216 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 {
7218 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7219 FNAME_MESS, TRUE, curbuf->b_ffname);
7220 if (fname == NULL)
7221 goto theend;
7222 eap->arg = fname;
7223 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007224# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007226# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007228# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007230# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007232# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 && eap->cmdidx != CMD_new)
7234 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007235# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007236 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007237# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007238 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007239# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007240 au_has_group((char_u *)"FileExplorer"))
7241 {
7242 /* No browsing supported but we do have the file explorer:
7243 * Edit the directory. */
7244 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7245 eap->arg = (char_u *)".";
7246 }
7247 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007248# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007249 {
7250 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007252 if (fname == NULL)
7253 goto theend;
7254 eap->arg = fname;
7255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 }
7257 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007260 /*
7261 * Either open new tab page or split the window.
7262 */
7263 if (eap->cmdidx == CMD_tabedit
7264 || eap->cmdidx == CMD_tabfind
7265 || eap->cmdidx == CMD_tabnew)
7266 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007267 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7268 : eap->addr_count == 0 ? 0
7269 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007270 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007271 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007272
7273 /* set the alternate buffer for the window we came from */
7274 if (curwin != old_curwin
7275 && win_valid(old_curwin)
7276 && old_curwin->w_buffer != curbuf
7277 && !cmdmod.keepalt)
7278 old_curwin->w_alt_fnum = curbuf->b_fnum;
7279 }
7280 }
7281 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7283 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007284# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 /* Reset 'scrollbind' when editing another file, but keep it when
7286 * doing ":split" without arguments. */
7287 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007288# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 )
7292 curwin->w_p_scb = FALSE;
7293 else
7294 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 do_exedit(eap, old_curwin);
7297 }
7298
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007299# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007303# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304theend:
7305 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007306# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007308
7309/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007310 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311 */
7312 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007313tabpage_new()
7314{
7315 exarg_T ea;
7316
7317 vim_memset(&ea, 0, sizeof(ea));
7318 ea.cmdidx = CMD_tabnew;
7319 ea.cmd = (char_u *)"tabn";
7320 ea.arg = (char_u *)"";
7321 ex_splitview(&ea);
7322}
7323
7324/*
7325 * :tabnext command
7326 */
7327 static void
7328ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007329 exarg_T *eap;
7330{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007331 switch (eap->cmdidx)
7332 {
7333 case CMD_tabfirst:
7334 case CMD_tabrewind:
7335 goto_tabpage(1);
7336 break;
7337 case CMD_tablast:
7338 goto_tabpage(9999);
7339 break;
7340 case CMD_tabprevious:
7341 case CMD_tabNext:
7342 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7343 break;
7344 default: /* CMD_tabnext */
7345 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7346 break;
7347 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007348}
7349
7350/*
7351 * :tabmove command
7352 */
7353 static void
7354ex_tabmove(eap)
7355 exarg_T *eap;
7356{
7357 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007358}
7359
7360/*
7361 * :tabs command: List tabs and their contents.
7362 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363 static void
7364ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007365 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007366{
7367 tabpage_T *tp;
7368 win_T *wp;
7369 int tabcount = 1;
7370
7371 msg_start();
7372 msg_scroll = TRUE;
7373 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7374 {
7375 msg_putchar('\n');
7376 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7377 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7378 out_flush(); /* output one line at a time */
7379 ui_breakcheck();
7380
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007381 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007382 wp = firstwin;
7383 else
7384 wp = tp->tp_firstwin;
7385 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7386 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007387 msg_putchar('\n');
7388 msg_putchar(wp == curwin ? '>' : ' ');
7389 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007390 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7391 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007392 if (buf_spname(wp->w_buffer) != NULL)
7393 STRCPY(IObuff, buf_spname(wp->w_buffer));
7394 else
7395 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7396 IObuff, IOSIZE, TRUE);
7397 msg_outtrans(IObuff);
7398 out_flush(); /* output one line at a time */
7399 ui_breakcheck();
7400 }
7401 }
7402}
7403
7404#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405
7406/*
7407 * ":mode": Set screen mode.
7408 * If no argument given, just get the screen size and redraw.
7409 */
7410 static void
7411ex_mode(eap)
7412 exarg_T *eap;
7413{
7414 if (*eap->arg == NUL)
7415 shell_resized();
7416 else
7417 mch_screenmode(eap->arg);
7418}
7419
7420#ifdef FEAT_WINDOWS
7421/*
7422 * ":resize".
7423 * set, increment or decrement current window height
7424 */
7425 static void
7426ex_resize(eap)
7427 exarg_T *eap;
7428{
7429 int n;
7430 win_T *wp = curwin;
7431
7432 if (eap->addr_count > 0)
7433 {
7434 n = eap->line2;
7435 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7436 ;
7437 }
7438
7439#ifdef FEAT_GUI
7440 need_mouse_correct = TRUE;
7441#endif
7442 n = atol((char *)eap->arg);
7443#ifdef FEAT_VERTSPLIT
7444 if (cmdmod.split & WSP_VERT)
7445 {
7446 if (*eap->arg == '-' || *eap->arg == '+')
7447 n += W_WIDTH(curwin);
7448 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7449 n = 9999;
7450 win_setwidth_win((int)n, wp);
7451 }
7452 else
7453#endif
7454 {
7455 if (*eap->arg == '-' || *eap->arg == '+')
7456 n += curwin->w_height;
7457 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7458 n = 9999;
7459 win_setheight_win((int)n, wp);
7460 }
7461}
7462#endif
7463
7464/*
7465 * ":find [+command] <file>" command.
7466 */
7467 static void
7468ex_find(eap)
7469 exarg_T *eap;
7470{
7471#ifdef FEAT_SEARCHPATH
7472 char_u *fname;
7473 int count;
7474
7475 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7476 TRUE, curbuf->b_ffname);
7477 if (eap->addr_count > 0)
7478 {
7479 /* Repeat finding the file "count" times. This matters when it
7480 * appears several times in the path. */
7481 count = eap->line2;
7482 while (fname != NULL && --count > 0)
7483 {
7484 vim_free(fname);
7485 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7486 FALSE, curbuf->b_ffname);
7487 }
7488 }
7489
7490 if (fname != NULL)
7491 {
7492 eap->arg = fname;
7493#endif
7494 do_exedit(eap, NULL);
7495#ifdef FEAT_SEARCHPATH
7496 vim_free(fname);
7497 }
7498#endif
7499}
7500
7501/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 * ":open" simulation: for now just work like ":visual".
7503 */
7504 static void
7505ex_open(eap)
7506 exarg_T *eap;
7507{
7508 regmatch_T regmatch;
7509 char_u *p;
7510
7511 curwin->w_cursor.lnum = eap->line2;
7512 beginline(BL_SOL | BL_FIX);
7513 if (*eap->arg == '/')
7514 {
7515 /* ":open /pattern/": put cursor in column found with pattern */
7516 ++eap->arg;
7517 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7518 *p = NUL;
7519 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7520 if (regmatch.regprog != NULL)
7521 {
7522 regmatch.rm_ic = p_ic;
7523 p = ml_get_curline();
7524 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007525 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007526 else
7527 EMSG(_(e_nomatch));
7528 vim_free(regmatch.regprog);
7529 }
7530 /* Move to the NUL, ignore any other arguments. */
7531 eap->arg += STRLEN(eap->arg);
7532 }
7533 check_cursor();
7534
7535 eap->cmdidx = CMD_visual;
7536 do_exedit(eap, NULL);
7537}
7538
7539/*
7540 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 */
7542 static void
7543ex_edit(eap)
7544 exarg_T *eap;
7545{
7546 do_exedit(eap, NULL);
7547}
7548
7549/*
7550 * ":edit <file>" command and alikes.
7551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 void
7553do_exedit(eap, old_curwin)
7554 exarg_T *eap;
7555 win_T *old_curwin; /* curwin before doing a split or NULL */
7556{
7557 int n;
7558#ifdef FEAT_WINDOWS
7559 int need_hide;
7560#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007561 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
7563 /*
7564 * ":vi" command ends Ex mode.
7565 */
7566 if (exmode_active && (eap->cmdidx == CMD_visual
7567 || eap->cmdidx == CMD_view))
7568 {
7569 exmode_active = FALSE;
7570 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007571 {
7572 /* Special case: ":global/pat/visual\NLvi-commands" */
7573 if (global_busy)
7574 {
7575 int rd = RedrawingDisabled;
7576 int nwr = no_wait_return;
7577 int ms = msg_scroll;
7578#ifdef FEAT_GUI
7579 int he = hold_gui_events;
7580#endif
7581
7582 if (eap->nextcmd != NULL)
7583 {
7584 stuffReadbuff(eap->nextcmd);
7585 eap->nextcmd = NULL;
7586 }
7587
7588 if (exmode_was != EXMODE_VIM)
7589 settmode(TMODE_RAW);
7590 RedrawingDisabled = 0;
7591 no_wait_return = 0;
7592 need_wait_return = FALSE;
7593 msg_scroll = 0;
7594#ifdef FEAT_GUI
7595 hold_gui_events = 0;
7596#endif
7597 must_redraw = CLEAR;
7598
7599 main_loop(FALSE, TRUE);
7600
7601 RedrawingDisabled = rd;
7602 no_wait_return = nwr;
7603 msg_scroll = ms;
7604#ifdef FEAT_GUI
7605 hold_gui_events = he;
7606#endif
7607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 }
7611
7612 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007613 || eap->cmdidx == CMD_tabnew
7614 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615#ifdef FEAT_VERTSPLIT
7616 || eap->cmdidx == CMD_vnew
7617#endif
7618 ) && *eap->arg == NUL)
7619 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007620 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 setpcmark();
7622 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007623 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7624 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 }
7626 else if ((eap->cmdidx != CMD_split
7627#ifdef FEAT_VERTSPLIT
7628 && eap->cmdidx != CMD_vsplit
7629#endif
7630 )
7631 || *eap->arg != NUL
7632#ifdef FEAT_BROWSE
7633 || cmdmod.browse
7634#endif
7635 )
7636 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007637#ifdef FEAT_AUTOCMD
7638 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7639 * can bring us here, others are stopped earlier. */
7640 if (*eap->arg != NUL && curbuf_locked())
7641 return;
7642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 n = readonlymode;
7644 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7645 readonlymode = TRUE;
7646 else if (eap->cmdidx == CMD_enew)
7647 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7648 empty buffer */
7649 setpcmark();
7650 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7651 NULL, eap,
7652 /* ":edit" goes to first line if Vi compatible */
7653 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7654 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7655 ? ECMD_ONE : eap->do_ecmd_lnum,
7656 (P_HID(curbuf) ? ECMD_HIDE : 0)
7657 + (eap->forceit ? ECMD_FORCEIT : 0)
7658#ifdef FEAT_LISTCMDS
7659 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7660#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007661 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {
7663 /* Editing the file failed. If the window was split, close it. */
7664#ifdef FEAT_WINDOWS
7665 if (old_curwin != NULL)
7666 {
7667 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7668 if (!need_hide || P_HID(curbuf))
7669 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007670# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7671 cleanup_T cs;
7672
7673 /* Reset the error/interrupt/exception state here so that
7674 * aborting() returns FALSE when closing a window. */
7675 enter_cleanup(&cs);
7676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677# ifdef FEAT_GUI
7678 need_mouse_correct = TRUE;
7679# endif
7680 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007681
7682# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7683 /* Restore the error/interrupt/exception state if not
7684 * discarded by a new aborting error, interrupt, or
7685 * uncaught exception. */
7686 leave_cleanup(&cs);
7687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689 }
7690#endif
7691 }
7692 else if (readonlymode && curbuf->b_nwindows == 1)
7693 {
7694 /* When editing an already visited buffer, 'readonly' won't be set
7695 * but the previous value is kept. With ":view" and ":sview" we
7696 * want the file to be readonly, except when another window is
7697 * editing the same buffer. */
7698 curbuf->b_p_ro = TRUE;
7699 }
7700 readonlymode = n;
7701 }
7702 else
7703 {
7704 if (eap->do_ecmd_cmd != NULL)
7705 do_cmdline_cmd(eap->do_ecmd_cmd);
7706#ifdef FEAT_TITLE
7707 n = curwin->w_arg_idx_invalid;
7708#endif
7709 check_arg_idx(curwin);
7710#ifdef FEAT_TITLE
7711 if (n != curwin->w_arg_idx_invalid)
7712 maketitle();
7713#endif
7714 }
7715
7716#ifdef FEAT_WINDOWS
7717 /*
7718 * if ":split file" worked, set alternate file name in old window to new
7719 * file
7720 */
7721 if (old_curwin != NULL
7722 && *eap->arg != NUL
7723 && curwin != old_curwin
7724 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007725 && old_curwin->w_buffer != curbuf
7726 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 old_curwin->w_alt_fnum = curbuf->b_fnum;
7728#endif
7729
7730 ex_no_reprint = TRUE;
7731}
7732
7733#ifndef FEAT_GUI
7734/*
7735 * ":gui" and ":gvim" when there is no GUI.
7736 */
7737 static void
7738ex_nogui(eap)
7739 exarg_T *eap;
7740{
7741 eap->errmsg = e_nogvim;
7742}
7743#endif
7744
7745#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7746 static void
7747ex_tearoff(eap)
7748 exarg_T *eap;
7749{
7750 gui_make_tearoff(eap->arg);
7751}
7752#endif
7753
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007754#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 static void
7756ex_popup(eap)
7757 exarg_T *eap;
7758{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007759 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760}
7761#endif
7762
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 static void
7764ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007765 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766{
7767 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7768 MSG(_("No swap file"));
7769 else
7770 msg(curbuf->b_ml.ml_mfp->mf_fname);
7771}
7772
7773/*
7774 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7775 * offset.
7776 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 static void
7779ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007780 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781{
7782#ifdef FEAT_SCROLLBIND
7783 win_T *wp;
7784 long topline;
7785 long y;
7786 linenr_T old_linenr = curwin->w_cursor.lnum;
7787
7788 setpcmark();
7789
7790 /*
7791 * determine max topline
7792 */
7793 if (curwin->w_p_scb)
7794 {
7795 topline = curwin->w_topline;
7796 for (wp = firstwin; wp; wp = wp->w_next)
7797 {
7798 if (wp->w_p_scb && wp->w_buffer)
7799 {
7800 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7801 if (topline > y)
7802 topline = y;
7803 }
7804 }
7805 if (topline < 1)
7806 topline = 1;
7807 }
7808 else
7809 {
7810 topline = 1;
7811 }
7812
7813
7814 /*
7815 * set all scrollbind windows to the same topline
7816 */
7817 wp = curwin;
7818 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7819 {
7820 if (curwin->w_p_scb)
7821 {
7822 y = topline - curwin->w_topline;
7823 if (y > 0)
7824 scrollup(y, TRUE);
7825 else
7826 scrolldown(-y, TRUE);
7827 curwin->w_scbind_pos = topline;
7828 redraw_later(VALID);
7829 cursor_correct();
7830#ifdef FEAT_WINDOWS
7831 curwin->w_redr_status = TRUE;
7832#endif
7833 }
7834 }
7835 curwin = wp;
7836 if (curwin->w_p_scb)
7837 {
7838 did_syncbind = TRUE;
7839 checkpcmark();
7840 if (old_linenr != curwin->w_cursor.lnum)
7841 {
7842 char_u ctrl_o[2];
7843
7844 ctrl_o[0] = Ctrl_O;
7845 ctrl_o[1] = 0;
7846 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7847 }
7848 }
7849#endif
7850}
7851
7852
7853 static void
7854ex_read(eap)
7855 exarg_T *eap;
7856{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007857 int i;
7858 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7859 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860
7861 if (eap->usefilter) /* :r!cmd */
7862 do_bang(1, eap, FALSE, FALSE, TRUE);
7863 else
7864 {
7865 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7866 return;
7867
7868#ifdef FEAT_BROWSE
7869 if (cmdmod.browse)
7870 {
7871 char_u *browseFile;
7872
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007873 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 NULL, NULL, NULL, curbuf);
7875 if (browseFile != NULL)
7876 {
7877 i = readfile(browseFile, NULL,
7878 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7879 vim_free(browseFile);
7880 }
7881 else
7882 i = OK;
7883 }
7884 else
7885#endif
7886 if (*eap->arg == NUL)
7887 {
7888 if (check_fname() == FAIL) /* check for no file name */
7889 return;
7890 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7891 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7892 }
7893 else
7894 {
7895 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7896 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7897 i = readfile(eap->arg, NULL,
7898 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7899
7900 }
7901 if (i == FAIL)
7902 {
7903#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7904 if (!aborting())
7905#endif
7906 EMSG2(_(e_notopen), eap->arg);
7907 }
7908 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007909 {
7910 if (empty && exmode_active)
7911 {
7912 /* Delete the empty line that remains. Historically ex does
7913 * this but vi doesn't. */
7914 if (eap->line2 == 0)
7915 lnum = curbuf->b_ml.ml_line_count;
7916 else
7917 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007918 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007919 {
7920 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007921 if (curwin->w_cursor.lnum > 1
7922 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007923 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007924 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007925 }
7926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 }
7930}
7931
Bram Moolenaarea408852005-06-25 22:49:46 +00007932static char_u *prev_dir = NULL;
7933
7934#if defined(EXITFREE) || defined(PROTO)
7935 void
7936free_cd_dir()
7937{
7938 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007939 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007940
7941 vim_free(globaldir);
7942 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007943}
7944#endif
7945
7946
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947/*
7948 * ":cd", ":lcd", ":chdir" and ":lchdir".
7949 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007950 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951ex_cd(eap)
7952 exarg_T *eap;
7953{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 char_u *new_dir;
7955 char_u *tofree;
7956
7957 new_dir = eap->arg;
7958#if !defined(UNIX) && !defined(VMS)
7959 /* for non-UNIX ":cd" means: print current directory */
7960 if (*new_dir == NUL)
7961 ex_pwd(NULL);
7962 else
7963#endif
7964 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007965#ifdef FEAT_AUTOCMD
7966 if (allbuf_locked())
7967 return;
7968#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007969 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7970 && !eap->forceit)
7971 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007972 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007973 return;
7974 }
7975
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 /* ":cd -": Change to previous directory */
7977 if (STRCMP(new_dir, "-") == 0)
7978 {
7979 if (prev_dir == NULL)
7980 {
7981 EMSG(_("E186: No previous directory"));
7982 return;
7983 }
7984 new_dir = prev_dir;
7985 }
7986
7987 /* Save current directory for next ":cd -" */
7988 tofree = prev_dir;
7989 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7990 prev_dir = vim_strsave(NameBuff);
7991 else
7992 prev_dir = NULL;
7993
7994#if defined(UNIX) || defined(VMS)
7995 /* for UNIX ":cd" means: go to home directory */
7996 if (*new_dir == NUL)
7997 {
7998 /* use NameBuff for home directory name */
7999# ifdef VMS
8000 char_u *p;
8001
8002 p = mch_getenv((char_u *)"SYS$LOGIN");
8003 if (p == NULL || *p == NUL) /* empty is the same as not set */
8004 NameBuff[0] = NUL;
8005 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008006 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007# else
8008 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8009# endif
8010 new_dir = NameBuff;
8011 }
8012#endif
8013 if (new_dir == NULL || vim_chdir(new_dir))
8014 EMSG(_(e_failed));
8015 else
8016 {
8017 vim_free(curwin->w_localdir);
8018 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8019 {
8020 /* If still in global directory, need to remember current
8021 * directory as global directory. */
8022 if (globaldir == NULL && prev_dir != NULL)
8023 globaldir = vim_strsave(prev_dir);
8024 /* Remember this local directory for the window. */
8025 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8026 curwin->w_localdir = vim_strsave(NameBuff);
8027 }
8028 else
8029 {
8030 /* We are now in the global directory, no need to remember its
8031 * name. */
8032 vim_free(globaldir);
8033 globaldir = NULL;
8034 curwin->w_localdir = NULL;
8035 }
8036
8037 shorten_fnames(TRUE);
8038
8039 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008040 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 ex_pwd(eap);
8042 }
8043 vim_free(tofree);
8044 }
8045}
8046
8047/*
8048 * ":pwd".
8049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 static void
8051ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008052 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
8054 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8055 {
8056#ifdef BACKSLASH_IN_FILENAME
8057 slash_adjust(NameBuff);
8058#endif
8059 msg(NameBuff);
8060 }
8061 else
8062 EMSG(_("E187: Unknown"));
8063}
8064
8065/*
8066 * ":=".
8067 */
8068 static void
8069ex_equal(eap)
8070 exarg_T *eap;
8071{
8072 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008073 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074}
8075
8076 static void
8077ex_sleep(eap)
8078 exarg_T *eap;
8079{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008080 int n;
8081 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082
8083 if (cursor_valid())
8084 {
8085 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8086 if (n >= 0)
8087 windgoto((int)n, curwin->w_wcol);
8088 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008089
8090 len = eap->line2;
8091 switch (*eap->arg)
8092 {
8093 case 'm': break;
8094 case NUL: len *= 1000L; break;
8095 default: EMSG2(_(e_invarg2), eap->arg); return;
8096 }
8097 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098}
8099
8100/*
8101 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8102 */
8103 void
8104do_sleep(msec)
8105 long msec;
8106{
8107 long done;
8108
8109 cursor_on();
8110 out_flush();
8111 for (done = 0; !got_int && done < msec; done += 1000L)
8112 {
8113 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8114 ui_breakcheck();
8115 }
8116}
8117
8118 static void
8119do_exmap(eap, isabbrev)
8120 exarg_T *eap;
8121 int isabbrev;
8122{
8123 int mode;
8124 char_u *cmdp;
8125
8126 cmdp = eap->cmd;
8127 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8128
8129 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8130 eap->arg, mode, isabbrev))
8131 {
8132 case 1: EMSG(_(e_invarg));
8133 break;
8134 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8135 break;
8136 }
8137}
8138
8139/*
8140 * ":winsize" command (obsolete).
8141 */
8142 static void
8143ex_winsize(eap)
8144 exarg_T *eap;
8145{
8146 int w, h;
8147 char_u *arg = eap->arg;
8148 char_u *p;
8149
8150 w = getdigits(&arg);
8151 arg = skipwhite(arg);
8152 p = arg;
8153 h = getdigits(&arg);
8154 if (*p != NUL && *arg == NUL)
8155 set_shellsize(w, h, TRUE);
8156 else
8157 EMSG(_("E465: :winsize requires two number arguments"));
8158}
8159
8160#ifdef FEAT_WINDOWS
8161 static void
8162ex_wincmd(eap)
8163 exarg_T *eap;
8164{
8165 int xchar = NUL;
8166 char_u *p;
8167
8168 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8169 {
8170 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8171 if (eap->arg[1] == NUL)
8172 {
8173 EMSG(_(e_invarg));
8174 return;
8175 }
8176 xchar = eap->arg[1];
8177 p = eap->arg + 2;
8178 }
8179 else
8180 p = eap->arg + 1;
8181
8182 eap->nextcmd = check_nextcmd(p);
8183 p = skipwhite(p);
8184 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8185 EMSG(_(e_invarg));
8186 else
8187 {
8188 /* Pass flags on for ":vertical wincmd ]". */
8189 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008190 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8192 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008193 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 }
8195}
8196#endif
8197
Bram Moolenaar843ee412004-06-30 16:16:41 +00008198#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199/*
8200 * ":winpos".
8201 */
8202 static void
8203ex_winpos(eap)
8204 exarg_T *eap;
8205{
8206 int x, y;
8207 char_u *arg = eap->arg;
8208 char_u *p;
8209
8210 if (*arg == NUL)
8211 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008212# if defined(FEAT_GUI) || defined(MSWIN)
8213# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008215# else
8216 if (mch_get_winpos(&x, &y) != FAIL)
8217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
8219 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8220 msg(IObuff);
8221 }
8222 else
8223# endif
8224 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8225 }
8226 else
8227 {
8228 x = getdigits(&arg);
8229 arg = skipwhite(arg);
8230 p = arg;
8231 y = getdigits(&arg);
8232 if (*p == NUL || *arg != NUL)
8233 {
8234 EMSG(_("E466: :winpos requires two number arguments"));
8235 return;
8236 }
8237# ifdef FEAT_GUI
8238 if (gui.in_use)
8239 gui_mch_set_winpos(x, y);
8240 else if (gui.starting)
8241 {
8242 /* Remember the coordinates for when the window is opened. */
8243 gui_win_x = x;
8244 gui_win_y = y;
8245 }
8246# ifdef HAVE_TGETENT
8247 else
8248# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008249# else
8250# ifdef MSWIN
8251 mch_set_winpos(x, y);
8252# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253# endif
8254# ifdef HAVE_TGETENT
8255 if (*T_CWP)
8256 term_set_winpos(x, y);
8257# endif
8258 }
8259}
8260#endif
8261
8262/*
8263 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8264 */
8265 static void
8266ex_operators(eap)
8267 exarg_T *eap;
8268{
8269 oparg_T oa;
8270
8271 clear_oparg(&oa);
8272 oa.regname = eap->regname;
8273 oa.start.lnum = eap->line1;
8274 oa.end.lnum = eap->line2;
8275 oa.line_count = eap->line2 - eap->line1 + 1;
8276 oa.motion_type = MLINE;
8277#ifdef FEAT_VIRTUALEDIT
8278 virtual_op = FALSE;
8279#endif
8280 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8281 {
8282 setpcmark();
8283 curwin->w_cursor.lnum = eap->line1;
8284 beginline(BL_SOL | BL_FIX);
8285 }
8286
8287 switch (eap->cmdidx)
8288 {
8289 case CMD_delete:
8290 oa.op_type = OP_DELETE;
8291 op_delete(&oa);
8292 break;
8293
8294 case CMD_yank:
8295 oa.op_type = OP_YANK;
8296 (void)op_yank(&oa, FALSE, TRUE);
8297 break;
8298
8299 default: /* CMD_rshift or CMD_lshift */
8300 if ((eap->cmdidx == CMD_rshift)
8301#ifdef FEAT_RIGHTLEFT
8302 ^ curwin->w_p_rl
8303#endif
8304 )
8305 oa.op_type = OP_RSHIFT;
8306 else
8307 oa.op_type = OP_LSHIFT;
8308 op_shift(&oa, FALSE, eap->amount);
8309 break;
8310 }
8311#ifdef FEAT_VIRTUALEDIT
8312 virtual_op = MAYBE;
8313#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008314 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315}
8316
8317/*
8318 * ":put".
8319 */
8320 static void
8321ex_put(eap)
8322 exarg_T *eap;
8323{
8324 /* ":0put" works like ":1put!". */
8325 if (eap->line2 == 0)
8326 {
8327 eap->line2 = 1;
8328 eap->forceit = TRUE;
8329 }
8330 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008331 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8332 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333}
8334
8335/*
8336 * Handle ":copy" and ":move".
8337 */
8338 static void
8339ex_copymove(eap)
8340 exarg_T *eap;
8341{
8342 long n;
8343
8344 n = get_address(&eap->arg, FALSE, FALSE);
8345 if (eap->arg == NULL) /* error detected */
8346 {
8347 eap->nextcmd = NULL;
8348 return;
8349 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008350 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351
8352 /*
8353 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8354 */
8355 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8356 {
8357 EMSG(_(e_invaddr));
8358 return;
8359 }
8360
8361 if (eap->cmdidx == CMD_move)
8362 {
8363 if (do_move(eap->line1, eap->line2, n) == FAIL)
8364 return;
8365 }
8366 else
8367 ex_copy(eap->line1, eap->line2, n);
8368 u_clearline();
8369 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008370 ex_may_print(eap);
8371}
8372
8373/*
8374 * Print the current line if flags were given to the Ex command.
8375 */
8376 static void
8377ex_may_print(eap)
8378 exarg_T *eap;
8379{
8380 if (eap->flags != 0)
8381 {
8382 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8383 (eap->flags & EXFLAG_LIST));
8384 ex_no_reprint = TRUE;
8385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386}
8387
8388/*
8389 * ":smagic" and ":snomagic".
8390 */
8391 static void
8392ex_submagic(eap)
8393 exarg_T *eap;
8394{
8395 int magic_save = p_magic;
8396
8397 p_magic = (eap->cmdidx == CMD_smagic);
8398 do_sub(eap);
8399 p_magic = magic_save;
8400}
8401
8402/*
8403 * ":join".
8404 */
8405 static void
8406ex_join(eap)
8407 exarg_T *eap;
8408{
8409 curwin->w_cursor.lnum = eap->line1;
8410 if (eap->line1 == eap->line2)
8411 {
8412 if (eap->addr_count >= 2) /* :2,2join does nothing */
8413 return;
8414 if (eap->line2 == curbuf->b_ml.ml_line_count)
8415 {
8416 beep_flush();
8417 return;
8418 }
8419 ++eap->line2;
8420 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008421 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008423 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424}
8425
8426/*
8427 * ":[addr]@r" or ":[addr]*r": execute register
8428 */
8429 static void
8430ex_at(eap)
8431 exarg_T *eap;
8432{
8433 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008434 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435
8436 curwin->w_cursor.lnum = eap->line2;
8437
8438#ifdef USE_ON_FLY_SCROLL
8439 dont_scroll = TRUE; /* disallow scrolling here */
8440#endif
8441
8442 /* get the register name. No name means to use the previous one */
8443 c = *eap->arg;
8444 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8445 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008446 /* Put the register in the typeahead buffer with the "silent" flag. */
8447 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8448 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 else
8453 {
8454 int save_efr = exec_from_reg;
8455
8456 exec_from_reg = TRUE;
8457
8458 /*
8459 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008460 * Continue until the stuff buffer is empty and all added characters
8461 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008463 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8465
8466 exec_from_reg = save_efr;
8467 }
8468}
8469
8470/*
8471 * ":!".
8472 */
8473 static void
8474ex_bang(eap)
8475 exarg_T *eap;
8476{
8477 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8478}
8479
8480/*
8481 * ":undo".
8482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 static void
8484ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008485 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008487 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008488 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008489 else
8490 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491}
8492
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008493#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008494 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008495ex_wundo(eap)
8496 exarg_T *eap;
8497{
8498 char_u hash[UNDO_HASH_SIZE];
8499
8500 u_compute_hash(hash);
8501 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8502}
8503
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008504 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008505ex_rundo(eap)
8506 exarg_T *eap;
8507{
8508 char_u hash[UNDO_HASH_SIZE];
8509
8510 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008511 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008512}
8513#endif
8514
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515/*
8516 * ":redo".
8517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 static void
8519ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008520 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521{
8522 u_redo(1);
8523}
8524
8525/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008526 * ":earlier" and ":later".
8527 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008528 static void
8529ex_later(eap)
8530 exarg_T *eap;
8531{
8532 long count = 0;
8533 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008534 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008535 char_u *p = eap->arg;
8536
8537 if (*p == NUL)
8538 count = 1;
8539 else if (isdigit(*p))
8540 {
8541 count = getdigits(&p);
8542 switch (*p)
8543 {
8544 case 's': ++p; sec = TRUE; break;
8545 case 'm': ++p; sec = TRUE; count *= 60; break;
8546 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008547 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8548 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008549 }
8550 }
8551
8552 if (*p != NUL)
8553 EMSG2(_(e_invarg2), eap->arg);
8554 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008555 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8556 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008557}
8558
8559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 * ":redir": start/stop redirection.
8561 */
8562 static void
8563ex_redir(eap)
8564 exarg_T *eap;
8565{
8566 char *mode;
8567 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008568 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569
8570 if (STRICMP(eap->arg, "END") == 0)
8571 close_redir();
8572 else
8573 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008574 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008576 ++arg;
8577 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008579 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 mode = "a";
8581 }
8582 else
8583 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008584 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585
8586 close_redir();
8587
8588 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008589 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 if (fname == NULL)
8591 return;
8592#ifdef FEAT_BROWSE
8593 if (cmdmod.browse)
8594 {
8595 char_u *browseFile;
8596
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008597 browseFile = do_browse(BROWSE_SAVE,
8598 (char_u *)_("Save Redirection"),
8599 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 if (browseFile == NULL)
8601 return; /* operation cancelled */
8602 vim_free(fname);
8603 fname = browseFile;
8604 eap->forceit = TRUE; /* since dialog already asked */
8605 }
8606#endif
8607
8608 redir_fd = open_exfile(fname, eap->forceit, mode);
8609 vim_free(fname);
8610 }
8611#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008612 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {
8614 /* redirect to a register a-z (resp. A-Z for appending) */
8615 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008616 ++arg;
8617 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008619 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008620 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008622 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008624 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008625 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008626 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008627 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008629 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008630 if (*arg == '>')
8631 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008632 /* Make register empty when not using @A-@Z and the
8633 * command is valid. */
8634 if (*arg == NUL && !isupper(redir_reg))
8635 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008637 }
8638 if (*arg != NUL)
8639 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008640 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008641 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008642 }
8643 }
8644 else if (*arg == '=' && arg[1] == '>')
8645 {
8646 int append;
8647
8648 /* redirect to a variable */
8649 close_redir();
8650 arg += 2;
8651
8652 if (*arg == '>')
8653 {
8654 ++arg;
8655 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 }
8657 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008658 append = FALSE;
8659
8660 if (var_redir_start(skipwhite(arg), append) == OK)
8661 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 }
8663#endif
8664
8665 /* TODO: redirect to a buffer */
8666
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008668 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008670
8671 /* Make sure redirection is not off. Can happen for cmdline completion
8672 * that indirectly invokes a command to catch its output. */
8673 if (redir_fd != NULL
8674#ifdef FEAT_EVAL
8675 || redir_reg || redir_vname
8676#endif
8677 )
8678 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679}
8680
8681/*
8682 * ":redraw": force redraw
8683 */
8684 static void
8685ex_redraw(eap)
8686 exarg_T *eap;
8687{
8688 int r = RedrawingDisabled;
8689 int p = p_lz;
8690
8691 RedrawingDisabled = 0;
8692 p_lz = FALSE;
8693 update_topline();
8694 update_screen(eap->forceit ? CLEAR :
8695#ifdef FEAT_VISUAL
8696 VIsual_active ? INVERTED :
8697#endif
8698 0);
8699#ifdef FEAT_TITLE
8700 if (need_maketitle)
8701 maketitle();
8702#endif
8703 RedrawingDisabled = r;
8704 p_lz = p;
8705
8706 /* Reset msg_didout, so that a message that's there is overwritten. */
8707 msg_didout = FALSE;
8708 msg_col = 0;
8709
8710 out_flush();
8711}
8712
8713/*
8714 * ":redrawstatus": force redraw of status line(s)
8715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 static void
8717ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008718 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720#if defined(FEAT_WINDOWS)
8721 int r = RedrawingDisabled;
8722 int p = p_lz;
8723
8724 RedrawingDisabled = 0;
8725 p_lz = FALSE;
8726 if (eap->forceit)
8727 status_redraw_all();
8728 else
8729 status_redraw_curbuf();
8730 update_screen(
8731# ifdef FEAT_VISUAL
8732 VIsual_active ? INVERTED :
8733# endif
8734 0);
8735 RedrawingDisabled = r;
8736 p_lz = p;
8737 out_flush();
8738#endif
8739}
8740
8741 static void
8742close_redir()
8743{
8744 if (redir_fd != NULL)
8745 {
8746 fclose(redir_fd);
8747 redir_fd = NULL;
8748 }
8749#ifdef FEAT_EVAL
8750 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008751 if (redir_vname)
8752 {
8753 var_redir_stop();
8754 redir_vname = 0;
8755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756#endif
8757}
8758
8759#if defined(FEAT_SESSION) && defined(USE_CRNL)
8760# define MKSESSION_NL
8761static int mksession_nl = FALSE; /* use NL only in put_eol() */
8762#endif
8763
8764/*
8765 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8766 */
8767 static void
8768ex_mkrc(eap)
8769 exarg_T *eap;
8770{
8771 FILE *fd;
8772 int failed = FALSE;
8773 char_u *fname;
8774#ifdef FEAT_BROWSE
8775 char_u *browseFile = NULL;
8776#endif
8777#ifdef FEAT_SESSION
8778 int view_session = FALSE;
8779 int using_vdir = FALSE; /* using 'viewdir'? */
8780 char_u *viewFile = NULL;
8781 unsigned *flagp;
8782#endif
8783
8784 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8785 {
8786#ifdef FEAT_SESSION
8787 view_session = TRUE;
8788#else
8789 ex_ni(eap);
8790 return;
8791#endif
8792 }
8793
8794#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008795 /* Use the short file name until ":lcd" is used. We also don't use the
8796 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008797 did_lcd = FALSE;
8798
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8800 if (eap->cmdidx == CMD_mkview
8801 && (*eap->arg == NUL
8802 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8803 {
8804 eap->forceit = TRUE;
8805 fname = get_view_file(*eap->arg);
8806 if (fname == NULL)
8807 return;
8808 viewFile = fname;
8809 using_vdir = TRUE;
8810 }
8811 else
8812#endif
8813 if (*eap->arg != NUL)
8814 fname = eap->arg;
8815 else if (eap->cmdidx == CMD_mkvimrc)
8816 fname = (char_u *)VIMRC_FILE;
8817#ifdef FEAT_SESSION
8818 else if (eap->cmdidx == CMD_mksession)
8819 fname = (char_u *)SESSION_FILE;
8820#endif
8821 else
8822 fname = (char_u *)EXRC_FILE;
8823
8824#ifdef FEAT_BROWSE
8825 if (cmdmod.browse)
8826 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008827 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828# ifdef FEAT_SESSION
8829 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8830 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8831# endif
8832 (char_u *)_("Save Setup"),
8833 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8834 if (browseFile == NULL)
8835 goto theend;
8836 fname = browseFile;
8837 eap->forceit = TRUE; /* since dialog already asked */
8838 }
8839#endif
8840
8841#if defined(FEAT_SESSION) && defined(vim_mkdir)
8842 /* When using 'viewdir' may have to create the directory. */
8843 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008844 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845#endif
8846
8847 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8848 if (fd != NULL)
8849 {
8850#ifdef FEAT_SESSION
8851 if (eap->cmdidx == CMD_mkview)
8852 flagp = &vop_flags;
8853 else
8854 flagp = &ssop_flags;
8855#endif
8856
8857#ifdef MKSESSION_NL
8858 /* "unix" in 'sessionoptions': use NL line separator */
8859 if (view_session && (*flagp & SSOP_UNIX))
8860 mksession_nl = TRUE;
8861#endif
8862
8863 /* Write the version command for :mkvimrc */
8864 if (eap->cmdidx == CMD_mkvimrc)
8865 (void)put_line(fd, "version 6.0");
8866
8867#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008868 if (eap->cmdidx == CMD_mksession)
8869 {
8870 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8871 failed = TRUE;
8872 }
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 if (eap->cmdidx != CMD_mkview)
8875#endif
8876 {
8877 /* Write setting 'compatible' first, because it has side effects.
8878 * For that same reason only do it when needed. */
8879 if (p_cp)
8880 (void)put_line(fd, "if !&cp | set cp | endif");
8881 else
8882 (void)put_line(fd, "if &cp | set nocp | endif");
8883 }
8884
8885#ifdef FEAT_SESSION
8886 if (!view_session
8887 || (eap->cmdidx == CMD_mksession
8888 && (*flagp & SSOP_OPTIONS)))
8889#endif
8890 failed |= (makemap(fd, NULL) == FAIL
8891 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8892
8893#ifdef FEAT_SESSION
8894 if (!failed && view_session)
8895 {
8896 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8897 failed = TRUE;
8898 if (eap->cmdidx == CMD_mksession)
8899 {
8900 char_u dirnow[MAXPATHL]; /* current directory */
8901
8902 /*
8903 * Change to session file's dir.
8904 */
8905 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8906 || mch_chdir((char *)dirnow) != 0)
8907 *dirnow = NUL;
8908 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8909 {
8910 if (vim_chdirfile(fname) == OK)
8911 shorten_fnames(TRUE);
8912 }
8913 else if (*dirnow != NUL
8914 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8915 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008916 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008917 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 }
8919
8920 failed |= (makeopens(fd, dirnow) == FAIL);
8921
8922 /* restore original dir */
8923 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8924 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8925 {
8926 if (mch_chdir((char *)dirnow) != 0)
8927 EMSG(_(e_prev_dir));
8928 shorten_fnames(TRUE);
8929 }
8930 }
8931 else
8932 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008933 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8934 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 }
8936 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8937 == FAIL)
8938 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008939 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8940 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008941 if (eap->cmdidx == CMD_mksession)
8942 {
8943 if (put_line(fd, "unlet SessionLoad") == FAIL)
8944 failed = TRUE;
8945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 }
8947#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008948 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8949 failed = TRUE;
8950
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 failed |= fclose(fd);
8952
8953 if (failed)
8954 EMSG(_(e_write));
8955#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8956 else if (eap->cmdidx == CMD_mksession)
8957 {
8958 /* successful session write - set this_session var */
8959 char_u tbuf[MAXPATHL];
8960
8961 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8962 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8963 }
8964#endif
8965#ifdef MKSESSION_NL
8966 mksession_nl = FALSE;
8967#endif
8968 }
8969
8970#ifdef FEAT_BROWSE
8971theend:
8972 vim_free(browseFile);
8973#endif
8974#ifdef FEAT_SESSION
8975 vim_free(viewFile);
8976#endif
8977}
8978
Bram Moolenaardf177f62005-02-22 08:39:57 +00008979#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8980 || defined(PROTO)
8981 int
8982vim_mkdir_emsg(name, prot)
8983 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00008984 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008985{
8986 if (vim_mkdir(name, prot) != 0)
8987 {
8988 EMSG2(_("E739: Cannot create directory: %s"), name);
8989 return FAIL;
8990 }
8991 return OK;
8992}
8993#endif
8994
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995/*
8996 * Open a file for writing for an Ex command, with some checks.
8997 * Return file descriptor, or NULL on failure.
8998 */
8999 FILE *
9000open_exfile(fname, forceit, mode)
9001 char_u *fname;
9002 int forceit;
9003 char *mode; /* "w" for create new file or "a" for append */
9004{
9005 FILE *fd;
9006
9007#ifdef UNIX
9008 /* with Unix it is possible to open a directory */
9009 if (mch_isdir(fname))
9010 {
9011 EMSG2(_(e_isadir2), fname);
9012 return NULL;
9013 }
9014#endif
9015 if (!forceit && *mode != 'a' && vim_fexists(fname))
9016 {
9017 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9018 return NULL;
9019 }
9020
9021 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9022 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9023
9024 return fd;
9025}
9026
9027/*
9028 * ":mark" and ":k".
9029 */
9030 static void
9031ex_mark(eap)
9032 exarg_T *eap;
9033{
9034 pos_T pos;
9035
9036 if (*eap->arg == NUL) /* No argument? */
9037 EMSG(_(e_argreq));
9038 else if (eap->arg[1] != NUL) /* more than one character? */
9039 EMSG(_(e_trailing));
9040 else
9041 {
9042 pos = curwin->w_cursor; /* save curwin->w_cursor */
9043 curwin->w_cursor.lnum = eap->line2;
9044 beginline(BL_WHITE | BL_FIX);
9045 if (setmark(*eap->arg) == FAIL) /* set mark */
9046 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9047 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9048 }
9049}
9050
9051/*
9052 * Update w_topline, w_leftcol and the cursor position.
9053 */
9054 void
9055update_topline_cursor()
9056{
9057 check_cursor(); /* put cursor on valid line */
9058 update_topline();
9059 if (!curwin->w_p_wrap)
9060 validate_cursor();
9061 update_curswant();
9062}
9063
9064#ifdef FEAT_EX_EXTRA
9065/*
9066 * ":normal[!] {commands}": Execute normal mode commands.
9067 */
9068 static void
9069ex_normal(eap)
9070 exarg_T *eap;
9071{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 int save_msg_scroll = msg_scroll;
9073 int save_restart_edit = restart_edit;
9074 int save_msg_didout = msg_didout;
9075 int save_State = State;
9076 tasave_T tabuf;
9077 int save_insertmode = p_im;
9078 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009079 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080#ifdef FEAT_MBYTE
9081 char_u *arg = NULL;
9082 int l;
9083 char_u *p;
9084#endif
9085
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009086 if (ex_normal_lock > 0)
9087 {
9088 EMSG(_(e_secure));
9089 return;
9090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 if (ex_normal_busy >= p_mmd)
9092 {
9093 EMSG(_("E192: Recursive use of :normal too deep"));
9094 return;
9095 }
9096 ++ex_normal_busy;
9097
9098 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9099 restart_edit = 0; /* don't go to Insert mode */
9100 p_im = FALSE; /* don't use 'insertmode' */
9101
9102#ifdef FEAT_MBYTE
9103 /*
9104 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9105 * this for the K_SPECIAL leading byte, otherwise special keys will not
9106 * work.
9107 */
9108 if (has_mbyte)
9109 {
9110 int len = 0;
9111
9112 /* Count the number of characters to be escaped. */
9113 for (p = eap->arg; *p != NUL; ++p)
9114 {
9115# ifdef FEAT_GUI
9116 if (*p == CSI) /* leadbyte CSI */
9117 len += 2;
9118# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009119 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9121# ifdef FEAT_GUI
9122 || *p == CSI
9123# endif
9124 )
9125 len += 2;
9126 }
9127 if (len > 0)
9128 {
9129 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9130 if (arg != NULL)
9131 {
9132 len = 0;
9133 for (p = eap->arg; *p != NUL; ++p)
9134 {
9135 arg[len++] = *p;
9136# ifdef FEAT_GUI
9137 if (*p == CSI)
9138 {
9139 arg[len++] = KS_EXTRA;
9140 arg[len++] = (int)KE_CSI;
9141 }
9142# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009143 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 {
9145 arg[len++] = *++p;
9146 if (*p == K_SPECIAL)
9147 {
9148 arg[len++] = KS_SPECIAL;
9149 arg[len++] = KE_FILLER;
9150 }
9151# ifdef FEAT_GUI
9152 else if (*p == CSI)
9153 {
9154 arg[len++] = KS_EXTRA;
9155 arg[len++] = (int)KE_CSI;
9156 }
9157# endif
9158 }
9159 arg[len] = NUL;
9160 }
9161 }
9162 }
9163 }
9164#endif
9165
9166 /*
9167 * Save the current typeahead. This is required to allow using ":normal"
9168 * from an event handler and makes sure we don't hang when the argument
9169 * ends with half a command.
9170 */
9171 save_typeahead(&tabuf);
9172 if (tabuf.typebuf_valid)
9173 {
9174 /*
9175 * Repeat the :normal command for each line in the range. When no
9176 * range given, execute it just once, without positioning the cursor
9177 * first.
9178 */
9179 do
9180 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 if (eap->addr_count != 0)
9182 {
9183 curwin->w_cursor.lnum = eap->line1++;
9184 curwin->w_cursor.col = 0;
9185 }
9186
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009187 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188#ifdef FEAT_MBYTE
9189 arg != NULL ? arg :
9190#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009191 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 }
9193 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9194 }
9195
9196 /* Might not return to the main loop when in an event handler. */
9197 update_topline_cursor();
9198
9199 /* Restore the previous typeahead. */
9200 restore_typeahead(&tabuf);
9201
9202 --ex_normal_busy;
9203 msg_scroll = save_msg_scroll;
9204 restart_edit = save_restart_edit;
9205 p_im = save_insertmode;
9206 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009207 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9209
9210 /* Restore the state (needed when called from a function executed for
9211 * 'indentexpr'). */
9212 State = save_State;
9213#ifdef FEAT_MBYTE
9214 vim_free(arg);
9215#endif
9216}
9217
9218/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009219 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 */
9221 static void
9222ex_startinsert(eap)
9223 exarg_T *eap;
9224{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009225 if (eap->forceit)
9226 {
9227 coladvance((colnr_T)MAXCOL);
9228 curwin->w_curswant = MAXCOL;
9229 curwin->w_set_curswant = FALSE;
9230 }
9231
Bram Moolenaara40c5002005-01-09 21:16:21 +00009232 /* Ignore the command when already in Insert mode. Inserting an
9233 * expression register that invokes a function can do this. */
9234 if (State & INSERT)
9235 return;
9236
Bram Moolenaar64969662005-12-14 21:59:55 +00009237 if (eap->cmdidx == CMD_startinsert)
9238 restart_edit = 'a';
9239 else if (eap->cmdidx == CMD_startreplace)
9240 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009242 restart_edit = 'V';
9243
9244 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009246 if (eap->cmdidx == CMD_startinsert)
9247 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 curwin->w_curswant = 0; /* avoid MAXCOL */
9249 }
9250}
9251
9252/*
9253 * ":stopinsert"
9254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 static void
9256ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009257 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 restart_edit = 0;
9260 stop_insert_mode = TRUE;
9261}
9262#endif
9263
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009264#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9265/*
9266 * Execute normal mode command "cmd".
9267 * "remap" can be REMAP_NONE or REMAP_YES.
9268 */
9269 void
9270exec_normal_cmd(cmd, remap, silent)
9271 char_u *cmd;
9272 int remap;
9273 int silent;
9274{
9275 oparg_T oa;
9276
9277 /*
9278 * Stuff the argument into the typeahead buffer.
9279 * Execute normal_cmd() until there is no typeahead left.
9280 */
9281 clear_oparg(&oa);
9282 finish_op = FALSE;
9283 ins_typebuf(cmd, remap, 0, TRUE, silent);
9284 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9285 && !got_int)
9286 {
9287 update_topline_cursor();
9288 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9289 }
9290}
9291#endif
9292
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293#ifdef FEAT_FIND_ID
9294 static void
9295ex_checkpath(eap)
9296 exarg_T *eap;
9297{
9298 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9299 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9300 (linenr_T)1, (linenr_T)MAXLNUM);
9301}
9302
9303#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9304/*
9305 * ":psearch"
9306 */
9307 static void
9308ex_psearch(eap)
9309 exarg_T *eap;
9310{
9311 g_do_tagpreview = p_pvh;
9312 ex_findpat(eap);
9313 g_do_tagpreview = 0;
9314}
9315#endif
9316
9317 static void
9318ex_findpat(eap)
9319 exarg_T *eap;
9320{
9321 int whole = TRUE;
9322 long n;
9323 char_u *p;
9324 int action;
9325
9326 switch (cmdnames[eap->cmdidx].cmd_name[2])
9327 {
9328 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9329 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9330 action = ACTION_GOTO;
9331 else
9332 action = ACTION_SHOW;
9333 break;
9334 case 'i': /* ":ilist" and ":dlist" */
9335 action = ACTION_SHOW_ALL;
9336 break;
9337 case 'u': /* ":ijump" and ":djump" */
9338 action = ACTION_GOTO;
9339 break;
9340 default: /* ":isplit" and ":dsplit" */
9341 action = ACTION_SPLIT;
9342 break;
9343 }
9344
9345 n = 1;
9346 if (vim_isdigit(*eap->arg)) /* get count */
9347 {
9348 n = getdigits(&eap->arg);
9349 eap->arg = skipwhite(eap->arg);
9350 }
9351 if (*eap->arg == '/') /* Match regexp, not just whole words */
9352 {
9353 whole = FALSE;
9354 ++eap->arg;
9355 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9356 if (*p)
9357 {
9358 *p++ = NUL;
9359 p = skipwhite(p);
9360
9361 /* Check for trailing illegal characters */
9362 if (!ends_excmd(*p))
9363 eap->errmsg = e_trailing;
9364 else
9365 eap->nextcmd = check_nextcmd(p);
9366 }
9367 }
9368 if (!eap->skip)
9369 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9370 whole, !eap->forceit,
9371 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9372 n, action, eap->line1, eap->line2);
9373}
9374#endif
9375
9376#ifdef FEAT_WINDOWS
9377
9378# ifdef FEAT_QUICKFIX
9379/*
9380 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9381 */
9382 static void
9383ex_ptag(eap)
9384 exarg_T *eap;
9385{
9386 g_do_tagpreview = p_pvh;
9387 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9388}
9389
9390/*
9391 * ":pedit"
9392 */
9393 static void
9394ex_pedit(eap)
9395 exarg_T *eap;
9396{
9397 win_T *curwin_save = curwin;
9398
9399 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009400 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 keep_help_flag = curwin_save->w_buffer->b_help;
9402 do_exedit(eap, NULL);
9403 keep_help_flag = FALSE;
9404 if (curwin != curwin_save && win_valid(curwin_save))
9405 {
9406 /* Return cursor to where we were */
9407 validate_cursor();
9408 redraw_later(VALID);
9409 win_enter(curwin_save, TRUE);
9410 }
9411 g_do_tagpreview = 0;
9412}
9413# endif
9414
9415/*
9416 * ":stag", ":stselect" and ":stjump".
9417 */
9418 static void
9419ex_stag(eap)
9420 exarg_T *eap;
9421{
9422 postponed_split = -1;
9423 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009424 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009425 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9426 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009427 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428}
9429#endif
9430
9431/*
9432 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9433 */
9434 static void
9435ex_tag(eap)
9436 exarg_T *eap;
9437{
9438 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9439}
9440
9441 static void
9442ex_tag_cmd(eap, name)
9443 exarg_T *eap;
9444 char_u *name;
9445{
9446 int cmd;
9447
9448 switch (name[1])
9449 {
9450 case 'j': cmd = DT_JUMP; /* ":tjump" */
9451 break;
9452 case 's': cmd = DT_SELECT; /* ":tselect" */
9453 break;
9454 case 'p': cmd = DT_PREV; /* ":tprevious" */
9455 break;
9456 case 'N': cmd = DT_PREV; /* ":tNext" */
9457 break;
9458 case 'n': cmd = DT_NEXT; /* ":tnext" */
9459 break;
9460 case 'o': cmd = DT_POP; /* ":pop" */
9461 break;
9462 case 'f': /* ":tfirst" */
9463 case 'r': cmd = DT_FIRST; /* ":trewind" */
9464 break;
9465 case 'l': cmd = DT_LAST; /* ":tlast" */
9466 break;
9467 default: /* ":tag" */
9468#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009469 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 {
9471 do_cstag(eap);
9472 return;
9473 }
9474#endif
9475 cmd = DT_TAG;
9476 break;
9477 }
9478
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009479 if (name[0] == 'l')
9480 {
9481#ifndef FEAT_QUICKFIX
9482 ex_ni(eap);
9483 return;
9484#else
9485 cmd = DT_LTAG;
9486#endif
9487 }
9488
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9490 eap->forceit, TRUE);
9491}
9492
9493/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009494 * Check "str" for starting with a special cmdline variable.
9495 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9496 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9497 */
9498 int
9499find_cmdline_var(src, usedlen)
9500 char_u *src;
9501 int *usedlen;
9502{
9503 int len;
9504 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009505 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009506 "%",
9507#define SPEC_PERC 0
9508 "#",
9509#define SPEC_HASH 1
9510 "<cword>", /* cursor word */
9511#define SPEC_CWORD 2
9512 "<cWORD>", /* cursor WORD */
9513#define SPEC_CCWORD 3
9514 "<cfile>", /* cursor path name */
9515#define SPEC_CFILE 4
9516 "<sfile>", /* ":so" file name */
9517#define SPEC_SFILE 5
9518#ifdef FEAT_AUTOCMD
9519 "<afile>", /* autocommand file name */
9520# define SPEC_AFILE 6
9521 "<abuf>", /* autocommand buffer number */
9522# define SPEC_ABUF 7
9523 "<amatch>", /* autocommand match name */
9524# define SPEC_AMATCH 8
9525#endif
9526#ifdef FEAT_CLIENTSERVER
9527 "<client>"
9528# define SPEC_CLIENT 9
9529#endif
9530 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009531
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009532 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009533 {
9534 len = (int)STRLEN(spec_str[i]);
9535 if (STRNCMP(src, spec_str[i], len) == 0)
9536 {
9537 *usedlen = len;
9538 return i;
9539 }
9540 }
9541 return -1;
9542}
9543
9544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 * Evaluate cmdline variables.
9546 *
9547 * change '%' to curbuf->b_ffname
9548 * '#' to curwin->w_altfile
9549 * '<cword>' to word under the cursor
9550 * '<cWORD>' to WORD under the cursor
9551 * '<cfile>' to path name under the cursor
9552 * '<sfile>' to sourced file name
9553 * '<afile>' to file name for autocommand
9554 * '<abuf>' to buffer number for autocommand
9555 * '<amatch>' to matching name for autocommand
9556 *
9557 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9558 * "" for error without a message) and NULL is returned.
9559 * Returns an allocated string if a valid match was found.
9560 * Returns NULL if no match was found. "usedlen" then still contains the
9561 * number of characters to skip.
9562 */
9563 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009564eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009566 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567 int *usedlen; /* characters after src that are used */
9568 linenr_T *lnump; /* line number for :e command, or NULL */
9569 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009570 int *escaped; /* return value has escaped white space (can
9571 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 int i;
9574 char_u *s;
9575 char_u *result;
9576 char_u *resultbuf = NULL;
9577 int resultlen;
9578 buf_T *buf;
9579 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9580 int spec_idx;
9581#ifdef FEAT_MODIFY_FNAME
9582 int skip_mod = FALSE;
9583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
9585#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9586 char_u strbuf[30];
9587#endif
9588
9589 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009590 if (escaped != NULL)
9591 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592
9593 /*
9594 * Check if there is something to do.
9595 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009596 spec_idx = find_cmdline_var(src, usedlen);
9597 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 {
9599 *usedlen = 1;
9600 return NULL;
9601 }
9602
9603 /*
9604 * Skip when preceded with a backslash "\%" and "\#".
9605 * Note: In "\\%" the % is also not recognized!
9606 */
9607 if (src > srcstart && src[-1] == '\\')
9608 {
9609 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009610 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 return NULL;
9612 }
9613
9614 /*
9615 * word or WORD under cursor
9616 */
9617 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9618 {
9619 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9620 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9621 if (resultlen == 0)
9622 {
9623 *errormsg = (char_u *)"";
9624 return NULL;
9625 }
9626 }
9627
9628 /*
9629 * '#': Alternate file name
9630 * '%': Current file name
9631 * File name under the cursor
9632 * File name for autocommand
9633 * and following modifiers
9634 */
9635 else
9636 {
9637 switch (spec_idx)
9638 {
9639 case SPEC_PERC: /* '%': current file */
9640 if (curbuf->b_fname == NULL)
9641 {
9642 result = (char_u *)"";
9643 valid = 0; /* Must have ":p:h" to be valid */
9644 }
9645 else
9646#ifdef RISCOS
9647 /* Always use the full path for RISC OS if possible. */
9648 result = curbuf->b_ffname;
9649 if (result == NULL)
9650 result = curbuf->b_fname;
9651#else
9652 result = curbuf->b_fname;
9653#endif
9654 break;
9655
9656 case SPEC_HASH: /* '#' or "#99": alternate file */
9657 if (src[1] == '#') /* "##": the argument list */
9658 {
9659 result = arg_all();
9660 resultbuf = result;
9661 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009662 if (escaped != NULL)
9663 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664#ifdef FEAT_MODIFY_FNAME
9665 skip_mod = TRUE;
9666#endif
9667 break;
9668 }
9669 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009670 if (*s == '<') /* "#<99" uses v:oldfiles */
9671 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 i = (int)getdigits(&s);
9673 *usedlen = (int)(s - src); /* length of what we expand */
9674
Bram Moolenaard812df62008-11-09 12:46:09 +00009675 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009677 if (*usedlen < 2)
9678 {
9679 /* Should we give an error message for #<text? */
9680 *usedlen = 1;
9681 return NULL;
9682 }
9683#ifdef FEAT_EVAL
9684 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9685 (long)i);
9686 if (result == NULL)
9687 {
9688 *errormsg = (char_u *)"";
9689 return NULL;
9690 }
9691#else
9692 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 }
9696 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009697 {
9698 buf = buflist_findnr(i);
9699 if (buf == NULL)
9700 {
9701 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9702 return NULL;
9703 }
9704 if (lnump != NULL)
9705 *lnump = ECMD_LAST;
9706 if (buf->b_fname == NULL)
9707 {
9708 result = (char_u *)"";
9709 valid = 0; /* Must have ":p:h" to be valid */
9710 }
9711 else
9712 result = buf->b_fname;
9713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 break;
9715
9716#ifdef FEAT_SEARCHPATH
9717 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009718 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 if (result == NULL)
9720 {
9721 *errormsg = (char_u *)"";
9722 return NULL;
9723 }
9724 resultbuf = result; /* remember allocated string */
9725 break;
9726#endif
9727
9728#ifdef FEAT_AUTOCMD
9729 case SPEC_AFILE: /* file name for autocommand */
9730 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009731 if (result != NULL && !autocmd_fname_full)
9732 {
9733 /* Still need to turn the fname into a full path. It is
9734 * postponed to avoid a delay when <afile> is not used. */
9735 autocmd_fname_full = TRUE;
9736 result = FullName_save(autocmd_fname, FALSE);
9737 vim_free(autocmd_fname);
9738 autocmd_fname = result;
9739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 if (result == NULL)
9741 {
9742 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9743 return NULL;
9744 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009745 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 break;
9747
9748 case SPEC_ABUF: /* buffer number for autocommand */
9749 if (autocmd_bufnr <= 0)
9750 {
9751 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9752 return NULL;
9753 }
9754 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9755 result = strbuf;
9756 break;
9757
9758 case SPEC_AMATCH: /* match name for autocommand */
9759 result = autocmd_match;
9760 if (result == NULL)
9761 {
9762 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9763 return NULL;
9764 }
9765 break;
9766
9767#endif
9768 case SPEC_SFILE: /* file name for ":so" command */
9769 result = sourcing_name;
9770 if (result == NULL)
9771 {
9772 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9773 return NULL;
9774 }
9775 break;
9776#if defined(FEAT_CLIENTSERVER)
9777 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009778 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9779 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 result = strbuf;
9781 break;
9782#endif
9783 }
9784
9785 resultlen = (int)STRLEN(result); /* length of new string */
9786 if (src[*usedlen] == '<') /* remove the file name extension */
9787 {
9788 ++*usedlen;
9789#ifdef RISCOS
9790 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9791#else
9792 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9793#endif
9794 resultlen = (int)(s - result);
9795 }
9796#ifdef FEAT_MODIFY_FNAME
9797 else if (!skip_mod)
9798 {
9799 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9800 &resultlen);
9801 if (result == NULL)
9802 {
9803 *errormsg = (char_u *)"";
9804 return NULL;
9805 }
9806 }
9807#endif
9808 }
9809
9810 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9811 {
9812 if (valid != VALID_HEAD + VALID_PATH)
9813 /* xgettext:no-c-format */
9814 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9815 else
9816 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9817 result = NULL;
9818 }
9819 else
9820 result = vim_strnsave(result, resultlen);
9821 vim_free(resultbuf);
9822 return result;
9823}
9824
9825/*
9826 * Concatenate all files in the argument list, separated by spaces, and return
9827 * it in one allocated string.
9828 * Spaces and backslashes in the file names are escaped with a backslash.
9829 * Returns NULL when out of memory.
9830 */
9831 static char_u *
9832arg_all()
9833{
9834 int len;
9835 int idx;
9836 char_u *retval = NULL;
9837 char_u *p;
9838
9839 /*
9840 * Do this loop two times:
9841 * first time: compute the total length
9842 * second time: concatenate the names
9843 */
9844 for (;;)
9845 {
9846 len = 0;
9847 for (idx = 0; idx < ARGCOUNT; ++idx)
9848 {
9849 p = alist_name(&ARGLIST[idx]);
9850 if (p != NULL)
9851 {
9852 if (len > 0)
9853 {
9854 /* insert a space in between names */
9855 if (retval != NULL)
9856 retval[len] = ' ';
9857 ++len;
9858 }
9859 for ( ; *p != NUL; ++p)
9860 {
9861 if (*p == ' ' || *p == '\\')
9862 {
9863 /* insert a backslash */
9864 if (retval != NULL)
9865 retval[len] = '\\';
9866 ++len;
9867 }
9868 if (retval != NULL)
9869 retval[len] = *p;
9870 ++len;
9871 }
9872 }
9873 }
9874
9875 /* second time: break here */
9876 if (retval != NULL)
9877 {
9878 retval[len] = NUL;
9879 break;
9880 }
9881
9882 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009883 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884 if (retval == NULL)
9885 break;
9886 }
9887
9888 return retval;
9889}
9890
9891#if defined(FEAT_AUTOCMD) || defined(PROTO)
9892/*
9893 * Expand the <sfile> string in "arg".
9894 *
9895 * Returns an allocated string, or NULL for any error.
9896 */
9897 char_u *
9898expand_sfile(arg)
9899 char_u *arg;
9900{
9901 char_u *errormsg;
9902 int len;
9903 char_u *result;
9904 char_u *newres;
9905 char_u *repl;
9906 int srclen;
9907 char_u *p;
9908
9909 result = vim_strsave(arg);
9910 if (result == NULL)
9911 return NULL;
9912
9913 for (p = result; *p; )
9914 {
9915 if (STRNCMP(p, "<sfile>", 7) != 0)
9916 ++p;
9917 else
9918 {
9919 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009920 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 if (errormsg != NULL)
9922 {
9923 if (*errormsg)
9924 emsg(errormsg);
9925 vim_free(result);
9926 return NULL;
9927 }
9928 if (repl == NULL) /* no match (cannot happen) */
9929 {
9930 p += srclen;
9931 continue;
9932 }
9933 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9934 newres = alloc(len);
9935 if (newres == NULL)
9936 {
9937 vim_free(repl);
9938 vim_free(result);
9939 return NULL;
9940 }
9941 mch_memmove(newres, result, (size_t)(p - result));
9942 STRCPY(newres + (p - result), repl);
9943 len = (int)STRLEN(newres);
9944 STRCAT(newres, p + srclen);
9945 vim_free(repl);
9946 vim_free(result);
9947 result = newres;
9948 p = newres + len; /* continue after the match */
9949 }
9950 }
9951
9952 return result;
9953}
9954#endif
9955
9956#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009957static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9958 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9960static frame_T *ses_skipframe __ARGS((frame_T *fr));
9961static int ses_do_frame __ARGS((frame_T *fr));
9962static int ses_do_win __ARGS((win_T *wp));
9963static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9964static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9965static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9966
9967/*
9968 * Write openfile commands for the current buffers to an .exrc file.
9969 * Return FAIL on error, OK otherwise.
9970 */
9971 static int
9972makeopens(fd, dirnow)
9973 FILE *fd;
9974 char_u *dirnow; /* Current directory name */
9975{
9976 buf_T *buf;
9977 int only_save_windows = TRUE;
9978 int nr;
9979 int cnr = 1;
9980 int restore_size = TRUE;
9981 win_T *wp;
9982 char_u *sname;
9983 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009984 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009985 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009986 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009987 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009988 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989
9990 if (ssop_flags & SSOP_BUFFERS)
9991 only_save_windows = FALSE; /* Save ALL buffers */
9992
9993 /*
9994 * Begin by setting the this_session variable, and then other
9995 * sessionable variables.
9996 */
9997#ifdef FEAT_EVAL
9998 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9999 return FAIL;
10000 if (ssop_flags & SSOP_GLOBALS)
10001 if (store_session_globals(fd) == FAIL)
10002 return FAIL;
10003#endif
10004
10005 /*
10006 * Close all windows but one.
10007 */
10008 if (put_line(fd, "silent only") == FAIL)
10009 return FAIL;
10010
10011 /*
10012 * Now a :cd command to the session directory or the current directory
10013 */
10014 if (ssop_flags & SSOP_SESDIR)
10015 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010016 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10017 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 return FAIL;
10019 }
10020 else if (ssop_flags & SSOP_CURDIR)
10021 {
10022 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10023 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010024 || fputs("cd ", fd) < 0
10025 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10026 || put_eol(fd) == FAIL)
10027 {
10028 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010029 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 vim_free(sname);
10032 }
10033
10034 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010035 * If there is an empty, unnamed buffer we will wipe it out later.
10036 * Remember the buffer number.
10037 */
10038 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10039 return FAIL;
10040 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10041 return FAIL;
10042 if (put_line(fd, "endif") == FAIL)
10043 return FAIL;
10044
10045 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046 * Now save the current files, current buffer first.
10047 */
10048 if (put_line(fd, "set shortmess=aoO") == FAIL)
10049 return FAIL;
10050
10051 /* Now put the other buffers into the buffer list */
10052 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10053 {
10054 if (!(only_save_windows && buf->b_nwindows == 0)
10055 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10056 && buf->b_fname != NULL
10057 && buf->b_p_bl)
10058 {
10059 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10060 : buf->b_wininfo->wi_fpos.lnum) < 0
10061 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10062 return FAIL;
10063 }
10064 }
10065
10066 /* the global argument list */
10067 if (ses_arglist(fd, "args", &global_alist.al_ga,
10068 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10069 return FAIL;
10070
10071 if (ssop_flags & SSOP_RESIZE)
10072 {
10073 /* Note: after the restore we still check it worked!*/
10074 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10075 || put_eol(fd) == FAIL)
10076 return FAIL;
10077 }
10078
10079#ifdef FEAT_GUI
10080 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10081 {
10082 int x, y;
10083
10084 if (gui_mch_get_winpos(&x, &y) == OK)
10085 {
10086 /* Note: after the restore we still check it worked!*/
10087 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10088 return FAIL;
10089 }
10090 }
10091#endif
10092
10093 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010094 * May repeat putting Windows for each tab, when "tabpages" is in
10095 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010096 * Don't use goto_tabpage(), it may change directory and trigger
10097 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010099 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010100 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010101 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010103 int need_tabnew = FALSE;
10104
Bram Moolenaar18144c82006-04-12 21:52:12 +000010105 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010107 tabpage_T *tp = find_tabpage(tabnr);
10108
10109 if (tp == NULL)
10110 break; /* done all tab pages */
10111 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010112 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010113 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010114 tab_topframe = topframe;
10115 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010116 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010117 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010118 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010119 tab_topframe = tp->tp_topframe;
10120 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010121 if (tabnr > 1)
10122 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124
Bram Moolenaar18144c82006-04-12 21:52:12 +000010125 /*
10126 * Before creating the window layout, try loading one file. If this
10127 * is aborted we don't end up with a number of useless windows.
10128 * This may have side effects! (e.g., compressed or network file).
10129 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010130 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010131 {
10132 if (ses_do_win(wp)
10133 && wp->w_buffer->b_ffname != NULL
10134 && !wp->w_buffer->b_help
10135#ifdef FEAT_QUICKFIX
10136 && !bt_nofile(wp->w_buffer)
10137#endif
10138 )
10139 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010140 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010141 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10142 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010143 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010144 if (!wp->w_arg_idx_invalid)
10145 edited_win = wp;
10146 break;
10147 }
10148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010150 /* If no file got edited create an empty tab page. */
10151 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10152 return FAIL;
10153
Bram Moolenaar18144c82006-04-12 21:52:12 +000010154 /*
10155 * Save current window layout.
10156 */
10157 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010159 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010161 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10162 return FAIL;
10163 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10164 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165
Bram Moolenaar18144c82006-04-12 21:52:12 +000010166 /*
10167 * Check if window sizes can be restored (no windows omitted).
10168 * Remember the window number of the current window after restoring.
10169 */
10170 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010171 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010172 {
10173 if (ses_do_win(wp))
10174 ++nr;
10175 else
10176 restore_size = FALSE;
10177 if (curwin == wp)
10178 cnr = nr;
10179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
Bram Moolenaar18144c82006-04-12 21:52:12 +000010181 /* Go to the first window. */
10182 if (put_line(fd, "wincmd t") == FAIL)
10183 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010184
Bram Moolenaar18144c82006-04-12 21:52:12 +000010185 /*
10186 * If more than one window, see if sizes can be restored.
10187 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10188 * resized when moving between windows.
10189 * Do this before restoring the view, so that the topline and the
10190 * cursor can be set. This is done again below.
10191 */
10192 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10193 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010194 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010195 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196
Bram Moolenaar18144c82006-04-12 21:52:12 +000010197 /*
10198 * Restore the view of the window (options, file, cursor, etc.).
10199 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010200 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010201 {
10202 if (!ses_do_win(wp))
10203 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010204 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10205 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010206 return FAIL;
10207 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10208 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010209 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010210 }
10211
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010212 /* The argument index in the first tab page is zero, need to set it in
10213 * each window. For further tab pages it's the window where we do
10214 * "tabedit". */
10215 cur_arg_idx = next_arg_idx;
10216
Bram Moolenaar18144c82006-04-12 21:52:12 +000010217 /*
10218 * Restore cursor to the current window if it's not the first one.
10219 */
10220 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10221 || put_eol(fd) == FAIL))
10222 return FAIL;
10223
10224 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010225 * Restore window sizes again after jumping around in windows, because
10226 * the current window has a minimum size while others may not.
10227 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010228 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010229 return FAIL;
10230
Bram Moolenaar18144c82006-04-12 21:52:12 +000010231 /* Don't continue in another tab page when doing only the current one
10232 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010233 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010234 break;
10235 }
10236
10237 if (ssop_flags & SSOP_TABPAGES)
10238 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010239 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10240 || put_eol(fd) == FAIL)
10241 return FAIL;
10242 }
10243
Bram Moolenaar9c102382006-05-03 21:26:49 +000010244 /*
10245 * Wipe out an empty unnamed buffer we started in.
10246 */
10247 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10248 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010249 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010250 return FAIL;
10251 if (put_line(fd, "endif") == FAIL)
10252 return FAIL;
10253 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10254 return FAIL;
10255
10256 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10257 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10258 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10259 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260
10261 /*
10262 * Lastly, execute the x.vim file if it exists.
10263 */
10264 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10265 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010266 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 || put_line(fd, "endif") == FAIL)
10268 return FAIL;
10269
10270 return OK;
10271}
10272
10273 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010274ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275 FILE *fd;
10276 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010277 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278{
10279 int n = 0;
10280 win_T *wp;
10281
10282 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10283 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010284 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 {
10286 if (!ses_do_win(wp))
10287 continue;
10288 ++n;
10289
10290 /* restore height when not full height */
10291 if (wp->w_height + wp->w_status_height < topframe->fr_height
10292 && (fprintf(fd,
10293 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10294 n, (long)wp->w_height, Rows / 2, Rows) < 0
10295 || put_eol(fd) == FAIL))
10296 return FAIL;
10297
10298 /* restore width when not full width */
10299 if (wp->w_width < Columns && (fprintf(fd,
10300 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10301 n, (long)wp->w_width, Columns / 2, Columns) < 0
10302 || put_eol(fd) == FAIL))
10303 return FAIL;
10304 }
10305 }
10306 else
10307 {
10308 /* Just equalise window sizes */
10309 if (put_line(fd, "wincmd =") == FAIL)
10310 return FAIL;
10311 }
10312 return OK;
10313}
10314
10315/*
10316 * Write commands to "fd" to recursively create windows for frame "fr",
10317 * horizontally and vertically split.
10318 * After the commands the last window in the frame is the current window.
10319 * Returns FAIL when writing the commands to "fd" fails.
10320 */
10321 static int
10322ses_win_rec(fd, fr)
10323 FILE *fd;
10324 frame_T *fr;
10325{
10326 frame_T *frc;
10327 int count = 0;
10328
10329 if (fr->fr_layout != FR_LEAF)
10330 {
10331 /* Find first frame that's not skipped and then create a window for
10332 * each following one (first frame is already there). */
10333 frc = ses_skipframe(fr->fr_child);
10334 if (frc != NULL)
10335 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10336 {
10337 /* Make window as big as possible so that we have lots of room
10338 * to split. */
10339 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10340 || put_line(fd, fr->fr_layout == FR_COL
10341 ? "split" : "vsplit") == FAIL)
10342 return FAIL;
10343 ++count;
10344 }
10345
10346 /* Go back to the first window. */
10347 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10348 ? "%dwincmd k" : "%dwincmd h", count) < 0
10349 || put_eol(fd) == FAIL))
10350 return FAIL;
10351
10352 /* Recursively create frames/windows in each window of this column or
10353 * row. */
10354 frc = ses_skipframe(fr->fr_child);
10355 while (frc != NULL)
10356 {
10357 ses_win_rec(fd, frc);
10358 frc = ses_skipframe(frc->fr_next);
10359 /* Go to next window. */
10360 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10361 return FAIL;
10362 }
10363 }
10364 return OK;
10365}
10366
10367/*
10368 * Skip frames that don't contain windows we want to save in the Session.
10369 * Returns NULL when there none.
10370 */
10371 static frame_T *
10372ses_skipframe(fr)
10373 frame_T *fr;
10374{
10375 frame_T *frc;
10376
10377 for (frc = fr; frc != NULL; frc = frc->fr_next)
10378 if (ses_do_frame(frc))
10379 break;
10380 return frc;
10381}
10382
10383/*
10384 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10385 * the Session.
10386 */
10387 static int
10388ses_do_frame(fr)
10389 frame_T *fr;
10390{
10391 frame_T *frc;
10392
10393 if (fr->fr_layout == FR_LEAF)
10394 return ses_do_win(fr->fr_win);
10395 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10396 if (ses_do_frame(frc))
10397 return TRUE;
10398 return FALSE;
10399}
10400
10401/*
10402 * Return non-zero if window "wp" is to be stored in the Session.
10403 */
10404 static int
10405ses_do_win(wp)
10406 win_T *wp;
10407{
10408 if (wp->w_buffer->b_fname == NULL
10409#ifdef FEAT_QUICKFIX
10410 /* When 'buftype' is "nofile" can't restore the window contents. */
10411 || bt_nofile(wp->w_buffer)
10412#endif
10413 )
10414 return (ssop_flags & SSOP_BLANK);
10415 if (wp->w_buffer->b_help)
10416 return (ssop_flags & SSOP_HELP);
10417 return TRUE;
10418}
10419
10420/*
10421 * Write commands to "fd" to restore the view of a window.
10422 * Caller must make sure 'scrolloff' is zero.
10423 */
10424 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010425put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 FILE *fd;
10427 win_T *wp;
10428 int add_edit; /* add ":edit" command to view */
10429 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010430 int current_arg_idx; /* current argument index of the window, use
10431 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433 win_T *save_curwin;
10434 int f;
10435 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010436 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437
10438 /* Always restore cursor position for ":mksession". For ":mkview" only
10439 * when 'viewoptions' contains "cursor". */
10440 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10441
10442 /*
10443 * Local argument list.
10444 */
10445 if (wp->w_alist == &global_alist)
10446 {
10447 if (put_line(fd, "argglobal") == FAIL)
10448 return FAIL;
10449 }
10450 else
10451 {
10452 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10453 flagp == &vop_flags
10454 || !(*flagp & SSOP_CURDIR)
10455 || wp->w_localdir != NULL, flagp) == FAIL)
10456 return FAIL;
10457 }
10458
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010459 /* Only when part of a session: restore the argument index. Some
10460 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010461 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010462 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010464 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 || put_eol(fd) == FAIL)
10466 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010467 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 }
10469
10470 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010471 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
10473 /*
10474 * Load the file.
10475 */
10476 if (wp->w_buffer->b_ffname != NULL
10477#ifdef FEAT_QUICKFIX
10478 && !bt_nofile(wp->w_buffer)
10479#endif
10480 )
10481 {
10482 /*
10483 * Editing a file in this buffer: use ":edit file".
10484 * This may have side effects! (e.g., compressed or network file).
10485 */
10486 if (fputs("edit ", fd) < 0
10487 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10488 return FAIL;
10489 }
10490 else
10491 {
10492 /* No file in this buffer, just make it empty. */
10493 if (put_line(fd, "enew") == FAIL)
10494 return FAIL;
10495#ifdef FEAT_QUICKFIX
10496 if (wp->w_buffer->b_ffname != NULL)
10497 {
10498 /* The buffer does have a name, but it's not a file name. */
10499 if (fputs("file ", fd) < 0
10500 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10501 return FAIL;
10502 }
10503#endif
10504 do_cursor = FALSE;
10505 }
10506 }
10507
10508 /*
10509 * Local mappings and abbreviations.
10510 */
10511 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10512 && makemap(fd, wp->w_buffer) == FAIL)
10513 return FAIL;
10514
10515 /*
10516 * Local options. Need to go to the window temporarily.
10517 * Store only local values when using ":mkview" and when ":mksession" is
10518 * used and 'sessionoptions' doesn't include "options".
10519 * Some folding options are always stored when "folds" is included,
10520 * otherwise the folds would not be restored correctly.
10521 */
10522 save_curwin = curwin;
10523 curwin = wp;
10524 curbuf = curwin->w_buffer;
10525 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10526 f = makeset(fd, OPT_LOCAL,
10527 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10528#ifdef FEAT_FOLDING
10529 else if (*flagp & SSOP_FOLDS)
10530 f = makefoldset(fd);
10531#endif
10532 else
10533 f = OK;
10534 curwin = save_curwin;
10535 curbuf = curwin->w_buffer;
10536 if (f == FAIL)
10537 return FAIL;
10538
10539#ifdef FEAT_FOLDING
10540 /*
10541 * Save Folds when 'buftype' is empty and for help files.
10542 */
10543 if ((*flagp & SSOP_FOLDS)
10544 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010545# ifdef FEAT_QUICKFIX
10546 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10547# endif
10548 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 {
10550 if (put_folds(fd, wp) == FAIL)
10551 return FAIL;
10552 }
10553#endif
10554
10555 /*
10556 * Set the cursor after creating folds, since that moves the cursor.
10557 */
10558 if (do_cursor)
10559 {
10560
10561 /* Restore the cursor line in the file and relatively in the
10562 * window. Don't use "G", it changes the jumplist. */
10563 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10564 (long)wp->w_cursor.lnum,
10565 (long)(wp->w_cursor.lnum - wp->w_topline),
10566 (long)wp->w_height / 2, (long)wp->w_height) < 0
10567 || put_eol(fd) == FAIL
10568 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10569 || put_line(fd, "exe s:l") == FAIL
10570 || put_line(fd, "normal! zt") == FAIL
10571 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10572 || put_eol(fd) == FAIL)
10573 return FAIL;
10574 /* Restore the cursor column and left offset when not wrapping. */
10575 if (wp->w_cursor.col == 0)
10576 {
10577 if (put_line(fd, "normal! 0") == FAIL)
10578 return FAIL;
10579 }
10580 else
10581 {
10582 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10583 {
10584 if (fprintf(fd,
10585 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10586 (long)wp->w_cursor.col,
10587 (long)(wp->w_cursor.col - wp->w_leftcol),
10588 (long)wp->w_width / 2, (long)wp->w_width) < 0
10589 || put_eol(fd) == FAIL
10590 || put_line(fd, "if s:c > 0") == FAIL
10591 || fprintf(fd,
10592 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10593 (long)wp->w_cursor.col) < 0
10594 || put_eol(fd) == FAIL
10595 || put_line(fd, "else") == FAIL
10596 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10597 || put_eol(fd) == FAIL
10598 || put_line(fd, "endif") == FAIL)
10599 return FAIL;
10600 }
10601 else
10602 {
10603 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10604 || put_eol(fd) == FAIL)
10605 return FAIL;
10606 }
10607 }
10608 }
10609
10610 /*
10611 * Local directory.
10612 */
10613 if (wp->w_localdir != NULL)
10614 {
10615 if (fputs("lcd ", fd) < 0
10616 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10617 || put_eol(fd) == FAIL)
10618 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010619 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 }
10621
10622 return OK;
10623}
10624
10625/*
10626 * Write an argument list to the session file.
10627 * Returns FAIL if writing fails.
10628 */
10629 static int
10630ses_arglist(fd, cmd, gap, fullname, flagp)
10631 FILE *fd;
10632 char *cmd;
10633 garray_T *gap;
10634 int fullname; /* TRUE: use full path name */
10635 unsigned *flagp;
10636{
10637 int i;
10638 char_u buf[MAXPATHL];
10639 char_u *s;
10640
10641 if (gap->ga_len == 0)
10642 return put_line(fd, "silent! argdel *");
10643 if (fputs(cmd, fd) < 0)
10644 return FAIL;
10645 for (i = 0; i < gap->ga_len; ++i)
10646 {
10647 /* NULL file names are skipped (only happens when out of memory). */
10648 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10649 if (s != NULL)
10650 {
10651 if (fullname)
10652 {
10653 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10654 s = buf;
10655 }
10656 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10657 return FAIL;
10658 }
10659 }
10660 return put_eol(fd);
10661}
10662
10663/*
10664 * Write a buffer name to the session file.
10665 * Also ends the line.
10666 * Returns FAIL if writing fails.
10667 */
10668 static int
10669ses_fname(fd, buf, flagp)
10670 FILE *fd;
10671 buf_T *buf;
10672 unsigned *flagp;
10673{
10674 char_u *name;
10675
10676 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010677 * the session file will be sourced.
10678 * Don't do this for ":mkview", we don't know the current directory.
10679 * Don't do this after ":lcd", we don't keep track of what the current
10680 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 if (buf->b_sfname != NULL
10682 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010683 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010684#ifdef FEAT_AUTOCHDIR
10685 && !p_acd
10686#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010687 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 name = buf->b_sfname;
10689 else
10690 name = buf->b_ffname;
10691 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10692 return FAIL;
10693 return OK;
10694}
10695
10696/*
10697 * Write a file name to the session file.
10698 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10699 * characters.
10700 * Returns FAIL if writing fails.
10701 */
10702 static int
10703ses_put_fname(fd, name, flagp)
10704 FILE *fd;
10705 char_u *name;
10706 unsigned *flagp;
10707{
10708 char_u *sname;
10709 int retval = OK;
10710 int c;
10711
10712 sname = home_replace_save(NULL, name);
10713 if (sname != NULL)
10714 name = sname;
10715 while (*name != NUL)
10716 {
10717#ifdef FEAT_MBYTE
10718 {
10719 int l;
10720
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010721 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 {
10723 /* copy a multibyte char */
10724 while (--l >= 0)
10725 {
10726 if (putc(*name, fd) != *name)
10727 retval = FAIL;
10728 ++name;
10729 }
10730 continue;
10731 }
10732 }
10733#endif
10734 c = *name++;
10735 if (c == '\\' && (*flagp & SSOP_SLASH))
10736 /* change a backslash to a forward slash */
10737 c = '/';
10738 else if ((vim_strchr(escape_chars, c) != NULL
10739#ifdef BACKSLASH_IN_FILENAME
10740 && c != '\\'
10741#endif
10742 ) || c == '#' || c == '%')
10743 {
10744 /* escape a special character with a backslash */
10745 if (putc('\\', fd) != '\\')
10746 retval = FAIL;
10747 }
10748 if (putc(c, fd) != c)
10749 retval = FAIL;
10750 }
10751 vim_free(sname);
10752 return retval;
10753}
10754
10755/*
10756 * ":loadview [nr]"
10757 */
10758 static void
10759ex_loadview(eap)
10760 exarg_T *eap;
10761{
10762 char_u *fname;
10763
10764 fname = get_view_file(*eap->arg);
10765 if (fname != NULL)
10766 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010767 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 vim_free(fname);
10769 }
10770}
10771
10772/*
10773 * Get the name of the view file for the current buffer.
10774 */
10775 static char_u *
10776get_view_file(c)
10777 int c;
10778{
10779 int len = 0;
10780 char_u *p, *s;
10781 char_u *retval;
10782 char_u *sname;
10783
10784 if (curbuf->b_ffname == NULL)
10785 {
10786 EMSG(_(e_noname));
10787 return NULL;
10788 }
10789 sname = home_replace_save(NULL, curbuf->b_ffname);
10790 if (sname == NULL)
10791 return NULL;
10792
10793 /*
10794 * We want a file name without separators, because we're not going to make
10795 * a directory.
10796 * "normal" path separator -> "=+"
10797 * "=" -> "=="
10798 * ":" path separator -> "=-"
10799 */
10800 for (p = sname; *p; ++p)
10801 if (*p == '=' || vim_ispathsep(*p))
10802 ++len;
10803 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10804 if (retval != NULL)
10805 {
10806 STRCPY(retval, p_vdir);
10807 add_pathsep(retval);
10808 s = retval + STRLEN(retval);
10809 for (p = sname; *p; ++p)
10810 {
10811 if (*p == '=')
10812 {
10813 *s++ = '=';
10814 *s++ = '=';
10815 }
10816 else if (vim_ispathsep(*p))
10817 {
10818 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010819#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 || defined(VMS)
10821 if (*p == ':')
10822 *s++ = '-';
10823 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010825 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010826 }
10827 else
10828 *s++ = *p;
10829 }
10830 *s++ = '=';
10831 *s++ = c;
10832 STRCPY(s, ".vim");
10833 }
10834
10835 vim_free(sname);
10836 return retval;
10837}
10838
10839#endif /* FEAT_SESSION */
10840
10841/*
10842 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10843 * Return FAIL for a write error.
10844 */
10845 int
10846put_eol(fd)
10847 FILE *fd;
10848{
10849 if (
10850#ifdef USE_CRNL
10851 (
10852# ifdef MKSESSION_NL
10853 !mksession_nl &&
10854# endif
10855 (putc('\r', fd) < 0)) ||
10856#endif
10857 (putc('\n', fd) < 0))
10858 return FAIL;
10859 return OK;
10860}
10861
10862/*
10863 * Write a line to "fd".
10864 * Return FAIL for a write error.
10865 */
10866 int
10867put_line(fd, s)
10868 FILE *fd;
10869 char *s;
10870{
10871 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10872 return FAIL;
10873 return OK;
10874}
10875
10876#ifdef FEAT_VIMINFO
10877/*
10878 * ":rviminfo" and ":wviminfo".
10879 */
10880 static void
10881ex_viminfo(eap)
10882 exarg_T *eap;
10883{
10884 char_u *save_viminfo;
10885
10886 save_viminfo = p_viminfo;
10887 if (*p_viminfo == NUL)
10888 p_viminfo = (char_u *)"'100";
10889 if (eap->cmdidx == CMD_rviminfo)
10890 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010891 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10892 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 EMSG(_("E195: Cannot open viminfo file for reading"));
10894 }
10895 else
10896 write_viminfo(eap->arg, eap->forceit);
10897 p_viminfo = save_viminfo;
10898}
10899#endif
10900
10901#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010902/*
10903 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010904 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010905 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 void
10907dialog_msg(buff, format, fname)
10908 char_u *buff;
10909 char *format;
10910 char_u *fname;
10911{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 if (fname == NULL)
10913 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010914 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915}
10916#endif
10917
10918/*
10919 * ":behave {mswin,xterm}"
10920 */
10921 static void
10922ex_behave(eap)
10923 exarg_T *eap;
10924{
10925 if (STRCMP(eap->arg, "mswin") == 0)
10926 {
10927 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10928 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10929 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10930 set_option_value((char_u *)"keymodel", 0L,
10931 (char_u *)"startsel,stopsel", 0);
10932 }
10933 else if (STRCMP(eap->arg, "xterm") == 0)
10934 {
10935 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10936 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10937 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10938 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10939 }
10940 else
10941 EMSG2(_(e_invarg2), eap->arg);
10942}
10943
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010944#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10945/*
10946 * Function given to ExpandGeneric() to obtain the possible arguments of the
10947 * ":behave {mswin,xterm}" command.
10948 */
10949 char_u *
10950get_behave_arg(xp, idx)
10951 expand_T *xp UNUSED;
10952 int idx;
10953{
10954 if (idx == 0)
10955 return (char_u *)"mswin";
10956 if (idx == 1)
10957 return (char_u *)"xterm";
10958 return NULL;
10959}
10960#endif
10961
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962#ifdef FEAT_AUTOCMD
10963static int filetype_detect = FALSE;
10964static int filetype_plugin = FALSE;
10965static int filetype_indent = FALSE;
10966
10967/*
10968 * ":filetype [plugin] [indent] {on,off,detect}"
10969 * on: Load the filetype.vim file to install autocommands for file types.
10970 * off: Load the ftoff.vim file to remove all autocommands for file types.
10971 * plugin on: load filetype.vim and ftplugin.vim
10972 * plugin off: load ftplugof.vim
10973 * indent on: load filetype.vim and indent.vim
10974 * indent off: load indoff.vim
10975 */
10976 static void
10977ex_filetype(eap)
10978 exarg_T *eap;
10979{
10980 char_u *arg = eap->arg;
10981 int plugin = FALSE;
10982 int indent = FALSE;
10983
10984 if (*eap->arg == NUL)
10985 {
10986 /* Print current status. */
10987 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10988 filetype_detect ? "ON" : "OFF",
10989 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10990 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10991 return;
10992 }
10993
10994 /* Accept "plugin" and "indent" in any order. */
10995 for (;;)
10996 {
10997 if (STRNCMP(arg, "plugin", 6) == 0)
10998 {
10999 plugin = TRUE;
11000 arg = skipwhite(arg + 6);
11001 continue;
11002 }
11003 if (STRNCMP(arg, "indent", 6) == 0)
11004 {
11005 indent = TRUE;
11006 arg = skipwhite(arg + 6);
11007 continue;
11008 }
11009 break;
11010 }
11011 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11012 {
11013 if (*arg == 'o' || !filetype_detect)
11014 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011015 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 filetype_detect = TRUE;
11017 if (plugin)
11018 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011019 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 filetype_plugin = TRUE;
11021 }
11022 if (indent)
11023 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011024 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 filetype_indent = TRUE;
11026 }
11027 }
11028 if (*arg == 'd')
11029 {
11030 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011031 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033 }
11034 else if (STRCMP(arg, "off") == 0)
11035 {
11036 if (plugin || indent)
11037 {
11038 if (plugin)
11039 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011040 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 filetype_plugin = FALSE;
11042 }
11043 if (indent)
11044 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011045 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 filetype_indent = FALSE;
11047 }
11048 }
11049 else
11050 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011051 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 filetype_detect = FALSE;
11053 }
11054 }
11055 else
11056 EMSG2(_(e_invarg2), arg);
11057}
11058
11059/*
11060 * ":setfiletype {name}"
11061 */
11062 static void
11063ex_setfiletype(eap)
11064 exarg_T *eap;
11065{
11066 if (!did_filetype)
11067 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11068}
11069#endif
11070
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 static void
11072ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011073 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
11075#ifdef FEAT_DIGRAPHS
11076 if (*eap->arg != NUL)
11077 putdigraph(eap->arg);
11078 else
11079 listdigraphs();
11080#else
11081 EMSG(_("E196: No digraphs in this version"));
11082#endif
11083}
11084
11085 static void
11086ex_set(eap)
11087 exarg_T *eap;
11088{
11089 int flags = 0;
11090
11091 if (eap->cmdidx == CMD_setlocal)
11092 flags = OPT_LOCAL;
11093 else if (eap->cmdidx == CMD_setglobal)
11094 flags = OPT_GLOBAL;
11095#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11096 if (cmdmod.browse && flags == 0)
11097 ex_options(eap);
11098 else
11099#endif
11100 (void)do_set(eap->arg, flags);
11101}
11102
11103#ifdef FEAT_SEARCH_EXTRA
11104/*
11105 * ":nohlsearch"
11106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011107 static void
11108ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011109 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110{
11111 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011112 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011113}
11114
11115/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011116 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 * Sets nextcmd to the start of the next command, if any. Also called when
11118 * skipping commands to find the next command.
11119 */
11120 static void
11121ex_match(eap)
11122 exarg_T *eap;
11123{
11124 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011125 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 char_u *end;
11127 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011128 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011129
11130 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011131 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011132 else
11133 {
11134 EMSG(e_invcmd);
11135 return;
11136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137
11138 /* First clear any old pattern. */
11139 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011140 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141
11142 if (ends_excmd(*eap->arg))
11143 end = eap->arg;
11144 else if ((STRNICMP(eap->arg, "none", 4) == 0
11145 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11146 end = eap->arg + 4;
11147 else
11148 {
11149 p = skiptowhite(eap->arg);
11150 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011151 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011152 p = skipwhite(p);
11153 if (*p == NUL)
11154 {
11155 /* There must be two arguments. */
11156 EMSG2(_(e_invarg2), eap->arg);
11157 return;
11158 }
11159 end = skip_regexp(p + 1, *p, TRUE, NULL);
11160 if (!eap->skip)
11161 {
11162 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11163 {
11164 eap->errmsg = e_trailing;
11165 return;
11166 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011167 if (*end != *p)
11168 {
11169 EMSG2(_(e_invarg2), p);
11170 return;
11171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172
11173 c = *end;
11174 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011175 match_add(curwin, g, p + 1, 10, id);
11176 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011177 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 }
11179 }
11180 eap->nextcmd = find_nextcmd(end);
11181}
11182#endif
11183
11184#ifdef FEAT_CRYPT
11185/*
11186 * ":X": Get crypt key
11187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 static void
11189ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011190 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191{
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011192 if (curbuf->b_p_cm == 0 || blowfish_self_test() == OK)
11193 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194}
11195#endif
11196
11197#ifdef FEAT_FOLDING
11198 static void
11199ex_fold(eap)
11200 exarg_T *eap;
11201{
11202 if (foldManualAllowed(TRUE))
11203 foldCreate(eap->line1, eap->line2);
11204}
11205
11206 static void
11207ex_foldopen(eap)
11208 exarg_T *eap;
11209{
11210 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11211 eap->forceit, FALSE);
11212}
11213
11214 static void
11215ex_folddo(eap)
11216 exarg_T *eap;
11217{
11218 linenr_T lnum;
11219
11220 /* First set the marks for all lines closed/open. */
11221 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11222 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11223 ml_setmarked(lnum);
11224
11225 /* Execute the command on the marked lines. */
11226 global_exe(eap->arg);
11227 ml_clearmarked(); /* clear rest of the marks */
11228}
11229#endif