blob: 2b63c0850597919a1e19ef140704d5ded49ecf32 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
22#ifdef FEAT_USR_CMDS
23typedef struct ucmd
24{
25 char_u *uc_name; /* The command name */
26 long_u uc_argt; /* The argument type */
27 char_u *uc_rep; /* The command's replacement string */
28 long uc_def; /* The default value for a range/count */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int uc_compl; /* completion type */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010030# ifdef FEAT_EVAL
31 scid_T uc_scriptID; /* SID where the command was defined */
32# ifdef FEAT_CMDL_COMPL
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 char_u *uc_compl_arg; /* completion argument if any */
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010034# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000035# endif
36} ucmd_T;
37
38#define UC_BUFFER 1 /* -buffer: local to current buffer */
39
Bram Moolenaar2c29bee2005-06-01 21:46:07 +000040static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42#define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i])
43#define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i])
44
45static void do_ucmd __ARGS((exarg_T *eap));
46static void ex_command __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static void ex_delcommand __ARGS((exarg_T *eap));
48# ifdef FEAT_CMDL_COMPL
49static char_u *get_user_command_name __ARGS((int idx));
50# endif
51
52#else
53# define ex_command ex_ni
54# define ex_comclear ex_ni
55# define ex_delcommand ex_ni
56#endif
57
58#ifdef FEAT_EVAL
Bram Moolenaar89d40322006-08-29 15:30:07 +000059static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#else
Bram Moolenaar89d40322006-08-29 15:30:07 +000061static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie));
Bram Moolenaar071d4272004-06-13 20:20:40 +000062static int if_level = 0; /* depth in :if */
63#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000064static char_u *find_command __ARGS((exarg_T *eap, int *full));
65
66static void ex_abbreviate __ARGS((exarg_T *eap));
67static void ex_map __ARGS((exarg_T *eap));
68static void ex_unmap __ARGS((exarg_T *eap));
69static void ex_mapclear __ARGS((exarg_T *eap));
70static void ex_abclear __ARGS((exarg_T *eap));
71#ifndef FEAT_MENU
72# define ex_emenu ex_ni
73# define ex_menu ex_ni
74# define ex_menutranslate ex_ni
75#endif
76#ifdef FEAT_AUTOCMD
77static void ex_autocmd __ARGS((exarg_T *eap));
78static void ex_doautocmd __ARGS((exarg_T *eap));
79#else
80# define ex_autocmd ex_ni
81# define ex_doautocmd ex_ni
82# define ex_doautoall ex_ni
83#endif
84#ifdef FEAT_LISTCMDS
85static void ex_bunload __ARGS((exarg_T *eap));
86static void ex_buffer __ARGS((exarg_T *eap));
87static void ex_bmodified __ARGS((exarg_T *eap));
88static void ex_bnext __ARGS((exarg_T *eap));
89static void ex_bprevious __ARGS((exarg_T *eap));
90static void ex_brewind __ARGS((exarg_T *eap));
91static void ex_blast __ARGS((exarg_T *eap));
92#else
93# define ex_bunload ex_ni
94# define ex_buffer ex_ni
95# define ex_bmodified ex_ni
96# define ex_bnext ex_ni
97# define ex_bprevious ex_ni
98# define ex_brewind ex_ni
99# define ex_blast ex_ni
100# define buflist_list ex_ni
101# define ex_checktime ex_ni
102#endif
103#if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS)
104# define ex_buffer_all ex_ni
105#endif
106static char_u *getargcmd __ARGS((char_u **));
107static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs));
108static int getargopt __ARGS((exarg_T *eap));
109#ifndef FEAT_QUICKFIX
110# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000111# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112# define ex_cc ex_ni
113# define ex_cnext ex_ni
114# define ex_cfile ex_ni
115# define qf_list ex_ni
116# define qf_age ex_ni
117# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +0000118# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#endif
120#if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS)
121# define ex_cclose ex_ni
122# define ex_copen ex_ni
123# define ex_cwindow ex_ni
124#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +0000125#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
126# define ex_cexpr ex_ni
127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129static int check_more __ARGS((int, int));
130static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000131static void get_flags __ARGS((exarg_T *eap));
Bram Moolenaar85363ab2010-07-18 13:58:26 +0200132#if !defined(FEAT_PERL) \
133 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
134 || !defined(FEAT_TCL) \
135 || !defined(FEAT_RUBY) \
136 || !defined(FEAT_LUA) \
137 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +0000138# define HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139static void ex_script_ni __ARGS((exarg_T *eap));
140#endif
141static char_u *invalid_range __ARGS((exarg_T *eap));
142static void correct_range __ARGS((exarg_T *eap));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000143#ifdef FEAT_QUICKFIX
144static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep));
145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep));
147static void ex_highlight __ARGS((exarg_T *eap));
148static void ex_colorscheme __ARGS((exarg_T *eap));
149static void ex_quit __ARGS((exarg_T *eap));
150static void ex_cquit __ARGS((exarg_T *eap));
151static void ex_quit_all __ARGS((exarg_T *eap));
152#ifdef FEAT_WINDOWS
153static void ex_close __ARGS((exarg_T *eap));
Bram Moolenaarf740b292006-02-16 22:11:02 +0000154static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ex_only __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156static void ex_resize __ARGS((exarg_T *eap));
157static void ex_stag __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000158static void ex_tabclose __ARGS((exarg_T *eap));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000159static void ex_tabonly __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000160static void ex_tabnext __ARGS((exarg_T *eap));
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000161static void ex_tabmove __ARGS((exarg_T *eap));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162static void ex_tabs __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#else
164# define ex_close ex_ni
165# define ex_only ex_ni
166# define ex_all ex_ni
167# define ex_resize ex_ni
168# define ex_splitview ex_ni
169# define ex_stag ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000170# define ex_tabnext ex_ni
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000171# define ex_tabmove ex_ni
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000172# define ex_tabs ex_ni
173# define ex_tabclose ex_ni
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000174# define ex_tabonly ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175#endif
176#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
177static void ex_pclose __ARGS((exarg_T *eap));
178static void ex_ptag __ARGS((exarg_T *eap));
179static void ex_pedit __ARGS((exarg_T *eap));
180#else
181# define ex_pclose ex_ni
182# define ex_ptag ex_ni
183# define ex_pedit ex_ni
184#endif
185static void ex_hide __ARGS((exarg_T *eap));
186static void ex_stop __ARGS((exarg_T *eap));
187static void ex_exit __ARGS((exarg_T *eap));
188static void ex_print __ARGS((exarg_T *eap));
189#ifdef FEAT_BYTEOFF
190static void ex_goto __ARGS((exarg_T *eap));
191#else
192# define ex_goto ex_ni
193#endif
194static void ex_shell __ARGS((exarg_T *eap));
195static void ex_preserve __ARGS((exarg_T *eap));
196static void ex_recover __ARGS((exarg_T *eap));
197#ifndef FEAT_LISTCMDS
198# define ex_argedit ex_ni
199# define ex_argadd ex_ni
200# define ex_argdelete ex_ni
201# define ex_listdo ex_ni
202#endif
203static void ex_mode __ARGS((exarg_T *eap));
204static void ex_wrongmodifier __ARGS((exarg_T *eap));
205static void ex_find __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000206static void ex_open __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207static void ex_edit __ARGS((exarg_T *eap));
208#if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER)
209# define ex_drop ex_ni
210#endif
211#ifndef FEAT_GUI
212# define ex_gui ex_nogui
213static void ex_nogui __ARGS((exarg_T *eap));
214#endif
215#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
216static void ex_tearoff __ARGS((exarg_T *eap));
217#else
218# define ex_tearoff ex_ni
219#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000220#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221static void ex_popup __ARGS((exarg_T *eap));
222#else
223# define ex_popup ex_ni
224#endif
225#ifndef FEAT_GUI_MSWIN
226# define ex_simalt ex_ni
227#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000228#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229# define gui_mch_find_dialog ex_ni
230# define gui_mch_replace_dialog ex_ni
231#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000232#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233# define ex_helpfind ex_ni
234#endif
235#ifndef FEAT_CSCOPE
236# define do_cscope ex_ni
237# define do_scscope ex_ni
238# define do_cstag ex_ni
239#endif
240#ifndef FEAT_SYN_HL
241# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200242# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000243#endif
244#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000245# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000246# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000247# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000248# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000249# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000250#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200251#ifndef FEAT_PERSISTENT_UNDO
252# define ex_rundo ex_ni
253# define ex_wundo ex_ni
254#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200255#ifndef FEAT_LUA
256# define ex_lua ex_script_ni
257# define ex_luado ex_ni
258# define ex_luafile ex_ni
259#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000260#ifndef FEAT_MZSCHEME
261# define ex_mzscheme ex_script_ni
262# define ex_mzfile ex_ni
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_PERL
265# define ex_perl ex_script_ni
266# define ex_perldo ex_ni
267#endif
268#ifndef FEAT_PYTHON
269# define ex_python ex_script_ni
270# define ex_pyfile ex_ni
271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200272#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200273# define ex_py3 ex_script_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200274# define ex_py3file ex_ni
275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276#ifndef FEAT_TCL
277# define ex_tcl ex_script_ni
278# define ex_tcldo ex_ni
279# define ex_tclfile ex_ni
280#endif
281#ifndef FEAT_RUBY
282# define ex_ruby ex_script_ni
283# define ex_rubydo ex_ni
284# define ex_rubyfile ex_ni
285#endif
286#ifndef FEAT_SNIFF
287# define ex_sniff ex_ni
288#endif
289#ifndef FEAT_KEYMAP
290# define ex_loadkeymap ex_ni
291#endif
292static void ex_swapname __ARGS((exarg_T *eap));
293static void ex_syncbind __ARGS((exarg_T *eap));
294static void ex_read __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295static void ex_pwd __ARGS((exarg_T *eap));
296static void ex_equal __ARGS((exarg_T *eap));
297static void ex_sleep __ARGS((exarg_T *eap));
298static void do_exmap __ARGS((exarg_T *eap, int isabbrev));
299static void ex_winsize __ARGS((exarg_T *eap));
300#ifdef FEAT_WINDOWS
301static void ex_wincmd __ARGS((exarg_T *eap));
302#else
303# define ex_wincmd ex_ni
304#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000305#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306static void ex_winpos __ARGS((exarg_T *eap));
307#else
308# define ex_winpos ex_ni
309#endif
310static void ex_operators __ARGS((exarg_T *eap));
311static void ex_put __ARGS((exarg_T *eap));
312static void ex_copymove __ARGS((exarg_T *eap));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000313static void ex_may_print __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314static void ex_submagic __ARGS((exarg_T *eap));
315static void ex_join __ARGS((exarg_T *eap));
316static void ex_at __ARGS((exarg_T *eap));
317static void ex_bang __ARGS((exarg_T *eap));
318static void ex_undo __ARGS((exarg_T *eap));
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200319#ifdef FEAT_PERSISTENT_UNDO
320static void ex_wundo __ARGS((exarg_T *eap));
321static void ex_rundo __ARGS((exarg_T *eap));
322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323static void ex_redo __ARGS((exarg_T *eap));
Bram Moolenaarc7d89352006-03-14 22:53:34 +0000324static void ex_later __ARGS((exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325static void ex_redir __ARGS((exarg_T *eap));
326static void ex_redraw __ARGS((exarg_T *eap));
327static void ex_redrawstatus __ARGS((exarg_T *eap));
328static void close_redir __ARGS((void));
329static void ex_mkrc __ARGS((exarg_T *eap));
330static void ex_mark __ARGS((exarg_T *eap));
331#ifdef FEAT_USR_CMDS
332static char_u *uc_fun_cmd __ARGS((void));
Bram Moolenaar52b4b552005-03-07 23:00:57 +0000333static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#endif
335#ifdef FEAT_EX_EXTRA
336static void ex_normal __ARGS((exarg_T *eap));
337static void ex_startinsert __ARGS((exarg_T *eap));
338static void ex_stopinsert __ARGS((exarg_T *eap));
339#else
340# define ex_normal ex_ni
341# define ex_align ex_ni
342# define ex_retab ex_ni
343# define ex_startinsert ex_ni
344# define ex_stopinsert ex_ni
345# define ex_helptags ex_ni
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +0000346# define ex_sort ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
348#ifdef FEAT_FIND_ID
349static void ex_checkpath __ARGS((exarg_T *eap));
350static void ex_findpat __ARGS((exarg_T *eap));
351#else
352# define ex_findpat ex_ni
353# define ex_checkpath ex_ni
354#endif
355#if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
356static void ex_psearch __ARGS((exarg_T *eap));
357#else
358# define ex_psearch ex_ni
359#endif
360static void ex_tag __ARGS((exarg_T *eap));
361static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name));
362#ifndef FEAT_EVAL
363# define ex_scriptnames ex_ni
364# define ex_finish ex_ni
365# define ex_echo ex_ni
366# define ex_echohl ex_ni
367# define ex_execute ex_ni
368# define ex_call ex_ni
369# define ex_if ex_ni
370# define ex_endif ex_ni
371# define ex_else ex_ni
372# define ex_while ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000373# define ex_for ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374# define ex_continue ex_ni
375# define ex_break ex_ni
376# define ex_endwhile ex_ni
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000377# define ex_endfor ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378# define ex_throw ex_ni
379# define ex_try ex_ni
380# define ex_catch ex_ni
381# define ex_finally ex_ni
382# define ex_endtry ex_ni
383# define ex_endfunction ex_ni
384# define ex_let ex_ni
385# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000386# define ex_lockvar ex_ni
387# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# define ex_function ex_ni
389# define ex_delfunction ex_ni
390# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000391# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392#endif
393static char_u *arg_all __ARGS((void));
394#ifdef FEAT_SESSION
395static int makeopens __ARGS((FILE *fd, char_u *dirnow));
Bram Moolenaarf13be0d2008-01-02 14:13:10 +0000396static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397static void ex_loadview __ARGS((exarg_T *eap));
398static char_u *get_view_file __ARGS((int c));
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000399static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400#else
401# define ex_loadview ex_ni
402#endif
403#ifndef FEAT_EVAL
404# define ex_compiler ex_ni
405#endif
406#ifdef FEAT_VIMINFO
407static void ex_viminfo __ARGS((exarg_T *eap));
408#else
409# define ex_viminfo ex_ni
410#endif
411static void ex_behave __ARGS((exarg_T *eap));
412#ifdef FEAT_AUTOCMD
413static void ex_filetype __ARGS((exarg_T *eap));
414static void ex_setfiletype __ARGS((exarg_T *eap));
415#else
416# define ex_filetype ex_ni
417# define ex_setfiletype ex_ni
418#endif
419#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000420# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421# define ex_diffpatch ex_ni
422# define ex_diffgetput ex_ni
423# define ex_diffsplit ex_ni
424# define ex_diffthis ex_ni
425# define ex_diffupdate ex_ni
426#endif
427static void ex_digraphs __ARGS((exarg_T *eap));
428static void ex_set __ARGS((exarg_T *eap));
429#if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD)
430# define ex_options ex_ni
431#endif
432#ifdef FEAT_SEARCH_EXTRA
433static void ex_nohlsearch __ARGS((exarg_T *eap));
434static void ex_match __ARGS((exarg_T *eap));
435#else
436# define ex_nohlsearch ex_ni
437# define ex_match ex_ni
438#endif
439#ifdef FEAT_CRYPT
440static void ex_X __ARGS((exarg_T *eap));
441#else
442# define ex_X ex_ni
443#endif
444#ifdef FEAT_FOLDING
445static void ex_fold __ARGS((exarg_T *eap));
446static void ex_foldopen __ARGS((exarg_T *eap));
447static void ex_folddo __ARGS((exarg_T *eap));
448#else
449# define ex_fold ex_ni
450# define ex_foldopen ex_ni
451# define ex_folddo ex_ni
452#endif
453#if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
454 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)))
455# define ex_language ex_ni
456#endif
457#ifndef FEAT_SIGNS
458# define ex_sign ex_ni
459#endif
460#ifndef FEAT_SUN_WORKSHOP
461# define ex_wsverb ex_ni
462#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000463#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200464# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000465# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200466# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468
469#ifndef FEAT_EVAL
470# define ex_debug ex_ni
471# define ex_breakadd ex_ni
472# define ex_debuggreedy ex_ni
473# define ex_breakdel ex_ni
474# define ex_breaklist ex_ni
475#endif
476
477#ifndef FEAT_CMDHIST
478# define ex_history ex_ni
479#endif
480#ifndef FEAT_JUMPLIST
481# define ex_jumps ex_ni
482# define ex_changes ex_ni
483#endif
484
Bram Moolenaar05159a02005-02-26 23:04:13 +0000485#ifndef FEAT_PROFILE
486# define ex_profile ex_ni
487#endif
488
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489/*
490 * Declare cmdnames[].
491 */
492#define DO_DECLARE_EXCMD
493#include "ex_cmds.h"
494
495/*
496 * Table used to quickly search for a command, based on its first character.
497 */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +0000498static cmdidx_T cmdidxs[27] =
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 CMD_append,
501 CMD_buffer,
502 CMD_change,
503 CMD_delete,
504 CMD_edit,
505 CMD_file,
506 CMD_global,
507 CMD_help,
508 CMD_insert,
509 CMD_join,
510 CMD_k,
511 CMD_list,
512 CMD_move,
513 CMD_next,
514 CMD_open,
515 CMD_print,
516 CMD_quit,
517 CMD_read,
518 CMD_substitute,
519 CMD_t,
520 CMD_undo,
521 CMD_vglobal,
522 CMD_write,
523 CMD_xit,
524 CMD_yank,
525 CMD_z,
526 CMD_bang
527};
528
529static char_u dollar_command[2] = {'$', 0};
530
531
532#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000533/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534typedef struct
535{
536 char_u *line; /* command line */
537 linenr_T lnum; /* sourcing_lnum of the line */
538} wcmd_T;
539
540/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000541 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000543 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000545struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546{
547 garray_T *lines_gap; /* growarray with line info */
548 int current_line; /* last read line from growarray */
549 int repeating; /* TRUE when looping a second time */
550 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
551 char_u *(*getline) __ARGS((int, void *, int));
552 void *cookie;
553};
554
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000555static char_u *get_loop_line __ARGS((int c, void *cookie, int indent));
556static int store_loop_line __ARGS((garray_T *gap, char_u *line));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557static void free_cmdlines __ARGS((garray_T *gap));
Bram Moolenaared203462004-06-16 11:19:22 +0000558
559/* Struct to save a few things while debugging. Used in do_cmdline() only. */
560struct dbg_stuff
561{
562 int trylevel;
563 int force_abort;
564 except_T *caught_stack;
565 char_u *vv_exception;
566 char_u *vv_throwpoint;
567 int did_emsg;
568 int got_int;
569 int did_throw;
570 int need_rethrow;
571 int check_cstack;
572 except_T *current_exception;
573};
574
575static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp));
576static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp));
577
578 static void
579save_dbg_stuff(dsp)
580 struct dbg_stuff *dsp;
581{
582 dsp->trylevel = trylevel; trylevel = 0;
583 dsp->force_abort = force_abort; force_abort = FALSE;
584 dsp->caught_stack = caught_stack; caught_stack = NULL;
585 dsp->vv_exception = v_exception(NULL);
586 dsp->vv_throwpoint = v_throwpoint(NULL);
587
588 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
589 dsp->did_emsg = did_emsg; did_emsg = FALSE;
590 dsp->got_int = got_int; got_int = FALSE;
591 dsp->did_throw = did_throw; did_throw = FALSE;
592 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
593 dsp->check_cstack = check_cstack; check_cstack = FALSE;
594 dsp->current_exception = current_exception; current_exception = NULL;
595}
596
597 static void
598restore_dbg_stuff(dsp)
599 struct dbg_stuff *dsp;
600{
601 suppress_errthrow = FALSE;
602 trylevel = dsp->trylevel;
603 force_abort = dsp->force_abort;
604 caught_stack = dsp->caught_stack;
605 (void)v_exception(dsp->vv_exception);
606 (void)v_throwpoint(dsp->vv_throwpoint);
607 did_emsg = dsp->did_emsg;
608 got_int = dsp->got_int;
609 did_throw = dsp->did_throw;
610 need_rethrow = dsp->need_rethrow;
611 check_cstack = dsp->check_cstack;
612 current_exception = dsp->current_exception;
613}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614#endif
615
616
617/*
618 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
619 * command is given.
620 */
621 void
622do_exmode(improved)
623 int improved; /* TRUE for "improved Ex" mode */
624{
625 int save_msg_scroll;
626 int prev_msg_row;
627 linenr_T prev_line;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000628 int changedtick;
629
630 if (improved)
631 exmode_active = EXMODE_VIM;
632 else
633 exmode_active = EXMODE_NORMAL;
634 State = NORMAL;
635
636 /* When using ":global /pat/ visual" and then "Q" we return to continue
637 * the :global command. */
638 if (global_busy)
639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
641 save_msg_scroll = msg_scroll;
642 ++RedrawingDisabled; /* don't redisplay the window */
643 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_GUI
645 /* Ignore scrollbar and mouse events in Ex mode */
646 ++hold_gui_events;
647#endif
648#ifdef FEAT_SNIFF
649 want_sniff_request = 0; /* No K_SNIFF wanted */
650#endif
651
652 MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
653 while (exmode_active)
654 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000655#ifdef FEAT_EX_EXTRA
656 /* Check for a ":normal" command and no more characters left. */
657 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
658 {
659 exmode_active = FALSE;
660 break;
661 }
662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 msg_scroll = TRUE;
664 need_wait_return = FALSE;
665 ex_pressedreturn = FALSE;
666 ex_no_reprint = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000667 changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 prev_msg_row = msg_row;
669 prev_line = curwin->w_cursor.lnum;
670#ifdef FEAT_SNIFF
671 ProcessSniffRequests();
672#endif
673 if (improved)
674 {
675 cmdline_row = msg_row;
676 do_cmdline(NULL, getexline, NULL, 0);
677 }
678 else
679 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
680 lines_left = Rows - 1;
681
Bram Moolenaardf177f62005-02-22 08:39:57 +0000682 if ((prev_line != curwin->w_cursor.lnum
683 || changedtick != curbuf->b_changedtick) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000685 if (curbuf->b_ml.ml_flags & ML_EMPTY)
686 EMSG(_(e_emptybuf));
687 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000689 if (ex_pressedreturn)
690 {
691 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000692 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 msg_row = prev_msg_row;
694 if (prev_msg_row == Rows - 1)
695 msg_row--;
696 }
697 msg_col = 0;
698 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
699 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000702 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
703 {
704 if (curbuf->b_ml.ml_flags & ML_EMPTY)
705 EMSG(_(e_emptybuf));
706 else
707 EMSG(_("E501: At end-of-file"));
708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710
711#ifdef FEAT_GUI
712 --hold_gui_events;
713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 --RedrawingDisabled;
715 --no_wait_return;
716 update_screen(CLEAR);
717 need_wait_return = FALSE;
718 msg_scroll = save_msg_scroll;
719}
720
721/*
722 * Execute a simple command line. Used for translated commands like "*".
723 */
724 int
725do_cmdline_cmd(cmd)
726 char_u *cmd;
727{
728 return do_cmdline(cmd, NULL, NULL,
729 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
730}
731
732/*
733 * do_cmdline(): execute one Ex command line
734 *
735 * 1. Execute "cmdline" when it is not NULL.
736 * If "cmdline" is NULL, or more lines are needed, getline() is used.
737 * 2. Split up in parts separated with '|'.
738 *
739 * This function can be called recursively!
740 *
741 * flags:
742 * DOCMD_VERBOSE - The command will be included in the error message.
743 * DOCMD_NOWAIT - Don't call wait_return() and friends.
744 * DOCMD_REPEAT - Repeat execution until getline() returns NULL.
745 * DOCMD_KEYTYPED - Don't reset KeyTyped.
746 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
747 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
748 *
749 * return FAIL if cmdline could not be executed, OK otherwise
750 */
751 int
752do_cmdline(cmdline, getline, cookie, flags)
753 char_u *cmdline;
754 char_u *(*getline) __ARGS((int, void *, int));
755 void *cookie; /* argument for getline() */
756 int flags;
757{
758 char_u *next_cmdline; /* next cmd to execute */
759 char_u *cmdline_copy = NULL; /* copy of cmd line */
760 int used_getline = FALSE; /* used "getline" to obtain command */
761 static int recursive = 0; /* recursive depth */
762 int msg_didout_before_start = 0;
763 int count = 0; /* line number count */
764 int did_inc = FALSE; /* incremented RedrawingDisabled */
765 int retval = OK;
766#ifdef FEAT_EVAL
767 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000768 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 int current_line = 0; /* active line in lines_ga */
770 char_u *fname = NULL; /* function or script name */
771 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
772 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000773 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 int initial_trylevel;
775 struct msglist **saved_msg_list = NULL;
776 struct msglist *private_msg_list;
777
778 /* "getline" and "cookie" passed to do_one_cmd() */
779 char_u *(*cmd_getline) __ARGS((int, void *, int));
780 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784#else
785# define cmd_getline getline
786# define cmd_cookie cookie
787#endif
788 static int call_depth = 0; /* recursiveness */
789
790#ifdef FEAT_EVAL
791 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
792 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000793 * This ensures that the do_errthrow() call in do_one_cmd() does not
794 * combine the messages stored by an earlier invocation of do_one_cmd()
795 * with the command name of the later one. This would happen when
796 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 saved_msg_list = msg_list;
798 msg_list = &private_msg_list;
799 private_msg_list = NULL;
800#endif
801
802 /* It's possible to create an endless loop with ":execute", catch that
803 * here. The value of 200 allows nested function calls, ":source", etc. */
804 if (call_depth == 200)
805 {
806 EMSG(_("E169: Command too recursive"));
807#ifdef FEAT_EVAL
808 /* When converting to an exception, we do not include the command name
809 * since this is not an error of the specific command. */
810 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
811 msg_list = saved_msg_list;
812#endif
813 return FAIL;
814 }
815 ++call_depth;
816
817#ifdef FEAT_EVAL
818 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000819 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 cstack.cs_trylevel = 0;
821 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000822 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
824
825 real_cookie = getline_cookie(getline, cookie);
826
827 /* Inside a function use a higher nesting level. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828 getline_is_func = getline_equal(getline, cookie, get_func_line);
829 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 ++ex_nesting_level;
831
832 /* Get the function or script name and the address where the next breakpoint
833 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000834 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
836 fname = func_name(real_cookie);
837 breakpoint = func_breakpoint(real_cookie);
838 dbg_tick = func_dbg_tick(real_cookie);
839 }
840 else if (getline_equal(getline, cookie, getsourceline))
841 {
842 fname = sourcing_name;
843 breakpoint = source_breakpoint(real_cookie);
844 dbg_tick = source_dbg_tick(real_cookie);
845 }
846
847 /*
848 * Initialize "force_abort" and "suppress_errthrow" at the top level.
849 */
850 if (!recursive)
851 {
852 force_abort = FALSE;
853 suppress_errthrow = FALSE;
854 }
855
856 /*
857 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000858 * exception handling (used when debugging). Otherwise clear it to avoid
859 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000861 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000862 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000863 else
Bram Moolenaar7db5fc82010-05-24 11:59:29 +0200864 vim_memset(&debug_saved, 0, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 initial_trylevel = trylevel;
867
868 /*
869 * "did_throw" will be set to TRUE when an exception is being thrown.
870 */
871 did_throw = FALSE;
872#endif
873 /*
874 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000875 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 * If force_abort is set, we cancel everything.
877 */
878 did_emsg = FALSE;
879
880 /*
881 * KeyTyped is only set when calling vgetc(). Reset it here when not
882 * calling vgetc() (sourced command lines).
883 */
884 if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline))
885 KeyTyped = FALSE;
886
887 /*
888 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000889 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 * - for multiple commands on one line, separated with '|'
891 * - when repeating until there are no more lines (for ":source")
892 */
893 next_cmdline = cmdline;
894 do
895 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000896#ifdef FEAT_EVAL
897 getline_is_func = getline_equal(getline, cookie, get_func_line);
898#endif
899
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000900 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (next_cmdline == NULL
902#ifdef FEAT_EVAL
903 && !force_abort
904 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000905 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#endif
907 )
908 did_emsg = FALSE;
909
910 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000911 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 * 2. If no line given: Get an allocated line with getline().
913 * 3. If a line is given: Make a copy, so we can mess with it.
914 */
915
916#ifdef FEAT_EVAL
917 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 /* Each '|' separated command is stored separately in lines_ga, to
921 * be able to jump to it. Don't use next_cmdline now. */
922 vim_free(cmdline_copy);
923 cmdline_copy = NULL;
924
925 /* Check if a function has returned or, unless it has an unclosed
926 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000927 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000929# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000930 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000931 func_line_end(real_cookie);
932# endif
933 if (func_has_ended(real_cookie))
934 {
935 retval = FAIL;
936 break;
937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000939#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000940 else if (do_profiling == PROF_YES
Bram Moolenaar05159a02005-02-26 23:04:13 +0000941 && getline_equal(getline, cookie, getsourceline))
942 script_line_end();
943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945 /* Check if a sourced file hit a ":finish" command. */
946 if (source_finished(getline, cookie))
947 {
948 retval = FAIL;
949 break;
950 }
951
952 /* If breakpoints have been added/deleted need to check for it. */
953 if (breakpoint != NULL && dbg_tick != NULL
954 && *dbg_tick != debug_tick)
955 {
956 *breakpoint = dbg_find_breakpoint(
957 getline_equal(getline, cookie, getsourceline),
958 fname, sourcing_lnum);
959 *dbg_tick = debug_tick;
960 }
961
962 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
963 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
964
965 /* Did we encounter a breakpoint? */
966 if (breakpoint != NULL && *breakpoint != 0
967 && *breakpoint <= sourcing_lnum)
968 {
969 dbg_breakpoint(fname, sourcing_lnum);
970 /* Find next breakpoint. */
971 *breakpoint = dbg_find_breakpoint(
972 getline_equal(getline, cookie, getsourceline),
973 fname, sourcing_lnum);
974 *dbg_tick = debug_tick;
975 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000976# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000978 {
979 if (getline_is_func)
980 func_line_start(real_cookie);
981 else if (getline_equal(getline, cookie, getsourceline))
982 script_line_start();
983 }
984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 }
986
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 * again. Pass a different "getline" function to do_one_cmd()
991 * below, so that it stores lines in or reads them from
992 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 * while/for loop. */
994 cmd_getline = get_loop_line;
995 cmd_cookie = (void *)&cmd_loop_cookie;
996 cmd_loop_cookie.lines_gap = &lines_ga;
997 cmd_loop_cookie.current_line = current_line;
998 cmd_loop_cookie.getline = getline;
999 cmd_loop_cookie.cookie = cookie;
1000 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002 else
1003 {
1004 cmd_getline = getline;
1005 cmd_cookie = cookie;
1006 }
1007#endif
1008
1009 /* 2. If no line given, get an allocated line with getline(). */
1010 if (next_cmdline == NULL)
1011 {
1012 /*
1013 * Need to set msg_didout for the first line after an ":if",
1014 * otherwise the ":if" will be overwritten.
1015 */
1016 if (count == 1 && getline_equal(getline, cookie, getexline))
1017 msg_didout = TRUE;
1018 if (getline == NULL || (next_cmdline = getline(':', cookie,
1019#ifdef FEAT_EVAL
1020 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
1021#else
1022 0
1023#endif
1024 )) == NULL)
1025 {
1026 /* Don't call wait_return for aborted command line. The NULL
1027 * returned for the end of a sourced file or executed function
1028 * doesn't do this. */
1029 if (KeyTyped && !(flags & DOCMD_REPEAT))
1030 need_wait_return = FALSE;
1031 retval = FAIL;
1032 break;
1033 }
1034 used_getline = TRUE;
1035
1036 /*
1037 * Keep the first typed line. Clear it when more lines are typed.
1038 */
1039 if (flags & DOCMD_KEEPLINE)
1040 {
1041 vim_free(repeat_cmdline);
1042 if (count == 0)
1043 repeat_cmdline = vim_strsave(next_cmdline);
1044 else
1045 repeat_cmdline = NULL;
1046 }
1047 }
1048
1049 /* 3. Make a copy of the command so we can mess with it. */
1050 else if (cmdline_copy == NULL)
1051 {
1052 next_cmdline = vim_strsave(next_cmdline);
1053 if (next_cmdline == NULL)
1054 {
1055 EMSG(_(e_outofmem));
1056 retval = FAIL;
1057 break;
1058 }
1059 }
1060 cmdline_copy = next_cmdline;
1061
1062#ifdef FEAT_EVAL
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * Save the current line when inside a ":while" or ":for", and when
1065 * the command looks like a ":while" or ":for", because we may need it
1066 * later. When there is a '|' and another command, it is stored
1067 * separately, because we need to be able to jump back to it from an
1068 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 if (current_line == lines_ga.ga_len
1071 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
1075 retval = FAIL;
1076 break;
1077 }
1078 }
1079 did_endif = FALSE;
1080#endif
1081
1082 if (count++ == 0)
1083 {
1084 /*
1085 * All output from the commands is put below each other, without
1086 * waiting for a return. Don't do this when executing commands
1087 * from a script or when being called recursive (e.g. for ":e
1088 * +command file").
1089 */
1090 if (!(flags & DOCMD_NOWAIT) && !recursive)
1091 {
1092 msg_didout_before_start = msg_didout;
1093 msg_didany = FALSE; /* no output yet */
1094 msg_start();
1095 msg_scroll = TRUE; /* put messages below each other */
1096 ++no_wait_return; /* dont wait for return until finished */
1097 ++RedrawingDisabled;
1098 did_inc = TRUE;
1099 }
1100 }
1101
1102 if (p_verbose >= 15 && sourcing_name != NULL)
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001105 verbose_enter_scroll();
1106
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 smsg((char_u *)_("line %ld: %s"),
1108 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +00001109 if (msg_silent == 0)
1110 msg_puts((char_u *)"\n"); /* don't overwrite this */
1111
1112 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 --no_wait_return;
1114 }
1115
1116 /*
1117 * 2. Execute one '|' separated command.
1118 * do_one_cmd() will return NULL if there is no trailing '|'.
1119 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1120 */
1121 ++recursive;
1122 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
1123#ifdef FEAT_EVAL
1124 &cstack,
1125#endif
1126 cmd_getline, cmd_cookie);
1127 --recursive;
1128
1129#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001130 if (cmd_cookie == (void *)&cmd_loop_cookie)
1131 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135
1136 if (next_cmdline == NULL)
1137 {
1138 vim_free(cmdline_copy);
1139 cmdline_copy = NULL;
1140#ifdef FEAT_CMDHIST
1141 /*
1142 * If the command was typed, remember it for the ':' register.
1143 * Do this AFTER executing the command to make :@: work.
1144 */
1145 if (getline_equal(getline, cookie, getexline)
1146 && new_last_cmdline != NULL)
1147 {
1148 vim_free(last_cmdline);
1149 last_cmdline = new_last_cmdline;
1150 new_last_cmdline = NULL;
1151 }
1152#endif
1153 }
1154 else
1155 {
1156 /* need to copy the command after the '|' to cmdline_copy, for the
1157 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001158 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 next_cmdline = cmdline_copy;
1160 }
1161
1162
1163#ifdef FEAT_EVAL
1164 /* reset did_emsg for a function that is not aborted by an error */
1165 if (did_emsg && !force_abort
1166 && getline_equal(getline, cookie, get_func_line)
1167 && !func_has_abort(real_cookie))
1168 did_emsg = FALSE;
1169
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001170 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 {
1172 ++current_line;
1173
1174 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * An ":endwhile", ":endfor" and ":continue" is handled here.
1176 * If we were executing commands, jump back to the ":while" or
1177 * ":for".
1178 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001180 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001182 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001184 /* Jump back to the matching ":while" or ":for". Be careful
1185 * not to use a cs_line[] from an entry that isn't a ":while"
1186 * or ":for": It would make "current_line" invalid and can
1187 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 if (!did_emsg && !got_int && !did_throw
1189 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001190 && (cstack.cs_flags[cstack.cs_idx]
1191 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && cstack.cs_line[cstack.cs_idx] >= 0
1193 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1194 {
1195 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001196 /* remember we jumped there */
1197 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 line_breakcheck(); /* check if CTRL-C typed */
1199
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001200 /* Check for the next breakpoint at or after the ":while"
1201 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (breakpoint != NULL)
1203 {
1204 *breakpoint = dbg_find_breakpoint(
1205 getline_equal(getline, cookie, getsourceline),
1206 fname,
1207 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1208 *dbg_tick = debug_tick;
1209 }
1210 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001211 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001213 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1216 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 }
1219
1220 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001221 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001223 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1227 }
1228 }
1229
1230 /*
1231 * When not inside any ":while" loop, clear remembered lines.
1232 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 {
1235 if (lines_ga.ga_len > 0)
1236 {
1237 sourcing_lnum =
1238 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1239 free_cmdlines(&lines_ga);
1240 }
1241 current_line = 0;
1242 }
1243
1244 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001245 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1246 * being restored at the ":endtry". Reset them here and set the
1247 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1248 * This includes the case where a missing ":endif", ":endwhile" or
1249 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001251 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001253 cstack.cs_lflags &= ~CSL_HAD_FINA;
1254 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1255 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 did_throw ? (void *)current_exception : NULL);
1257 did_emsg = got_int = did_throw = FALSE;
1258 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1259 }
1260
1261 /* Update global "trylevel" for recursive calls to do_cmdline() from
1262 * within this loop. */
1263 trylevel = initial_trylevel + cstack.cs_trylevel;
1264
1265 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001266 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 * files) is aborted because of an error, an interrupt, or an uncaught
1268 * exception, cancel everything. If it is left normally, reset
1269 * force_abort to get the non-EH compatible abortion behavior for
1270 * the rest of the script.
1271 */
1272 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1273 force_abort = FALSE;
1274
1275 /* Convert an interrupt to an exception if appropriate. */
1276 (void)do_intthrow(&cstack);
1277#endif /* FEAT_EVAL */
1278
1279 }
1280 /*
1281 * Continue executing command lines when:
1282 * - no CTRL-C typed, no aborting error, no exception thrown or try
1283 * conditionals need to be checked for executing finally clauses or
1284 * catching an interrupt exception
1285 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001286 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 * looping for ":source" command or function call.
1288 */
1289 while (!((got_int
1290#ifdef FEAT_EVAL
1291 || (did_emsg && force_abort) || did_throw
1292#endif
1293 )
1294#ifdef FEAT_EVAL
1295 && cstack.cs_trylevel == 0
1296#endif
1297 )
1298 && !(did_emsg && used_getline
1299 && (getline_equal(getline, cookie, getexmodeline)
1300 || getline_equal(getline, cookie, getexline)))
1301 && (next_cmdline != NULL
1302#ifdef FEAT_EVAL
1303 || cstack.cs_idx >= 0
1304#endif
1305 || (flags & DOCMD_REPEAT)));
1306
1307 vim_free(cmdline_copy);
1308#ifdef FEAT_EVAL
1309 free_cmdlines(&lines_ga);
1310 ga_clear(&lines_ga);
1311
1312 if (cstack.cs_idx >= 0)
1313 {
1314 /*
1315 * If a sourced file or executed function ran to its end, report the
1316 * unclosed conditional.
1317 */
1318 if (!got_int && !did_throw
1319 && ((getline_equal(getline, cookie, getsourceline)
1320 && !source_finished(getline, cookie))
1321 || (getline_equal(getline, cookie, get_func_line)
1322 && !func_has_ended(real_cookie))))
1323 {
1324 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
1325 EMSG(_(e_endtry));
1326 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
1327 EMSG(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001328 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
1329 EMSG(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 else
1331 EMSG(_(e_endif));
1332 }
1333
1334 /*
1335 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1336 * ":endtry" in a sourced file or executed function. If the try
1337 * conditional is in its finally clause, ignore anything pending.
1338 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001339 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 */
1341 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001342 {
1343 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1344
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001345 if (idx >= 0)
1346 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001347 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1348 &cstack.cs_looplevel);
1349 }
1350 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 trylevel = initial_trylevel;
1352 }
1353
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001354 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1355 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 * exception, do this now after rewinding the cstack. */
1357 do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line)
1358 ? (char_u *)"endfunction" : (char_u *)NULL);
1359
1360 if (trylevel == 0)
1361 {
1362 /*
1363 * When an exception is being thrown out of the outermost try
1364 * conditional, discard the uncaught exception, disable the conversion
1365 * of interrupts or errors to exceptions, and ensure that no more
1366 * commands are executed.
1367 */
1368 if (did_throw)
1369 {
1370 void *p = NULL;
1371 char_u *saved_sourcing_name;
1372 int saved_sourcing_lnum;
1373 struct msglist *messages = NULL, *next;
1374
1375 /*
1376 * If the uncaught exception is a user exception, report it as an
1377 * error. If it is an error exception, display the saved error
1378 * message now. For an interrupt exception, do nothing; the
1379 * interrupt message is given elsewhere.
1380 */
1381 switch (current_exception->type)
1382 {
1383 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001384 vim_snprintf((char *)IObuff, IOSIZE,
1385 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 current_exception->value);
1387 p = vim_strsave(IObuff);
1388 break;
1389 case ET_ERROR:
1390 messages = current_exception->messages;
1391 current_exception->messages = NULL;
1392 break;
1393 case ET_INTERRUPT:
1394 break;
1395 default:
1396 p = vim_strsave((char_u *)_(e_internal));
1397 }
1398
1399 saved_sourcing_name = sourcing_name;
1400 saved_sourcing_lnum = sourcing_lnum;
1401 sourcing_name = current_exception->throw_name;
1402 sourcing_lnum = current_exception->throw_lnum;
1403 current_exception->throw_name = NULL;
1404
1405 discard_current_exception(); /* uses IObuff if 'verbose' */
1406 suppress_errthrow = TRUE;
1407 force_abort = TRUE;
1408
1409 if (messages != NULL)
1410 {
1411 do
1412 {
1413 next = messages->next;
1414 emsg(messages->msg);
1415 vim_free(messages->msg);
1416 vim_free(messages);
1417 messages = next;
1418 }
1419 while (messages != NULL);
1420 }
1421 else if (p != NULL)
1422 {
1423 emsg(p);
1424 vim_free(p);
1425 }
1426 vim_free(sourcing_name);
1427 sourcing_name = saved_sourcing_name;
1428 sourcing_lnum = saved_sourcing_lnum;
1429 }
1430
1431 /*
1432 * On an interrupt or an aborting error not converted to an exception,
1433 * disable the conversion of errors to exceptions. (Interrupts are not
1434 * converted any more, here.) This enables also the interrupt message
1435 * when force_abort is set and did_emsg unset in case of an interrupt
1436 * from a finally clause after an error.
1437 */
1438 else if (got_int || (did_emsg && force_abort))
1439 suppress_errthrow = TRUE;
1440 }
1441
1442 /*
1443 * The current cstack will be freed when do_cmdline() returns. An uncaught
1444 * exception will have to be rethrown in the previous cstack. If a function
1445 * has just returned or a script file was just finished and the previous
1446 * cstack belongs to the same function or, respectively, script file, it
1447 * will have to be checked for finally clauses to be executed due to the
1448 * ":return" or ":finish". This is done in do_one_cmd().
1449 */
1450 if (did_throw)
1451 need_rethrow = TRUE;
1452 if ((getline_equal(getline, cookie, getsourceline)
1453 && ex_nesting_level > source_level(real_cookie))
1454 || (getline_equal(getline, cookie, get_func_line)
1455 && ex_nesting_level > func_level(real_cookie) + 1))
1456 {
1457 if (!did_throw)
1458 check_cstack = TRUE;
1459 }
1460 else
1461 {
1462 /* When leaving a function, reduce nesting level. */
1463 if (getline_equal(getline, cookie, get_func_line))
1464 --ex_nesting_level;
1465 /*
1466 * Go to debug mode when returning from a function in which we are
1467 * single-stepping.
1468 */
1469 if ((getline_equal(getline, cookie, getsourceline)
1470 || getline_equal(getline, cookie, get_func_line))
1471 && ex_nesting_level + 1 <= debug_break_level)
1472 do_debug(getline_equal(getline, cookie, getsourceline)
1473 ? (char_u *)_("End of sourced file")
1474 : (char_u *)_("End of function"));
1475 }
1476
1477 /*
1478 * Restore the exception environment (done after returning from the
1479 * debugger).
1480 */
1481 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001482 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
1484 msg_list = saved_msg_list;
1485#endif /* FEAT_EVAL */
1486
1487 /*
1488 * If there was too much output to fit on the command line, ask the user to
1489 * hit return before redrawing the screen. With the ":global" command we do
1490 * this only once after the command is finished.
1491 */
1492 if (did_inc)
1493 {
1494 --RedrawingDisabled;
1495 --no_wait_return;
1496 msg_scroll = FALSE;
1497
1498 /*
1499 * When just finished an ":if"-":else" which was typed, no need to
1500 * wait for hit-return. Also for an error situation.
1501 */
1502 if (retval == FAIL
1503#ifdef FEAT_EVAL
1504 || (did_endif && KeyTyped && !did_emsg)
1505#endif
1506 )
1507 {
1508 need_wait_return = FALSE;
1509 msg_didany = FALSE; /* don't wait when restarting edit */
1510 }
1511 else if (need_wait_return)
1512 {
1513 /*
1514 * The msg_start() above clears msg_didout. The wait_return we do
1515 * here should not overwrite the command that may be shown before
1516 * doing that.
1517 */
1518 msg_didout |= msg_didout_before_start;
1519 wait_return(FALSE);
1520 }
1521 }
1522
1523#ifndef FEAT_EVAL
1524 /*
1525 * Reset if_level, in case a sourced script file contains more ":if" than
1526 * ":endif" (could be ":if x | foo | endif").
1527 */
1528 if_level = 0;
1529#endif
1530
1531 --call_depth;
1532 return retval;
1533}
1534
1535#ifdef FEAT_EVAL
1536/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001537 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 */
1539 static char_u *
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001540get_loop_line(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 int c;
1542 void *cookie;
1543 int indent;
1544{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001545 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 wcmd_T *wp;
1547 char_u *line;
1548
1549 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1550 {
1551 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001552 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 if (cp->getline == NULL)
1556 line = getcmdline(c, 0L, indent);
1557 else
1558 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001559 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 ++cp->current_line;
1561
1562 return line;
1563 }
1564
1565 KeyTyped = FALSE;
1566 ++cp->current_line;
1567 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1568 sourcing_lnum = wp->lnum;
1569 return vim_strsave(wp->line);
1570}
1571
1572/*
1573 * Store a line in "gap" so that a ":while" loop can execute it again.
1574 */
1575 static int
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001576store_loop_line(gap, line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 garray_T *gap;
1578 char_u *line;
1579{
1580 if (ga_grow(gap, 1) == FAIL)
1581 return FAIL;
1582 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1583 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1584 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 return OK;
1586}
1587
1588/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001589 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 static void
1592free_cmdlines(gap)
1593 garray_T *gap;
1594{
1595 while (gap->ga_len > 0)
1596 {
1597 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1598 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 }
1600}
1601#endif
1602
1603/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001604 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1605 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 int
Bram Moolenaar89d40322006-08-29 15:30:07 +00001608getline_equal(fgetline, cookie, func)
1609 char_u *(*fgetline) __ARGS((int, void *, int));
Bram Moolenaar78a15312009-05-15 19:33:18 +00001610 void *cookie UNUSED; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 char_u *(*func) __ARGS((int, void *, int));
1612{
1613#ifdef FEAT_EVAL
1614 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001615 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001618 * function that's originally used to obtain the lines. This may be
1619 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001620 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001621 cp = (struct loop_cookie *)cookie;
1622 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 gp = cp->getline;
1625 cp = cp->cookie;
1626 }
1627 return gp == func;
1628#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001629 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
1631}
1632
1633#if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO)
1634/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001635 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 * getline function. Otherwise return "cookie".
1637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 void *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639getline_cookie(fgetline, cookie)
Bram Moolenaar78a15312009-05-15 19:33:18 +00001640 char_u *(*fgetline) __ARGS((int, void *, int)) UNUSED;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001641 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642{
1643# ifdef FEAT_EVAL
1644 char_u *(*gp) __ARGS((int, void *, int));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001645 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar89d40322006-08-29 15:30:07 +00001647 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001648 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001650 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001651 cp = (struct loop_cookie *)cookie;
1652 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 {
1654 gp = cp->getline;
1655 cp = cp->cookie;
1656 }
1657 return cp;
1658# else
1659 return cookie;
1660# endif
1661}
1662#endif
1663
1664/*
1665 * Execute one Ex command.
1666 *
1667 * If 'sourcing' is TRUE, the command will be included in the error message.
1668 *
1669 * 1. skip comment lines and leading space
1670 * 2. handle command modifiers
1671 * 3. parse range
1672 * 4. parse command
1673 * 5. parse arguments
1674 * 6. switch on command name
1675 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001676 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 *
1678 * This function may be called recursively!
1679 */
1680#if (_MSC_VER == 1200)
1681/*
Bram Moolenaared203462004-06-16 11:19:22 +00001682 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001684 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#endif
1686 static char_u *
1687do_one_cmd(cmdlinep, sourcing,
1688#ifdef FEAT_EVAL
1689 cstack,
1690#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001691 fgetline, cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 char_u **cmdlinep;
1693 int sourcing;
1694#ifdef FEAT_EVAL
1695 struct condstack *cstack;
1696#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00001697 char_u *(*fgetline) __ARGS((int, void *, int));
1698 void *cookie; /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699{
1700 char_u *p;
1701 linenr_T lnum;
1702 long n;
1703 char_u *errormsg = NULL; /* error message */
1704 exarg_T ea; /* Ex command arguments */
1705 long verbose_save = -1;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001706 int save_msg_scroll = msg_scroll;
1707 int save_msg_silent = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 int did_esilent = 0;
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001709#ifdef HAVE_SANDBOX
1710 int did_sandbox = FALSE;
1711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 cmdmod_T save_cmdmod;
1713 int ni; /* set when Not Implemented */
1714
1715 vim_memset(&ea, 0, sizeof(ea));
1716 ea.line1 = 1;
1717 ea.line2 = 1;
1718#ifdef FEAT_EVAL
1719 ++ex_nesting_level;
1720#endif
1721
1722 /* when not editing the last file :q has to be typed twice */
1723 if (quitmore
1724#ifdef FEAT_EVAL
1725 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001726 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727#endif
1728 )
1729 --quitmore;
1730
1731 /*
1732 * Reset browse, confirm, etc.. They are restored when returning, for
1733 * recursive calls.
1734 */
1735 save_cmdmod = cmdmod;
1736 vim_memset(&cmdmod, 0, sizeof(cmdmod));
1737
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001738 /* "#!anything" is handled like a comment. */
1739 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1740 goto doend;
1741
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 /*
1743 * Repeat until no more command modifiers are found.
1744 */
1745 ea.cmd = *cmdlinep;
1746 for (;;)
1747 {
1748/*
1749 * 1. skip comment lines and leading white space and colons
1750 */
1751 while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':')
1752 ++ea.cmd;
1753
1754 /* in ex mode, an empty line works like :+ */
1755 if (*ea.cmd == NUL && exmode_active
Bram Moolenaar89d40322006-08-29 15:30:07 +00001756 && (getline_equal(fgetline, cookie, getexmodeline)
1757 || getline_equal(fgetline, cookie, getexline))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001758 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 {
1760 ea.cmd = (char_u *)"+";
1761 ex_pressedreturn = TRUE;
1762 }
1763
1764 /* ignore comment and empty lines */
Bram Moolenaarf998c042007-11-20 11:31:26 +00001765 if (*ea.cmd == '"')
1766 goto doend;
1767 if (*ea.cmd == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001768 {
1769 ex_pressedreturn = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772
1773/*
1774 * 2. handle command modifiers.
1775 */
1776 p = ea.cmd;
1777 if (VIM_ISDIGIT(*ea.cmd))
1778 p = skipwhite(skipdigits(ea.cmd));
1779 switch (*p)
1780 {
1781 /* When adding an entry, also modify cmd_exists(). */
1782 case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3))
1783 break;
1784#ifdef FEAT_WINDOWS
1785 cmdmod.split |= WSP_ABOVE;
1786#endif
1787 continue;
1788
1789 case 'b': if (checkforcmd(&ea.cmd, "belowright", 3))
1790 {
1791#ifdef FEAT_WINDOWS
1792 cmdmod.split |= WSP_BELOW;
1793#endif
1794 continue;
1795 }
1796 if (checkforcmd(&ea.cmd, "browse", 3))
1797 {
Bram Moolenaard812df62008-11-09 12:46:09 +00001798#ifdef FEAT_BROWSE_CMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 cmdmod.browse = TRUE;
1800#endif
1801 continue;
1802 }
1803 if (!checkforcmd(&ea.cmd, "botright", 2))
1804 break;
1805#ifdef FEAT_WINDOWS
1806 cmdmod.split |= WSP_BOT;
1807#endif
1808 continue;
1809
1810 case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4))
1811 break;
1812#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1813 cmdmod.confirm = TRUE;
1814#endif
1815 continue;
1816
1817 case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3))
1818 {
1819 cmdmod.keepmarks = TRUE;
1820 continue;
1821 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001822 if (checkforcmd(&ea.cmd, "keepalt", 5))
1823 {
1824 cmdmod.keepalt = TRUE;
1825 continue;
1826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if (!checkforcmd(&ea.cmd, "keepjumps", 5))
1828 break;
1829 cmdmod.keepjumps = TRUE;
1830 continue;
1831
1832 /* ":hide" and ":hide | cmd" are not modifiers */
1833 case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3)
1834 || *p == NUL || ends_excmd(*p))
1835 break;
1836 ea.cmd = p;
1837 cmdmod.hide = TRUE;
1838 continue;
1839
1840 case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3))
1841 {
1842 cmdmod.lockmarks = TRUE;
1843 continue;
1844 }
1845
1846 if (!checkforcmd(&ea.cmd, "leftabove", 5))
1847 break;
1848#ifdef FEAT_WINDOWS
1849 cmdmod.split |= WSP_ABOVE;
1850#endif
1851 continue;
1852
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001853 case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3))
1854 break;
1855#ifdef FEAT_AUTOCMD
1856 if (cmdmod.save_ei == NULL)
1857 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00001858 /* Set 'eventignore' to "all". Restore the
1859 * existing option value later. */
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001860 cmdmod.save_ei = vim_strsave(p_ei);
1861 set_string_option_direct((char_u *)"ei", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001862 (char_u *)"all", OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00001863 }
1864#endif
1865 continue;
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6))
1868 break;
1869#ifdef FEAT_WINDOWS
1870 cmdmod.split |= WSP_BELOW;
1871#endif
1872 continue;
1873
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001874 case 's': if (checkforcmd(&ea.cmd, "sandbox", 3))
1875 {
1876#ifdef HAVE_SANDBOX
1877 if (!did_sandbox)
1878 ++sandbox;
1879 did_sandbox = TRUE;
1880#endif
1881 continue;
1882 }
1883 if (!checkforcmd(&ea.cmd, "silent", 3))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 break;
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001885 if (save_msg_silent == -1)
1886 save_msg_silent = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 ++msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1]))
1889 {
1890 /* ":silent!", but not "silent !cmd" */
1891 ea.cmd = skipwhite(ea.cmd + 1);
1892 ++emsg_silent;
1893 ++did_esilent;
1894 }
1895 continue;
1896
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001897 case 't': if (checkforcmd(&p, "tab", 3))
1898 {
1899#ifdef FEAT_WINDOWS
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001900 if (vim_isdigit(*ea.cmd))
1901 cmdmod.tab = atoi((char *)ea.cmd) + 1;
1902 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001903 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001904 ea.cmd = p;
1905#endif
1906 continue;
1907 }
1908 if (!checkforcmd(&ea.cmd, "topleft", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 break;
1910#ifdef FEAT_WINDOWS
1911 cmdmod.split |= WSP_TOP;
1912#endif
1913 continue;
1914
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001915 case 'u': if (!checkforcmd(&ea.cmd, "unsilent", 3))
1916 break;
1917 if (save_msg_silent == -1)
1918 save_msg_silent = msg_silent;
1919 msg_silent = 0;
1920 continue;
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 case 'v': if (checkforcmd(&ea.cmd, "vertical", 4))
1923 {
1924#ifdef FEAT_VERTSPLIT
1925 cmdmod.split |= WSP_VERT;
1926#endif
1927 continue;
1928 }
1929 if (!checkforcmd(&p, "verbose", 4))
1930 break;
1931 if (verbose_save < 0)
1932 verbose_save = p_verbose;
Bram Moolenaared203462004-06-16 11:19:22 +00001933 if (vim_isdigit(*ea.cmd))
1934 p_verbose = atoi((char *)ea.cmd);
1935 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 p_verbose = 1;
1937 ea.cmd = p;
1938 continue;
1939 }
1940 break;
1941 }
1942
1943#ifdef FEAT_EVAL
1944 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1945 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1946#else
1947 ea.skip = (if_level > 0);
1948#endif
1949
1950#ifdef FEAT_EVAL
Bram Moolenaar05159a02005-02-26 23:04:13 +00001951# ifdef FEAT_PROFILE
1952 /* Count this line for profiling if ea.skip is FALSE. */
Bram Moolenaar371d5402006-03-20 21:47:49 +00001953 if (do_profiling == PROF_YES && !ea.skip)
Bram Moolenaar05159a02005-02-26 23:04:13 +00001954 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001955 if (getline_equal(fgetline, cookie, get_func_line))
1956 func_line_exec(getline_cookie(fgetline, cookie));
1957 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +00001958 script_line_exec();
1959 }
1960#endif
1961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 /* May go to debug mode. If this happens and the ">quit" debug command is
1963 * used, throw an interrupt exception and skip the next command. */
1964 dbg_check_breakpoint(&ea);
1965 if (!ea.skip && got_int)
1966 {
1967 ea.skip = TRUE;
1968 (void)do_intthrow(cstack);
1969 }
1970#endif
1971
1972/*
1973 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
1974 *
1975 * where 'addr' is:
1976 *
1977 * % (entire file)
1978 * $ [+-NUM]
1979 * 'x [+-NUM] (where x denotes a currently defined mark)
1980 * . [+-NUM]
1981 * [+-NUM]..
1982 * NUM
1983 *
1984 * The ea.cmd pointer is updated to point to the first character following the
1985 * range spec. If an initial address is found, but no second, the upper bound
1986 * is equal to the lower.
1987 */
1988
1989 /* repeat for all ',' or ';' separated addresses */
1990 for (;;)
1991 {
1992 ea.line1 = ea.line2;
1993 ea.line2 = curwin->w_cursor.lnum; /* default is current line number */
1994 ea.cmd = skipwhite(ea.cmd);
1995 lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0);
1996 if (ea.cmd == NULL) /* error detected */
1997 goto doend;
1998 if (lnum == MAXLNUM)
1999 {
2000 if (*ea.cmd == '%') /* '%' - all lines */
2001 {
2002 ++ea.cmd;
2003 ea.line1 = 1;
2004 ea.line2 = curbuf->b_ml.ml_line_count;
2005 ++ea.addr_count;
2006 }
2007 /* '*' - visual area */
2008 else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2009 {
2010 pos_T *fp;
2011
2012 ++ea.cmd;
2013 if (!ea.skip)
2014 {
2015 fp = getmark('<', FALSE);
2016 if (check_mark(fp) == FAIL)
2017 goto doend;
2018 ea.line1 = fp->lnum;
2019 fp = getmark('>', FALSE);
2020 if (check_mark(fp) == FAIL)
2021 goto doend;
2022 ea.line2 = fp->lnum;
2023 ++ea.addr_count;
2024 }
2025 }
2026 }
2027 else
2028 ea.line2 = lnum;
2029 ea.addr_count++;
2030
2031 if (*ea.cmd == ';')
2032 {
2033 if (!ea.skip)
2034 curwin->w_cursor.lnum = ea.line2;
2035 }
2036 else if (*ea.cmd != ',')
2037 break;
2038 ++ea.cmd;
2039 }
2040
2041 /* One address given: set start and end lines */
2042 if (ea.addr_count == 1)
2043 {
2044 ea.line1 = ea.line2;
2045 /* ... but only implicit: really no address given */
2046 if (lnum == MAXLNUM)
2047 ea.addr_count = 0;
2048 }
2049
2050 /* Don't leave the cursor on an illegal line (caused by ';') */
2051 check_cursor_lnum();
2052
2053/*
2054 * 4. parse command
2055 */
2056
2057 /*
2058 * Skip ':' and any white space
2059 */
2060 ea.cmd = skipwhite(ea.cmd);
2061 while (*ea.cmd == ':')
2062 ea.cmd = skipwhite(ea.cmd + 1);
2063
2064 /*
2065 * If we got a line, but no command, then go to the line.
2066 * If we find a '|' or '\n' we set ea.nextcmd.
2067 */
2068 if (*ea.cmd == NUL || *ea.cmd == '"' ||
2069 (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
2070 {
2071 /*
2072 * strange vi behaviour:
2073 * ":3" jumps to line 3
2074 * ":3|..." prints line 3
2075 * ":|" prints current line
2076 */
2077 if (ea.skip) /* skip this if inside :if */
2078 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002079 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
2081 ea.cmdidx = CMD_print;
2082 ea.argt = RANGE+COUNT+TRLBAR;
2083 if ((errormsg = invalid_range(&ea)) == NULL)
2084 {
2085 correct_range(&ea);
2086 ex_print(&ea);
2087 }
2088 }
2089 else if (ea.addr_count != 0)
2090 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002091 if (ea.line2 > curbuf->b_ml.ml_line_count)
2092 {
2093 /* With '-' in 'cpoptions' a line number past the file is an
2094 * error, otherwise put it at the end of the file. */
2095 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
2096 ea.line2 = -1;
2097 else
2098 ea.line2 = curbuf->b_ml.ml_line_count;
2099 }
2100
2101 if (ea.line2 < 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002102 errormsg = (char_u *)_(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 else
2104 {
2105 if (ea.line2 == 0)
2106 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 else
2108 curwin->w_cursor.lnum = ea.line2;
2109 beginline(BL_SOL | BL_FIX);
2110 }
2111 }
2112 goto doend;
2113 }
2114
2115 /* Find the command and let "p" point to after it. */
2116 p = find_command(&ea, NULL);
2117
2118#ifdef FEAT_USR_CMDS
2119 if (p == NULL)
2120 {
2121 if (!ea.skip)
2122 errormsg = (char_u *)_("E464: Ambiguous use of user-defined command");
2123 goto doend;
2124 }
2125 /* Check for wrong commands. */
2126 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78)
2127 {
2128 errormsg = uc_fun_cmd();
2129 goto doend;
2130 }
2131#endif
2132 if (ea.cmdidx == CMD_SIZE)
2133 {
2134 if (!ea.skip)
2135 {
2136 STRCPY(IObuff, _("E492: Not an editor command"));
2137 if (!sourcing)
2138 {
2139 STRCAT(IObuff, ": ");
2140 STRNCAT(IObuff, *cmdlinep, 40);
2141 }
2142 errormsg = IObuff;
2143 }
2144 goto doend;
2145 }
2146
2147 ni = (
2148#ifdef FEAT_USR_CMDS
2149 !USER_CMDIDX(ea.cmdidx) &&
2150#endif
Bram Moolenaar3ebc1e52007-07-05 07:54:17 +00002151 (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002152#ifdef HAVE_EX_SCRIPT_NI
2153 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2154#endif
2155 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157#ifndef FEAT_EVAL
2158 /*
2159 * When the expression evaluation is disabled, recognize the ":if" and
2160 * ":endif" commands and ignore everything in between it.
2161 */
2162 if (ea.cmdidx == CMD_if)
2163 ++if_level;
2164 if (if_level)
2165 {
2166 if (ea.cmdidx == CMD_endif)
2167 --if_level;
2168 goto doend;
2169 }
2170
2171#endif
2172
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002173 /* forced commands */
2174 if (*p == '!' && ea.cmdidx != CMD_substitute
2175 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
2177 ++p;
2178 ea.forceit = TRUE;
2179 }
2180 else
2181 ea.forceit = FALSE;
2182
2183/*
2184 * 5. parse arguments
2185 */
2186#ifdef FEAT_USR_CMDS
2187 if (!USER_CMDIDX(ea.cmdidx))
2188#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002189 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190
2191 if (!ea.skip)
2192 {
2193#ifdef HAVE_SANDBOX
2194 if (sandbox != 0 && !(ea.argt & SBOXOK))
2195 {
2196 /* Command not allowed in sandbox. */
2197 errormsg = (char_u *)_(e_sandbox);
2198 goto doend;
2199 }
2200#endif
2201 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
2202 {
2203 /* Command not allowed in non-'modifiable' buffer */
2204 errormsg = (char_u *)_(e_modifiable);
2205 goto doend;
2206 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002207
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002208 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209# ifdef FEAT_USR_CMDS
2210 && !USER_CMDIDX(ea.cmdidx)
2211# endif
2212 )
2213 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002214 /* Command not allowed when editing the command line. */
2215#ifdef FEAT_CMDWIN
2216 if (cmdwin_type != 0)
2217 errormsg = (char_u *)_(e_cmdwin);
2218 else
2219#endif
2220 errormsg = (char_u *)_(e_secure);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 goto doend;
2222 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002223#ifdef FEAT_AUTOCMD
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002224 /* Disallow editing another buffer when "curbuf_lock" is set.
2225 * Do allow ":edit" (check for argument later).
2226 * Do allow ":checktime" (it's postponed). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002227 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002228 && ea.cmdidx != CMD_edit
2229 && ea.cmdidx != CMD_checktime
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002230# ifdef FEAT_USR_CMDS
2231 && !USER_CMDIDX(ea.cmdidx)
2232# endif
2233 && curbuf_locked())
2234 goto doend;
2235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
2237 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2238 {
2239 /* no range allowed */
2240 errormsg = (char_u *)_(e_norange);
2241 goto doend;
2242 }
2243 }
2244
2245 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2246 {
2247 errormsg = (char_u *)_(e_nobang);
2248 goto doend;
2249 }
2250
2251 /*
2252 * Don't complain about the range if it is not used
2253 * (could happen if line_count is accidentally set to 0).
2254 */
2255 if (!ea.skip && !ni)
2256 {
2257 /*
2258 * If the range is backwards, ask for confirmation and, if given, swap
2259 * ea.line1 & ea.line2 so it's forwards again.
2260 * When global command is busy, don't ask, will fail below.
2261 */
2262 if (!global_busy && ea.line1 > ea.line2)
2263 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002264 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002266 if (sourcing || exmode_active)
2267 {
2268 errormsg = (char_u *)_("E493: Backwards range given");
2269 goto doend;
2270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 if (ask_yesno((char_u *)
2272 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002273 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275 lnum = ea.line1;
2276 ea.line1 = ea.line2;
2277 ea.line2 = lnum;
2278 }
2279 if ((errormsg = invalid_range(&ea)) != NULL)
2280 goto doend;
2281 }
2282
2283 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2284 ea.line2 = 1;
2285
2286 correct_range(&ea);
2287
2288#ifdef FEAT_FOLDING
2289 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy)
2290 {
2291 /* Put the first line at the start of a closed fold, put the last line
2292 * at the end of a closed fold. */
2293 (void)hasFolding(ea.line1, &ea.line1, NULL);
2294 (void)hasFolding(ea.line2, NULL, &ea.line2);
2295 }
2296#endif
2297
2298#ifdef FEAT_QUICKFIX
2299 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002300 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * option here, so things like % get expanded.
2302 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002303 p = replace_makeprg(&ea, p, cmdlinep);
2304 if (p == NULL)
2305 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306#endif
2307
2308 /*
2309 * Skip to start of argument.
2310 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2311 */
2312 if (ea.cmdidx == CMD_bang)
2313 ea.arg = p;
2314 else
2315 ea.arg = skipwhite(p);
2316
2317 /*
2318 * Check for "++opt=val" argument.
2319 * Must be first, allow ":w ++enc=utf8 !cmd"
2320 */
2321 if (ea.argt & ARGOPT)
2322 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2323 if (getargopt(&ea) == FAIL && !ni)
2324 {
2325 errormsg = (char_u *)_(e_invarg);
2326 goto doend;
2327 }
2328
2329 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2330 {
2331 if (*ea.arg == '>') /* append */
2332 {
2333 if (*++ea.arg != '>') /* typed wrong */
2334 {
2335 errormsg = (char_u *)_("E494: Use w or w>>");
2336 goto doend;
2337 }
2338 ea.arg = skipwhite(ea.arg + 1);
2339 ea.append = TRUE;
2340 }
2341 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2342 {
2343 ++ea.arg;
2344 ea.usefilter = TRUE;
2345 }
2346 }
2347
2348 if (ea.cmdidx == CMD_read)
2349 {
2350 if (ea.forceit)
2351 {
2352 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2353 ea.forceit = FALSE;
2354 }
2355 else if (*ea.arg == '!') /* :r !filter */
2356 {
2357 ++ea.arg;
2358 ea.usefilter = TRUE;
2359 }
2360 }
2361
2362 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2363 {
2364 ea.amount = 1;
2365 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2366 {
2367 ++ea.arg;
2368 ++ea.amount;
2369 }
2370 ea.arg = skipwhite(ea.arg);
2371 }
2372
2373 /*
2374 * Check for "+command" argument, before checking for next command.
2375 * Don't do this for ":read !cmd" and ":write !cmd".
2376 */
2377 if ((ea.argt & EDITCMD) && !ea.usefilter)
2378 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2379
2380 /*
2381 * Check for '|' to separate commands and '"' to start comments.
2382 * Don't do this for ":read !cmd" and ":write !cmd".
2383 */
2384 if ((ea.argt & TRLBAR) && !ea.usefilter)
2385 separate_nextcmd(&ea);
2386
2387 /*
2388 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002389 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2390 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002392 else if (ea.cmdidx == CMD_bang
2393 || ea.cmdidx == CMD_global
2394 || ea.cmdidx == CMD_vglobal
2395 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 {
2397 for (p = ea.arg; *p; ++p)
2398 {
2399 /* Remove one backslash before a newline, so that it's possible to
2400 * pass a newline to the shell and also a newline that is preceded
2401 * with a backslash. This makes it impossible to end a shell
2402 * command in a backslash, but that doesn't appear useful.
2403 * Halving the number of backslashes is incompatible with previous
2404 * versions. */
2405 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002406 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 else if (*p == '\n')
2408 {
2409 ea.nextcmd = p + 1;
2410 *p = NUL;
2411 break;
2412 }
2413 }
2414 }
2415
2416 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2417 {
2418 ea.line1 = 1;
2419 ea.line2 = curbuf->b_ml.ml_line_count;
2420 }
2421
2422 /* accept numbered register only when no count allowed (:put) */
2423 if ( (ea.argt & REGSTR)
2424 && *ea.arg != NUL
2425#ifdef FEAT_USR_CMDS
2426 && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2427 && USER_CMDIDX(ea.cmdidx)))
2428 /* Do not allow register = for user commands */
2429 && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
2430#else
2431 && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put)
2432#endif
2433 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2434 {
2435 ea.regname = *ea.arg++;
2436#ifdef FEAT_EVAL
2437 /* for '=' register: accept the rest of the line as an expression */
2438 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2439 {
2440 set_expr_line(vim_strsave(ea.arg));
2441 ea.arg += STRLEN(ea.arg);
2442 }
2443#endif
2444 ea.arg = skipwhite(ea.arg);
2445 }
2446
2447 /*
2448 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2449 * count, it's a buffer name.
2450 */
2451 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2452 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
2453 || vim_iswhite(*p)))
2454 {
2455 n = getdigits(&ea.arg);
2456 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002457 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
2459 errormsg = (char_u *)_(e_zerocount);
2460 goto doend;
2461 }
2462 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2463 {
2464 ea.line2 = n;
2465 if (ea.addr_count == 0)
2466 ea.addr_count = 1;
2467 }
2468 else
2469 {
2470 ea.line1 = ea.line2;
2471 ea.line2 += n - 1;
2472 ++ea.addr_count;
2473 /*
2474 * Be vi compatible: no error message for out of range.
2475 */
2476 if (ea.line2 > curbuf->b_ml.ml_line_count)
2477 ea.line2 = curbuf->b_ml.ml_line_count;
2478 }
2479 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002480
2481 /*
2482 * Check for flags: 'l', 'p' and '#'.
2483 */
2484 if (ea.argt & EXFLAGS)
2485 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002487 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002488 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 errormsg = (char_u *)_(e_trailing);
2491 goto doend;
2492 }
2493
2494 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2495 {
2496 errormsg = (char_u *)_(e_argreq);
2497 goto doend;
2498 }
2499
2500#ifdef FEAT_EVAL
2501 /*
2502 * Skip the command when it's not going to be executed.
2503 * The commands like :if, :endif, etc. always need to be executed.
2504 * Also make an exception for commands that handle a trailing command
2505 * themselves.
2506 */
2507 if (ea.skip)
2508 {
2509 switch (ea.cmdidx)
2510 {
2511 /* commands that need evaluation */
2512 case CMD_while:
2513 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002514 case CMD_for:
2515 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 case CMD_if:
2517 case CMD_elseif:
2518 case CMD_else:
2519 case CMD_endif:
2520 case CMD_try:
2521 case CMD_catch:
2522 case CMD_finally:
2523 case CMD_endtry:
2524 case CMD_function:
2525 break;
2526
2527 /* Commands that handle '|' themselves. Check: A command should
2528 * either have the TRLBAR flag, appear in this list or appear in
2529 * the list at ":help :bar". */
2530 case CMD_aboveleft:
2531 case CMD_and:
2532 case CMD_belowright:
2533 case CMD_botright:
2534 case CMD_browse:
2535 case CMD_call:
2536 case CMD_confirm:
2537 case CMD_delfunction:
2538 case CMD_djump:
2539 case CMD_dlist:
2540 case CMD_dsearch:
2541 case CMD_dsplit:
2542 case CMD_echo:
2543 case CMD_echoerr:
2544 case CMD_echomsg:
2545 case CMD_echon:
2546 case CMD_execute:
2547 case CMD_help:
2548 case CMD_hide:
2549 case CMD_ijump:
2550 case CMD_ilist:
2551 case CMD_isearch:
2552 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002553 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 case CMD_keepjumps:
2555 case CMD_keepmarks:
2556 case CMD_leftabove:
2557 case CMD_let:
2558 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002559 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002561 case CMD_mzscheme:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 case CMD_perl:
2563 case CMD_psearch:
2564 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002565 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002566 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 case CMD_return:
2568 case CMD_rightbelow:
2569 case CMD_ruby:
2570 case CMD_silent:
2571 case CMD_smagic:
2572 case CMD_snomagic:
2573 case CMD_substitute:
2574 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002575 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 case CMD_tcl:
2577 case CMD_throw:
2578 case CMD_tilde:
2579 case CMD_topleft:
2580 case CMD_unlet:
2581 case CMD_verbose:
2582 case CMD_vertical:
2583 break;
2584
2585 default: goto doend;
2586 }
2587 }
2588#endif
2589
2590 if (ea.argt & XFILE)
2591 {
2592 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2593 goto doend;
2594 }
2595
2596#ifdef FEAT_LISTCMDS
2597 /*
2598 * Accept buffer name. Cannot be used at the same time with a buffer
2599 * number. Don't do this for a user command.
2600 */
2601 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
2602# ifdef FEAT_USR_CMDS
2603 && !USER_CMDIDX(ea.cmdidx)
2604# endif
2605 )
2606 {
2607 /*
2608 * :bdelete, :bwipeout and :bunload take several arguments, separated
2609 * by spaces: find next space (skipping over escaped characters).
2610 * The others take one argument: ignore trailing spaces.
2611 */
2612 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2613 || ea.cmdidx == CMD_bunload)
2614 p = skiptowhite_esc(ea.arg);
2615 else
2616 {
2617 p = ea.arg + STRLEN(ea.arg);
2618 while (p > ea.arg && vim_iswhite(p[-1]))
2619 --p;
2620 }
2621 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE);
2622 if (ea.line2 < 0) /* failed */
2623 goto doend;
2624 ea.addr_count = 1;
2625 ea.arg = skipwhite(p);
2626 }
2627#endif
2628
2629/*
2630 * 6. switch on command name
2631 *
2632 * The "ea" structure holds the arguments that can be used.
2633 */
2634 ea.cmdlinep = cmdlinep;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002635 ea.getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 ea.cookie = cookie;
2637#ifdef FEAT_EVAL
2638 ea.cstack = cstack;
2639#endif
2640
2641#ifdef FEAT_USR_CMDS
2642 if (USER_CMDIDX(ea.cmdidx))
2643 {
2644 /*
2645 * Execute a user-defined command.
2646 */
2647 do_ucmd(&ea);
2648 }
2649 else
2650#endif
2651 {
2652 /*
2653 * Call the function to execute the command.
2654 */
2655 ea.errmsg = NULL;
2656 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2657 if (ea.errmsg != NULL)
2658 errormsg = (char_u *)_(ea.errmsg);
2659 }
2660
2661#ifdef FEAT_EVAL
2662 /*
2663 * If the command just executed called do_cmdline(), any throw or ":return"
2664 * or ":finish" encountered there must also check the cstack of the still
2665 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2666 * exception, or reanimate a returned function or finished script file and
2667 * return or finish it again.
2668 */
2669 if (need_rethrow)
2670 do_throw(cstack);
2671 else if (check_cstack)
2672 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002673 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002675 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 && current_func_returned())
2677 do_return(&ea, TRUE, FALSE, NULL);
2678 }
2679 need_rethrow = check_cstack = FALSE;
2680#endif
2681
2682doend:
2683 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
2684 curwin->w_cursor.lnum = 1;
2685
2686 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2687 {
2688 if (sourcing)
2689 {
2690 if (errormsg != IObuff)
2691 {
2692 STRCPY(IObuff, errormsg);
2693 errormsg = IObuff;
2694 }
2695 STRCAT(errormsg, ": ");
Bram Moolenaar6c964832007-12-09 18:38:35 +00002696 STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 }
2698 emsg(errormsg);
2699 }
2700#ifdef FEAT_EVAL
2701 do_errthrow(cstack,
2702 (ea.cmdidx != CMD_SIZE
2703# ifdef FEAT_USR_CMDS
2704 && !USER_CMDIDX(ea.cmdidx)
2705# endif
2706 ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
2707#endif
2708
2709 if (verbose_save >= 0)
2710 p_verbose = verbose_save;
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002711#ifdef FEAT_AUTOCMD
2712 if (cmdmod.save_ei != NULL)
2713 {
2714 /* Restore 'eventignore' to the value before ":noautocmd". */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002715 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2716 OPT_FREE, SID_NONE);
Bram Moolenaarcdbac1e2005-12-11 21:27:22 +00002717 free_string_option(cmdmod.save_ei);
2718 }
2719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 cmdmod = save_cmdmod;
2722
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002723 if (save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
2725 /* messages could be enabled for a serious error, need to check if the
2726 * counters don't become negative */
Bram Moolenaarbecf4282009-09-30 11:24:36 +00002727 if (!did_emsg || msg_silent > save_msg_silent)
Bram Moolenaar8e258a42009-07-09 13:55:43 +00002728 msg_silent = save_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 emsg_silent -= did_esilent;
2730 if (emsg_silent < 0)
2731 emsg_silent = 0;
2732 /* Restore msg_scroll, it's set by file I/O commands, even when no
2733 * message is actually displayed. */
2734 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002735
2736 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2737 * somewhere in the line. Put it back in the first column. */
2738 if (redirecting())
2739 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002742#ifdef HAVE_SANDBOX
2743 if (did_sandbox)
2744 --sandbox;
2745#endif
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2748 ea.nextcmd = NULL;
2749
2750#ifdef FEAT_EVAL
2751 --ex_nesting_level;
2752#endif
2753
2754 return ea.nextcmd;
2755}
2756#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002757 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758#endif
2759
2760/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002761 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 * If there is a match advance "pp" to the argument and return TRUE.
2763 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002764 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765checkforcmd(pp, cmd, len)
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00002766 char_u **pp; /* start of command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 char *cmd; /* name of command */
2768 int len; /* required length */
2769{
2770 int i;
2771
2772 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002773 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 break;
2775 if (i >= len && !isalpha((*pp)[i]))
2776 {
2777 *pp = skipwhite(*pp + i);
2778 return TRUE;
2779 }
2780 return FALSE;
2781}
2782
2783/*
2784 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002785 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002787 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 * Returns NULL for an ambiguous user command.
2789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 static char_u *
2791find_command(eap, full)
2792 exarg_T *eap;
Bram Moolenaar78a15312009-05-15 19:33:18 +00002793 int *full UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794{
2795 int len;
2796 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002797 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799 /*
2800 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00002801 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 * - the 'k' command can directly be followed by any character.
2803 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
2804 * but :sre[wind] is another command, as are :scrip[tnames],
2805 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00002806 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 */
2808 p = eap->cmd;
2809 if (*p == 'k')
2810 {
2811 eap->cmdidx = CMD_k;
2812 ++p;
2813 }
2814 else if (p[0] == 's'
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002815 && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r'
2816 && p[3] != 'i' && p[4] != 'p')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 || p[1] == 'g'
2818 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
2819 || p[1] == 'I'
2820 || (p[1] == 'r' && p[2] != 'e')))
2821 {
2822 eap->cmdidx = CMD_substitute;
2823 ++p;
2824 }
2825 else
2826 {
2827 while (ASCII_ISALPHA(*p))
2828 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02002829 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02002830 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02002831 while (ASCII_ISALNUM(*p))
2832 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02002833
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 /* check for non-alpha command */
2835 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
2836 ++p;
2837 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002838 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
2839 {
2840 /* Check for ":dl", ":dell", etc. to ":deletel": that's
2841 * :delete with the 'l' flag. Same for 'p'. */
2842 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00002843 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00002844 break;
2845 if (i == len - 1)
2846 {
2847 --len;
2848 if (p[-1] == 'l')
2849 eap->flags |= EXFLAG_LIST;
2850 else
2851 eap->flags |= EXFLAG_PRINT;
2852 }
2853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854
2855 if (ASCII_ISLOWER(*eap->cmd))
2856 eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)];
2857 else
2858 eap->cmdidx = cmdidxs[26];
2859
2860 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
2861 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
2862 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
2863 (size_t)len) == 0)
2864 {
2865#ifdef FEAT_EVAL
2866 if (full != NULL
2867 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
2868 *full = TRUE;
2869#endif
2870 break;
2871 }
2872
2873#ifdef FEAT_USR_CMDS
2874 /* Look for a user defined command as a last resort */
2875 if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
2876 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002877 /* User defined commands may contain digits. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 while (ASCII_ISALNUM(*p))
2879 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002880 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 }
2882#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002883 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 eap->cmdidx = CMD_SIZE;
2885 }
2886
2887 return p;
2888}
2889
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002890#ifdef FEAT_USR_CMDS
2891/*
2892 * Search for a user command that matches "eap->cmd".
2893 * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx".
2894 * Return a pointer to just after the command.
2895 * Return NULL if there is no matching command.
2896 */
2897 static char_u *
2898find_ucmd(eap, p, full, xp, compl)
2899 exarg_T *eap;
2900 char_u *p; /* end of the command (possibly including count) */
2901 int *full; /* set to TRUE for a full match */
2902 expand_T *xp; /* used for completion, NULL otherwise */
2903 int *compl; /* completion flags or NULL */
2904{
2905 int len = (int)(p - eap->cmd);
2906 int j, k, matchlen = 0;
2907 ucmd_T *uc;
2908 int found = FALSE;
2909 int possible = FALSE;
2910 char_u *cp, *np; /* Point into typed cmd and test name */
2911 garray_T *gap;
2912 int amb_local = FALSE; /* Found ambiguous buffer-local command,
2913 only full match global is accepted. */
2914
2915 /*
2916 * Look for buffer-local user commands first, then global ones.
2917 */
2918 gap = &curbuf->b_ucmds;
2919 for (;;)
2920 {
2921 for (j = 0; j < gap->ga_len; ++j)
2922 {
2923 uc = USER_CMD_GA(gap, j);
2924 cp = eap->cmd;
2925 np = uc->uc_name;
2926 k = 0;
2927 while (k < len && *np != NUL && *cp++ == *np++)
2928 k++;
2929 if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k])))
2930 {
2931 /* If finding a second match, the command is ambiguous. But
2932 * not if a buffer-local command wasn't a full match and a
2933 * global command is a full match. */
2934 if (k == len && found && *np != NUL)
2935 {
2936 if (gap == &ucmds)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002937 return NULL;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002938 amb_local = TRUE;
2939 }
2940
2941 if (!found || (k == len && *np == NUL))
2942 {
2943 /* If we matched up to a digit, then there could
2944 * be another command including the digit that we
2945 * should use instead.
2946 */
2947 if (k == len)
2948 found = TRUE;
2949 else
2950 possible = TRUE;
2951
2952 if (gap == &ucmds)
2953 eap->cmdidx = CMD_USER;
2954 else
2955 eap->cmdidx = CMD_USER_BUF;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002956 eap->argt = (long)uc->uc_argt;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00002957 eap->useridx = j;
2958
2959# ifdef FEAT_CMDL_COMPL
2960 if (compl != NULL)
2961 *compl = uc->uc_compl;
2962# ifdef FEAT_EVAL
2963 if (xp != NULL)
2964 {
2965 xp->xp_arg = uc->uc_compl_arg;
2966 xp->xp_scriptID = uc->uc_scriptID;
2967 }
2968# endif
2969# endif
2970 /* Do not search for further abbreviations
2971 * if this is an exact match. */
2972 matchlen = k;
2973 if (k == len && *np == NUL)
2974 {
2975 if (full != NULL)
2976 *full = TRUE;
2977 amb_local = FALSE;
2978 break;
2979 }
2980 }
2981 }
2982 }
2983
2984 /* Stop if we found a full match or searched all. */
2985 if (j < gap->ga_len || gap == &ucmds)
2986 break;
2987 gap = &ucmds;
2988 }
2989
2990 /* Only found ambiguous matches. */
2991 if (amb_local)
2992 {
2993 if (xp != NULL)
2994 xp->xp_context = EXPAND_UNSUCCESSFUL;
2995 return NULL;
2996 }
2997
2998 /* The match we found may be followed immediately by a number. Move "p"
2999 * back to point to it. */
3000 if (found || possible)
3001 return p + (matchlen - len);
3002 return p;
3003}
3004#endif
3005
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003007static struct cmdmod
3008{
3009 char *name;
3010 int minlen;
3011 int has_count; /* :123verbose :3tab */
3012} cmdmods[] = {
3013 {"aboveleft", 3, FALSE},
3014 {"belowright", 3, FALSE},
3015 {"botright", 2, FALSE},
3016 {"browse", 3, FALSE},
3017 {"confirm", 4, FALSE},
3018 {"hide", 3, FALSE},
3019 {"keepalt", 5, FALSE},
3020 {"keepjumps", 5, FALSE},
3021 {"keepmarks", 3, FALSE},
3022 {"leftabove", 5, FALSE},
3023 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003024 {"noautocmd", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003025 {"rightbelow", 6, FALSE},
3026 {"sandbox", 3, FALSE},
3027 {"silent", 3, FALSE},
3028 {"tab", 3, TRUE},
3029 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003030 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003031 {"verbose", 4, TRUE},
3032 {"vertical", 4, FALSE},
3033};
3034
3035/*
3036 * Return length of a command modifier (including optional count).
3037 * Return zero when it's not a modifier.
3038 */
3039 int
3040modifier_len(cmd)
3041 char_u *cmd;
3042{
3043 int i, j;
3044 char_u *p = cmd;
3045
3046 if (VIM_ISDIGIT(*cmd))
3047 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003048 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003049 {
3050 for (j = 0; p[j] != NUL; ++j)
3051 if (p[j] != cmdmods[i].name[j])
3052 break;
3053 if (!isalpha(p[j]) && j >= cmdmods[i].minlen
3054 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003055 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003056 }
3057 return 0;
3058}
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060/*
3061 * Return > 0 if an Ex command "name" exists.
3062 * Return 2 if there is an exact match.
3063 * Return 3 if there is an ambiguous match.
3064 */
3065 int
3066cmd_exists(name)
3067 char_u *name;
3068{
3069 exarg_T ea;
3070 int full = FALSE;
3071 int i;
3072 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003073 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074
3075 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003076 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
3078 for (j = 0; name[j] != NUL; ++j)
3079 if (name[j] != cmdmods[i].name[j])
3080 break;
3081 if (name[j] == NUL && j >= cmdmods[i].minlen)
3082 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3083 }
3084
Bram Moolenaara9587612006-05-04 21:47:50 +00003085 /* Check built-in commands and user defined commands.
3086 * For ":2match" and ":3match" we need to skip the number. */
3087 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003089 p = find_command(&ea, &full);
3090 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003092 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3093 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003094 if (*skipwhite(p) != NUL)
3095 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3097}
3098#endif
3099
3100/*
3101 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3102 * we don't need/want deleted. Maybe this could be done better if we didn't
3103 * repeat all this stuff. The only problem is that they may not stay
3104 * perfectly compatible with each other, but then the command line syntax
3105 * probably won't change that much -- webb.
3106 */
3107 char_u *
3108set_one_cmd_context(xp, buff)
3109 expand_T *xp;
3110 char_u *buff; /* buffer for command string */
3111{
3112 char_u *p;
3113 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003114 int len = 0;
3115 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3117 int compl = EXPAND_NOTHING;
3118#endif
3119#ifdef FEAT_CMDL_COMPL
3120 int delim;
3121#endif
3122 int forceit = FALSE;
3123 int usefilter = FALSE; /* filter instead of file name */
3124
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003125 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 xp->xp_pattern = buff;
3127 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003128 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129
3130/*
3131 * 2. skip comment lines and leading space, colons or bars
3132 */
3133 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3134 ;
3135 xp->xp_pattern = cmd;
3136
3137 if (*cmd == NUL)
3138 return NULL;
3139 if (*cmd == '"') /* ignore comment lines */
3140 {
3141 xp->xp_context = EXPAND_NOTHING;
3142 return NULL;
3143 }
3144
3145/*
3146 * 3. parse a range specifier of the form: addr [,addr] [;addr] ..
3147 */
3148 cmd = skip_range(cmd, &xp->xp_context);
3149
3150/*
3151 * 4. parse command
3152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 xp->xp_pattern = cmd;
3154 if (*cmd == NUL)
3155 return NULL;
3156 if (*cmd == '"')
3157 {
3158 xp->xp_context = EXPAND_NOTHING;
3159 return NULL;
3160 }
3161
3162 if (*cmd == '|' || *cmd == '\n')
3163 return cmd + 1; /* There's another command */
3164
3165 /*
3166 * Isolate the command and search for it in the command table.
3167 * Exceptions:
3168 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003169 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3171 */
3172 if (*cmd == 'k' && cmd[1] != 'e')
3173 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003174 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 p = cmd + 1;
3176 }
3177 else
3178 {
3179 p = cmd;
3180 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3181 ++p;
3182 /* check for non-alpha command */
3183 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3184 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003185 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003187 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 {
3189 xp->xp_context = EXPAND_UNSUCCESSFUL;
3190 return NULL;
3191 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003192 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003193 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3194 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3195 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 break;
3197
3198#ifdef FEAT_USR_CMDS
3199 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */
3201 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#endif
3203 }
3204
3205 /*
3206 * If the cursor is touching the command, and it ends in an alpha-numeric
3207 * character, complete the command name.
3208 */
3209 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3210 return NULL;
3211
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003212 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
3214 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3215 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003216 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 p = cmd + 1;
3218 }
3219#ifdef FEAT_USR_CMDS
3220 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3221 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003222 ea.cmd = cmd;
3223 p = find_ucmd(&ea, p, NULL, xp,
3224# if defined(FEAT_CMDL_COMPL)
3225 &compl
3226# else
3227 NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228# endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003229 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003230 if (p == NULL)
3231 ea.cmdidx = CMD_SIZE; /* ambiguous user command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233#endif
3234 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003235 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 {
3237 /* Not still touching the command and it was an illegal one */
3238 xp->xp_context = EXPAND_UNSUCCESSFUL;
3239 return NULL;
3240 }
3241
3242 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3243
3244 if (*p == '!') /* forced commands */
3245 {
3246 forceit = TRUE;
3247 ++p;
3248 }
3249
3250/*
3251 * 5. parse arguments
3252 */
3253#ifdef FEAT_USR_CMDS
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003254 if (!USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003256 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
3258 arg = skipwhite(p);
3259
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
3262 if (*arg == '>') /* append */
3263 {
3264 if (*++arg == '>')
3265 ++arg;
3266 arg = skipwhite(arg);
3267 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003268 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 {
3270 ++arg;
3271 usefilter = TRUE;
3272 }
3273 }
3274
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003275 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 {
3277 usefilter = forceit; /* :r! filter if forced */
3278 if (*arg == '!') /* :r !filter */
3279 {
3280 ++arg;
3281 usefilter = TRUE;
3282 }
3283 }
3284
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003285 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
3287 while (*arg == *cmd) /* allow any number of '>' or '<' */
3288 ++arg;
3289 arg = skipwhite(arg);
3290 }
3291
3292 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003293 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 {
3295 /* Check if we're in the +command */
3296 p = arg + 1;
3297 arg = skip_cmd_arg(arg, FALSE);
3298
3299 /* Still touching the command after '+'? */
3300 if (*arg == NUL)
3301 return p;
3302
3303 /* Skip space(s) after +command to get to the real argument */
3304 arg = skipwhite(arg);
3305 }
3306
3307 /*
3308 * Check for '|' to separate commands and '"' to start comments.
3309 * Don't do this for ":read !cmd" and ":write !cmd".
3310 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003311 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 p = arg;
3314 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003315 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 p += 2;
3317 while (*p)
3318 {
3319 if (*p == Ctrl_V)
3320 {
3321 if (p[1] != NUL)
3322 ++p;
3323 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 || *p == '|' || *p == '\n')
3326 {
3327 if (*(p - 1) != '\\')
3328 {
3329 if (*p == '|' || *p == '\n')
3330 return p + 1;
3331 return NULL; /* It's a comment */
3332 }
3333 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003334 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336 }
3337
3338 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003339 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 vim_strchr((char_u *)"|\"", *arg) == NULL)
3341 return NULL;
3342
3343 /* Find start of last argument (argument just before cursor): */
3344 p = buff + STRLEN(buff);
3345 while (p != arg && *p != ' ' && *p != TAB)
3346 p--;
3347 if (*p == ' ' || *p == TAB)
3348 p++;
3349 xp->xp_pattern = p;
3350
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003351 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003353 int c;
3354 int in_quote = FALSE;
3355 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
3357 /*
3358 * Allow spaces within back-quotes to count as part of the argument
3359 * being expanded.
3360 */
3361 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003362 p = xp->xp_pattern;
3363 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003365#ifdef FEAT_MBYTE
3366 if (has_mbyte)
3367 c = mb_ptr2char(p);
3368 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#endif
Bram Moolenaar6529c102007-08-18 15:47:34 +00003370 c = *p;
3371 if (c == '\\' && p[1] != NUL)
3372 ++p;
3373 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 {
3375 if (!in_quote)
3376 {
3377 xp->xp_pattern = p;
3378 bow = p + 1;
3379 }
3380 in_quote = !in_quote;
3381 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003382 /* An argument can contain just about everything, except
3383 * characters that end the command and white space. */
3384 else if (c == '|' || c == '\n' || c == '"' || (vim_iswhite(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003385#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003386 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003387#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003388 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003389 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003390 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003391 while (*p != NUL)
3392 {
3393#ifdef FEAT_MBYTE
3394 if (has_mbyte)
3395 c = mb_ptr2char(p);
3396 else
3397#endif
3398 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003399 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003400 break;
3401#ifdef FEAT_MBYTE
3402 if (has_mbyte)
3403 len = (*mb_ptr2len)(p);
3404 else
3405#endif
3406 len = 1;
3407 mb_ptr_adv(p);
3408 }
3409 if (in_quote)
3410 bow = p;
3411 else
3412 xp->xp_pattern = p;
3413 p -= len;
3414 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003415 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417
3418 /*
3419 * If we are still inside the quotes, and we passed a space, just
3420 * expand from there.
3421 */
3422 if (bow != NULL && in_quote)
3423 xp->xp_pattern = bow;
3424 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003425
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003426#ifndef BACKSLASH_IN_FILENAME
3427 /* For a shell command more chars need to be escaped. */
3428 if (usefilter || ea.cmdidx == CMD_bang)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003429 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003430 xp->xp_shell = TRUE;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003431
3432 /* When still after the command name expand executables. */
3433 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003434 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003435 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437
3438 /* Check for environment variable */
3439 if (*xp->xp_pattern == '$'
3440#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3441 || *xp->xp_pattern == '%'
3442#endif
3443 )
3444 {
3445 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3446 if (!vim_isIDc(*p))
3447 break;
3448 if (*p == NUL)
3449 {
3450 xp->xp_context = EXPAND_ENV_VARS;
3451 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003452#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
3453 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003454 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003455 compl = EXPAND_ENV_VARS;
3456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
3458 }
3459 }
3460
3461/*
3462 * 6. switch on command name
3463 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003464 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003466 case CMD_find:
3467 case CMD_sfind:
3468 case CMD_tabfind:
3469 xp->xp_context = EXPAND_FILES_IN_PATH;
3470 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 case CMD_cd:
3472 case CMD_chdir:
3473 case CMD_lcd:
3474 case CMD_lchdir:
3475 if (xp->xp_context == EXPAND_FILES)
3476 xp->xp_context = EXPAND_DIRECTORIES;
3477 break;
3478 case CMD_help:
3479 xp->xp_context = EXPAND_HELP;
3480 xp->xp_pattern = arg;
3481 break;
3482
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003483 /* Command modifiers: return the argument.
3484 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003486 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 case CMD_belowright:
3488 case CMD_botright:
3489 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003490 case CMD_bufdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003492 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 case CMD_folddoclosed:
3494 case CMD_folddoopen:
3495 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003496 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 case CMD_keepjumps:
3498 case CMD_keepmarks:
3499 case CMD_leftabove:
3500 case CMD_lockmarks:
3501 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003502 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003504 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 case CMD_topleft:
3506 case CMD_verbose:
3507 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003508 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 return arg;
3510
Bram Moolenaar4f688582007-07-24 12:34:30 +00003511#ifdef FEAT_CMDL_COMPL
3512# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 case CMD_match:
3514 if (*arg == NUL || !ends_excmd(*arg))
3515 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003516 /* also complete "None" */
3517 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 arg = skipwhite(skiptowhite(arg));
3519 if (*arg != NUL)
3520 {
3521 xp->xp_context = EXPAND_NOTHING;
3522 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3523 }
3524 }
3525 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528/*
3529 * All completion for the +cmdline_compl feature goes here.
3530 */
3531
3532# ifdef FEAT_USR_CMDS
3533 case CMD_command:
3534 /* Check for attributes */
3535 while (*arg == '-')
3536 {
3537 arg++; /* Skip "-" */
3538 p = skiptowhite(arg);
3539 if (*p == NUL)
3540 {
3541 /* Cursor is still in the attribute */
3542 p = vim_strchr(arg, '=');
3543 if (p == NULL)
3544 {
3545 /* No "=", so complete attribute names */
3546 xp->xp_context = EXPAND_USER_CMD_FLAGS;
3547 xp->xp_pattern = arg;
3548 return NULL;
3549 }
3550
3551 /* For the -complete and -nargs attributes, we complete
3552 * their arguments as well.
3553 */
3554 if (STRNICMP(arg, "complete", p - arg) == 0)
3555 {
3556 xp->xp_context = EXPAND_USER_COMPLETE;
3557 xp->xp_pattern = p + 1;
3558 return NULL;
3559 }
3560 else if (STRNICMP(arg, "nargs", p - arg) == 0)
3561 {
3562 xp->xp_context = EXPAND_USER_NARGS;
3563 xp->xp_pattern = p + 1;
3564 return NULL;
3565 }
3566 return NULL;
3567 }
3568 arg = skipwhite(p);
3569 }
3570
3571 /* After the attributes comes the new command name */
3572 p = skiptowhite(arg);
3573 if (*p == NUL)
3574 {
3575 xp->xp_context = EXPAND_USER_COMMANDS;
3576 xp->xp_pattern = arg;
3577 break;
3578 }
3579
3580 /* And finally comes a normal command */
3581 return skipwhite(p);
3582
3583 case CMD_delcommand:
3584 xp->xp_context = EXPAND_USER_COMMANDS;
3585 xp->xp_pattern = arg;
3586 break;
3587# endif
3588
3589 case CMD_global:
3590 case CMD_vglobal:
3591 delim = *arg; /* get the delimiter */
3592 if (delim)
3593 ++arg; /* skip delimiter if there is one */
3594
3595 while (arg[0] != NUL && arg[0] != delim)
3596 {
3597 if (arg[0] == '\\' && arg[1] != NUL)
3598 ++arg;
3599 ++arg;
3600 }
3601 if (arg[0] != NUL)
3602 return arg + 1;
3603 break;
3604 case CMD_and:
3605 case CMD_substitute:
3606 delim = *arg;
3607 if (delim)
3608 {
3609 /* skip "from" part */
3610 ++arg;
3611 arg = skip_regexp(arg, delim, p_magic, NULL);
3612 }
3613 /* skip "to" part */
3614 while (arg[0] != NUL && arg[0] != delim)
3615 {
3616 if (arg[0] == '\\' && arg[1] != NUL)
3617 ++arg;
3618 ++arg;
3619 }
3620 if (arg[0] != NUL) /* skip delimiter */
3621 ++arg;
3622 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3623 ++arg;
3624 if (arg[0] != NUL)
3625 return arg;
3626 break;
3627 case CMD_isearch:
3628 case CMD_dsearch:
3629 case CMD_ilist:
3630 case CMD_dlist:
3631 case CMD_ijump:
3632 case CMD_psearch:
3633 case CMD_djump:
3634 case CMD_isplit:
3635 case CMD_dsplit:
3636 arg = skipwhite(skipdigits(arg)); /* skip count */
3637 if (*arg == '/') /* Match regexp, not just whole words */
3638 {
3639 for (++arg; *arg && *arg != '/'; arg++)
3640 if (*arg == '\\' && arg[1] != NUL)
3641 arg++;
3642 if (*arg)
3643 {
3644 arg = skipwhite(arg + 1);
3645
3646 /* Check for trailing illegal characters */
3647 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3648 xp->xp_context = EXPAND_NOTHING;
3649 else
3650 return arg;
3651 }
3652 }
3653 break;
3654#ifdef FEAT_AUTOCMD
3655 case CMD_autocmd:
3656 return set_context_in_autocmd(xp, arg, FALSE);
3657
3658 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003659 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 return set_context_in_autocmd(xp, arg, TRUE);
3661#endif
3662 case CMD_set:
3663 set_context_in_set_cmd(xp, arg, 0);
3664 break;
3665 case CMD_setglobal:
3666 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3667 break;
3668 case CMD_setlocal:
3669 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3670 break;
3671 case CMD_tag:
3672 case CMD_stag:
3673 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003674 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 case CMD_tselect:
3676 case CMD_stselect:
3677 case CMD_ptselect:
3678 case CMD_tjump:
3679 case CMD_stjump:
3680 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003681 if (*p_wop != NUL)
3682 xp->xp_context = EXPAND_TAGS_LISTFILES;
3683 else
3684 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 xp->xp_pattern = arg;
3686 break;
3687 case CMD_augroup:
3688 xp->xp_context = EXPAND_AUGROUP;
3689 xp->xp_pattern = arg;
3690 break;
3691#ifdef FEAT_SYN_HL
3692 case CMD_syntax:
3693 set_context_in_syntax_cmd(xp, arg);
3694 break;
3695#endif
3696#ifdef FEAT_EVAL
3697 case CMD_let:
3698 case CMD_if:
3699 case CMD_elseif:
3700 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003701 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 case CMD_echo:
3703 case CMD_echon:
3704 case CMD_execute:
3705 case CMD_echomsg:
3706 case CMD_echoerr:
3707 case CMD_call:
3708 case CMD_return:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003709 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 break;
3711
3712 case CMD_unlet:
3713 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3714 arg = xp->xp_pattern + 1;
3715 xp->xp_context = EXPAND_USER_VARS;
3716 xp->xp_pattern = arg;
3717 break;
3718
3719 case CMD_function:
3720 case CMD_delfunction:
3721 xp->xp_context = EXPAND_USER_FUNC;
3722 xp->xp_pattern = arg;
3723 break;
3724
3725 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003726 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 break;
3728#endif
3729 case CMD_highlight:
3730 set_context_in_highlight_cmd(xp, arg);
3731 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003732#ifdef FEAT_CSCOPE
3733 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003734 case CMD_lcscope:
3735 case CMD_scscope:
3736 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003737 break;
3738#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003739#ifdef FEAT_SIGNS
3740 case CMD_sign:
3741 set_context_in_sign_cmd(xp, arg);
3742 break;
3743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744#ifdef FEAT_LISTCMDS
3745 case CMD_bdelete:
3746 case CMD_bwipeout:
3747 case CMD_bunload:
3748 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3749 arg = xp->xp_pattern + 1;
3750 /*FALLTHROUGH*/
3751 case CMD_buffer:
3752 case CMD_sbuffer:
3753 case CMD_checktime:
3754 xp->xp_context = EXPAND_BUFFERS;
3755 xp->xp_pattern = arg;
3756 break;
3757#endif
3758#ifdef FEAT_USR_CMDS
3759 case CMD_USER:
3760 case CMD_USER_BUF:
3761 if (compl != EXPAND_NOTHING)
3762 {
3763 /* XFILE: file names are handled above */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003764 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
3766# ifdef FEAT_MENU
3767 if (compl == EXPAND_MENUS)
3768 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3769# endif
3770 if (compl == EXPAND_COMMANDS)
3771 return arg;
3772 if (compl == EXPAND_MAPPINGS)
3773 return set_context_in_map_cmd(xp, (char_u *)"map",
3774 arg, forceit, FALSE, FALSE, CMD_map);
3775 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3776 arg = xp->xp_pattern + 1;
3777 xp->xp_pattern = arg;
3778 }
3779 xp->xp_context = compl;
3780 }
3781 break;
3782#endif
3783 case CMD_map: case CMD_noremap:
3784 case CMD_nmap: case CMD_nnoremap:
3785 case CMD_vmap: case CMD_vnoremap:
3786 case CMD_omap: case CMD_onoremap:
3787 case CMD_imap: case CMD_inoremap:
3788 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003789 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003791 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 case CMD_unmap:
3793 case CMD_nunmap:
3794 case CMD_vunmap:
3795 case CMD_ounmap:
3796 case CMD_iunmap:
3797 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003798 case CMD_lunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003800 FALSE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 case CMD_abbreviate: case CMD_noreabbrev:
3802 case CMD_cabbrev: case CMD_cnoreabbrev:
3803 case CMD_iabbrev: case CMD_inoreabbrev:
3804 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003805 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 case CMD_unabbreviate:
3807 case CMD_cunabbrev:
3808 case CMD_iunabbrev:
3809 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003810 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811#ifdef FEAT_MENU
3812 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
3813 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
3814 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
3815 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
3816 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
3817 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
3818 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
3819 case CMD_tmenu: case CMD_tunmenu:
3820 case CMD_popup: case CMD_tearoff: case CMD_emenu:
3821 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
3822#endif
3823
3824 case CMD_colorscheme:
3825 xp->xp_context = EXPAND_COLORS;
3826 xp->xp_pattern = arg;
3827 break;
3828
3829 case CMD_compiler:
3830 xp->xp_context = EXPAND_COMPILER;
3831 xp->xp_pattern = arg;
3832 break;
3833
Bram Moolenaar904fb862010-07-25 13:53:11 +02003834 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02003835 case CMD_ownsyntax:
3836 xp->xp_context = EXPAND_FILETYPE;
3837 xp->xp_pattern = arg;
3838 break;
3839
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
3841 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
3842 case CMD_language:
3843 if (*skiptowhite(arg) == NUL)
3844 {
3845 xp->xp_context = EXPAND_LANGUAGE;
3846 xp->xp_pattern = arg;
3847 }
3848 else
3849 xp->xp_context = EXPAND_NOTHING;
3850 break;
3851#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01003852#if defined(FEAT_PROFILE)
3853 case CMD_profile:
3854 set_context_in_profile_cmd(xp, arg);
3855 break;
3856#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003857 case CMD_behave:
3858 xp->xp_context = EXPAND_BEHAVE;
3859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
3861#endif /* FEAT_CMDL_COMPL */
3862
3863 default:
3864 break;
3865 }
3866 return NULL;
3867}
3868
3869/*
3870 * skip a range specifier of the form: addr [,addr] [;addr] ..
3871 *
3872 * Backslashed delimiters after / or ? will be skipped, and commands will
3873 * not be expanded between /'s and ?'s or after "'".
3874 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003875 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 * Returns the "cmd" pointer advanced to beyond the range.
3877 */
3878 char_u *
3879skip_range(cmd, ctx)
3880 char_u *cmd;
3881 int *ctx; /* pointer to xp_context or NULL */
3882{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003883 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
Bram Moolenaardf177f62005-02-22 08:39:57 +00003885 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
3887 if (*cmd == '\'')
3888 {
3889 if (*++cmd == NUL && ctx != NULL)
3890 *ctx = EXPAND_NOTHING;
3891 }
3892 else if (*cmd == '/' || *cmd == '?')
3893 {
3894 delim = *cmd++;
3895 while (*cmd != NUL && *cmd != delim)
3896 if (*cmd++ == '\\' && *cmd != NUL)
3897 ++cmd;
3898 if (*cmd == NUL && ctx != NULL)
3899 *ctx = EXPAND_NOTHING;
3900 }
3901 if (*cmd != NUL)
3902 ++cmd;
3903 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003904
3905 /* Skip ":" and white space. */
3906 while (*cmd == ':')
3907 cmd = skipwhite(cmd + 1);
3908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 return cmd;
3910}
3911
3912/*
3913 * get a single EX address
3914 *
3915 * Set ptr to the next character after the part that was interpreted.
3916 * Set ptr to NULL when an error is encountered.
3917 *
3918 * Return MAXLNUM when no Ex address was found.
3919 */
3920 static linenr_T
3921get_address(ptr, skip, to_other_file)
3922 char_u **ptr;
3923 int skip; /* only skip the address, don't use it */
3924 int to_other_file; /* flag: may jump to other file */
3925{
3926 int c;
3927 int i;
3928 long n;
3929 char_u *cmd;
3930 pos_T pos;
3931 pos_T *fp;
3932 linenr_T lnum;
3933
3934 cmd = skipwhite(*ptr);
3935 lnum = MAXLNUM;
3936 do
3937 {
3938 switch (*cmd)
3939 {
3940 case '.': /* '.' - Cursor position */
3941 ++cmd;
3942 lnum = curwin->w_cursor.lnum;
3943 break;
3944
3945 case '$': /* '$' - last line */
3946 ++cmd;
3947 lnum = curbuf->b_ml.ml_line_count;
3948 break;
3949
3950 case '\'': /* ''' - mark */
3951 if (*++cmd == NUL)
3952 {
3953 cmd = NULL;
3954 goto error;
3955 }
3956 if (skip)
3957 ++cmd;
3958 else
3959 {
3960 /* Only accept a mark in another file when it is
3961 * used by itself: ":'M". */
3962 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3963 ++cmd;
3964 if (fp == (pos_T *)-1)
3965 /* Jumped to another file. */
3966 lnum = curwin->w_cursor.lnum;
3967 else
3968 {
3969 if (check_mark(fp) == FAIL)
3970 {
3971 cmd = NULL;
3972 goto error;
3973 }
3974 lnum = fp->lnum;
3975 }
3976 }
3977 break;
3978
3979 case '/':
3980 case '?': /* '/' or '?' - search */
3981 c = *cmd++;
3982 if (skip) /* skip "/pat/" */
3983 {
3984 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3985 if (*cmd == c)
3986 ++cmd;
3987 }
3988 else
3989 {
3990 pos = curwin->w_cursor; /* save curwin->w_cursor */
3991 /*
3992 * When '/' or '?' follows another address, start
3993 * from there.
3994 */
3995 if (lnum != MAXLNUM)
3996 curwin->w_cursor.lnum = lnum;
3997 /*
3998 * Start a forward search at the end of the line.
3999 * Start a backward search at the start of the line.
4000 * This makes sure we never match in the current
4001 * line, and can match anywhere in the
4002 * next/previous line.
4003 */
4004 if (c == '/')
4005 curwin->w_cursor.col = MAXCOL;
4006 else
4007 curwin->w_cursor.col = 0;
4008 searchcmdlen = 0;
4009 if (!do_search(NULL, c, cmd, 1L,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004010 SEARCH_HIS | SEARCH_MSG, NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 {
4012 curwin->w_cursor = pos;
4013 cmd = NULL;
4014 goto error;
4015 }
4016 lnum = curwin->w_cursor.lnum;
4017 curwin->w_cursor = pos;
4018 /* adjust command string pointer */
4019 cmd += searchcmdlen;
4020 }
4021 break;
4022
4023 case '\\': /* "\?", "\/" or "\&", repeat search */
4024 ++cmd;
4025 if (*cmd == '&')
4026 i = RE_SUBST;
4027 else if (*cmd == '?' || *cmd == '/')
4028 i = RE_SEARCH;
4029 else
4030 {
4031 EMSG(_(e_backslash));
4032 cmd = NULL;
4033 goto error;
4034 }
4035
4036 if (!skip)
4037 {
4038 /*
4039 * When search follows another address, start from
4040 * there.
4041 */
4042 if (lnum != MAXLNUM)
4043 pos.lnum = lnum;
4044 else
4045 pos.lnum = curwin->w_cursor.lnum;
4046
4047 /*
4048 * Start the search just like for the above
4049 * do_search().
4050 */
4051 if (*cmd != '?')
4052 pos.col = MAXCOL;
4053 else
4054 pos.col = 0;
4055 if (searchit(curwin, curbuf, &pos,
4056 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaaraad86642008-03-10 20:34:59 +00004057 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004058 i, (linenr_T)0, NULL) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 lnum = pos.lnum;
4060 else
4061 {
4062 cmd = NULL;
4063 goto error;
4064 }
4065 }
4066 ++cmd;
4067 break;
4068
4069 default:
4070 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4071 lnum = getdigits(&cmd);
4072 }
4073
4074 for (;;)
4075 {
4076 cmd = skipwhite(cmd);
4077 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4078 break;
4079
4080 if (lnum == MAXLNUM)
4081 lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */
4082 if (VIM_ISDIGIT(*cmd))
4083 i = '+'; /* "number" is same as "+number" */
4084 else
4085 i = *cmd++;
4086 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4087 n = 1;
4088 else
4089 n = getdigits(&cmd);
4090 if (i == '-')
4091 lnum -= n;
4092 else
4093 lnum += n;
4094 }
4095 } while (*cmd == '/' || *cmd == '?');
4096
4097error:
4098 *ptr = cmd;
4099 return lnum;
4100}
4101
4102/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004103 * Get flags from an Ex command argument.
4104 */
4105 static void
4106get_flags(eap)
4107 exarg_T *eap;
4108{
4109 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4110 {
4111 if (*eap->arg == 'l')
4112 eap->flags |= EXFLAG_LIST;
4113 else if (*eap->arg == 'p')
4114 eap->flags |= EXFLAG_PRINT;
4115 else
4116 eap->flags |= EXFLAG_NR;
4117 eap->arg = skipwhite(eap->arg + 1);
4118 }
4119}
4120
4121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Function called for command which is Not Implemented. NI!
4123 */
4124 void
4125ex_ni(eap)
4126 exarg_T *eap;
4127{
4128 if (!eap->skip)
4129 eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version");
4130}
4131
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004132#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133/*
4134 * Function called for script command which is Not Implemented. NI!
4135 * Skips over ":perl <<EOF" constructs.
4136 */
4137 static void
4138ex_script_ni(eap)
4139 exarg_T *eap;
4140{
4141 if (!eap->skip)
4142 ex_ni(eap);
4143 else
4144 vim_free(script_get(eap, eap->arg));
4145}
4146#endif
4147
4148/*
4149 * Check range in Ex command for validity.
4150 * Return NULL when valid, error message when invalid.
4151 */
4152 static char_u *
4153invalid_range(eap)
4154 exarg_T *eap;
4155{
4156 if ( eap->line1 < 0
4157 || eap->line2 < 0
4158 || eap->line1 > eap->line2
4159 || ((eap->argt & RANGE)
4160 && !(eap->argt & NOTADR)
4161 && eap->line2 > curbuf->b_ml.ml_line_count
4162#ifdef FEAT_DIFF
4163 + (eap->cmdidx == CMD_diffget)
4164#endif
4165 ))
4166 return (char_u *)_(e_invrange);
4167 return NULL;
4168}
4169
4170/*
4171 * Correct the range for zero line number, if required.
4172 */
4173 static void
4174correct_range(eap)
4175 exarg_T *eap;
4176{
4177 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4178 {
4179 if (eap->line1 == 0)
4180 eap->line1 = 1;
4181 if (eap->line2 == 0)
4182 eap->line2 = 1;
4183 }
4184}
4185
Bram Moolenaar748bf032005-02-02 23:04:36 +00004186#ifdef FEAT_QUICKFIX
4187static char_u *skip_grep_pat __ARGS((exarg_T *eap));
4188
4189/*
4190 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4191 * pattern. Otherwise return eap->arg.
4192 */
4193 static char_u *
4194skip_grep_pat(eap)
4195 exarg_T *eap;
4196{
4197 char_u *p = eap->arg;
4198
Bram Moolenaara37420f2006-02-04 22:37:47 +00004199 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4200 || eap->cmdidx == CMD_vimgrepadd
4201 || eap->cmdidx == CMD_lvimgrepadd
4202 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004203 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004204 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004205 if (p == NULL)
4206 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004207 }
4208 return p;
4209}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004210
4211/*
4212 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4213 * in the command line, so that things like % get expanded.
4214 */
4215 static char_u *
4216replace_makeprg(eap, p, cmdlinep)
4217 exarg_T *eap;
4218 char_u *p;
4219 char_u **cmdlinep;
4220{
4221 char_u *new_cmdline;
4222 char_u *program;
4223 char_u *pos;
4224 char_u *ptr;
4225 int len;
4226 int i;
4227
4228 /*
4229 * Don't do it when ":vimgrep" is used for ":grep".
4230 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004231 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4232 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4233 || eap->cmdidx == CMD_grepadd
4234 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004235 && !grep_internal(eap->cmdidx))
4236 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004237 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4238 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004239 {
4240 if (*curbuf->b_p_gp == NUL)
4241 program = p_gp;
4242 else
4243 program = curbuf->b_p_gp;
4244 }
4245 else
4246 {
4247 if (*curbuf->b_p_mp == NUL)
4248 program = p_mp;
4249 else
4250 program = curbuf->b_p_mp;
4251 }
4252
4253 p = skipwhite(p);
4254
4255 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4256 {
4257 /* replace $* by given arguments */
4258 i = 1;
4259 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4260 ++i;
4261 len = (int)STRLEN(p);
4262 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4263 if (new_cmdline == NULL)
4264 return NULL; /* out of memory */
4265 ptr = new_cmdline;
4266 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4267 {
4268 i = (int)(pos - program);
4269 STRNCPY(ptr, program, i);
4270 STRCPY(ptr += i, p);
4271 ptr += len;
4272 program = pos + 2;
4273 }
4274 STRCPY(ptr, program);
4275 }
4276 else
4277 {
4278 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4279 if (new_cmdline == NULL)
4280 return NULL; /* out of memory */
4281 STRCPY(new_cmdline, program);
4282 STRCAT(new_cmdline, " ");
4283 STRCAT(new_cmdline, p);
4284 }
4285 msg_make(p);
4286
4287 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4288 vim_free(*cmdlinep);
4289 *cmdlinep = new_cmdline;
4290 p = new_cmdline;
4291 }
4292 return p;
4293}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004294#endif
4295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296/*
4297 * Expand file name in Ex command argument.
4298 * Return FAIL for failure, OK otherwise.
4299 */
4300 int
4301expand_filename(eap, cmdlinep, errormsgp)
4302 exarg_T *eap;
4303 char_u **cmdlinep;
4304 char_u **errormsgp;
4305{
4306 int has_wildcards; /* need to expand wildcards */
4307 char_u *repl;
4308 int srclen;
4309 char_u *p;
4310 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004311 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
Bram Moolenaar748bf032005-02-02 23:04:36 +00004313#ifdef FEAT_QUICKFIX
4314 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4315 p = skip_grep_pat(eap);
4316#else
4317 p = eap->arg;
4318#endif
4319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 /*
4321 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4322 * the file name contains a wildcard it should not cause expanding.
4323 * (it will be expanded anyway if there is a wildcard before replacing).
4324 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004325 has_wildcards = mch_has_wildcard(p);
4326 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004328#ifdef FEAT_EVAL
4329 /* Skip over `=expr`, wildcards in it are not expanded. */
4330 if (p[0] == '`' && p[1] == '=')
4331 {
4332 p += 2;
4333 (void)skip_expr(&p);
4334 if (*p == '`')
4335 ++p;
4336 continue;
4337 }
4338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 /*
4340 * Quick check if this cannot be the start of a special string.
4341 * Also removes backslash before '%', '#' and '<'.
4342 */
4343 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4344 {
4345 ++p;
4346 continue;
4347 }
4348
4349 /*
4350 * Try to find a match at this position.
4351 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004352 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4353 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (*errormsgp != NULL) /* error detected */
4355 return FAIL;
4356 if (repl == NULL) /* no match found */
4357 {
4358 p += srclen;
4359 continue;
4360 }
4361
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004362 /* Wildcards won't be expanded below, the replacement is taken
4363 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4364 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4365 {
4366 char_u *l = repl;
4367
4368 repl = expand_env_save(repl);
4369 vim_free(l);
4370 }
4371
Bram Moolenaar63b92542007-03-27 14:57:09 +00004372 /* Need to escape white space et al. with a backslash.
4373 * Don't do this for:
4374 * - replacement that already has been escaped: "##"
4375 * - shell commands (may have to use quotes instead).
4376 * - non-unix systems when there is a single argument (spaces don't
4377 * separate arguments then).
4378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004380 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 && eap->cmdidx != CMD_bang
4382 && eap->cmdidx != CMD_make
Bram Moolenaara37420f2006-02-04 22:37:47 +00004383 && eap->cmdidx != CMD_lmake
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 && eap->cmdidx != CMD_grep
Bram Moolenaara37420f2006-02-04 22:37:47 +00004385 && eap->cmdidx != CMD_lgrep
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 && eap->cmdidx != CMD_grepadd
Bram Moolenaara37420f2006-02-04 22:37:47 +00004387 && eap->cmdidx != CMD_lgrepadd
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388#ifndef UNIX
4389 && !(eap->argt & NOSPC)
4390#endif
4391 )
4392 {
4393 char_u *l;
4394#ifdef BACKSLASH_IN_FILENAME
4395 /* Don't escape a backslash here, because rem_backslash() doesn't
4396 * remove it later. */
4397 static char_u *nobslash = (char_u *)" \t\"|";
4398# define ESCAPE_CHARS nobslash
4399#else
4400# define ESCAPE_CHARS escape_chars
4401#endif
4402
4403 for (l = repl; *l; ++l)
4404 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4405 {
4406 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4407 if (l != NULL)
4408 {
4409 vim_free(repl);
4410 repl = l;
4411 }
4412 break;
4413 }
4414 }
4415
4416 /* For a shell command a '!' must be escaped. */
4417 if ((eap->usefilter || eap->cmdidx == CMD_bang)
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004418 && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420 char_u *l;
4421
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004422 l = vim_strsave_escaped(repl, (char_u *)"!&;()<>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (l != NULL)
4424 {
4425 vim_free(repl);
4426 repl = l;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004427 /* For a sh-like shell escape "!" another time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (strstr((char *)p_sh, "sh") != NULL)
4429 {
4430 l = vim_strsave_escaped(repl, (char_u *)"!");
4431 if (l != NULL)
4432 {
4433 vim_free(repl);
4434 repl = l;
4435 }
4436 }
4437 }
4438 }
4439
4440 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4441 vim_free(repl);
4442 if (p == NULL)
4443 return FAIL;
4444 }
4445
4446 /*
4447 * One file argument: Expand wildcards.
4448 * Don't do this with ":r !command" or ":w !command".
4449 */
4450 if ((eap->argt & NOSPC) && !eap->usefilter)
4451 {
4452 /*
4453 * May do this twice:
4454 * 1. Replace environment variables.
4455 * 2. Replace any other wildcards, remove backslashes.
4456 */
4457 for (n = 1; n <= 2; ++n)
4458 {
4459 if (n == 2)
4460 {
4461#ifdef UNIX
4462 /*
4463 * Only for Unix we check for more than one file name.
4464 * For other systems spaces are considered to be part
4465 * of the file name.
4466 * Only check here if there is no wildcard, otherwise
4467 * ExpandOne() will check for errors. This allows
4468 * ":e `ls ve*.c`" on Unix.
4469 */
4470 if (!has_wildcards)
4471 for (p = eap->arg; *p; ++p)
4472 {
4473 /* skip escaped characters */
4474 if (p[1] && (*p == '\\' || *p == Ctrl_V))
4475 ++p;
4476 else if (vim_iswhite(*p))
4477 {
4478 *errormsgp = (char_u *)_("E172: Only one file name allowed");
4479 return FAIL;
4480 }
4481 }
4482#endif
4483
4484 /*
4485 * Halve the number of backslashes (this is Vi compatible).
4486 * For Unix and OS/2, when wildcards are expanded, this is
4487 * done by ExpandOne() below.
4488 */
4489#if defined(UNIX) || defined(OS2)
4490 if (!has_wildcards)
4491#endif
4492 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
4495 if (has_wildcards)
4496 {
4497 if (n == 1)
4498 {
4499 /*
4500 * First loop: May expand environment variables. This
4501 * can be done much faster with expand_env() than with
4502 * something else (e.g., calling a shell).
4503 * After expanding environment variables, check again
4504 * if there are still wildcards present.
4505 */
4506 if (vim_strchr(eap->arg, '$') != NULL
4507 || vim_strchr(eap->arg, '~') != NULL)
4508 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004509 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004510 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 has_wildcards = mch_has_wildcard(NameBuff);
4512 p = NameBuff;
4513 }
4514 else
4515 p = NULL;
4516 }
4517 else /* n == 2 */
4518 {
4519 expand_T xpc;
4520
4521 ExpandInit(&xpc);
4522 xpc.xp_context = EXPAND_FILES;
4523 p = ExpandOne(&xpc, eap->arg, NULL,
4524 WILD_LIST_NOTFOUND|WILD_ADD_SLASH,
4525 WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (p == NULL)
4527 return FAIL;
4528 }
4529 if (p != NULL)
4530 {
4531 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4532 p, cmdlinep);
4533 if (n == 2) /* p came from ExpandOne() */
4534 vim_free(p);
4535 }
4536 }
4537 }
4538 }
4539 return OK;
4540}
4541
4542/*
4543 * Replace part of the command line, keeping eap->cmd, eap->arg and
4544 * eap->nextcmd correct.
4545 * "src" points to the part that is to be replaced, of length "srclen".
4546 * "repl" is the replacement string.
4547 * Returns a pointer to the character after the replaced string.
4548 * Returns NULL for failure.
4549 */
4550 static char_u *
4551repl_cmdline(eap, src, srclen, repl, cmdlinep)
4552 exarg_T *eap;
4553 char_u *src;
4554 int srclen;
4555 char_u *repl;
4556 char_u **cmdlinep;
4557{
4558 int len;
4559 int i;
4560 char_u *new_cmdline;
4561
4562 /*
4563 * The new command line is build in new_cmdline[].
4564 * First allocate it.
4565 * Careful: a "+cmd" argument may have been NUL terminated.
4566 */
4567 len = (int)STRLEN(repl);
4568 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004569 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
4571 if ((new_cmdline = alloc((unsigned)i)) == NULL)
4572 return NULL; /* out of memory! */
4573
4574 /*
4575 * Copy the stuff before the expanded part.
4576 * Copy the expanded stuff.
4577 * Copy what came after the expanded part.
4578 * Copy the next commands, if there are any.
4579 */
4580 i = (int)(src - *cmdlinep); /* length of part before match */
4581 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 mch_memmove(new_cmdline + i, repl, (size_t)len);
4584 i += len; /* remember the end of the string */
4585 STRCPY(new_cmdline + i, src + srclen);
4586 src = new_cmdline + i; /* remember where to continue */
4587
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004588 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
4590 i = (int)STRLEN(new_cmdline) + 1;
4591 STRCPY(new_cmdline + i, eap->nextcmd);
4592 eap->nextcmd = new_cmdline + i;
4593 }
4594 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4595 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4596 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4597 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4598 vim_free(*cmdlinep);
4599 *cmdlinep = new_cmdline;
4600
4601 return src;
4602}
4603
4604/*
4605 * Check for '|' to separate commands and '"' to start comments.
4606 */
4607 void
4608separate_nextcmd(eap)
4609 exarg_T *eap;
4610{
4611 char_u *p;
4612
Bram Moolenaar86b68352004-12-27 21:59:20 +00004613#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004614 p = skip_grep_pat(eap);
4615#else
4616 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004617#endif
4618
4619 for ( ; *p; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
4621 if (*p == Ctrl_V)
4622 {
4623 if (eap->argt & (USECTRLV | XFILE))
4624 ++p; /* skip CTRL-V and next char */
4625 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004626 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004627 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if (*p == NUL) /* stop at NUL after CTRL-V */
4629 break;
4630 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004631
4632#ifdef FEAT_EVAL
4633 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00004634 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004635 {
4636 p += 2;
4637 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004638 }
4639#endif
4640
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 /* Check for '"': start of comment or '|': next command */
4642 /* :@" and :*" do not start a comment!
4643 * :redir @" doesn't either. */
4644 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
4645 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4646 || p != eap->arg)
4647 && (eap->cmdidx != CMD_redir
4648 || p != eap->arg + 1 || p[-1] != '@'))
4649 || *p == '|' || *p == '\n')
4650 {
4651 /*
4652 * We remove the '\' before the '|', unless USECTRLV is used
4653 * AND 'b' is present in 'cpoptions'.
4654 */
4655 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
4656 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
4657 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004658 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 --p;
4660 }
4661 else
4662 {
4663 eap->nextcmd = check_nextcmd(p);
4664 *p = NUL;
4665 break;
4666 }
4667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004669
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
4671 del_trailing_spaces(eap->arg);
4672}
4673
4674/*
4675 * get + command from ex argument
4676 */
4677 static char_u *
4678getargcmd(argp)
4679 char_u **argp;
4680{
4681 char_u *arg = *argp;
4682 char_u *command = NULL;
4683
4684 if (*arg == '+') /* +[command] */
4685 {
4686 ++arg;
4687 if (vim_isspace(*arg))
4688 command = dollar_command;
4689 else
4690 {
4691 command = arg;
4692 arg = skip_cmd_arg(command, TRUE);
4693 if (*arg != NUL)
4694 *arg++ = NUL; /* terminate command with NUL */
4695 }
4696
4697 arg = skipwhite(arg); /* skip over spaces */
4698 *argp = arg;
4699 }
4700 return command;
4701}
4702
4703/*
4704 * Find end of "+command" argument. Skip over "\ " and "\\".
4705 */
4706 static char_u *
4707skip_cmd_arg(p, rembs)
4708 char_u *p;
4709 int rembs; /* TRUE to halve the number of backslashes */
4710{
4711 while (*p && !vim_isspace(*p))
4712 {
4713 if (*p == '\\' && p[1] != NUL)
4714 {
4715 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004716 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 else
4718 ++p;
4719 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004720 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722 return p;
4723}
4724
4725/*
4726 * Get "++opt=arg" argument.
4727 * Return FAIL or OK.
4728 */
4729 static int
4730getargopt(eap)
4731 exarg_T *eap;
4732{
4733 char_u *arg = eap->arg + 2;
4734 int *pp = NULL;
4735#ifdef FEAT_MBYTE
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004736 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 char_u *p;
4738#endif
4739
4740 /* ":edit ++[no]bin[ary] file" */
4741 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4742 {
4743 if (*arg == 'n')
4744 {
4745 arg += 2;
4746 eap->force_bin = FORCE_NOBIN;
4747 }
4748 else
4749 eap->force_bin = FORCE_BIN;
4750 if (!checkforcmd(&arg, "binary", 3))
4751 return FAIL;
4752 eap->arg = skipwhite(arg);
4753 return OK;
4754 }
4755
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004756 /* ":read ++edit file" */
4757 if (STRNCMP(arg, "edit", 4) == 0)
4758 {
4759 eap->read_edit = TRUE;
4760 eap->arg = skipwhite(arg + 4);
4761 return OK;
4762 }
4763
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 if (STRNCMP(arg, "ff", 2) == 0)
4765 {
4766 arg += 2;
4767 pp = &eap->force_ff;
4768 }
4769 else if (STRNCMP(arg, "fileformat", 10) == 0)
4770 {
4771 arg += 10;
4772 pp = &eap->force_ff;
4773 }
4774#ifdef FEAT_MBYTE
4775 else if (STRNCMP(arg, "enc", 3) == 0)
4776 {
4777 arg += 3;
4778 pp = &eap->force_enc;
4779 }
4780 else if (STRNCMP(arg, "encoding", 8) == 0)
4781 {
4782 arg += 8;
4783 pp = &eap->force_enc;
4784 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004785 else if (STRNCMP(arg, "bad", 3) == 0)
4786 {
4787 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004788 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790#endif
4791
4792 if (pp == NULL || *arg != '=')
4793 return FAIL;
4794
4795 ++arg;
4796 *pp = (int)(arg - eap->cmd);
4797 arg = skip_cmd_arg(arg, FALSE);
4798 eap->arg = skipwhite(arg);
4799 *arg = NUL;
4800
4801#ifdef FEAT_MBYTE
4802 if (pp == &eap->force_ff)
4803 {
4804#endif
4805 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4806 return FAIL;
4807#ifdef FEAT_MBYTE
4808 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004809 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 /* Make 'fileencoding' lower case. */
4812 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4813 *p = TOLOWER_ASC(*p);
4814 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004815 else
4816 {
4817 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4818 * "drop". */
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004819 p = eap->cmd + bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004820 if (STRICMP(p, "keep") == 0)
4821 eap->bad_char = BAD_KEEP;
4822 else if (STRICMP(p, "drop") == 0)
4823 eap->bad_char = BAD_DROP;
4824 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4825 eap->bad_char = *p;
4826 else
4827 return FAIL;
4828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829#endif
4830
4831 return OK;
4832}
4833
4834/*
4835 * ":abbreviate" and friends.
4836 */
4837 static void
4838ex_abbreviate(eap)
4839 exarg_T *eap;
4840{
4841 do_exmap(eap, TRUE); /* almost the same as mapping */
4842}
4843
4844/*
4845 * ":map" and friends.
4846 */
4847 static void
4848ex_map(eap)
4849 exarg_T *eap;
4850{
4851 /*
4852 * If we are sourcing .exrc or .vimrc in current directory we
4853 * print the mappings for security reasons.
4854 */
4855 if (secure)
4856 {
4857 secure = 2;
4858 msg_outtrans(eap->cmd);
4859 msg_putchar('\n');
4860 }
4861 do_exmap(eap, FALSE);
4862}
4863
4864/*
4865 * ":unmap" and friends.
4866 */
4867 static void
4868ex_unmap(eap)
4869 exarg_T *eap;
4870{
4871 do_exmap(eap, FALSE);
4872}
4873
4874/*
4875 * ":mapclear" and friends.
4876 */
4877 static void
4878ex_mapclear(eap)
4879 exarg_T *eap;
4880{
4881 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
4882}
4883
4884/*
4885 * ":abclear" and friends.
4886 */
4887 static void
4888ex_abclear(eap)
4889 exarg_T *eap;
4890{
4891 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
4892}
4893
4894#ifdef FEAT_AUTOCMD
4895 static void
4896ex_autocmd(eap)
4897 exarg_T *eap;
4898{
4899 /*
4900 * Disallow auto commands from .exrc and .vimrc in current
4901 * directory for security reasons.
4902 */
4903 if (secure)
4904 {
4905 secure = 2;
4906 eap->errmsg = e_curdir;
4907 }
4908 else if (eap->cmdidx == CMD_autocmd)
4909 do_autocmd(eap->arg, eap->forceit);
4910 else
4911 do_augroup(eap->arg, eap->forceit);
4912}
4913
4914/*
4915 * ":doautocmd": Apply the automatic commands to the current buffer.
4916 */
4917 static void
4918ex_doautocmd(eap)
4919 exarg_T *eap;
4920{
4921 (void)do_doautocmd(eap->arg, TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00004922 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923}
4924#endif
4925
4926#ifdef FEAT_LISTCMDS
4927/*
4928 * :[N]bunload[!] [N] [bufname] unload buffer
4929 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4930 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4931 */
4932 static void
4933ex_bunload(eap)
4934 exarg_T *eap;
4935{
4936 eap->errmsg = do_bufdel(
4937 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4938 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4939 : DOBUF_UNLOAD, eap->arg,
4940 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4941}
4942
4943/*
4944 * :[N]buffer [N] to buffer N
4945 * :[N]sbuffer [N] to buffer N
4946 */
4947 static void
4948ex_buffer(eap)
4949 exarg_T *eap;
4950{
4951 if (*eap->arg)
4952 eap->errmsg = e_trailing;
4953 else
4954 {
4955 if (eap->addr_count == 0) /* default is current buffer */
4956 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4957 else
4958 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
4959 }
4960}
4961
4962/*
4963 * :[N]bmodified [N] to next mod. buffer
4964 * :[N]sbmodified [N] to next mod. buffer
4965 */
4966 static void
4967ex_bmodified(eap)
4968 exarg_T *eap;
4969{
4970 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
4971}
4972
4973/*
4974 * :[N]bnext [N] to next buffer
4975 * :[N]sbnext [N] split and to next buffer
4976 */
4977 static void
4978ex_bnext(eap)
4979 exarg_T *eap;
4980{
4981 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
4982}
4983
4984/*
4985 * :[N]bNext [N] to previous buffer
4986 * :[N]bprevious [N] to previous buffer
4987 * :[N]sbNext [N] split and to previous buffer
4988 * :[N]sbprevious [N] split and to previous buffer
4989 */
4990 static void
4991ex_bprevious(eap)
4992 exarg_T *eap;
4993{
4994 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
4995}
4996
4997/*
4998 * :brewind to first buffer
4999 * :bfirst to first buffer
5000 * :sbrewind split and to first buffer
5001 * :sbfirst split and to first buffer
5002 */
5003 static void
5004ex_brewind(eap)
5005 exarg_T *eap;
5006{
5007 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
5008}
5009
5010/*
5011 * :blast to last buffer
5012 * :sblast split and to last buffer
5013 */
5014 static void
5015ex_blast(eap)
5016 exarg_T *eap;
5017{
5018 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
5019}
5020#endif
5021
5022 int
5023ends_excmd(c)
5024 int c;
5025{
5026 return (c == NUL || c == '|' || c == '"' || c == '\n');
5027}
5028
5029#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5030 || defined(PROTO)
5031/*
5032 * Return the next command, after the first '|' or '\n'.
5033 * Return NULL if not found.
5034 */
5035 char_u *
5036find_nextcmd(p)
5037 char_u *p;
5038{
5039 while (*p != '|' && *p != '\n')
5040 {
5041 if (*p == NUL)
5042 return NULL;
5043 ++p;
5044 }
5045 return (p + 1);
5046}
5047#endif
5048
5049/*
5050 * Check if *p is a separator between Ex commands.
5051 * Return NULL if it isn't, (p + 1) if it is.
5052 */
5053 char_u *
5054check_nextcmd(p)
5055 char_u *p;
5056{
5057 p = skipwhite(p);
5058 if (*p == '|' || *p == '\n')
5059 return (p + 1);
5060 else
5061 return NULL;
5062}
5063
5064/*
5065 * - if there are more files to edit
5066 * - and this is the last window
5067 * - and forceit not used
5068 * - and not repeated twice on a row
5069 * return FAIL and give error message if 'message' TRUE
5070 * return OK otherwise
5071 */
5072 static int
5073check_more(message, forceit)
5074 int message; /* when FALSE check only, no messages */
5075 int forceit;
5076{
5077 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5078
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005079 if (!forceit && only_one_window()
5080 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
5082 if (message)
5083 {
5084#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5085 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5086 {
5087 char_u buff[IOSIZE];
5088
5089 if (n == 1)
5090 STRCPY(buff, _("1 more file to edit. Quit anyway?"));
5091 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005092 vim_snprintf((char *)buff, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 _("%d more files to edit. Quit anyway?"), n);
5094 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5095 return OK;
5096 return FAIL;
5097 }
5098#endif
5099 if (n == 1)
5100 EMSG(_("E173: 1 more file to edit"));
5101 else
5102 EMSGN(_("E173: %ld more files to edit"), n);
5103 quitmore = 2; /* next try to quit is allowed */
5104 }
5105 return FAIL;
5106 }
5107 return OK;
5108}
5109
5110#ifdef FEAT_CMDL_COMPL
5111/*
5112 * Function given to ExpandGeneric() to obtain the list of command names.
5113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 char_u *
5115get_command_name(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005116 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 int idx;
5118{
5119 if (idx >= (int)CMD_SIZE)
5120# ifdef FEAT_USR_CMDS
5121 return get_user_command_name(idx);
5122# else
5123 return NULL;
5124# endif
5125 return cmdnames[idx].cmd_name;
5126}
5127#endif
5128
5129#if defined(FEAT_USR_CMDS) || defined(PROTO)
5130static 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));
5131static void uc_list __ARGS((char_u *name, size_t name_len));
5132static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg));
5133static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp));
5134static 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));
5135
5136 static int
5137uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force)
5138 char_u *name;
5139 size_t name_len;
5140 char_u *rep;
5141 long argt;
5142 long def;
5143 int flags;
5144 int compl;
5145 char_u *compl_arg;
5146 int force;
5147{
5148 ucmd_T *cmd = NULL;
5149 char_u *p;
5150 int i;
5151 int cmp = 1;
5152 char_u *rep_buf = NULL;
5153 garray_T *gap;
5154
Bram Moolenaar9c102382006-05-03 21:26:49 +00005155 replace_termcodes(rep, &rep_buf, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (rep_buf == NULL)
5157 {
5158 /* Can't replace termcodes - try using the string as is */
5159 rep_buf = vim_strsave(rep);
5160
5161 /* Give up if out of memory */
5162 if (rep_buf == NULL)
5163 return FAIL;
5164 }
5165
5166 /* get address of growarray: global or in curbuf */
5167 if (flags & UC_BUFFER)
5168 {
5169 gap = &curbuf->b_ucmds;
5170 if (gap->ga_itemsize == 0)
5171 ga_init2(gap, (int)sizeof(ucmd_T), 4);
5172 }
5173 else
5174 gap = &ucmds;
5175
5176 /* Search for the command in the already defined commands. */
5177 for (i = 0; i < gap->ga_len; ++i)
5178 {
5179 size_t len;
5180
5181 cmd = USER_CMD_GA(gap, i);
5182 len = STRLEN(cmd->uc_name);
5183 cmp = STRNCMP(name, cmd->uc_name, name_len);
5184 if (cmp == 0)
5185 {
5186 if (name_len < len)
5187 cmp = -1;
5188 else if (name_len > len)
5189 cmp = 1;
5190 }
5191
5192 if (cmp == 0)
5193 {
5194 if (!force)
5195 {
5196 EMSG(_("E174: Command already exists: add ! to replace it"));
5197 goto fail;
5198 }
5199
5200 vim_free(cmd->uc_rep);
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00005201 cmd->uc_rep = NULL;
5202#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5203 vim_free(cmd->uc_compl_arg);
5204 cmd->uc_compl_arg = NULL;
5205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 break;
5207 }
5208
5209 /* Stop as soon as we pass the name to add */
5210 if (cmp < 0)
5211 break;
5212 }
5213
5214 /* Extend the array unless we're replacing an existing command */
5215 if (cmp != 0)
5216 {
5217 if (ga_grow(gap, 1) != OK)
5218 goto fail;
5219 if ((p = vim_strnsave(name, (int)name_len)) == NULL)
5220 goto fail;
5221
5222 cmd = USER_CMD_GA(gap, i);
5223 mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T));
5224
5225 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
5227 cmd->uc_name = p;
5228 }
5229
5230 cmd->uc_rep = rep_buf;
5231 cmd->uc_argt = argt;
5232 cmd->uc_def = def;
5233 cmd->uc_compl = compl;
5234#ifdef FEAT_EVAL
5235 cmd->uc_scriptID = current_SID;
5236# ifdef FEAT_CMDL_COMPL
5237 cmd->uc_compl_arg = compl_arg;
5238# endif
5239#endif
5240
5241 return OK;
5242
5243fail:
5244 vim_free(rep_buf);
5245#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5246 vim_free(compl_arg);
5247#endif
5248 return FAIL;
5249}
5250
5251/*
5252 * List of names for completion for ":command" with the EXPAND_ flag.
5253 * Must be alphabetical for completion.
5254 */
5255static struct
5256{
5257 int expand;
5258 char *name;
5259} command_complete[] =
5260{
5261 {EXPAND_AUGROUP, "augroup"},
5262 {EXPAND_BUFFERS, "buffer"},
5263 {EXPAND_COMMANDS, "command"},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005264#if defined(FEAT_CSCOPE)
5265 {EXPAND_CSCOPE, "cscope"},
5266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5268 {EXPAND_USER_DEFINED, "custom"},
Bram Moolenaara466c992005-07-09 21:03:22 +00005269 {EXPAND_USER_LIST, "customlist"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#endif
5271 {EXPAND_DIRECTORIES, "dir"},
5272 {EXPAND_ENV_VARS, "environment"},
5273 {EXPAND_EVENTS, "event"},
5274 {EXPAND_EXPRESSION, "expression"},
5275 {EXPAND_FILES, "file"},
5276 {EXPAND_FUNCTIONS, "function"},
5277 {EXPAND_HELP, "help"},
5278 {EXPAND_HIGHLIGHT, "highlight"},
5279 {EXPAND_MAPPINGS, "mapping"},
5280 {EXPAND_MENUS, "menu"},
5281 {EXPAND_SETTINGS, "option"},
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005282 {EXPAND_SHELLCMD, "shellcmd"},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005283#if defined(FEAT_SIGNS)
5284 {EXPAND_SIGN, "sign"},
5285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {EXPAND_TAGS, "tag"},
5287 {EXPAND_TAGS_LISTFILES, "tag_listfiles"},
5288 {EXPAND_USER_VARS, "var"},
5289 {0, NULL}
5290};
5291
5292 static void
5293uc_list(name, name_len)
5294 char_u *name;
5295 size_t name_len;
5296{
5297 int i, j;
5298 int found = FALSE;
5299 ucmd_T *cmd;
5300 int len;
5301 long a;
5302 garray_T *gap;
5303
5304 gap = &curbuf->b_ucmds;
5305 for (;;)
5306 {
5307 for (i = 0; i < gap->ga_len; ++i)
5308 {
5309 cmd = USER_CMD_GA(gap, i);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005310 a = (long)cmd->uc_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312 /* Skip commands which don't match the requested prefix */
5313 if (STRNCMP(name, cmd->uc_name, name_len) != 0)
5314 continue;
5315
5316 /* Put out the title first time */
5317 if (!found)
5318 MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition"));
5319 found = TRUE;
5320 msg_putchar('\n');
5321 if (got_int)
5322 break;
5323
5324 /* Special cases */
5325 msg_putchar(a & BANG ? '!' : ' ');
5326 msg_putchar(a & REGSTR ? '"' : ' ');
5327 msg_putchar(gap != &ucmds ? 'b' : ' ');
5328 msg_putchar(' ');
5329
5330 msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D));
5331 len = (int)STRLEN(cmd->uc_name) + 4;
5332
5333 do {
5334 msg_putchar(' ');
5335 ++len;
5336 } while (len < 16);
5337
5338 len = 0;
5339
5340 /* Arguments */
5341 switch ((int)(a & (EXTRA|NOSPC|NEEDARG)))
5342 {
5343 case 0: IObuff[len++] = '0'; break;
5344 case (EXTRA): IObuff[len++] = '*'; break;
5345 case (EXTRA|NOSPC): IObuff[len++] = '?'; break;
5346 case (EXTRA|NEEDARG): IObuff[len++] = '+'; break;
5347 case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break;
5348 }
5349
5350 do {
5351 IObuff[len++] = ' ';
5352 } while (len < 5);
5353
5354 /* Range */
5355 if (a & (RANGE|COUNT))
5356 {
5357 if (a & COUNT)
5358 {
5359 /* -count=N */
5360 sprintf((char *)IObuff + len, "%ldc", cmd->uc_def);
5361 len += (int)STRLEN(IObuff + len);
5362 }
5363 else if (a & DFLALL)
5364 IObuff[len++] = '%';
5365 else if (cmd->uc_def >= 0)
5366 {
5367 /* -range=N */
5368 sprintf((char *)IObuff + len, "%ld", cmd->uc_def);
5369 len += (int)STRLEN(IObuff + len);
5370 }
5371 else
5372 IObuff[len++] = '.';
5373 }
5374
5375 do {
5376 IObuff[len++] = ' ';
5377 } while (len < 11);
5378
5379 /* Completion */
5380 for (j = 0; command_complete[j].expand != 0; ++j)
5381 if (command_complete[j].expand == cmd->uc_compl)
5382 {
5383 STRCPY(IObuff + len, command_complete[j].name);
5384 len += (int)STRLEN(IObuff + len);
5385 break;
5386 }
5387
5388 do {
5389 IObuff[len++] = ' ';
5390 } while (len < 21);
5391
5392 IObuff[len] = '\0';
5393 msg_outtrans(IObuff);
5394
5395 msg_outtrans_special(cmd->uc_rep, FALSE);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00005396#ifdef FEAT_EVAL
5397 if (p_verbose > 0)
5398 last_set_msg(cmd->uc_scriptID);
5399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 out_flush();
5401 ui_breakcheck();
5402 if (got_int)
5403 break;
5404 }
5405 if (gap == &ucmds || i < gap->ga_len)
5406 break;
5407 gap = &ucmds;
5408 }
5409
5410 if (!found)
5411 MSG(_("No user-defined commands found"));
5412}
5413
5414 static char_u *
5415uc_fun_cmd()
5416{
5417 static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4,
5418 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60,
5419 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2,
5420 0xb9, 0x7f, 0};
5421 int i;
5422
5423 for (i = 0; fcmd[i]; ++i)
5424 IObuff[i] = fcmd[i] - 0x40;
5425 IObuff[i] = 0;
5426 return IObuff;
5427}
5428
5429 static int
5430uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg)
5431 char_u *attr;
5432 size_t len;
5433 long *argt;
5434 long *def;
5435 int *flags;
5436 int *compl;
5437 char_u **compl_arg;
5438{
5439 char_u *p;
5440
5441 if (len == 0)
5442 {
5443 EMSG(_("E175: No attribute specified"));
5444 return FAIL;
5445 }
5446
5447 /* First, try the simple attributes (no arguments) */
5448 if (STRNICMP(attr, "bang", len) == 0)
5449 *argt |= BANG;
5450 else if (STRNICMP(attr, "buffer", len) == 0)
5451 *flags |= UC_BUFFER;
5452 else if (STRNICMP(attr, "register", len) == 0)
5453 *argt |= REGSTR;
5454 else if (STRNICMP(attr, "bar", len) == 0)
5455 *argt |= TRLBAR;
5456 else
5457 {
5458 int i;
5459 char_u *val = NULL;
5460 size_t vallen = 0;
5461 size_t attrlen = len;
5462
5463 /* Look for the attribute name - which is the part before any '=' */
5464 for (i = 0; i < (int)len; ++i)
5465 {
5466 if (attr[i] == '=')
5467 {
5468 val = &attr[i + 1];
5469 vallen = len - i - 1;
5470 attrlen = i;
5471 break;
5472 }
5473 }
5474
5475 if (STRNICMP(attr, "nargs", attrlen) == 0)
5476 {
5477 if (vallen == 1)
5478 {
5479 if (*val == '0')
5480 /* Do nothing - this is the default */;
5481 else if (*val == '1')
5482 *argt |= (EXTRA | NOSPC | NEEDARG);
5483 else if (*val == '*')
5484 *argt |= EXTRA;
5485 else if (*val == '?')
5486 *argt |= (EXTRA | NOSPC);
5487 else if (*val == '+')
5488 *argt |= (EXTRA | NEEDARG);
5489 else
5490 goto wrong_nargs;
5491 }
5492 else
5493 {
5494wrong_nargs:
5495 EMSG(_("E176: Invalid number of arguments"));
5496 return FAIL;
5497 }
5498 }
5499 else if (STRNICMP(attr, "range", attrlen) == 0)
5500 {
5501 *argt |= RANGE;
5502 if (vallen == 1 && *val == '%')
5503 *argt |= DFLALL;
5504 else if (val != NULL)
5505 {
5506 p = val;
5507 if (*def >= 0)
5508 {
5509two_count:
5510 EMSG(_("E177: Count cannot be specified twice"));
5511 return FAIL;
5512 }
5513
5514 *def = getdigits(&p);
5515 *argt |= (ZEROR | NOTADR);
5516
5517 if (p != val + vallen || vallen == 0)
5518 {
5519invalid_count:
5520 EMSG(_("E178: Invalid default value for count"));
5521 return FAIL;
5522 }
5523 }
5524 }
5525 else if (STRNICMP(attr, "count", attrlen) == 0)
5526 {
Bram Moolenaar32e7b2d2005-02-27 22:36:47 +00005527 *argt |= (COUNT | ZEROR | RANGE | NOTADR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
5529 if (val != NULL)
5530 {
5531 p = val;
5532 if (*def >= 0)
5533 goto two_count;
5534
5535 *def = getdigits(&p);
5536
5537 if (p != val + vallen)
5538 goto invalid_count;
5539 }
5540
5541 if (*def < 0)
5542 *def = 0;
5543 }
5544 else if (STRNICMP(attr, "complete", attrlen) == 0)
5545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 if (val == NULL)
5547 {
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005548 EMSG(_("E179: argument required for -complete"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 return FAIL;
5550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005552 if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg)
5553 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556 else
5557 {
5558 char_u ch = attr[len];
5559 attr[len] = '\0';
5560 EMSG2(_("E181: Invalid attribute: %s"), attr);
5561 attr[len] = ch;
5562 return FAIL;
5563 }
5564 }
5565
5566 return OK;
5567}
5568
Bram Moolenaara850a712009-01-28 14:42:59 +00005569/*
5570 * ":command ..."
5571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 static void
5573ex_command(eap)
5574 exarg_T *eap;
5575{
5576 char_u *name;
5577 char_u *end;
5578 char_u *p;
5579 long argt = 0;
5580 long def = -1;
5581 int flags = 0;
5582 int compl = EXPAND_NOTHING;
5583 char_u *compl_arg = NULL;
5584 int has_attr = (eap->arg[0] == '-');
5585
5586 p = eap->arg;
5587
5588 /* Check for attributes */
5589 while (*p == '-')
5590 {
5591 ++p;
5592 end = skiptowhite(p);
5593 if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg)
5594 == FAIL)
5595 return;
5596 p = skipwhite(end);
5597 }
5598
5599 /* Get the name (if any) and skip to the following argument */
5600 name = p;
5601 if (ASCII_ISALPHA(*p))
5602 while (ASCII_ISALNUM(*p))
5603 ++p;
5604 if (!ends_excmd(*p) && !vim_iswhite(*p))
5605 {
5606 EMSG(_("E182: Invalid command name"));
5607 return;
5608 }
5609 end = p;
5610
5611 /* If there is nothing after the name, and no attributes were specified,
5612 * we are listing commands
5613 */
5614 p = skipwhite(end);
5615 if (!has_attr && ends_excmd(*p))
5616 {
5617 uc_list(name, end - name);
5618 }
5619 else if (!ASCII_ISUPPER(*name))
5620 {
5621 EMSG(_("E183: User defined commands must start with an uppercase letter"));
5622 return;
5623 }
5624 else
5625 uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg,
5626 eap->forceit);
5627}
5628
5629/*
5630 * ":comclear"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 * Clear all user commands, global and for current buffer.
5632 */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00005633 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634ex_comclear(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00005635 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 uc_clear(&ucmds);
5638 uc_clear(&curbuf->b_ucmds);
5639}
5640
5641/*
5642 * Clear all user commands for "gap".
5643 */
5644 void
5645uc_clear(gap)
5646 garray_T *gap;
5647{
5648 int i;
5649 ucmd_T *cmd;
5650
5651 for (i = 0; i < gap->ga_len; ++i)
5652 {
5653 cmd = USER_CMD_GA(gap, i);
5654 vim_free(cmd->uc_name);
5655 vim_free(cmd->uc_rep);
5656# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5657 vim_free(cmd->uc_compl_arg);
5658# endif
5659 }
5660 ga_clear(gap);
5661}
5662
5663 static void
5664ex_delcommand(eap)
5665 exarg_T *eap;
5666{
5667 int i = 0;
5668 ucmd_T *cmd = NULL;
5669 int cmp = -1;
5670 garray_T *gap;
5671
5672 gap = &curbuf->b_ucmds;
5673 for (;;)
5674 {
5675 for (i = 0; i < gap->ga_len; ++i)
5676 {
5677 cmd = USER_CMD_GA(gap, i);
5678 cmp = STRCMP(eap->arg, cmd->uc_name);
5679 if (cmp <= 0)
5680 break;
5681 }
5682 if (gap == &ucmds || cmp == 0)
5683 break;
5684 gap = &ucmds;
5685 }
5686
5687 if (cmp != 0)
5688 {
5689 EMSG2(_("E184: No such user-defined command: %s"), eap->arg);
5690 return;
5691 }
5692
5693 vim_free(cmd->uc_name);
5694 vim_free(cmd->uc_rep);
5695# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
5696 vim_free(cmd->uc_compl_arg);
5697# endif
5698
5699 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
5701 if (i < gap->ga_len)
5702 mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T));
5703}
5704
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005705/*
5706 * split and quote args for <f-args>
5707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 static char_u *
5709uc_split_args(arg, lenp)
5710 char_u *arg;
5711 size_t *lenp;
5712{
5713 char_u *buf;
5714 char_u *p;
5715 char_u *q;
5716 int len;
5717
5718 /* Precalculate length */
5719 p = arg;
5720 len = 2; /* Initial and final quotes */
5721
5722 while (*p)
5723 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005724 if (p[0] == '\\' && p[1] == '\\')
5725 {
5726 len += 2;
5727 p += 2;
5728 }
5729 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 {
5731 len += 1;
5732 p += 2;
5733 }
5734 else if (*p == '\\' || *p == '"')
5735 {
5736 len += 2;
5737 p += 1;
5738 }
5739 else if (vim_iswhite(*p))
5740 {
5741 p = skipwhite(p);
5742 if (*p == NUL)
5743 break;
5744 len += 3; /* "," */
5745 }
5746 else
5747 {
5748 ++len;
5749 ++p;
5750 }
5751 }
5752
5753 buf = alloc(len + 1);
5754 if (buf == NULL)
5755 {
5756 *lenp = 0;
5757 return buf;
5758 }
5759
5760 p = arg;
5761 q = buf;
5762 *q++ = '"';
5763 while (*p)
5764 {
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005765 if (p[0] == '\\' && p[1] == '\\')
5766 {
5767 *q++ = '\\';
5768 *q++ = '\\';
5769 p += 2;
5770 }
5771 else if (p[0] == '\\' && vim_iswhite(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 {
5773 *q++ = p[1];
5774 p += 2;
5775 }
5776 else if (*p == '\\' || *p == '"')
5777 {
5778 *q++ = '\\';
5779 *q++ = *p++;
5780 }
5781 else if (vim_iswhite(*p))
5782 {
5783 p = skipwhite(p);
5784 if (*p == NUL)
5785 break;
5786 *q++ = '"';
5787 *q++ = ',';
5788 *q++ = '"';
5789 }
5790 else
5791 {
5792 *q++ = *p++;
5793 }
5794 }
5795 *q++ = '"';
5796 *q = 0;
5797
5798 *lenp = len;
5799 return buf;
5800}
5801
5802/*
5803 * Check for a <> code in a user command.
5804 * "code" points to the '<'. "len" the length of the <> (inclusive).
5805 * "buf" is where the result is to be added.
5806 * "split_buf" points to a buffer used for splitting, caller should free it.
5807 * "split_len" is the length of what "split_buf" contains.
5808 * Returns the length of the replacement, which has been added to "buf".
5809 * Returns -1 if there was no match, and only the "<" has been copied.
5810 */
5811 static size_t
5812uc_check_code(code, len, buf, cmd, eap, split_buf, split_len)
5813 char_u *code;
5814 size_t len;
5815 char_u *buf;
5816 ucmd_T *cmd; /* the user command we're expanding */
5817 exarg_T *eap; /* ex arguments */
5818 char_u **split_buf;
5819 size_t *split_len;
5820{
5821 size_t result = 0;
5822 char_u *p = code + 1;
5823 size_t l = len - 2;
5824 int quote = 0;
5825 enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER,
5826 ct_LT, ct_NONE } type = ct_NONE;
5827
5828 if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-')
5829 {
5830 quote = (*p == 'q' || *p == 'Q') ? 1 : 2;
5831 p += 2;
5832 l -= 2;
5833 }
5834
Bram Moolenaar371d5402006-03-20 21:47:49 +00005835 ++l;
5836 if (l <= 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 type = ct_NONE;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005838 else if (STRNICMP(p, "args>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 type = ct_ARGS;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005840 else if (STRNICMP(p, "bang>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 type = ct_BANG;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005842 else if (STRNICMP(p, "count>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 type = ct_COUNT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005844 else if (STRNICMP(p, "line1>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 type = ct_LINE1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005846 else if (STRNICMP(p, "line2>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 type = ct_LINE2;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005848 else if (STRNICMP(p, "lt>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 type = ct_LT;
Bram Moolenaar371d5402006-03-20 21:47:49 +00005850 else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 type = ct_REGISTER;
5852
5853 switch (type)
5854 {
5855 case ct_ARGS:
5856 /* Simple case first */
5857 if (*eap->arg == NUL)
5858 {
5859 if (quote == 1)
5860 {
5861 result = 2;
5862 if (buf != NULL)
5863 STRCPY(buf, "''");
5864 }
5865 else
5866 result = 0;
5867 break;
5868 }
5869
5870 /* When specified there is a single argument don't split it.
5871 * Works for ":Cmd %" when % is "a b c". */
5872 if ((eap->argt & NOSPC) && quote == 2)
5873 quote = 1;
5874
5875 switch (quote)
5876 {
5877 case 0: /* No quoting, no splitting */
5878 result = STRLEN(eap->arg);
5879 if (buf != NULL)
5880 STRCPY(buf, eap->arg);
5881 break;
5882 case 1: /* Quote, but don't split */
5883 result = STRLEN(eap->arg) + 2;
5884 for (p = eap->arg; *p; ++p)
5885 {
5886 if (*p == '\\' || *p == '"')
5887 ++result;
5888 }
5889
5890 if (buf != NULL)
5891 {
5892 *buf++ = '"';
5893 for (p = eap->arg; *p; ++p)
5894 {
5895 if (*p == '\\' || *p == '"')
5896 *buf++ = '\\';
5897 *buf++ = *p;
5898 }
5899 *buf = '"';
5900 }
5901
5902 break;
Bram Moolenaar552f8a12007-03-08 17:12:08 +00005903 case 2: /* Quote and split (<f-args>) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 /* This is hard, so only do it once, and cache the result */
5905 if (*split_buf == NULL)
5906 *split_buf = uc_split_args(eap->arg, split_len);
5907
5908 result = *split_len;
5909 if (buf != NULL && result != 0)
5910 STRCPY(buf, *split_buf);
5911
5912 break;
5913 }
5914 break;
5915
5916 case ct_BANG:
5917 result = eap->forceit ? 1 : 0;
5918 if (quote)
5919 result += 2;
5920 if (buf != NULL)
5921 {
5922 if (quote)
5923 *buf++ = '"';
5924 if (eap->forceit)
5925 *buf++ = '!';
5926 if (quote)
5927 *buf = '"';
5928 }
5929 break;
5930
5931 case ct_LINE1:
5932 case ct_LINE2:
5933 case ct_COUNT:
5934 {
5935 char num_buf[20];
5936 long num = (type == ct_LINE1) ? eap->line1 :
5937 (type == ct_LINE2) ? eap->line2 :
5938 (eap->addr_count > 0) ? eap->line2 : cmd->uc_def;
5939 size_t num_len;
5940
5941 sprintf(num_buf, "%ld", num);
5942 num_len = STRLEN(num_buf);
5943 result = num_len;
5944
5945 if (quote)
5946 result += 2;
5947
5948 if (buf != NULL)
5949 {
5950 if (quote)
5951 *buf++ = '"';
5952 STRCPY(buf, num_buf);
5953 buf += num_len;
5954 if (quote)
5955 *buf = '"';
5956 }
5957
5958 break;
5959 }
5960
5961 case ct_REGISTER:
5962 result = eap->regname ? 1 : 0;
5963 if (quote)
5964 result += 2;
5965 if (buf != NULL)
5966 {
5967 if (quote)
5968 *buf++ = '\'';
5969 if (eap->regname)
5970 *buf++ = eap->regname;
5971 if (quote)
5972 *buf = '\'';
5973 }
5974 break;
5975
5976 case ct_LT:
5977 result = 1;
5978 if (buf != NULL)
5979 *buf = '<';
5980 break;
5981
5982 default:
5983 /* Not recognized: just copy the '<' and return -1. */
5984 result = (size_t)-1;
5985 if (buf != NULL)
5986 *buf = '<';
5987 break;
5988 }
5989
5990 return result;
5991}
5992
5993 static void
5994do_ucmd(eap)
5995 exarg_T *eap;
5996{
5997 char_u *buf;
5998 char_u *p;
5999 char_u *q;
6000
6001 char_u *start;
Bram Moolenaar25648a52009-02-21 19:37:46 +00006002 char_u *end = NULL;
Bram Moolenaara850a712009-01-28 14:42:59 +00006003 char_u *ksp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 size_t len, totlen;
6005
6006 size_t split_len = 0;
6007 char_u *split_buf = NULL;
6008 ucmd_T *cmd;
6009#ifdef FEAT_EVAL
6010 scid_T save_current_SID = current_SID;
6011#endif
6012
6013 if (eap->cmdidx == CMD_USER)
6014 cmd = USER_CMD(eap->useridx);
6015 else
6016 cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx);
6017
6018 /*
6019 * Replace <> in the command by the arguments.
Bram Moolenaara850a712009-01-28 14:42:59 +00006020 * First round: "buf" is NULL, compute length, allocate "buf".
6021 * Second round: copy result into "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 */
6023 buf = NULL;
6024 for (;;)
6025 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006026 p = cmd->uc_rep; /* source */
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00006027 q = buf; /* destination */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 totlen = 0;
Bram Moolenaara850a712009-01-28 14:42:59 +00006029
6030 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 {
Bram Moolenaara850a712009-01-28 14:42:59 +00006032 start = vim_strchr(p, '<');
6033 if (start != NULL)
6034 end = vim_strchr(start + 1, '>');
6035 if (buf != NULL)
6036 {
6037 ksp = vim_strchr(p, K_SPECIAL);
6038 if (ksp != NULL && (start == NULL || ksp < start || end == NULL)
6039 && ((ksp[1] == KS_SPECIAL && ksp[2] == KE_FILLER)
6040# ifdef FEAT_GUI
6041 || (ksp[1] == KS_EXTRA && ksp[2] == (int)KE_CSI)
6042# endif
6043 ))
6044 {
6045 /* K_SPECIAL han been put in the buffer as K_SPECIAL
6046 * KS_SPECIAL KE_FILLER, like for mappings, but
6047 * do_cmdline() doesn't handle that, so convert it back.
6048 * Also change K_SPECIAL KS_EXTRA KE_CSI into CSI. */
6049 len = ksp - p;
6050 if (len > 0)
6051 {
6052 mch_memmove(q, p, len);
6053 q += len;
6054 }
6055 *q++ = ksp[1] == KS_SPECIAL ? K_SPECIAL : CSI;
6056 p = ksp + 3;
6057 continue;
6058 }
6059 }
6060
6061 /* break if there no <item> is found */
6062 if (start == NULL || end == NULL)
6063 break;
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 /* Include the '>' */
6066 ++end;
6067
6068 /* Take everything up to the '<' */
6069 len = start - p;
6070 if (buf == NULL)
6071 totlen += len;
6072 else
6073 {
6074 mch_memmove(q, p, len);
6075 q += len;
6076 }
6077
6078 len = uc_check_code(start, end - start, q, cmd, eap,
6079 &split_buf, &split_len);
6080 if (len == (size_t)-1)
6081 {
6082 /* no match, continue after '<' */
6083 p = start + 1;
6084 len = 1;
6085 }
6086 else
6087 p = end;
6088 if (buf == NULL)
6089 totlen += len;
6090 else
6091 q += len;
6092 }
6093 if (buf != NULL) /* second time here, finished */
6094 {
6095 STRCPY(q, p);
6096 break;
6097 }
6098
6099 totlen += STRLEN(p); /* Add on the trailing characters */
6100 buf = alloc((unsigned)(totlen + 1));
6101 if (buf == NULL)
6102 {
6103 vim_free(split_buf);
6104 return;
6105 }
6106 }
6107
6108#ifdef FEAT_EVAL
6109 current_SID = cmd->uc_scriptID;
6110#endif
6111 (void)do_cmdline(buf, eap->getline, eap->cookie,
6112 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
6113#ifdef FEAT_EVAL
6114 current_SID = save_current_SID;
6115#endif
6116 vim_free(buf);
6117 vim_free(split_buf);
6118}
6119
6120# if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6121 static char_u *
6122get_user_command_name(idx)
6123 int idx;
6124{
6125 return get_user_commands(NULL, idx - (int)CMD_SIZE);
6126}
6127
6128/*
6129 * Function given to ExpandGeneric() to obtain the list of user command names.
6130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 char_u *
6132get_user_commands(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006133 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 int idx;
6135{
6136 if (idx < curbuf->b_ucmds.ga_len)
6137 return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name;
6138 idx -= curbuf->b_ucmds.ga_len;
6139 if (idx < ucmds.ga_len)
6140 return USER_CMD(idx)->uc_name;
6141 return NULL;
6142}
6143
6144/*
6145 * Function given to ExpandGeneric() to obtain the list of user command
6146 * attributes.
6147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 char_u *
6149get_user_cmd_flags(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006150 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 int idx;
6152{
6153 static char *user_cmd_flags[] =
6154 {"bang", "bar", "buffer", "complete", "count",
6155 "nargs", "range", "register"};
6156
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006157 if (idx >= (int)(sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 return NULL;
6159 return (char_u *)user_cmd_flags[idx];
6160}
6161
6162/*
6163 * Function given to ExpandGeneric() to obtain the list of values for -nargs.
6164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 char_u *
6166get_user_cmd_nargs(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006167 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 int idx;
6169{
6170 static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"};
6171
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00006172 if (idx >= (int)(sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 return NULL;
6174 return (char_u *)user_cmd_nargs[idx];
6175}
6176
6177/*
6178 * Function given to ExpandGeneric() to obtain the list of values for -complete.
6179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 char_u *
6181get_user_cmd_complete(xp, idx)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006182 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 int idx;
6184{
6185 return (char_u *)command_complete[idx].name;
6186}
6187# endif /* FEAT_CMDL_COMPL */
6188
6189#endif /* FEAT_USR_CMDS */
6190
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006191#if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO)
6192/*
6193 * Parse a completion argument "value[vallen]".
6194 * The detected completion goes in "*complp", argument type in "*argt".
6195 * When there is an argument, for function and user defined completion, it's
6196 * copied to allocated memory and stored in "*compl_arg".
6197 * Returns FAIL if something is wrong.
6198 */
6199 int
6200parse_compl_arg(value, vallen, complp, argt, compl_arg)
6201 char_u *value;
6202 int vallen;
6203 int *complp;
6204 long *argt;
6205 char_u **compl_arg;
6206{
6207 char_u *arg = NULL;
6208 size_t arglen = 0;
6209 int i;
6210 int valend = vallen;
6211
6212 /* Look for any argument part - which is the part after any ',' */
6213 for (i = 0; i < vallen; ++i)
6214 {
6215 if (value[i] == ',')
6216 {
6217 arg = &value[i + 1];
6218 arglen = vallen - i - 1;
6219 valend = i;
6220 break;
6221 }
6222 }
6223
6224 for (i = 0; command_complete[i].expand != 0; ++i)
6225 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006226 if ((int)STRLEN(command_complete[i].name) == valend
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006227 && STRNCMP(value, command_complete[i].name, valend) == 0)
6228 {
6229 *complp = command_complete[i].expand;
6230 if (command_complete[i].expand == EXPAND_BUFFERS)
6231 *argt |= BUFNAME;
6232 else if (command_complete[i].expand == EXPAND_DIRECTORIES
6233 || command_complete[i].expand == EXPAND_FILES)
6234 *argt |= XFILE;
6235 break;
6236 }
6237 }
6238
6239 if (command_complete[i].expand == 0)
6240 {
6241 EMSG2(_("E180: Invalid complete value: %s"), value);
6242 return FAIL;
6243 }
6244
6245# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6246 if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST
6247 && arg != NULL)
6248# else
6249 if (arg != NULL)
6250# endif
6251 {
6252 EMSG(_("E468: Completion argument only allowed for custom completion"));
6253 return FAIL;
6254 }
6255
6256# if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
6257 if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST)
6258 && arg == NULL)
6259 {
6260 EMSG(_("E467: Custom completion requires a function argument"));
6261 return FAIL;
6262 }
6263
6264 if (arg != NULL)
6265 *compl_arg = vim_strnsave(arg, (int)arglen);
6266# endif
6267 return OK;
6268}
6269#endif
6270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 static void
6272ex_colorscheme(eap)
6273 exarg_T *eap;
6274{
Bram Moolenaare6850792010-05-14 15:28:44 +02006275 if (*eap->arg == NUL)
6276 {
6277#ifdef FEAT_EVAL
6278 char_u *expr = vim_strsave((char_u *)"g:colors_name");
6279 char_u *p = NULL;
6280
6281 if (expr != NULL)
6282 {
6283 ++emsg_off;
6284 p = eval_to_string(expr, NULL, FALSE);
6285 --emsg_off;
6286 vim_free(expr);
6287 }
6288 if (p != NULL)
6289 {
6290 MSG(p);
6291 vim_free(p);
6292 }
6293 else
6294 MSG("default");
6295#else
6296 MSG(_("unknown"));
6297#endif
6298 }
6299 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 EMSG2(_("E185: Cannot find color scheme %s"), eap->arg);
6301}
6302
6303 static void
6304ex_highlight(eap)
6305 exarg_T *eap;
6306{
6307 if (*eap->arg == NUL && eap->cmd[2] == '!')
6308 MSG(_("Greetings, Vim user!"));
6309 do_highlight(eap->arg, eap->forceit, FALSE);
6310}
6311
6312
6313/*
6314 * Call this function if we thought we were going to exit, but we won't
6315 * (because of an error). May need to restore the terminal mode.
6316 */
6317 void
6318not_exiting()
6319{
6320 exiting = FALSE;
6321 settmode(TMODE_RAW);
6322}
6323
6324/*
6325 * ":quit": quit current window, quit Vim if closed the last window.
6326 */
6327 static void
6328ex_quit(eap)
6329 exarg_T *eap;
6330{
6331#ifdef FEAT_CMDWIN
6332 if (cmdwin_type != 0)
6333 {
6334 cmdwin_result = Ctrl_C;
6335 return;
6336 }
6337#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006338 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006339 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006340 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006341 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006342 return;
6343 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006344#ifdef FEAT_AUTOCMD
6345 if (curbuf_locked())
6346 return;
6347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
6349#ifdef FEAT_NETBEANS_INTG
6350 netbeansForcedQuit = eap->forceit;
6351#endif
6352
6353 /*
6354 * If there are more files or windows we won't exit.
6355 */
6356 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6357 exiting = TRUE;
6358 if ((!P_HID(curbuf)
6359 && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE))
6360 || check_more(TRUE, eap->forceit) == FAIL
6361 || (only_one_window() && check_changed_any(eap->forceit)))
6362 {
6363 not_exiting();
6364 }
6365 else
6366 {
6367#ifdef FEAT_WINDOWS
6368 if (only_one_window()) /* quit last window */
6369#endif
6370 getout(0);
6371#ifdef FEAT_WINDOWS
6372# ifdef FEAT_GUI
6373 need_mouse_correct = TRUE;
6374# endif
6375 /* close window; may free buffer */
6376 win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit);
6377#endif
6378 }
6379}
6380
6381/*
6382 * ":cquit".
6383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 static void
6385ex_cquit(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006386 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 getout(1); /* this does not always pass on the exit code to the Manx
6389 compiler. why? */
6390}
6391
6392/*
6393 * ":qall": try to quit all windows
6394 */
6395 static void
6396ex_quit_all(eap)
6397 exarg_T *eap;
6398{
6399# ifdef FEAT_CMDWIN
6400 if (cmdwin_type != 0)
6401 {
6402 if (eap->forceit)
6403 cmdwin_result = K_XF1; /* ex_window() takes care of this */
6404 else
6405 cmdwin_result = K_XF2;
6406 return;
6407 }
6408# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006409
6410 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006411 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006412 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006413 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006414 return;
6415 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006416#ifdef FEAT_AUTOCMD
6417 if (curbuf_locked())
6418 return;
6419#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006420
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 exiting = TRUE;
6422 if (eap->forceit || !check_changed_any(FALSE))
6423 getout(0);
6424 not_exiting();
6425}
6426
Bram Moolenaard9967712006-03-11 21:18:15 +00006427#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428/*
6429 * ":close": close current window, unless it is the last one
6430 */
6431 static void
6432ex_close(eap)
6433 exarg_T *eap;
6434{
6435# ifdef FEAT_CMDWIN
6436 if (cmdwin_type != 0)
6437 cmdwin_result = K_IGNORE;
6438 else
6439# endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006440 if (!text_locked()
6441#ifdef FEAT_AUTOCMD
6442 && !curbuf_locked()
6443#endif
6444 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006445 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446}
6447
Bram Moolenaard9967712006-03-11 21:18:15 +00006448# ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006449/*
6450 * ":pclose": Close any preview window.
6451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006453ex_pclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 exarg_T *eap;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006455{
6456 win_T *win;
6457
6458 for (win = firstwin; win != NULL; win = win->w_next)
6459 if (win->w_p_pvw)
6460 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006461 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006462 break;
6463 }
6464}
Bram Moolenaard9967712006-03-11 21:18:15 +00006465# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466
Bram Moolenaarf740b292006-02-16 22:11:02 +00006467/*
6468 * Close window "win" and take care of handling closing the last window for a
6469 * modified buffer.
6470 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471 static void
Bram Moolenaarf740b292006-02-16 22:11:02 +00006472ex_win_close(forceit, win, tp)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006473 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006475 tabpage_T *tp; /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477 int need_hide;
6478 buf_T *buf = win->w_buffer;
6479
6480 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006481 if (need_hide && !P_HID(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 {
Bram Moolenaard9967712006-03-11 21:18:15 +00006483# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 if ((p_confirm || cmdmod.confirm) && p_write)
6485 {
6486 dialog_changed(buf, FALSE);
6487 if (buf_valid(buf) && bufIsChanged(buf))
6488 return;
6489 need_hide = FALSE;
6490 }
6491 else
Bram Moolenaard9967712006-03-11 21:18:15 +00006492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 {
6494 EMSG(_(e_nowrtmsg));
6495 return;
6496 }
6497 }
6498
Bram Moolenaard9967712006-03-11 21:18:15 +00006499# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 need_mouse_correct = TRUE;
Bram Moolenaard9967712006-03-11 21:18:15 +00006501# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006502
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00006504 if (tp == NULL)
6505 win_close(win, !need_hide && !P_HID(buf));
6506 else
6507 win_close_othertab(win, !need_hide && !P_HID(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508}
6509
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006511 * ":tabclose": close current tab page, unless it is the last one.
6512 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 */
6514 static void
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006515ex_tabclose(eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 exarg_T *eap;
6517{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006518 tabpage_T *tp;
6519
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006520# ifdef FEAT_CMDWIN
6521 if (cmdwin_type != 0)
6522 cmdwin_result = K_IGNORE;
6523 else
6524# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006525 if (first_tabpage->tp_next == NULL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006526 EMSG(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006529 if (eap->addr_count > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006530 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006531 tp = find_tabpage((int)eap->line2);
6532 if (tp == NULL)
6533 {
6534 beep_flush();
6535 return;
6536 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006537 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006538 {
6539 tabpage_close_other(tp, eap->forceit);
6540 return;
6541 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006542 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006543 if (!text_locked()
6544#ifdef FEAT_AUTOCMD
6545 && !curbuf_locked()
6546#endif
6547 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00006548 tabpage_close(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006550}
6551
6552/*
6553 * ":tabonly": close all tab pages except the current one
6554 */
6555 static void
6556ex_tabonly(eap)
6557 exarg_T *eap;
6558{
6559 tabpage_T *tp;
6560 int done;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006561
6562# ifdef FEAT_CMDWIN
6563 if (cmdwin_type != 0)
6564 cmdwin_result = K_IGNORE;
6565 else
6566# endif
6567 if (first_tabpage->tp_next == NULL)
6568 MSG(_("Already only one tab page"));
6569 else
6570 {
6571 /* Repeat this up to a 1000 times, because autocommands may mess
6572 * up the lists. */
6573 for (done = 0; done < 1000; ++done)
6574 {
6575 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6576 if (tp->tp_topframe != topframe)
6577 {
6578 tabpage_close_other(tp, eap->forceit);
6579 /* if we failed to close it quit */
6580 if (valid_tabpage(tp))
6581 done = 1000;
6582 /* start over, "tp" is now invalid */
6583 break;
6584 }
6585 if (first_tabpage->tp_next == NULL)
6586 break;
6587 }
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
6591/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006592 * Close the current tab page.
6593 */
6594 void
6595tabpage_close(forceit)
6596 int forceit;
6597{
6598 /* First close all the windows but the current one. If that worked then
6599 * close the last window in this tab, that will close it. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006600 if (lastwin != firstwin)
6601 close_others(TRUE, forceit);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006602 if (lastwin == firstwin)
6603 ex_win_close(forceit, curwin, NULL);
6604# ifdef FEAT_GUI
6605 need_mouse_correct = TRUE;
6606# endif
6607}
6608
6609/*
6610 * Close tab page "tp", which is not the current tab page.
6611 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006612 * Also takes care of the tab pages line disappearing when closing the
6613 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006614 */
6615 void
6616tabpage_close_other(tp, forceit)
6617 tabpage_T *tp;
6618 int forceit;
6619{
6620 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006621 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006622 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006623
6624 /* Limit to 1000 windows, autocommands may add a window while we close
6625 * one. OK, so I'm paranoid... */
6626 while (++done < 1000)
6627 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006628 wp = tp->tp_firstwin;
6629 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006630
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006631 /* Autocommands may delete the tab page under our fingers and we may
6632 * fail to close a window with a modified buffer. */
6633 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006634 break;
6635 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006636
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006637 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006638 if (h != tabline_height())
6639 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006640}
6641
6642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 * ":only".
6644 */
6645 static void
6646ex_only(eap)
6647 exarg_T *eap;
6648{
6649# ifdef FEAT_GUI
6650 need_mouse_correct = TRUE;
6651# endif
6652 close_others(TRUE, eap->forceit);
6653}
6654
6655/*
6656 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006657 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006659 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660ex_all(eap)
6661 exarg_T *eap;
6662{
6663 if (eap->addr_count == 0)
6664 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006665 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666}
6667#endif /* FEAT_WINDOWS */
6668
6669 static void
6670ex_hide(eap)
6671 exarg_T *eap;
6672{
6673 if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL)
6674 eap->errmsg = e_invarg;
6675 else
6676 {
6677 /* ":hide" or ":hide | cmd": hide current window */
6678 eap->nextcmd = check_nextcmd(eap->arg);
6679#ifdef FEAT_WINDOWS
6680 if (!eap->skip)
6681 {
6682# ifdef FEAT_GUI
6683 need_mouse_correct = TRUE;
6684# endif
6685 win_close(curwin, FALSE); /* don't free buffer */
6686 }
6687#endif
6688 }
6689}
6690
6691/*
6692 * ":stop" and ":suspend": Suspend Vim.
6693 */
6694 static void
6695ex_stop(eap)
6696 exarg_T *eap;
6697{
6698 /*
6699 * Disallow suspending for "rvim".
6700 */
6701 if (!check_restricted()
6702#ifdef WIN3264
6703 /*
6704 * Check if external commands are allowed now.
6705 */
6706 && can_end_termcap_mode(TRUE)
6707#endif
6708 )
6709 {
6710 if (!eap->forceit)
6711 autowrite_all();
6712 windgoto((int)Rows - 1, 0);
6713 out_char('\n');
6714 out_flush();
6715 stoptermcap();
6716 out_flush(); /* needed for SUN to restore xterm buffer */
6717#ifdef FEAT_TITLE
6718 mch_restore_title(3); /* restore window titles */
6719#endif
6720 ui_suspend(); /* call machine specific function */
6721#ifdef FEAT_TITLE
6722 maketitle();
6723 resettitle(); /* force updating the title */
6724#endif
6725 starttermcap();
6726 scroll_start(); /* scroll screen before redrawing */
6727 redraw_later_clear();
6728 shell_resized(); /* may have resized window */
6729 }
6730}
6731
6732/*
6733 * ":exit", ":xit" and ":wq": Write file and exit Vim.
6734 */
6735 static void
6736ex_exit(eap)
6737 exarg_T *eap;
6738{
6739#ifdef FEAT_CMDWIN
6740 if (cmdwin_type != 0)
6741 {
6742 cmdwin_result = Ctrl_C;
6743 return;
6744 }
6745#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006746 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006747 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006748 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006749 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006750 return;
6751 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006752#ifdef FEAT_AUTOCMD
6753 if (curbuf_locked())
6754 return;
6755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756
6757 /*
6758 * if more files or windows we won't exit
6759 */
6760 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6761 exiting = TRUE;
6762 if ( ((eap->cmdidx == CMD_wq
6763 || curbufIsChanged())
6764 && do_write(eap) == FAIL)
6765 || check_more(TRUE, eap->forceit) == FAIL
6766 || (only_one_window() && check_changed_any(eap->forceit)))
6767 {
6768 not_exiting();
6769 }
6770 else
6771 {
6772#ifdef FEAT_WINDOWS
6773 if (only_one_window()) /* quit last window, exit Vim */
6774#endif
6775 getout(0);
6776#ifdef FEAT_WINDOWS
6777# ifdef FEAT_GUI
6778 need_mouse_correct = TRUE;
6779# endif
6780 /* quit current window, may free buffer */
6781 win_close(curwin, !P_HID(curwin->w_buffer));
6782#endif
6783 }
6784}
6785
6786/*
6787 * ":print", ":list", ":number".
6788 */
6789 static void
6790ex_print(eap)
6791 exarg_T *eap;
6792{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006793 if (curbuf->b_ml.ml_flags & ML_EMPTY)
6794 EMSG(_(e_emptybuf));
6795 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006797 for ( ;!got_int; ui_breakcheck())
6798 {
6799 print_line(eap->line1,
6800 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6801 || (eap->flags & EXFLAG_NR)),
6802 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6803 if (++eap->line1 > eap->line2)
6804 break;
6805 out_flush(); /* show one line at a time */
6806 }
6807 setpcmark();
6808 /* put cursor at last line */
6809 curwin->w_cursor.lnum = eap->line2;
6810 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
6816#ifdef FEAT_BYTEOFF
6817 static void
6818ex_goto(eap)
6819 exarg_T *eap;
6820{
6821 goto_byte(eap->line2);
6822}
6823#endif
6824
6825/*
6826 * ":shell".
6827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 static void
6829ex_shell(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00006830 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831{
6832 do_shell(NULL, 0);
6833}
6834
6835#if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \
6836 || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \
Bram Moolenaar371baa92005-12-29 22:43:53 +00006837 || defined(FEAT_GUI_MSWIN) \
6838 || defined(FEAT_GUI_MAC) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 || defined(PROTO)
6840
6841/*
6842 * Handle a file drop. The code is here because a drop is *nearly* like an
6843 * :args command, but not quite (we have a list of exact filenames, so we
6844 * don't want to (a) parse a command line, or (b) expand wildcards. So the
6845 * code is very similar to :args and hence needs access to a lot of the static
6846 * functions in this file.
6847 *
6848 * The list should be allocated using alloc(), as should each item in the
6849 * list. This function takes over responsibility for freeing the list.
6850 *
Bram Moolenaar81870892007-11-11 18:17:28 +00006851 * XXX The list is made into the argument list. This is freed using
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 * FreeWild(), which does a series of vim_free() calls, unless the two defines
6853 * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a
6854 * routine _fnexplodefree() is used. This may cause problems, but as the drop
6855 * file functionality is (currently) not in EMX this is not presently a
6856 * problem.
6857 */
6858 void
6859handle_drop(filec, filev, split)
6860 int filec; /* the number of files dropped */
6861 char_u **filev; /* the list of files dropped */
6862 int split; /* force splitting the window */
6863{
6864 exarg_T ea;
6865 int save_msg_scroll = msg_scroll;
6866
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006867 /* Postpone this while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006868 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 return;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006870#ifdef FEAT_AUTOCMD
6871 if (curbuf_locked())
6872 return;
6873#endif
Bram Moolenaarc236c162008-07-13 17:41:49 +00006874 /* When the screen is being updated we should not change buffers and
6875 * windows structures, it may cause freed memory to be used. */
6876 if (updating_screen)
6877 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878
6879 /* Check whether the current buffer is changed. If so, we will need
6880 * to split the current window or data could be lost.
6881 * We don't need to check if the 'hidden' option is set, as in this
6882 * case the buffer won't be lost.
6883 */
6884 if (!P_HID(curbuf) && !split)
6885 {
6886 ++emsg_off;
6887 split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE);
6888 --emsg_off;
6889 }
6890 if (split)
6891 {
6892# ifdef FEAT_WINDOWS
6893 if (win_split(0, 0) == FAIL)
6894 return;
6895# ifdef FEAT_SCROLLBIND
6896 curwin->w_p_scb = FALSE;
6897# endif
6898
6899 /* When splitting the window, create a new alist. Otherwise the
6900 * existing one is overwritten. */
6901 alist_unlink(curwin->w_alist);
6902 alist_new();
6903# else
6904 return; /* can't split, always fail */
6905# endif
6906 }
6907
6908 /*
6909 * Set up the new argument list.
6910 */
Bram Moolenaar86b68352004-12-27 21:59:20 +00006911 alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912
6913 /*
6914 * Move to the first file.
6915 */
6916 /* Fake up a minimal "next" command for do_argfile() */
6917 vim_memset(&ea, 0, sizeof(ea));
6918 ea.cmd = (char_u *)"next";
6919 do_argfile(&ea, 0);
6920
6921 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6922 * mode that is not needed here. */
6923 need_start_insertmode = FALSE;
6924
6925 /* Restore msg_scroll, otherwise a following command may cause scrolling
6926 * unexpectedly. The screen will be redrawn by the caller, thus
6927 * msg_scroll being set by displaying a message is irrelevant. */
6928 msg_scroll = save_msg_scroll;
6929}
6930#endif
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932/*
6933 * Clear an argument list: free all file names and reset it to zero entries.
6934 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006935 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936alist_clear(al)
6937 alist_T *al;
6938{
6939 while (--al->al_ga.ga_len >= 0)
6940 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6941 ga_clear(&al->al_ga);
6942}
6943
6944/*
6945 * Init an argument list.
6946 */
6947 void
6948alist_init(al)
6949 alist_T *al;
6950{
6951 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6952}
6953
6954#if defined(FEAT_WINDOWS) || defined(PROTO)
6955
6956/*
6957 * Remove a reference from an argument list.
6958 * Ignored when the argument list is the global one.
6959 * If the argument list is no longer used by any window, free it.
6960 */
6961 void
6962alist_unlink(al)
6963 alist_T *al;
6964{
6965 if (al != &global_alist && --al->al_refcount <= 0)
6966 {
6967 alist_clear(al);
6968 vim_free(al);
6969 }
6970}
6971
6972# if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO)
6973/*
6974 * Create a new argument list and use it for the current window.
6975 */
6976 void
6977alist_new()
6978{
6979 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6980 if (curwin->w_alist == NULL)
6981 {
6982 curwin->w_alist = &global_alist;
6983 ++global_alist.al_refcount;
6984 }
6985 else
6986 {
6987 curwin->w_alist->al_refcount = 1;
6988 alist_init(curwin->w_alist);
6989 }
6990}
6991# endif
6992#endif
6993
6994#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO)
6995/*
6996 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006997 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6998 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 */
7000 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007001alist_expand(fnum_list, fnum_len)
7002 int *fnum_list;
7003 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007006 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 char_u **new_arg_files;
7008 int new_arg_file_count;
7009 char_u *save_p_su = p_su;
7010 int i;
7011
7012 /* Don't use 'suffixes' here. This should work like the shell did the
7013 * expansion. Also, the vimrc file isn't read yet, thus the user
7014 * can't set the options. */
7015 p_su = empty_option;
7016 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
7017 if (old_arg_files != NULL)
7018 {
7019 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00007020 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
7021 old_arg_count = GARGCOUNT;
7022 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 &new_arg_file_count, &new_arg_files,
7024 EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK
7025 && new_arg_file_count > 0)
7026 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00007027 alist_set(&global_alist, new_arg_file_count, new_arg_files,
7028 TRUE, fnum_list, fnum_len);
7029 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 }
7032 p_su = save_p_su;
7033}
7034#endif
7035
7036/*
7037 * Set the argument list for the current window.
7038 * Takes over the allocated files[] and the allocated fnames in it.
7039 */
7040 void
Bram Moolenaar86b68352004-12-27 21:59:20 +00007041alist_set(al, count, files, use_curbuf, fnum_list, fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 alist_T *al;
7043 int count;
7044 char_u **files;
7045 int use_curbuf;
Bram Moolenaar86b68352004-12-27 21:59:20 +00007046 int *fnum_list;
7047 int fnum_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 int i;
7050
7051 alist_clear(al);
7052 if (ga_grow(&al->al_ga, count) == OK)
7053 {
7054 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007055 {
7056 if (got_int)
7057 {
7058 /* When adding many buffers this can take a long time. Allow
7059 * interrupting here. */
7060 while (i < count)
7061 vim_free(files[i++]);
7062 break;
7063 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00007064
7065 /* May set buffer name of a buffer previously used for the
7066 * argument list, so that it's re-used by alist_add. */
7067 if (fnum_list != NULL && i < fnum_len)
7068 buf_set_name(fnum_list[i], files[i]);
7069
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00007071 ui_breakcheck();
7072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 vim_free(files);
7074 }
7075 else
7076 FreeWild(count, files);
7077#ifdef FEAT_WINDOWS
7078 if (al == &global_alist)
7079#endif
7080 arg_had_last = FALSE;
7081}
7082
7083/*
7084 * Add file "fname" to argument list "al".
7085 * "fname" must have been allocated and "al" must have been checked for room.
7086 */
7087 void
7088alist_add(al, fname, set_fnum)
7089 alist_T *al;
7090 char_u *fname;
7091 int set_fnum; /* 1: set buffer number; 2: re-use curbuf */
7092{
7093 if (fname == NULL) /* don't add NULL file names */
7094 return;
7095#ifdef BACKSLASH_IN_FILENAME
7096 slash_adjust(fname);
7097#endif
7098 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
7099 if (set_fnum > 0)
7100 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
7101 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
7102 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103}
7104
7105#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7106/*
7107 * Adjust slashes in file names. Called after 'shellslash' was set.
7108 */
7109 void
7110alist_slash_adjust()
7111{
7112 int i;
7113# ifdef FEAT_WINDOWS
7114 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00007115 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116# endif
7117
7118 for (i = 0; i < GARGCOUNT; ++i)
7119 if (GARGLIST[i].ae_fname != NULL)
7120 slash_adjust(GARGLIST[i].ae_fname);
7121# ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00007122 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 if (wp->w_alist != &global_alist)
7124 for (i = 0; i < WARGCOUNT(wp); ++i)
7125 if (WARGLIST(wp)[i].ae_fname != NULL)
7126 slash_adjust(WARGLIST(wp)[i].ae_fname);
7127# endif
7128}
7129#endif
7130
7131/*
7132 * ":preserve".
7133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 static void
7135ex_preserve(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007136 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007138 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 ml_preserve(curbuf, TRUE);
7140}
7141
7142/*
7143 * ":recover".
7144 */
7145 static void
7146ex_recover(eap)
7147 exarg_T *eap;
7148{
7149 /* Set recoverymode right away to avoid the ATTENTION prompt. */
7150 recoverymode = TRUE;
7151 if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE)
7152 && (*eap->arg == NUL
7153 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
7154 ml_recover();
7155 recoverymode = FALSE;
7156}
7157
7158/*
7159 * Command modifier used in a wrong way.
7160 */
7161 static void
7162ex_wrongmodifier(eap)
7163 exarg_T *eap;
7164{
7165 eap->errmsg = e_invcmd;
7166}
7167
7168#ifdef FEAT_WINDOWS
7169/*
7170 * :sview [+command] file split window with new file, read-only
7171 * :split [[+command] file] split window with current or new file
7172 * :vsplit [[+command] file] split window vertically with current or new file
7173 * :new [[+command] file] split window with no or new file
7174 * :vnew [[+command] file] split vertically window with no or new file
7175 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007176 *
7177 * :tabedit open new Tab page with empty window
7178 * :tabedit [+command] file open new Tab page and edit "file"
7179 * :tabnew [[+command] file] just like :tabedit
7180 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 */
7182 void
7183ex_splitview(eap)
7184 exarg_T *eap;
7185{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007186 win_T *old_curwin = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007187# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 char_u *fname = NULL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007189# endif
7190# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 int browse_flag = cmdmod.browse;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007194# ifndef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew)
7196 {
7197 ex_ni(eap);
7198 return;
7199 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007202# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 need_mouse_correct = TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007206# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007208 * quickfix windows. But it's OK when doing ":tab split". */
7209 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {
7211 if (eap->cmdidx == CMD_split)
7212 eap->cmdidx = CMD_new;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007213# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 if (eap->cmdidx == CMD_vsplit)
7215 eap->cmdidx = CMD_vnew;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007220# ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007221 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 {
7223 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
7224 FNAME_MESS, TRUE, curbuf->b_ffname);
7225 if (fname == NULL)
7226 goto theend;
7227 eap->arg = fname;
7228 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007229# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007233# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 if (cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007235# ifdef FEAT_VERTSPLIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 && eap->cmdidx != CMD_vnew
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007237# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 && eap->cmdidx != CMD_new)
7239 {
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007240# ifdef FEAT_AUTOCMD
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007241 if (
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007242# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007243 !gui.in_use &&
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007244# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007245 au_has_group((char_u *)"FileExplorer"))
7246 {
7247 /* No browsing supported but we do have the file explorer:
7248 * Edit the directory. */
7249 if (*eap->arg == NUL || !mch_isdir(eap->arg))
7250 eap->arg = (char_u *)".";
7251 }
7252 else
Bram Moolenaar15bfa092008-07-24 16:45:38 +00007253# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007254 {
7255 fname = do_browse(0, (char_u *)_("Edit File in new window"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00007257 if (fname == NULL)
7258 goto theend;
7259 eap->arg = fname;
7260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 }
7262 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007265 /*
7266 * Either open new tab page or split the window.
7267 */
7268 if (eap->cmdidx == CMD_tabedit
7269 || eap->cmdidx == CMD_tabfind
7270 || eap->cmdidx == CMD_tabnew)
7271 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00007272 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
7273 : eap->addr_count == 0 ? 0
7274 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007275 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00007276 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007277
7278 /* set the alternate buffer for the window we came from */
7279 if (curwin != old_curwin
7280 && win_valid(old_curwin)
7281 && old_curwin->w_buffer != curbuf
7282 && !cmdmod.keepalt)
7283 old_curwin->w_alt_fnum = curbuf->b_fnum;
7284 }
7285 }
7286 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
7288 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007289# ifdef FEAT_SCROLLBIND
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 /* Reset 'scrollbind' when editing another file, but keep it when
7291 * doing ":split" without arguments. */
7292 if (*eap->arg != NUL
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007293# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 || cmdmod.browse
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 )
7297 curwin->w_p_scb = FALSE;
7298 else
7299 do_check_scrollbind(FALSE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 do_exedit(eap, old_curwin);
7302 }
7303
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007304# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 cmdmod.browse = browse_flag;
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# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309theend:
7310 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007313
7314/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007315 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007316 */
7317 void
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007318tabpage_new()
7319{
7320 exarg_T ea;
7321
7322 vim_memset(&ea, 0, sizeof(ea));
7323 ea.cmdidx = CMD_tabnew;
7324 ea.cmd = (char_u *)"tabn";
7325 ea.arg = (char_u *)"";
7326 ex_splitview(&ea);
7327}
7328
7329/*
7330 * :tabnext command
7331 */
7332 static void
7333ex_tabnext(eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007334 exarg_T *eap;
7335{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00007336 switch (eap->cmdidx)
7337 {
7338 case CMD_tabfirst:
7339 case CMD_tabrewind:
7340 goto_tabpage(1);
7341 break;
7342 case CMD_tablast:
7343 goto_tabpage(9999);
7344 break;
7345 case CMD_tabprevious:
7346 case CMD_tabNext:
7347 goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2);
7348 break;
7349 default: /* CMD_tabnext */
7350 goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2);
7351 break;
7352 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007353}
7354
7355/*
7356 * :tabmove command
7357 */
7358 static void
7359ex_tabmove(eap)
7360 exarg_T *eap;
7361{
7362 tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007363}
7364
7365/*
7366 * :tabs command: List tabs and their contents.
7367 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007368 static void
7369ex_tabs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007370 exarg_T *eap UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007371{
7372 tabpage_T *tp;
7373 win_T *wp;
7374 int tabcount = 1;
7375
7376 msg_start();
7377 msg_scroll = TRUE;
7378 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
7379 {
7380 msg_putchar('\n');
7381 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
7382 msg_outtrans_attr(IObuff, hl_attr(HLF_T));
7383 out_flush(); /* output one line at a time */
7384 ui_breakcheck();
7385
Bram Moolenaar030f0df2006-02-21 22:02:53 +00007386 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007387 wp = firstwin;
7388 else
7389 wp = tp->tp_firstwin;
7390 for ( ; wp != NULL && !got_int; wp = wp->w_next)
7391 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00007392 msg_putchar('\n');
7393 msg_putchar(wp == curwin ? '>' : ' ');
7394 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00007395 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
7396 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007397 if (buf_spname(wp->w_buffer) != NULL)
7398 STRCPY(IObuff, buf_spname(wp->w_buffer));
7399 else
7400 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7401 IObuff, IOSIZE, TRUE);
7402 msg_outtrans(IObuff);
7403 out_flush(); /* output one line at a time */
7404 ui_breakcheck();
7405 }
7406 }
7407}
7408
7409#endif /* FEAT_WINDOWS */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410
7411/*
7412 * ":mode": Set screen mode.
7413 * If no argument given, just get the screen size and redraw.
7414 */
7415 static void
7416ex_mode(eap)
7417 exarg_T *eap;
7418{
7419 if (*eap->arg == NUL)
7420 shell_resized();
7421 else
7422 mch_screenmode(eap->arg);
7423}
7424
7425#ifdef FEAT_WINDOWS
7426/*
7427 * ":resize".
7428 * set, increment or decrement current window height
7429 */
7430 static void
7431ex_resize(eap)
7432 exarg_T *eap;
7433{
7434 int n;
7435 win_T *wp = curwin;
7436
7437 if (eap->addr_count > 0)
7438 {
7439 n = eap->line2;
7440 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7441 ;
7442 }
7443
7444#ifdef FEAT_GUI
7445 need_mouse_correct = TRUE;
7446#endif
7447 n = atol((char *)eap->arg);
7448#ifdef FEAT_VERTSPLIT
7449 if (cmdmod.split & WSP_VERT)
7450 {
7451 if (*eap->arg == '-' || *eap->arg == '+')
7452 n += W_WIDTH(curwin);
7453 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7454 n = 9999;
7455 win_setwidth_win((int)n, wp);
7456 }
7457 else
7458#endif
7459 {
7460 if (*eap->arg == '-' || *eap->arg == '+')
7461 n += curwin->w_height;
7462 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7463 n = 9999;
7464 win_setheight_win((int)n, wp);
7465 }
7466}
7467#endif
7468
7469/*
7470 * ":find [+command] <file>" command.
7471 */
7472 static void
7473ex_find(eap)
7474 exarg_T *eap;
7475{
7476#ifdef FEAT_SEARCHPATH
7477 char_u *fname;
7478 int count;
7479
7480 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7481 TRUE, curbuf->b_ffname);
7482 if (eap->addr_count > 0)
7483 {
7484 /* Repeat finding the file "count" times. This matters when it
7485 * appears several times in the path. */
7486 count = eap->line2;
7487 while (fname != NULL && --count > 0)
7488 {
7489 vim_free(fname);
7490 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7491 FALSE, curbuf->b_ffname);
7492 }
7493 }
7494
7495 if (fname != NULL)
7496 {
7497 eap->arg = fname;
7498#endif
7499 do_exedit(eap, NULL);
7500#ifdef FEAT_SEARCHPATH
7501 vim_free(fname);
7502 }
7503#endif
7504}
7505
7506/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007507 * ":open" simulation: for now just work like ":visual".
7508 */
7509 static void
7510ex_open(eap)
7511 exarg_T *eap;
7512{
7513 regmatch_T regmatch;
7514 char_u *p;
7515
7516 curwin->w_cursor.lnum = eap->line2;
7517 beginline(BL_SOL | BL_FIX);
7518 if (*eap->arg == '/')
7519 {
7520 /* ":open /pattern/": put cursor in column found with pattern */
7521 ++eap->arg;
7522 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7523 *p = NUL;
7524 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7525 if (regmatch.regprog != NULL)
7526 {
7527 regmatch.rm_ic = p_ic;
7528 p = ml_get_curline();
7529 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007530 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007531 else
7532 EMSG(_(e_nomatch));
7533 vim_free(regmatch.regprog);
7534 }
7535 /* Move to the NUL, ignore any other arguments. */
7536 eap->arg += STRLEN(eap->arg);
7537 }
7538 check_cursor();
7539
7540 eap->cmdidx = CMD_visual;
7541 do_exedit(eap, NULL);
7542}
7543
7544/*
7545 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 */
7547 static void
7548ex_edit(eap)
7549 exarg_T *eap;
7550{
7551 do_exedit(eap, NULL);
7552}
7553
7554/*
7555 * ":edit <file>" command and alikes.
7556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 void
7558do_exedit(eap, old_curwin)
7559 exarg_T *eap;
7560 win_T *old_curwin; /* curwin before doing a split or NULL */
7561{
7562 int n;
7563#ifdef FEAT_WINDOWS
7564 int need_hide;
7565#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007566 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567
7568 /*
7569 * ":vi" command ends Ex mode.
7570 */
7571 if (exmode_active && (eap->cmdidx == CMD_visual
7572 || eap->cmdidx == CMD_view))
7573 {
7574 exmode_active = FALSE;
7575 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007576 {
7577 /* Special case: ":global/pat/visual\NLvi-commands" */
7578 if (global_busy)
7579 {
7580 int rd = RedrawingDisabled;
7581 int nwr = no_wait_return;
7582 int ms = msg_scroll;
7583#ifdef FEAT_GUI
7584 int he = hold_gui_events;
7585#endif
7586
7587 if (eap->nextcmd != NULL)
7588 {
7589 stuffReadbuff(eap->nextcmd);
7590 eap->nextcmd = NULL;
7591 }
7592
7593 if (exmode_was != EXMODE_VIM)
7594 settmode(TMODE_RAW);
7595 RedrawingDisabled = 0;
7596 no_wait_return = 0;
7597 need_wait_return = FALSE;
7598 msg_scroll = 0;
7599#ifdef FEAT_GUI
7600 hold_gui_events = 0;
7601#endif
7602 must_redraw = CLEAR;
7603
7604 main_loop(FALSE, TRUE);
7605
7606 RedrawingDisabled = rd;
7607 no_wait_return = nwr;
7608 msg_scroll = ms;
7609#ifdef FEAT_GUI
7610 hold_gui_events = he;
7611#endif
7612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 }
7616
7617 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007618 || eap->cmdidx == CMD_tabnew
7619 || eap->cmdidx == CMD_tabedit
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620#ifdef FEAT_VERTSPLIT
7621 || eap->cmdidx == CMD_vnew
7622#endif
7623 ) && *eap->arg == NUL)
7624 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007625 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 setpcmark();
7627 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007628 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7629 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
7631 else if ((eap->cmdidx != CMD_split
7632#ifdef FEAT_VERTSPLIT
7633 && eap->cmdidx != CMD_vsplit
7634#endif
7635 )
7636 || *eap->arg != NUL
7637#ifdef FEAT_BROWSE
7638 || cmdmod.browse
7639#endif
7640 )
7641 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007642#ifdef FEAT_AUTOCMD
7643 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7644 * can bring us here, others are stopped earlier. */
7645 if (*eap->arg != NUL && curbuf_locked())
7646 return;
7647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 n = readonlymode;
7649 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7650 readonlymode = TRUE;
7651 else if (eap->cmdidx == CMD_enew)
7652 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7653 empty buffer */
7654 setpcmark();
7655 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7656 NULL, eap,
7657 /* ":edit" goes to first line if Vi compatible */
7658 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7659 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7660 ? ECMD_ONE : eap->do_ecmd_lnum,
7661 (P_HID(curbuf) ? ECMD_HIDE : 0)
7662 + (eap->forceit ? ECMD_FORCEIT : 0)
7663#ifdef FEAT_LISTCMDS
7664 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
7665#endif
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007666 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 {
7668 /* Editing the file failed. If the window was split, close it. */
7669#ifdef FEAT_WINDOWS
7670 if (old_curwin != NULL)
7671 {
7672 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
7673 if (!need_hide || P_HID(curbuf))
7674 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007675# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7676 cleanup_T cs;
7677
7678 /* Reset the error/interrupt/exception state here so that
7679 * aborting() returns FALSE when closing a window. */
7680 enter_cleanup(&cs);
7681# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682# ifdef FEAT_GUI
7683 need_mouse_correct = TRUE;
7684# endif
7685 win_close(curwin, !need_hide && !P_HID(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007686
7687# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7688 /* Restore the error/interrupt/exception state if not
7689 * discarded by a new aborting error, interrupt, or
7690 * uncaught exception. */
7691 leave_cleanup(&cs);
7692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 }
7694 }
7695#endif
7696 }
7697 else if (readonlymode && curbuf->b_nwindows == 1)
7698 {
7699 /* When editing an already visited buffer, 'readonly' won't be set
7700 * but the previous value is kept. With ":view" and ":sview" we
7701 * want the file to be readonly, except when another window is
7702 * editing the same buffer. */
7703 curbuf->b_p_ro = TRUE;
7704 }
7705 readonlymode = n;
7706 }
7707 else
7708 {
7709 if (eap->do_ecmd_cmd != NULL)
7710 do_cmdline_cmd(eap->do_ecmd_cmd);
7711#ifdef FEAT_TITLE
7712 n = curwin->w_arg_idx_invalid;
7713#endif
7714 check_arg_idx(curwin);
7715#ifdef FEAT_TITLE
7716 if (n != curwin->w_arg_idx_invalid)
7717 maketitle();
7718#endif
7719 }
7720
7721#ifdef FEAT_WINDOWS
7722 /*
7723 * if ":split file" worked, set alternate file name in old window to new
7724 * file
7725 */
7726 if (old_curwin != NULL
7727 && *eap->arg != NUL
7728 && curwin != old_curwin
7729 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007730 && old_curwin->w_buffer != curbuf
7731 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 old_curwin->w_alt_fnum = curbuf->b_fnum;
7733#endif
7734
7735 ex_no_reprint = TRUE;
7736}
7737
7738#ifndef FEAT_GUI
7739/*
7740 * ":gui" and ":gvim" when there is no GUI.
7741 */
7742 static void
7743ex_nogui(eap)
7744 exarg_T *eap;
7745{
7746 eap->errmsg = e_nogvim;
7747}
7748#endif
7749
7750#if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
7751 static void
7752ex_tearoff(eap)
7753 exarg_T *eap;
7754{
7755 gui_make_tearoff(eap->arg);
7756}
7757#endif
7758
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007759#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 static void
7761ex_popup(eap)
7762 exarg_T *eap;
7763{
Bram Moolenaar97409f12005-07-08 22:17:29 +00007764 gui_make_popup(eap->arg, eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765}
7766#endif
7767
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 static void
7769ex_swapname(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007770 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771{
7772 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
7773 MSG(_("No swap file"));
7774 else
7775 msg(curbuf->b_ml.ml_mfp->mf_fname);
7776}
7777
7778/*
7779 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7780 * offset.
7781 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 static void
7784ex_syncbind(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007785 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787#ifdef FEAT_SCROLLBIND
7788 win_T *wp;
7789 long topline;
7790 long y;
7791 linenr_T old_linenr = curwin->w_cursor.lnum;
7792
7793 setpcmark();
7794
7795 /*
7796 * determine max topline
7797 */
7798 if (curwin->w_p_scb)
7799 {
7800 topline = curwin->w_topline;
7801 for (wp = firstwin; wp; wp = wp->w_next)
7802 {
7803 if (wp->w_p_scb && wp->w_buffer)
7804 {
7805 y = wp->w_buffer->b_ml.ml_line_count - p_so;
7806 if (topline > y)
7807 topline = y;
7808 }
7809 }
7810 if (topline < 1)
7811 topline = 1;
7812 }
7813 else
7814 {
7815 topline = 1;
7816 }
7817
7818
7819 /*
7820 * set all scrollbind windows to the same topline
7821 */
7822 wp = curwin;
7823 for (curwin = firstwin; curwin; curwin = curwin->w_next)
7824 {
7825 if (curwin->w_p_scb)
7826 {
7827 y = topline - curwin->w_topline;
7828 if (y > 0)
7829 scrollup(y, TRUE);
7830 else
7831 scrolldown(-y, TRUE);
7832 curwin->w_scbind_pos = topline;
7833 redraw_later(VALID);
7834 cursor_correct();
7835#ifdef FEAT_WINDOWS
7836 curwin->w_redr_status = TRUE;
7837#endif
7838 }
7839 }
7840 curwin = wp;
7841 if (curwin->w_p_scb)
7842 {
7843 did_syncbind = TRUE;
7844 checkpcmark();
7845 if (old_linenr != curwin->w_cursor.lnum)
7846 {
7847 char_u ctrl_o[2];
7848
7849 ctrl_o[0] = Ctrl_O;
7850 ctrl_o[1] = 0;
7851 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7852 }
7853 }
7854#endif
7855}
7856
7857
7858 static void
7859ex_read(eap)
7860 exarg_T *eap;
7861{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007862 int i;
7863 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7864 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865
7866 if (eap->usefilter) /* :r!cmd */
7867 do_bang(1, eap, FALSE, FALSE, TRUE);
7868 else
7869 {
7870 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7871 return;
7872
7873#ifdef FEAT_BROWSE
7874 if (cmdmod.browse)
7875 {
7876 char_u *browseFile;
7877
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007878 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 NULL, NULL, NULL, curbuf);
7880 if (browseFile != NULL)
7881 {
7882 i = readfile(browseFile, NULL,
7883 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7884 vim_free(browseFile);
7885 }
7886 else
7887 i = OK;
7888 }
7889 else
7890#endif
7891 if (*eap->arg == NUL)
7892 {
7893 if (check_fname() == FAIL) /* check for no file name */
7894 return;
7895 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7896 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7897 }
7898 else
7899 {
7900 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7901 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7902 i = readfile(eap->arg, NULL,
7903 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7904
7905 }
7906 if (i == FAIL)
7907 {
7908#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7909 if (!aborting())
7910#endif
7911 EMSG2(_(e_notopen), eap->arg);
7912 }
7913 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914 {
7915 if (empty && exmode_active)
7916 {
7917 /* Delete the empty line that remains. Historically ex does
7918 * this but vi doesn't. */
7919 if (eap->line2 == 0)
7920 lnum = curbuf->b_ml.ml_line_count;
7921 else
7922 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007923 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007924 {
7925 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007926 if (curwin->w_cursor.lnum > 1
7927 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007928 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007929 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007930 }
7931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 }
7935}
7936
Bram Moolenaarea408852005-06-25 22:49:46 +00007937static char_u *prev_dir = NULL;
7938
7939#if defined(EXITFREE) || defined(PROTO)
7940 void
7941free_cd_dir()
7942{
7943 vim_free(prev_dir);
Bram Moolenaara0174af2008-01-02 20:08:25 +00007944 prev_dir = NULL;
Bram Moolenaar60f39ae2009-03-11 14:10:38 +00007945
7946 vim_free(globaldir);
7947 globaldir = NULL;
Bram Moolenaarea408852005-06-25 22:49:46 +00007948}
7949#endif
7950
7951
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952/*
7953 * ":cd", ":lcd", ":chdir" and ":lchdir".
7954 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007955 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956ex_cd(eap)
7957 exarg_T *eap;
7958{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 char_u *new_dir;
7960 char_u *tofree;
7961
7962 new_dir = eap->arg;
7963#if !defined(UNIX) && !defined(VMS)
7964 /* for non-UNIX ":cd" means: print current directory */
7965 if (*new_dir == NUL)
7966 ex_pwd(NULL);
7967 else
7968#endif
7969 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007970#ifdef FEAT_AUTOCMD
7971 if (allbuf_locked())
7972 return;
7973#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00007974 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7975 && !eap->forceit)
7976 {
Bram Moolenaar81870892007-11-11 18:17:28 +00007977 EMSG(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007978 return;
7979 }
7980
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 /* ":cd -": Change to previous directory */
7982 if (STRCMP(new_dir, "-") == 0)
7983 {
7984 if (prev_dir == NULL)
7985 {
7986 EMSG(_("E186: No previous directory"));
7987 return;
7988 }
7989 new_dir = prev_dir;
7990 }
7991
7992 /* Save current directory for next ":cd -" */
7993 tofree = prev_dir;
7994 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7995 prev_dir = vim_strsave(NameBuff);
7996 else
7997 prev_dir = NULL;
7998
7999#if defined(UNIX) || defined(VMS)
8000 /* for UNIX ":cd" means: go to home directory */
8001 if (*new_dir == NUL)
8002 {
8003 /* use NameBuff for home directory name */
8004# ifdef VMS
8005 char_u *p;
8006
8007 p = mch_getenv((char_u *)"SYS$LOGIN");
8008 if (p == NULL || *p == NUL) /* empty is the same as not set */
8009 NameBuff[0] = NUL;
8010 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00008011 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012# else
8013 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
8014# endif
8015 new_dir = NameBuff;
8016 }
8017#endif
8018 if (new_dir == NULL || vim_chdir(new_dir))
8019 EMSG(_(e_failed));
8020 else
8021 {
8022 vim_free(curwin->w_localdir);
8023 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
8024 {
8025 /* If still in global directory, need to remember current
8026 * directory as global directory. */
8027 if (globaldir == NULL && prev_dir != NULL)
8028 globaldir = vim_strsave(prev_dir);
8029 /* Remember this local directory for the window. */
8030 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8031 curwin->w_localdir = vim_strsave(NameBuff);
8032 }
8033 else
8034 {
8035 /* We are now in the global directory, no need to remember its
8036 * name. */
8037 vim_free(globaldir);
8038 globaldir = NULL;
8039 curwin->w_localdir = NULL;
8040 }
8041
8042 shorten_fnames(TRUE);
8043
8044 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00008045 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 ex_pwd(eap);
8047 }
8048 vim_free(tofree);
8049 }
8050}
8051
8052/*
8053 * ":pwd".
8054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 static void
8056ex_pwd(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008057 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058{
8059 if (mch_dirname(NameBuff, MAXPATHL) == OK)
8060 {
8061#ifdef BACKSLASH_IN_FILENAME
8062 slash_adjust(NameBuff);
8063#endif
8064 msg(NameBuff);
8065 }
8066 else
8067 EMSG(_("E187: Unknown"));
8068}
8069
8070/*
8071 * ":=".
8072 */
8073 static void
8074ex_equal(eap)
8075 exarg_T *eap;
8076{
8077 smsg((char_u *)"%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008078 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079}
8080
8081 static void
8082ex_sleep(eap)
8083 exarg_T *eap;
8084{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008085 int n;
8086 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087
8088 if (cursor_valid())
8089 {
8090 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
8091 if (n >= 0)
8092 windgoto((int)n, curwin->w_wcol);
8093 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00008094
8095 len = eap->line2;
8096 switch (*eap->arg)
8097 {
8098 case 'm': break;
8099 case NUL: len *= 1000L; break;
8100 default: EMSG2(_(e_invarg2), eap->arg); return;
8101 }
8102 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103}
8104
8105/*
8106 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
8107 */
8108 void
8109do_sleep(msec)
8110 long msec;
8111{
8112 long done;
8113
8114 cursor_on();
8115 out_flush();
8116 for (done = 0; !got_int && done < msec; done += 1000L)
8117 {
8118 ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE);
8119 ui_breakcheck();
8120 }
8121}
8122
8123 static void
8124do_exmap(eap, isabbrev)
8125 exarg_T *eap;
8126 int isabbrev;
8127{
8128 int mode;
8129 char_u *cmdp;
8130
8131 cmdp = eap->cmd;
8132 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
8133
8134 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
8135 eap->arg, mode, isabbrev))
8136 {
8137 case 1: EMSG(_(e_invarg));
8138 break;
8139 case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap));
8140 break;
8141 }
8142}
8143
8144/*
8145 * ":winsize" command (obsolete).
8146 */
8147 static void
8148ex_winsize(eap)
8149 exarg_T *eap;
8150{
8151 int w, h;
8152 char_u *arg = eap->arg;
8153 char_u *p;
8154
8155 w = getdigits(&arg);
8156 arg = skipwhite(arg);
8157 p = arg;
8158 h = getdigits(&arg);
8159 if (*p != NUL && *arg == NUL)
8160 set_shellsize(w, h, TRUE);
8161 else
8162 EMSG(_("E465: :winsize requires two number arguments"));
8163}
8164
8165#ifdef FEAT_WINDOWS
8166 static void
8167ex_wincmd(eap)
8168 exarg_T *eap;
8169{
8170 int xchar = NUL;
8171 char_u *p;
8172
8173 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
8174 {
8175 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
8176 if (eap->arg[1] == NUL)
8177 {
8178 EMSG(_(e_invarg));
8179 return;
8180 }
8181 xchar = eap->arg[1];
8182 p = eap->arg + 2;
8183 }
8184 else
8185 p = eap->arg + 1;
8186
8187 eap->nextcmd = check_nextcmd(p);
8188 p = skipwhite(p);
8189 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
8190 EMSG(_(e_invarg));
8191 else
8192 {
8193 /* Pass flags on for ":vertical wincmd ]". */
8194 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008195 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
8197 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008198 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 }
8200}
8201#endif
8202
Bram Moolenaar843ee412004-06-30 16:16:41 +00008203#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204/*
8205 * ":winpos".
8206 */
8207 static void
8208ex_winpos(eap)
8209 exarg_T *eap;
8210{
8211 int x, y;
8212 char_u *arg = eap->arg;
8213 char_u *p;
8214
8215 if (*arg == NUL)
8216 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00008217# if defined(FEAT_GUI) || defined(MSWIN)
8218# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00008220# else
8221 if (mch_get_winpos(&x, &y) != FAIL)
8222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {
8224 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
8225 msg(IObuff);
8226 }
8227 else
8228# endif
8229 EMSG(_("E188: Obtaining window position not implemented for this platform"));
8230 }
8231 else
8232 {
8233 x = getdigits(&arg);
8234 arg = skipwhite(arg);
8235 p = arg;
8236 y = getdigits(&arg);
8237 if (*p == NUL || *arg != NUL)
8238 {
8239 EMSG(_("E466: :winpos requires two number arguments"));
8240 return;
8241 }
8242# ifdef FEAT_GUI
8243 if (gui.in_use)
8244 gui_mch_set_winpos(x, y);
8245 else if (gui.starting)
8246 {
8247 /* Remember the coordinates for when the window is opened. */
8248 gui_win_x = x;
8249 gui_win_y = y;
8250 }
8251# ifdef HAVE_TGETENT
8252 else
8253# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008254# else
8255# ifdef MSWIN
8256 mch_set_winpos(x, y);
8257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258# endif
8259# ifdef HAVE_TGETENT
8260 if (*T_CWP)
8261 term_set_winpos(x, y);
8262# endif
8263 }
8264}
8265#endif
8266
8267/*
8268 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
8269 */
8270 static void
8271ex_operators(eap)
8272 exarg_T *eap;
8273{
8274 oparg_T oa;
8275
8276 clear_oparg(&oa);
8277 oa.regname = eap->regname;
8278 oa.start.lnum = eap->line1;
8279 oa.end.lnum = eap->line2;
8280 oa.line_count = eap->line2 - eap->line1 + 1;
8281 oa.motion_type = MLINE;
8282#ifdef FEAT_VIRTUALEDIT
8283 virtual_op = FALSE;
8284#endif
8285 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
8286 {
8287 setpcmark();
8288 curwin->w_cursor.lnum = eap->line1;
8289 beginline(BL_SOL | BL_FIX);
8290 }
8291
8292 switch (eap->cmdidx)
8293 {
8294 case CMD_delete:
8295 oa.op_type = OP_DELETE;
8296 op_delete(&oa);
8297 break;
8298
8299 case CMD_yank:
8300 oa.op_type = OP_YANK;
8301 (void)op_yank(&oa, FALSE, TRUE);
8302 break;
8303
8304 default: /* CMD_rshift or CMD_lshift */
8305 if ((eap->cmdidx == CMD_rshift)
8306#ifdef FEAT_RIGHTLEFT
8307 ^ curwin->w_p_rl
8308#endif
8309 )
8310 oa.op_type = OP_RSHIFT;
8311 else
8312 oa.op_type = OP_LSHIFT;
8313 op_shift(&oa, FALSE, eap->amount);
8314 break;
8315 }
8316#ifdef FEAT_VIRTUALEDIT
8317 virtual_op = MAYBE;
8318#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00008319 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320}
8321
8322/*
8323 * ":put".
8324 */
8325 static void
8326ex_put(eap)
8327 exarg_T *eap;
8328{
8329 /* ":0put" works like ":1put!". */
8330 if (eap->line2 == 0)
8331 {
8332 eap->line2 = 1;
8333 eap->forceit = TRUE;
8334 }
8335 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008336 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
8337 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338}
8339
8340/*
8341 * Handle ":copy" and ":move".
8342 */
8343 static void
8344ex_copymove(eap)
8345 exarg_T *eap;
8346{
8347 long n;
8348
8349 n = get_address(&eap->arg, FALSE, FALSE);
8350 if (eap->arg == NULL) /* error detected */
8351 {
8352 eap->nextcmd = NULL;
8353 return;
8354 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008355 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 /*
8358 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8359 */
8360 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8361 {
8362 EMSG(_(e_invaddr));
8363 return;
8364 }
8365
8366 if (eap->cmdidx == CMD_move)
8367 {
8368 if (do_move(eap->line1, eap->line2, n) == FAIL)
8369 return;
8370 }
8371 else
8372 ex_copy(eap->line1, eap->line2, n);
8373 u_clearline();
8374 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008375 ex_may_print(eap);
8376}
8377
8378/*
8379 * Print the current line if flags were given to the Ex command.
8380 */
8381 static void
8382ex_may_print(eap)
8383 exarg_T *eap;
8384{
8385 if (eap->flags != 0)
8386 {
8387 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8388 (eap->flags & EXFLAG_LIST));
8389 ex_no_reprint = TRUE;
8390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391}
8392
8393/*
8394 * ":smagic" and ":snomagic".
8395 */
8396 static void
8397ex_submagic(eap)
8398 exarg_T *eap;
8399{
8400 int magic_save = p_magic;
8401
8402 p_magic = (eap->cmdidx == CMD_smagic);
8403 do_sub(eap);
8404 p_magic = magic_save;
8405}
8406
8407/*
8408 * ":join".
8409 */
8410 static void
8411ex_join(eap)
8412 exarg_T *eap;
8413{
8414 curwin->w_cursor.lnum = eap->line1;
8415 if (eap->line1 == eap->line2)
8416 {
8417 if (eap->addr_count >= 2) /* :2,2join does nothing */
8418 return;
8419 if (eap->line2 == curbuf->b_ml.ml_line_count)
8420 {
8421 beep_flush();
8422 return;
8423 }
8424 ++eap->line2;
8425 }
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008426 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008428 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429}
8430
8431/*
8432 * ":[addr]@r" or ":[addr]*r": execute register
8433 */
8434 static void
8435ex_at(eap)
8436 exarg_T *eap;
8437{
8438 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008439 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440
8441 curwin->w_cursor.lnum = eap->line2;
8442
8443#ifdef USE_ON_FLY_SCROLL
8444 dont_scroll = TRUE; /* disallow scrolling here */
8445#endif
8446
8447 /* get the register name. No name means to use the previous one */
8448 c = *eap->arg;
8449 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8450 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008451 /* Put the register in the typeahead buffer with the "silent" flag. */
8452 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8453 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 else
8458 {
8459 int save_efr = exec_from_reg;
8460
8461 exec_from_reg = TRUE;
8462
8463 /*
8464 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008465 * Continue until the stuff buffer is empty and all added characters
8466 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008468 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8470
8471 exec_from_reg = save_efr;
8472 }
8473}
8474
8475/*
8476 * ":!".
8477 */
8478 static void
8479ex_bang(eap)
8480 exarg_T *eap;
8481{
8482 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8483}
8484
8485/*
8486 * ":undo".
8487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 static void
8489ex_undo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008490 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008492 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008493 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008494 else
8495 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496}
8497
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008498#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008499 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008500ex_wundo(eap)
8501 exarg_T *eap;
8502{
8503 char_u hash[UNDO_HASH_SIZE];
8504
8505 u_compute_hash(hash);
8506 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8507}
8508
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008509 static void
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008510ex_rundo(eap)
8511 exarg_T *eap;
8512{
8513 char_u hash[UNDO_HASH_SIZE];
8514
8515 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008516 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008517}
8518#endif
8519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520/*
8521 * ":redo".
8522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 static void
8524ex_redo(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008525 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526{
8527 u_redo(1);
8528}
8529
8530/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008531 * ":earlier" and ":later".
8532 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008533 static void
8534ex_later(eap)
8535 exarg_T *eap;
8536{
8537 long count = 0;
8538 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008539 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008540 char_u *p = eap->arg;
8541
8542 if (*p == NUL)
8543 count = 1;
8544 else if (isdigit(*p))
8545 {
8546 count = getdigits(&p);
8547 switch (*p)
8548 {
8549 case 's': ++p; sec = TRUE; break;
8550 case 'm': ++p; sec = TRUE; count *= 60; break;
8551 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008552 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8553 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008554 }
8555 }
8556
8557 if (*p != NUL)
8558 EMSG2(_(e_invarg2), eap->arg);
8559 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008560 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8561 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008562}
8563
8564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 * ":redir": start/stop redirection.
8566 */
8567 static void
8568ex_redir(eap)
8569 exarg_T *eap;
8570{
8571 char *mode;
8572 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008573 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
8575 if (STRICMP(eap->arg, "END") == 0)
8576 close_redir();
8577 else
8578 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008579 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008581 ++arg;
8582 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008584 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 mode = "a";
8586 }
8587 else
8588 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008589 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590
8591 close_redir();
8592
8593 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008594 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 if (fname == NULL)
8596 return;
8597#ifdef FEAT_BROWSE
8598 if (cmdmod.browse)
8599 {
8600 char_u *browseFile;
8601
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008602 browseFile = do_browse(BROWSE_SAVE,
8603 (char_u *)_("Save Redirection"),
8604 fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 if (browseFile == NULL)
8606 return; /* operation cancelled */
8607 vim_free(fname);
8608 fname = browseFile;
8609 eap->forceit = TRUE; /* since dialog already asked */
8610 }
8611#endif
8612
8613 redir_fd = open_exfile(fname, eap->forceit, mode);
8614 vim_free(fname);
8615 }
8616#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008617 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {
8619 /* redirect to a register a-z (resp. A-Z for appending) */
8620 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008621 ++arg;
8622 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008624 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008625 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008627 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008629 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008630 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008631 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008632 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008634 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008635 if (*arg == '>')
8636 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008637 /* Make register empty when not using @A-@Z and the
8638 * command is valid. */
8639 if (*arg == NUL && !isupper(redir_reg))
8640 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008642 }
8643 if (*arg != NUL)
8644 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008645 redir_reg = 0;
Bram Moolenaard9d30582005-05-18 22:10:28 +00008646 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008647 }
8648 }
8649 else if (*arg == '=' && arg[1] == '>')
8650 {
8651 int append;
8652
8653 /* redirect to a variable */
8654 close_redir();
8655 arg += 2;
8656
8657 if (*arg == '>')
8658 {
8659 ++arg;
8660 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 }
8662 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008663 append = FALSE;
8664
8665 if (var_redir_start(skipwhite(arg), append) == OK)
8666 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 }
8668#endif
8669
8670 /* TODO: redirect to a buffer */
8671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 else
Bram Moolenaarca472992005-01-21 11:46:23 +00008673 EMSG2(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008675
8676 /* Make sure redirection is not off. Can happen for cmdline completion
8677 * that indirectly invokes a command to catch its output. */
8678 if (redir_fd != NULL
8679#ifdef FEAT_EVAL
8680 || redir_reg || redir_vname
8681#endif
8682 )
8683 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684}
8685
8686/*
8687 * ":redraw": force redraw
8688 */
8689 static void
8690ex_redraw(eap)
8691 exarg_T *eap;
8692{
8693 int r = RedrawingDisabled;
8694 int p = p_lz;
8695
8696 RedrawingDisabled = 0;
8697 p_lz = FALSE;
8698 update_topline();
8699 update_screen(eap->forceit ? CLEAR :
8700#ifdef FEAT_VISUAL
8701 VIsual_active ? INVERTED :
8702#endif
8703 0);
8704#ifdef FEAT_TITLE
8705 if (need_maketitle)
8706 maketitle();
8707#endif
8708 RedrawingDisabled = r;
8709 p_lz = p;
8710
8711 /* Reset msg_didout, so that a message that's there is overwritten. */
8712 msg_didout = FALSE;
8713 msg_col = 0;
8714
8715 out_flush();
8716}
8717
8718/*
8719 * ":redrawstatus": force redraw of status line(s)
8720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 static void
8722ex_redrawstatus(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008723 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724{
8725#if defined(FEAT_WINDOWS)
8726 int r = RedrawingDisabled;
8727 int p = p_lz;
8728
8729 RedrawingDisabled = 0;
8730 p_lz = FALSE;
8731 if (eap->forceit)
8732 status_redraw_all();
8733 else
8734 status_redraw_curbuf();
8735 update_screen(
8736# ifdef FEAT_VISUAL
8737 VIsual_active ? INVERTED :
8738# endif
8739 0);
8740 RedrawingDisabled = r;
8741 p_lz = p;
8742 out_flush();
8743#endif
8744}
8745
8746 static void
8747close_redir()
8748{
8749 if (redir_fd != NULL)
8750 {
8751 fclose(redir_fd);
8752 redir_fd = NULL;
8753 }
8754#ifdef FEAT_EVAL
8755 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008756 if (redir_vname)
8757 {
8758 var_redir_stop();
8759 redir_vname = 0;
8760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761#endif
8762}
8763
8764#if defined(FEAT_SESSION) && defined(USE_CRNL)
8765# define MKSESSION_NL
8766static int mksession_nl = FALSE; /* use NL only in put_eol() */
8767#endif
8768
8769/*
8770 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8771 */
8772 static void
8773ex_mkrc(eap)
8774 exarg_T *eap;
8775{
8776 FILE *fd;
8777 int failed = FALSE;
8778 char_u *fname;
8779#ifdef FEAT_BROWSE
8780 char_u *browseFile = NULL;
8781#endif
8782#ifdef FEAT_SESSION
8783 int view_session = FALSE;
8784 int using_vdir = FALSE; /* using 'viewdir'? */
8785 char_u *viewFile = NULL;
8786 unsigned *flagp;
8787#endif
8788
8789 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8790 {
8791#ifdef FEAT_SESSION
8792 view_session = TRUE;
8793#else
8794 ex_ni(eap);
8795 return;
8796#endif
8797 }
8798
8799#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008800 /* Use the short file name until ":lcd" is used. We also don't use the
8801 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008802 did_lcd = FALSE;
8803
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8805 if (eap->cmdidx == CMD_mkview
8806 && (*eap->arg == NUL
8807 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8808 {
8809 eap->forceit = TRUE;
8810 fname = get_view_file(*eap->arg);
8811 if (fname == NULL)
8812 return;
8813 viewFile = fname;
8814 using_vdir = TRUE;
8815 }
8816 else
8817#endif
8818 if (*eap->arg != NUL)
8819 fname = eap->arg;
8820 else if (eap->cmdidx == CMD_mkvimrc)
8821 fname = (char_u *)VIMRC_FILE;
8822#ifdef FEAT_SESSION
8823 else if (eap->cmdidx == CMD_mksession)
8824 fname = (char_u *)SESSION_FILE;
8825#endif
8826 else
8827 fname = (char_u *)EXRC_FILE;
8828
8829#ifdef FEAT_BROWSE
8830 if (cmdmod.browse)
8831 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008832 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833# ifdef FEAT_SESSION
8834 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8835 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8836# endif
8837 (char_u *)_("Save Setup"),
8838 fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL);
8839 if (browseFile == NULL)
8840 goto theend;
8841 fname = browseFile;
8842 eap->forceit = TRUE; /* since dialog already asked */
8843 }
8844#endif
8845
8846#if defined(FEAT_SESSION) && defined(vim_mkdir)
8847 /* When using 'viewdir' may have to create the directory. */
8848 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008849 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850#endif
8851
8852 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8853 if (fd != NULL)
8854 {
8855#ifdef FEAT_SESSION
8856 if (eap->cmdidx == CMD_mkview)
8857 flagp = &vop_flags;
8858 else
8859 flagp = &ssop_flags;
8860#endif
8861
8862#ifdef MKSESSION_NL
8863 /* "unix" in 'sessionoptions': use NL line separator */
8864 if (view_session && (*flagp & SSOP_UNIX))
8865 mksession_nl = TRUE;
8866#endif
8867
8868 /* Write the version command for :mkvimrc */
8869 if (eap->cmdidx == CMD_mkvimrc)
8870 (void)put_line(fd, "version 6.0");
8871
8872#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008873 if (eap->cmdidx == CMD_mksession)
8874 {
8875 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8876 failed = TRUE;
8877 }
8878
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 if (eap->cmdidx != CMD_mkview)
8880#endif
8881 {
8882 /* Write setting 'compatible' first, because it has side effects.
8883 * For that same reason only do it when needed. */
8884 if (p_cp)
8885 (void)put_line(fd, "if !&cp | set cp | endif");
8886 else
8887 (void)put_line(fd, "if &cp | set nocp | endif");
8888 }
8889
8890#ifdef FEAT_SESSION
8891 if (!view_session
8892 || (eap->cmdidx == CMD_mksession
8893 && (*flagp & SSOP_OPTIONS)))
8894#endif
8895 failed |= (makemap(fd, NULL) == FAIL
8896 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8897
8898#ifdef FEAT_SESSION
8899 if (!failed && view_session)
8900 {
8901 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8902 failed = TRUE;
8903 if (eap->cmdidx == CMD_mksession)
8904 {
8905 char_u dirnow[MAXPATHL]; /* current directory */
8906
8907 /*
8908 * Change to session file's dir.
8909 */
8910 if (mch_dirname(dirnow, MAXPATHL) == FAIL
8911 || mch_chdir((char *)dirnow) != 0)
8912 *dirnow = NUL;
8913 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8914 {
8915 if (vim_chdirfile(fname) == OK)
8916 shorten_fnames(TRUE);
8917 }
8918 else if (*dirnow != NUL
8919 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8920 {
Bram Moolenaarc0b35652009-03-01 01:45:35 +00008921 if (mch_chdir((char *)globaldir) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00008922 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 }
8924
8925 failed |= (makeopens(fd, dirnow) == FAIL);
8926
8927 /* restore original dir */
8928 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
8929 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
8930 {
8931 if (mch_chdir((char *)dirnow) != 0)
8932 EMSG(_(e_prev_dir));
8933 shorten_fnames(TRUE);
8934 }
8935 }
8936 else
8937 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008938 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8939 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 }
8941 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8942 == FAIL)
8943 failed = TRUE;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008944 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8945 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008946 if (eap->cmdidx == CMD_mksession)
8947 {
8948 if (put_line(fd, "unlet SessionLoad") == FAIL)
8949 failed = TRUE;
8950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 }
8952#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008953 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8954 failed = TRUE;
8955
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 failed |= fclose(fd);
8957
8958 if (failed)
8959 EMSG(_(e_write));
8960#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8961 else if (eap->cmdidx == CMD_mksession)
8962 {
8963 /* successful session write - set this_session var */
8964 char_u tbuf[MAXPATHL];
8965
8966 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8967 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8968 }
8969#endif
8970#ifdef MKSESSION_NL
8971 mksession_nl = FALSE;
8972#endif
8973 }
8974
8975#ifdef FEAT_BROWSE
8976theend:
8977 vim_free(browseFile);
8978#endif
8979#ifdef FEAT_SESSION
8980 vim_free(viewFile);
8981#endif
8982}
8983
Bram Moolenaardf177f62005-02-22 08:39:57 +00008984#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8985 || defined(PROTO)
8986 int
8987vim_mkdir_emsg(name, prot)
8988 char_u *name;
Bram Moolenaar78a15312009-05-15 19:33:18 +00008989 int prot UNUSED;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008990{
8991 if (vim_mkdir(name, prot) != 0)
8992 {
8993 EMSG2(_("E739: Cannot create directory: %s"), name);
8994 return FAIL;
8995 }
8996 return OK;
8997}
8998#endif
8999
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000/*
9001 * Open a file for writing for an Ex command, with some checks.
9002 * Return file descriptor, or NULL on failure.
9003 */
9004 FILE *
9005open_exfile(fname, forceit, mode)
9006 char_u *fname;
9007 int forceit;
9008 char *mode; /* "w" for create new file or "a" for append */
9009{
9010 FILE *fd;
9011
9012#ifdef UNIX
9013 /* with Unix it is possible to open a directory */
9014 if (mch_isdir(fname))
9015 {
9016 EMSG2(_(e_isadir2), fname);
9017 return NULL;
9018 }
9019#endif
9020 if (!forceit && *mode != 'a' && vim_fexists(fname))
9021 {
9022 EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
9023 return NULL;
9024 }
9025
9026 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
9027 EMSG2(_("E190: Cannot open \"%s\" for writing"), fname);
9028
9029 return fd;
9030}
9031
9032/*
9033 * ":mark" and ":k".
9034 */
9035 static void
9036ex_mark(eap)
9037 exarg_T *eap;
9038{
9039 pos_T pos;
9040
9041 if (*eap->arg == NUL) /* No argument? */
9042 EMSG(_(e_argreq));
9043 else if (eap->arg[1] != NUL) /* more than one character? */
9044 EMSG(_(e_trailing));
9045 else
9046 {
9047 pos = curwin->w_cursor; /* save curwin->w_cursor */
9048 curwin->w_cursor.lnum = eap->line2;
9049 beginline(BL_WHITE | BL_FIX);
9050 if (setmark(*eap->arg) == FAIL) /* set mark */
9051 EMSG(_("E191: Argument must be a letter or forward/backward quote"));
9052 curwin->w_cursor = pos; /* restore curwin->w_cursor */
9053 }
9054}
9055
9056/*
9057 * Update w_topline, w_leftcol and the cursor position.
9058 */
9059 void
9060update_topline_cursor()
9061{
9062 check_cursor(); /* put cursor on valid line */
9063 update_topline();
9064 if (!curwin->w_p_wrap)
9065 validate_cursor();
9066 update_curswant();
9067}
9068
9069#ifdef FEAT_EX_EXTRA
9070/*
9071 * ":normal[!] {commands}": Execute normal mode commands.
9072 */
9073 static void
9074ex_normal(eap)
9075 exarg_T *eap;
9076{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077 int save_msg_scroll = msg_scroll;
9078 int save_restart_edit = restart_edit;
9079 int save_msg_didout = msg_didout;
9080 int save_State = State;
9081 tasave_T tabuf;
9082 int save_insertmode = p_im;
9083 int save_finish_op = finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009084 int save_opcount = opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085#ifdef FEAT_MBYTE
9086 char_u *arg = NULL;
9087 int l;
9088 char_u *p;
9089#endif
9090
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009091 if (ex_normal_lock > 0)
9092 {
9093 EMSG(_(e_secure));
9094 return;
9095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 if (ex_normal_busy >= p_mmd)
9097 {
9098 EMSG(_("E192: Recursive use of :normal too deep"));
9099 return;
9100 }
9101 ++ex_normal_busy;
9102
9103 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
9104 restart_edit = 0; /* don't go to Insert mode */
9105 p_im = FALSE; /* don't use 'insertmode' */
9106
9107#ifdef FEAT_MBYTE
9108 /*
9109 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
9110 * this for the K_SPECIAL leading byte, otherwise special keys will not
9111 * work.
9112 */
9113 if (has_mbyte)
9114 {
9115 int len = 0;
9116
9117 /* Count the number of characters to be escaped. */
9118 for (p = eap->arg; *p != NUL; ++p)
9119 {
9120# ifdef FEAT_GUI
9121 if (*p == CSI) /* leadbyte CSI */
9122 len += 2;
9123# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009124 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
9126# ifdef FEAT_GUI
9127 || *p == CSI
9128# endif
9129 )
9130 len += 2;
9131 }
9132 if (len > 0)
9133 {
9134 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
9135 if (arg != NULL)
9136 {
9137 len = 0;
9138 for (p = eap->arg; *p != NUL; ++p)
9139 {
9140 arg[len++] = *p;
9141# ifdef FEAT_GUI
9142 if (*p == CSI)
9143 {
9144 arg[len++] = KS_EXTRA;
9145 arg[len++] = (int)KE_CSI;
9146 }
9147# endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009148 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 {
9150 arg[len++] = *++p;
9151 if (*p == K_SPECIAL)
9152 {
9153 arg[len++] = KS_SPECIAL;
9154 arg[len++] = KE_FILLER;
9155 }
9156# ifdef FEAT_GUI
9157 else if (*p == CSI)
9158 {
9159 arg[len++] = KS_EXTRA;
9160 arg[len++] = (int)KE_CSI;
9161 }
9162# endif
9163 }
9164 arg[len] = NUL;
9165 }
9166 }
9167 }
9168 }
9169#endif
9170
9171 /*
9172 * Save the current typeahead. This is required to allow using ":normal"
9173 * from an event handler and makes sure we don't hang when the argument
9174 * ends with half a command.
9175 */
9176 save_typeahead(&tabuf);
9177 if (tabuf.typebuf_valid)
9178 {
9179 /*
9180 * Repeat the :normal command for each line in the range. When no
9181 * range given, execute it just once, without positioning the cursor
9182 * first.
9183 */
9184 do
9185 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 if (eap->addr_count != 0)
9187 {
9188 curwin->w_cursor.lnum = eap->line1++;
9189 curwin->w_cursor.col = 0;
9190 }
9191
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009192 exec_normal_cmd(
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#ifdef FEAT_MBYTE
9194 arg != NULL ? arg :
9195#endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009196 eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 }
9198 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
9199 }
9200
9201 /* Might not return to the main loop when in an event handler. */
9202 update_topline_cursor();
9203
9204 /* Restore the previous typeahead. */
9205 restore_typeahead(&tabuf);
9206
9207 --ex_normal_busy;
9208 msg_scroll = save_msg_scroll;
9209 restart_edit = save_restart_edit;
9210 p_im = save_insertmode;
9211 finish_op = save_finish_op;
Bram Moolenaar38084112008-07-26 14:05:07 +00009212 opcount = save_opcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 msg_didout |= save_msg_didout; /* don't reset msg_didout now */
9214
9215 /* Restore the state (needed when called from a function executed for
9216 * 'indentexpr'). */
9217 State = save_State;
9218#ifdef FEAT_MBYTE
9219 vim_free(arg);
9220#endif
9221}
9222
9223/*
Bram Moolenaar64969662005-12-14 21:59:55 +00009224 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 */
9226 static void
9227ex_startinsert(eap)
9228 exarg_T *eap;
9229{
Bram Moolenaarfd371682005-01-14 21:42:54 +00009230 if (eap->forceit)
9231 {
9232 coladvance((colnr_T)MAXCOL);
9233 curwin->w_curswant = MAXCOL;
9234 curwin->w_set_curswant = FALSE;
9235 }
9236
Bram Moolenaara40c5002005-01-09 21:16:21 +00009237 /* Ignore the command when already in Insert mode. Inserting an
9238 * expression register that invokes a function can do this. */
9239 if (State & INSERT)
9240 return;
9241
Bram Moolenaar64969662005-12-14 21:59:55 +00009242 if (eap->cmdidx == CMD_startinsert)
9243 restart_edit = 'a';
9244 else if (eap->cmdidx == CMD_startreplace)
9245 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 else
Bram Moolenaar64969662005-12-14 21:59:55 +00009247 restart_edit = 'V';
9248
9249 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009251 if (eap->cmdidx == CMD_startinsert)
9252 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 curwin->w_curswant = 0; /* avoid MAXCOL */
9254 }
9255}
9256
9257/*
9258 * ":stopinsert"
9259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 static void
9261ex_stopinsert(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009262 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263{
9264 restart_edit = 0;
9265 stop_insert_mode = TRUE;
9266}
9267#endif
9268
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009269#if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO)
9270/*
9271 * Execute normal mode command "cmd".
9272 * "remap" can be REMAP_NONE or REMAP_YES.
9273 */
9274 void
9275exec_normal_cmd(cmd, remap, silent)
9276 char_u *cmd;
9277 int remap;
9278 int silent;
9279{
9280 oparg_T oa;
9281
9282 /*
9283 * Stuff the argument into the typeahead buffer.
9284 * Execute normal_cmd() until there is no typeahead left.
9285 */
9286 clear_oparg(&oa);
9287 finish_op = FALSE;
9288 ins_typebuf(cmd, remap, 0, TRUE, silent);
9289 while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0))
9290 && !got_int)
9291 {
9292 update_topline_cursor();
9293 normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */
9294 }
9295}
9296#endif
9297
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298#ifdef FEAT_FIND_ID
9299 static void
9300ex_checkpath(eap)
9301 exarg_T *eap;
9302{
9303 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9304 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9305 (linenr_T)1, (linenr_T)MAXLNUM);
9306}
9307
9308#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
9309/*
9310 * ":psearch"
9311 */
9312 static void
9313ex_psearch(eap)
9314 exarg_T *eap;
9315{
9316 g_do_tagpreview = p_pvh;
9317 ex_findpat(eap);
9318 g_do_tagpreview = 0;
9319}
9320#endif
9321
9322 static void
9323ex_findpat(eap)
9324 exarg_T *eap;
9325{
9326 int whole = TRUE;
9327 long n;
9328 char_u *p;
9329 int action;
9330
9331 switch (cmdnames[eap->cmdidx].cmd_name[2])
9332 {
9333 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9334 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9335 action = ACTION_GOTO;
9336 else
9337 action = ACTION_SHOW;
9338 break;
9339 case 'i': /* ":ilist" and ":dlist" */
9340 action = ACTION_SHOW_ALL;
9341 break;
9342 case 'u': /* ":ijump" and ":djump" */
9343 action = ACTION_GOTO;
9344 break;
9345 default: /* ":isplit" and ":dsplit" */
9346 action = ACTION_SPLIT;
9347 break;
9348 }
9349
9350 n = 1;
9351 if (vim_isdigit(*eap->arg)) /* get count */
9352 {
9353 n = getdigits(&eap->arg);
9354 eap->arg = skipwhite(eap->arg);
9355 }
9356 if (*eap->arg == '/') /* Match regexp, not just whole words */
9357 {
9358 whole = FALSE;
9359 ++eap->arg;
9360 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9361 if (*p)
9362 {
9363 *p++ = NUL;
9364 p = skipwhite(p);
9365
9366 /* Check for trailing illegal characters */
9367 if (!ends_excmd(*p))
9368 eap->errmsg = e_trailing;
9369 else
9370 eap->nextcmd = check_nextcmd(p);
9371 }
9372 }
9373 if (!eap->skip)
9374 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9375 whole, !eap->forceit,
9376 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9377 n, action, eap->line1, eap->line2);
9378}
9379#endif
9380
9381#ifdef FEAT_WINDOWS
9382
9383# ifdef FEAT_QUICKFIX
9384/*
9385 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9386 */
9387 static void
9388ex_ptag(eap)
9389 exarg_T *eap;
9390{
9391 g_do_tagpreview = p_pvh;
9392 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9393}
9394
9395/*
9396 * ":pedit"
9397 */
9398 static void
9399ex_pedit(eap)
9400 exarg_T *eap;
9401{
9402 win_T *curwin_save = curwin;
9403
9404 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009405 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 keep_help_flag = curwin_save->w_buffer->b_help;
9407 do_exedit(eap, NULL);
9408 keep_help_flag = FALSE;
9409 if (curwin != curwin_save && win_valid(curwin_save))
9410 {
9411 /* Return cursor to where we were */
9412 validate_cursor();
9413 redraw_later(VALID);
9414 win_enter(curwin_save, TRUE);
9415 }
9416 g_do_tagpreview = 0;
9417}
9418# endif
9419
9420/*
9421 * ":stag", ":stselect" and ":stjump".
9422 */
9423 static void
9424ex_stag(eap)
9425 exarg_T *eap;
9426{
9427 postponed_split = -1;
9428 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009429 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9431 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009432 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433}
9434#endif
9435
9436/*
9437 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9438 */
9439 static void
9440ex_tag(eap)
9441 exarg_T *eap;
9442{
9443 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9444}
9445
9446 static void
9447ex_tag_cmd(eap, name)
9448 exarg_T *eap;
9449 char_u *name;
9450{
9451 int cmd;
9452
9453 switch (name[1])
9454 {
9455 case 'j': cmd = DT_JUMP; /* ":tjump" */
9456 break;
9457 case 's': cmd = DT_SELECT; /* ":tselect" */
9458 break;
9459 case 'p': cmd = DT_PREV; /* ":tprevious" */
9460 break;
9461 case 'N': cmd = DT_PREV; /* ":tNext" */
9462 break;
9463 case 'n': cmd = DT_NEXT; /* ":tnext" */
9464 break;
9465 case 'o': cmd = DT_POP; /* ":pop" */
9466 break;
9467 case 'f': /* ":tfirst" */
9468 case 'r': cmd = DT_FIRST; /* ":trewind" */
9469 break;
9470 case 'l': cmd = DT_LAST; /* ":tlast" */
9471 break;
9472 default: /* ":tag" */
9473#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009474 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
9476 do_cstag(eap);
9477 return;
9478 }
9479#endif
9480 cmd = DT_TAG;
9481 break;
9482 }
9483
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009484 if (name[0] == 'l')
9485 {
9486#ifndef FEAT_QUICKFIX
9487 ex_ni(eap);
9488 return;
9489#else
9490 cmd = DT_LTAG;
9491#endif
9492 }
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9495 eap->forceit, TRUE);
9496}
9497
9498/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009499 * Check "str" for starting with a special cmdline variable.
9500 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9501 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9502 */
9503 int
9504find_cmdline_var(src, usedlen)
9505 char_u *src;
9506 int *usedlen;
9507{
9508 int len;
9509 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009510 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009511 "%",
9512#define SPEC_PERC 0
9513 "#",
9514#define SPEC_HASH 1
9515 "<cword>", /* cursor word */
9516#define SPEC_CWORD 2
9517 "<cWORD>", /* cursor WORD */
9518#define SPEC_CCWORD 3
9519 "<cfile>", /* cursor path name */
9520#define SPEC_CFILE 4
9521 "<sfile>", /* ":so" file name */
9522#define SPEC_SFILE 5
9523#ifdef FEAT_AUTOCMD
9524 "<afile>", /* autocommand file name */
9525# define SPEC_AFILE 6
9526 "<abuf>", /* autocommand buffer number */
9527# define SPEC_ABUF 7
9528 "<amatch>", /* autocommand match name */
9529# define SPEC_AMATCH 8
9530#endif
9531#ifdef FEAT_CLIENTSERVER
9532 "<client>"
9533# define SPEC_CLIENT 9
9534#endif
9535 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009536
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009537 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009538 {
9539 len = (int)STRLEN(spec_str[i]);
9540 if (STRNCMP(src, spec_str[i], len) == 0)
9541 {
9542 *usedlen = len;
9543 return i;
9544 }
9545 }
9546 return -1;
9547}
9548
9549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 * Evaluate cmdline variables.
9551 *
9552 * change '%' to curbuf->b_ffname
9553 * '#' to curwin->w_altfile
9554 * '<cword>' to word under the cursor
9555 * '<cWORD>' to WORD under the cursor
9556 * '<cfile>' to path name under the cursor
9557 * '<sfile>' to sourced file name
9558 * '<afile>' to file name for autocommand
9559 * '<abuf>' to buffer number for autocommand
9560 * '<amatch>' to matching name for autocommand
9561 *
9562 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9563 * "" for error without a message) and NULL is returned.
9564 * Returns an allocated string if a valid match was found.
9565 * Returns NULL if no match was found. "usedlen" then still contains the
9566 * number of characters to skip.
9567 */
9568 char_u *
Bram Moolenaar63b92542007-03-27 14:57:09 +00009569eval_vars(src, srcstart, usedlen, lnump, errormsg, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 char_u *src; /* pointer into commandline */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009571 char_u *srcstart; /* beginning of valid memory for src */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 int *usedlen; /* characters after src that are used */
9573 linenr_T *lnump; /* line number for :e command, or NULL */
9574 char_u **errormsg; /* pointer to error message */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009575 int *escaped; /* return value has escaped white space (can
9576 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578 int i;
9579 char_u *s;
9580 char_u *result;
9581 char_u *resultbuf = NULL;
9582 int resultlen;
9583 buf_T *buf;
9584 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9585 int spec_idx;
9586#ifdef FEAT_MODIFY_FNAME
9587 int skip_mod = FALSE;
9588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589
9590#if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER)
9591 char_u strbuf[30];
9592#endif
9593
9594 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009595 if (escaped != NULL)
9596 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597
9598 /*
9599 * Check if there is something to do.
9600 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009601 spec_idx = find_cmdline_var(src, usedlen);
9602 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 {
9604 *usedlen = 1;
9605 return NULL;
9606 }
9607
9608 /*
9609 * Skip when preceded with a backslash "\%" and "\#".
9610 * Note: In "\\%" the % is also not recognized!
9611 */
9612 if (src > srcstart && src[-1] == '\\')
9613 {
9614 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009615 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 return NULL;
9617 }
9618
9619 /*
9620 * word or WORD under cursor
9621 */
9622 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD)
9623 {
9624 resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ?
9625 (FIND_IDENT|FIND_STRING) : FIND_STRING);
9626 if (resultlen == 0)
9627 {
9628 *errormsg = (char_u *)"";
9629 return NULL;
9630 }
9631 }
9632
9633 /*
9634 * '#': Alternate file name
9635 * '%': Current file name
9636 * File name under the cursor
9637 * File name for autocommand
9638 * and following modifiers
9639 */
9640 else
9641 {
9642 switch (spec_idx)
9643 {
9644 case SPEC_PERC: /* '%': current file */
9645 if (curbuf->b_fname == NULL)
9646 {
9647 result = (char_u *)"";
9648 valid = 0; /* Must have ":p:h" to be valid */
9649 }
9650 else
9651#ifdef RISCOS
9652 /* Always use the full path for RISC OS if possible. */
9653 result = curbuf->b_ffname;
9654 if (result == NULL)
9655 result = curbuf->b_fname;
9656#else
9657 result = curbuf->b_fname;
9658#endif
9659 break;
9660
9661 case SPEC_HASH: /* '#' or "#99": alternate file */
9662 if (src[1] == '#') /* "##": the argument list */
9663 {
9664 result = arg_all();
9665 resultbuf = result;
9666 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009667 if (escaped != NULL)
9668 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669#ifdef FEAT_MODIFY_FNAME
9670 skip_mod = TRUE;
9671#endif
9672 break;
9673 }
9674 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009675 if (*s == '<') /* "#<99" uses v:oldfiles */
9676 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 i = (int)getdigits(&s);
9678 *usedlen = (int)(s - src); /* length of what we expand */
9679
Bram Moolenaard812df62008-11-09 12:46:09 +00009680 if (src[1] == '<')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009682 if (*usedlen < 2)
9683 {
9684 /* Should we give an error message for #<text? */
9685 *usedlen = 1;
9686 return NULL;
9687 }
9688#ifdef FEAT_EVAL
9689 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9690 (long)i);
9691 if (result == NULL)
9692 {
9693 *errormsg = (char_u *)"";
9694 return NULL;
9695 }
9696#else
9697 *errormsg = (char_u *)_("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 }
9701 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009702 {
9703 buf = buflist_findnr(i);
9704 if (buf == NULL)
9705 {
9706 *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'");
9707 return NULL;
9708 }
9709 if (lnump != NULL)
9710 *lnump = ECMD_LAST;
9711 if (buf->b_fname == NULL)
9712 {
9713 result = (char_u *)"";
9714 valid = 0; /* Must have ":p:h" to be valid */
9715 }
9716 else
9717 result = buf->b_fname;
9718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 break;
9720
9721#ifdef FEAT_SEARCHPATH
9722 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009723 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 if (result == NULL)
9725 {
9726 *errormsg = (char_u *)"";
9727 return NULL;
9728 }
9729 resultbuf = result; /* remember allocated string */
9730 break;
9731#endif
9732
9733#ifdef FEAT_AUTOCMD
9734 case SPEC_AFILE: /* file name for autocommand */
9735 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009736 if (result != NULL && !autocmd_fname_full)
9737 {
9738 /* Still need to turn the fname into a full path. It is
9739 * postponed to avoid a delay when <afile> is not used. */
9740 autocmd_fname_full = TRUE;
9741 result = FullName_save(autocmd_fname, FALSE);
9742 vim_free(autocmd_fname);
9743 autocmd_fname = result;
9744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (result == NULL)
9746 {
9747 *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\"");
9748 return NULL;
9749 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009750 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751 break;
9752
9753 case SPEC_ABUF: /* buffer number for autocommand */
9754 if (autocmd_bufnr <= 0)
9755 {
9756 *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\"");
9757 return NULL;
9758 }
9759 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9760 result = strbuf;
9761 break;
9762
9763 case SPEC_AMATCH: /* match name for autocommand */
9764 result = autocmd_match;
9765 if (result == NULL)
9766 {
9767 *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\"");
9768 return NULL;
9769 }
9770 break;
9771
9772#endif
9773 case SPEC_SFILE: /* file name for ":so" command */
9774 result = sourcing_name;
9775 if (result == NULL)
9776 {
9777 *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\"");
9778 return NULL;
9779 }
9780 break;
9781#if defined(FEAT_CLIENTSERVER)
9782 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009783 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9784 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 result = strbuf;
9786 break;
9787#endif
9788 }
9789
9790 resultlen = (int)STRLEN(result); /* length of new string */
9791 if (src[*usedlen] == '<') /* remove the file name extension */
9792 {
9793 ++*usedlen;
9794#ifdef RISCOS
9795 if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result))
9796#else
9797 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
9798#endif
9799 resultlen = (int)(s - result);
9800 }
9801#ifdef FEAT_MODIFY_FNAME
9802 else if (!skip_mod)
9803 {
9804 valid |= modify_fname(src, usedlen, &result, &resultbuf,
9805 &resultlen);
9806 if (result == NULL)
9807 {
9808 *errormsg = (char_u *)"";
9809 return NULL;
9810 }
9811 }
9812#endif
9813 }
9814
9815 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9816 {
9817 if (valid != VALID_HEAD + VALID_PATH)
9818 /* xgettext:no-c-format */
9819 *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\"");
9820 else
9821 *errormsg = (char_u *)_("E500: Evaluates to an empty string");
9822 result = NULL;
9823 }
9824 else
9825 result = vim_strnsave(result, resultlen);
9826 vim_free(resultbuf);
9827 return result;
9828}
9829
9830/*
9831 * Concatenate all files in the argument list, separated by spaces, and return
9832 * it in one allocated string.
9833 * Spaces and backslashes in the file names are escaped with a backslash.
9834 * Returns NULL when out of memory.
9835 */
9836 static char_u *
9837arg_all()
9838{
9839 int len;
9840 int idx;
9841 char_u *retval = NULL;
9842 char_u *p;
9843
9844 /*
9845 * Do this loop two times:
9846 * first time: compute the total length
9847 * second time: concatenate the names
9848 */
9849 for (;;)
9850 {
9851 len = 0;
9852 for (idx = 0; idx < ARGCOUNT; ++idx)
9853 {
9854 p = alist_name(&ARGLIST[idx]);
9855 if (p != NULL)
9856 {
9857 if (len > 0)
9858 {
9859 /* insert a space in between names */
9860 if (retval != NULL)
9861 retval[len] = ' ';
9862 ++len;
9863 }
9864 for ( ; *p != NUL; ++p)
9865 {
9866 if (*p == ' ' || *p == '\\')
9867 {
9868 /* insert a backslash */
9869 if (retval != NULL)
9870 retval[len] = '\\';
9871 ++len;
9872 }
9873 if (retval != NULL)
9874 retval[len] = *p;
9875 ++len;
9876 }
9877 }
9878 }
9879
9880 /* second time: break here */
9881 if (retval != NULL)
9882 {
9883 retval[len] = NUL;
9884 break;
9885 }
9886
9887 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009888 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (retval == NULL)
9890 break;
9891 }
9892
9893 return retval;
9894}
9895
9896#if defined(FEAT_AUTOCMD) || defined(PROTO)
9897/*
9898 * Expand the <sfile> string in "arg".
9899 *
9900 * Returns an allocated string, or NULL for any error.
9901 */
9902 char_u *
9903expand_sfile(arg)
9904 char_u *arg;
9905{
9906 char_u *errormsg;
9907 int len;
9908 char_u *result;
9909 char_u *newres;
9910 char_u *repl;
9911 int srclen;
9912 char_u *p;
9913
9914 result = vim_strsave(arg);
9915 if (result == NULL)
9916 return NULL;
9917
9918 for (p = result; *p; )
9919 {
9920 if (STRNCMP(p, "<sfile>", 7) != 0)
9921 ++p;
9922 else
9923 {
9924 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009925 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 if (errormsg != NULL)
9927 {
9928 if (*errormsg)
9929 emsg(errormsg);
9930 vim_free(result);
9931 return NULL;
9932 }
9933 if (repl == NULL) /* no match (cannot happen) */
9934 {
9935 p += srclen;
9936 continue;
9937 }
9938 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9939 newres = alloc(len);
9940 if (newres == NULL)
9941 {
9942 vim_free(repl);
9943 vim_free(result);
9944 return NULL;
9945 }
9946 mch_memmove(newres, result, (size_t)(p - result));
9947 STRCPY(newres + (p - result), repl);
9948 len = (int)STRLEN(newres);
9949 STRCAT(newres, p + srclen);
9950 vim_free(repl);
9951 vim_free(result);
9952 result = newres;
9953 p = newres + len; /* continue after the match */
9954 }
9955 }
9956
9957 return result;
9958}
9959#endif
9960
9961#ifdef FEAT_SESSION
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009962static int ses_winsizes __ARGS((FILE *fd, int restore_size,
9963 win_T *tab_firstwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964static int ses_win_rec __ARGS((FILE *fd, frame_T *fr));
9965static frame_T *ses_skipframe __ARGS((frame_T *fr));
9966static int ses_do_frame __ARGS((frame_T *fr));
9967static int ses_do_win __ARGS((win_T *wp));
9968static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp));
9969static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp));
9970static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp));
9971
9972/*
9973 * Write openfile commands for the current buffers to an .exrc file.
9974 * Return FAIL on error, OK otherwise.
9975 */
9976 static int
9977makeopens(fd, dirnow)
9978 FILE *fd;
9979 char_u *dirnow; /* Current directory name */
9980{
9981 buf_T *buf;
9982 int only_save_windows = TRUE;
9983 int nr;
9984 int cnr = 1;
9985 int restore_size = TRUE;
9986 win_T *wp;
9987 char_u *sname;
9988 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009989 int tabnr;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009990 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009991 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009992 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009993 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994
9995 if (ssop_flags & SSOP_BUFFERS)
9996 only_save_windows = FALSE; /* Save ALL buffers */
9997
9998 /*
9999 * Begin by setting the this_session variable, and then other
10000 * sessionable variables.
10001 */
10002#ifdef FEAT_EVAL
10003 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
10004 return FAIL;
10005 if (ssop_flags & SSOP_GLOBALS)
10006 if (store_session_globals(fd) == FAIL)
10007 return FAIL;
10008#endif
10009
10010 /*
10011 * Close all windows but one.
10012 */
10013 if (put_line(fd, "silent only") == FAIL)
10014 return FAIL;
10015
10016 /*
10017 * Now a :cd command to the session directory or the current directory
10018 */
10019 if (ssop_flags & SSOP_SESDIR)
10020 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010021 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
10022 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 return FAIL;
10024 }
10025 else if (ssop_flags & SSOP_CURDIR)
10026 {
10027 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
10028 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010029 || fputs("cd ", fd) < 0
10030 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
10031 || put_eol(fd) == FAIL)
10032 {
10033 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 vim_free(sname);
10037 }
10038
10039 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010040 * If there is an empty, unnamed buffer we will wipe it out later.
10041 * Remember the buffer number.
10042 */
10043 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
10044 return FAIL;
10045 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
10046 return FAIL;
10047 if (put_line(fd, "endif") == FAIL)
10048 return FAIL;
10049
10050 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051 * Now save the current files, current buffer first.
10052 */
10053 if (put_line(fd, "set shortmess=aoO") == FAIL)
10054 return FAIL;
10055
10056 /* Now put the other buffers into the buffer list */
10057 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10058 {
10059 if (!(only_save_windows && buf->b_nwindows == 0)
10060 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10061 && buf->b_fname != NULL
10062 && buf->b_p_bl)
10063 {
10064 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10065 : buf->b_wininfo->wi_fpos.lnum) < 0
10066 || ses_fname(fd, buf, &ssop_flags) == FAIL)
10067 return FAIL;
10068 }
10069 }
10070
10071 /* the global argument list */
10072 if (ses_arglist(fd, "args", &global_alist.al_ga,
10073 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
10074 return FAIL;
10075
10076 if (ssop_flags & SSOP_RESIZE)
10077 {
10078 /* Note: after the restore we still check it worked!*/
10079 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
10080 || put_eol(fd) == FAIL)
10081 return FAIL;
10082 }
10083
10084#ifdef FEAT_GUI
10085 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
10086 {
10087 int x, y;
10088
10089 if (gui_mch_get_winpos(&x, &y) == OK)
10090 {
10091 /* Note: after the restore we still check it worked!*/
10092 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
10093 return FAIL;
10094 }
10095 }
10096#endif
10097
10098 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010099 * May repeat putting Windows for each tab, when "tabpages" is in
10100 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010101 * Don't use goto_tabpage(), it may change directory and trigger
10102 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010104 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010105 tab_topframe = topframe;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010106 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010108 int need_tabnew = FALSE;
10109
Bram Moolenaar18144c82006-04-12 21:52:12 +000010110 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010112 tabpage_T *tp = find_tabpage(tabnr);
10113
10114 if (tp == NULL)
10115 break; /* done all tab pages */
10116 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010117 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010118 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010119 tab_topframe = topframe;
10120 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010121 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010122 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010123 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010124 tab_topframe = tp->tp_topframe;
10125 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010126 if (tabnr > 1)
10127 need_tabnew = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129
Bram Moolenaar18144c82006-04-12 21:52:12 +000010130 /*
10131 * Before creating the window layout, try loading one file. If this
10132 * is aborted we don't end up with a number of useless windows.
10133 * This may have side effects! (e.g., compressed or network file).
10134 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010135 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010136 {
10137 if (ses_do_win(wp)
10138 && wp->w_buffer->b_ffname != NULL
10139 && !wp->w_buffer->b_help
10140#ifdef FEAT_QUICKFIX
10141 && !bt_nofile(wp->w_buffer)
10142#endif
10143 )
10144 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010145 if (fputs(need_tabnew ? "tabedit " : "edit ", fd) < 0
Bram Moolenaar18144c82006-04-12 21:52:12 +000010146 || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL)
10147 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010148 need_tabnew = FALSE;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010149 if (!wp->w_arg_idx_invalid)
10150 edited_win = wp;
10151 break;
10152 }
10153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010155 /* If no file got edited create an empty tab page. */
10156 if (need_tabnew && put_line(fd, "tabnew") == FAIL)
10157 return FAIL;
10158
Bram Moolenaar18144c82006-04-12 21:52:12 +000010159 /*
10160 * Save current window layout.
10161 */
10162 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +000010164 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010166 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
10167 return FAIL;
10168 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
10169 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170
Bram Moolenaar18144c82006-04-12 21:52:12 +000010171 /*
10172 * Check if window sizes can be restored (no windows omitted).
10173 * Remember the window number of the current window after restoring.
10174 */
10175 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010176 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010177 {
10178 if (ses_do_win(wp))
10179 ++nr;
10180 else
10181 restore_size = FALSE;
10182 if (curwin == wp)
10183 cnr = nr;
10184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185
Bram Moolenaar18144c82006-04-12 21:52:12 +000010186 /* Go to the first window. */
10187 if (put_line(fd, "wincmd t") == FAIL)
10188 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010189
Bram Moolenaar18144c82006-04-12 21:52:12 +000010190 /*
10191 * If more than one window, see if sizes can be restored.
10192 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
10193 * resized when moving between windows.
10194 * Do this before restoring the view, so that the topline and the
10195 * cursor can be set. This is done again below.
10196 */
10197 if (put_line(fd, "set winheight=1 winwidth=1") == FAIL)
10198 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010199 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010200 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201
Bram Moolenaar18144c82006-04-12 21:52:12 +000010202 /*
10203 * Restore the view of the window (options, file, cursor, etc.).
10204 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010205 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010206 {
10207 if (!ses_do_win(wp))
10208 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010209 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10210 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010211 return FAIL;
10212 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10213 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010214 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010215 }
10216
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010217 /* The argument index in the first tab page is zero, need to set it in
10218 * each window. For further tab pages it's the window where we do
10219 * "tabedit". */
10220 cur_arg_idx = next_arg_idx;
10221
Bram Moolenaar18144c82006-04-12 21:52:12 +000010222 /*
10223 * Restore cursor to the current window if it's not the first one.
10224 */
10225 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10226 || put_eol(fd) == FAIL))
10227 return FAIL;
10228
10229 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010230 * Restore window sizes again after jumping around in windows, because
10231 * the current window has a minimum size while others may not.
10232 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010233 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010234 return FAIL;
10235
Bram Moolenaar18144c82006-04-12 21:52:12 +000010236 /* Don't continue in another tab page when doing only the current one
10237 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010238 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010239 break;
10240 }
10241
10242 if (ssop_flags & SSOP_TABPAGES)
10243 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010244 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10245 || put_eol(fd) == FAIL)
10246 return FAIL;
10247 }
10248
Bram Moolenaar9c102382006-05-03 21:26:49 +000010249 /*
10250 * Wipe out an empty unnamed buffer we started in.
10251 */
10252 if (put_line(fd, "if exists('s:wipebuf')") == FAIL)
10253 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010254 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010255 return FAIL;
10256 if (put_line(fd, "endif") == FAIL)
10257 return FAIL;
10258 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10259 return FAIL;
10260
10261 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10262 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10263 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265
10266 /*
10267 * Lastly, execute the x.vim file if it exists.
10268 */
10269 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10270 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010271 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010272 || put_line(fd, "endif") == FAIL)
10273 return FAIL;
10274
10275 return OK;
10276}
10277
10278 static int
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010279ses_winsizes(fd, restore_size, tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 FILE *fd;
10281 int restore_size;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010282 win_T *tab_firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283{
10284 int n = 0;
10285 win_T *wp;
10286
10287 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10288 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010289 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 {
10291 if (!ses_do_win(wp))
10292 continue;
10293 ++n;
10294
10295 /* restore height when not full height */
10296 if (wp->w_height + wp->w_status_height < topframe->fr_height
10297 && (fprintf(fd,
10298 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10299 n, (long)wp->w_height, Rows / 2, Rows) < 0
10300 || put_eol(fd) == FAIL))
10301 return FAIL;
10302
10303 /* restore width when not full width */
10304 if (wp->w_width < Columns && (fprintf(fd,
10305 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10306 n, (long)wp->w_width, Columns / 2, Columns) < 0
10307 || put_eol(fd) == FAIL))
10308 return FAIL;
10309 }
10310 }
10311 else
10312 {
10313 /* Just equalise window sizes */
10314 if (put_line(fd, "wincmd =") == FAIL)
10315 return FAIL;
10316 }
10317 return OK;
10318}
10319
10320/*
10321 * Write commands to "fd" to recursively create windows for frame "fr",
10322 * horizontally and vertically split.
10323 * After the commands the last window in the frame is the current window.
10324 * Returns FAIL when writing the commands to "fd" fails.
10325 */
10326 static int
10327ses_win_rec(fd, fr)
10328 FILE *fd;
10329 frame_T *fr;
10330{
10331 frame_T *frc;
10332 int count = 0;
10333
10334 if (fr->fr_layout != FR_LEAF)
10335 {
10336 /* Find first frame that's not skipped and then create a window for
10337 * each following one (first frame is already there). */
10338 frc = ses_skipframe(fr->fr_child);
10339 if (frc != NULL)
10340 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10341 {
10342 /* Make window as big as possible so that we have lots of room
10343 * to split. */
10344 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10345 || put_line(fd, fr->fr_layout == FR_COL
10346 ? "split" : "vsplit") == FAIL)
10347 return FAIL;
10348 ++count;
10349 }
10350
10351 /* Go back to the first window. */
10352 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10353 ? "%dwincmd k" : "%dwincmd h", count) < 0
10354 || put_eol(fd) == FAIL))
10355 return FAIL;
10356
10357 /* Recursively create frames/windows in each window of this column or
10358 * row. */
10359 frc = ses_skipframe(fr->fr_child);
10360 while (frc != NULL)
10361 {
10362 ses_win_rec(fd, frc);
10363 frc = ses_skipframe(frc->fr_next);
10364 /* Go to next window. */
10365 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10366 return FAIL;
10367 }
10368 }
10369 return OK;
10370}
10371
10372/*
10373 * Skip frames that don't contain windows we want to save in the Session.
10374 * Returns NULL when there none.
10375 */
10376 static frame_T *
10377ses_skipframe(fr)
10378 frame_T *fr;
10379{
10380 frame_T *frc;
10381
10382 for (frc = fr; frc != NULL; frc = frc->fr_next)
10383 if (ses_do_frame(frc))
10384 break;
10385 return frc;
10386}
10387
10388/*
10389 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10390 * the Session.
10391 */
10392 static int
10393ses_do_frame(fr)
10394 frame_T *fr;
10395{
10396 frame_T *frc;
10397
10398 if (fr->fr_layout == FR_LEAF)
10399 return ses_do_win(fr->fr_win);
10400 for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next)
10401 if (ses_do_frame(frc))
10402 return TRUE;
10403 return FALSE;
10404}
10405
10406/*
10407 * Return non-zero if window "wp" is to be stored in the Session.
10408 */
10409 static int
10410ses_do_win(wp)
10411 win_T *wp;
10412{
10413 if (wp->w_buffer->b_fname == NULL
10414#ifdef FEAT_QUICKFIX
10415 /* When 'buftype' is "nofile" can't restore the window contents. */
10416 || bt_nofile(wp->w_buffer)
10417#endif
10418 )
10419 return (ssop_flags & SSOP_BLANK);
10420 if (wp->w_buffer->b_help)
10421 return (ssop_flags & SSOP_HELP);
10422 return TRUE;
10423}
10424
10425/*
10426 * Write commands to "fd" to restore the view of a window.
10427 * Caller must make sure 'scrolloff' is zero.
10428 */
10429 static int
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010430put_view(fd, wp, add_edit, flagp, current_arg_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 FILE *fd;
10432 win_T *wp;
10433 int add_edit; /* add ":edit" command to view */
10434 unsigned *flagp; /* vop_flags or ssop_flags */
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010435 int current_arg_idx; /* current argument index of the window, use
10436 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
10438 win_T *save_curwin;
10439 int f;
10440 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010441 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
10443 /* Always restore cursor position for ":mksession". For ":mkview" only
10444 * when 'viewoptions' contains "cursor". */
10445 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10446
10447 /*
10448 * Local argument list.
10449 */
10450 if (wp->w_alist == &global_alist)
10451 {
10452 if (put_line(fd, "argglobal") == FAIL)
10453 return FAIL;
10454 }
10455 else
10456 {
10457 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10458 flagp == &vop_flags
10459 || !(*flagp & SSOP_CURDIR)
10460 || wp->w_localdir != NULL, flagp) == FAIL)
10461 return FAIL;
10462 }
10463
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010464 /* Only when part of a session: restore the argument index. Some
10465 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010466 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010467 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010469 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 || put_eol(fd) == FAIL)
10471 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010472 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 }
10474
10475 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010476 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 {
10478 /*
10479 * Load the file.
10480 */
10481 if (wp->w_buffer->b_ffname != NULL
10482#ifdef FEAT_QUICKFIX
10483 && !bt_nofile(wp->w_buffer)
10484#endif
10485 )
10486 {
10487 /*
10488 * Editing a file in this buffer: use ":edit file".
10489 * This may have side effects! (e.g., compressed or network file).
10490 */
10491 if (fputs("edit ", fd) < 0
10492 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10493 return FAIL;
10494 }
10495 else
10496 {
10497 /* No file in this buffer, just make it empty. */
10498 if (put_line(fd, "enew") == FAIL)
10499 return FAIL;
10500#ifdef FEAT_QUICKFIX
10501 if (wp->w_buffer->b_ffname != NULL)
10502 {
10503 /* The buffer does have a name, but it's not a file name. */
10504 if (fputs("file ", fd) < 0
10505 || ses_fname(fd, wp->w_buffer, flagp) == FAIL)
10506 return FAIL;
10507 }
10508#endif
10509 do_cursor = FALSE;
10510 }
10511 }
10512
10513 /*
10514 * Local mappings and abbreviations.
10515 */
10516 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10517 && makemap(fd, wp->w_buffer) == FAIL)
10518 return FAIL;
10519
10520 /*
10521 * Local options. Need to go to the window temporarily.
10522 * Store only local values when using ":mkview" and when ":mksession" is
10523 * used and 'sessionoptions' doesn't include "options".
10524 * Some folding options are always stored when "folds" is included,
10525 * otherwise the folds would not be restored correctly.
10526 */
10527 save_curwin = curwin;
10528 curwin = wp;
10529 curbuf = curwin->w_buffer;
10530 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10531 f = makeset(fd, OPT_LOCAL,
10532 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10533#ifdef FEAT_FOLDING
10534 else if (*flagp & SSOP_FOLDS)
10535 f = makefoldset(fd);
10536#endif
10537 else
10538 f = OK;
10539 curwin = save_curwin;
10540 curbuf = curwin->w_buffer;
10541 if (f == FAIL)
10542 return FAIL;
10543
10544#ifdef FEAT_FOLDING
10545 /*
10546 * Save Folds when 'buftype' is empty and for help files.
10547 */
10548 if ((*flagp & SSOP_FOLDS)
10549 && wp->w_buffer->b_ffname != NULL
Bram Moolenaarb1b715d2006-01-21 22:09:43 +000010550# ifdef FEAT_QUICKFIX
10551 && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help)
10552# endif
10553 )
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 {
10555 if (put_folds(fd, wp) == FAIL)
10556 return FAIL;
10557 }
10558#endif
10559
10560 /*
10561 * Set the cursor after creating folds, since that moves the cursor.
10562 */
10563 if (do_cursor)
10564 {
10565
10566 /* Restore the cursor line in the file and relatively in the
10567 * window. Don't use "G", it changes the jumplist. */
10568 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10569 (long)wp->w_cursor.lnum,
10570 (long)(wp->w_cursor.lnum - wp->w_topline),
10571 (long)wp->w_height / 2, (long)wp->w_height) < 0
10572 || put_eol(fd) == FAIL
10573 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10574 || put_line(fd, "exe s:l") == FAIL
10575 || put_line(fd, "normal! zt") == FAIL
10576 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10577 || put_eol(fd) == FAIL)
10578 return FAIL;
10579 /* Restore the cursor column and left offset when not wrapping. */
10580 if (wp->w_cursor.col == 0)
10581 {
10582 if (put_line(fd, "normal! 0") == FAIL)
10583 return FAIL;
10584 }
10585 else
10586 {
10587 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10588 {
10589 if (fprintf(fd,
10590 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
10591 (long)wp->w_cursor.col,
10592 (long)(wp->w_cursor.col - wp->w_leftcol),
10593 (long)wp->w_width / 2, (long)wp->w_width) < 0
10594 || put_eol(fd) == FAIL
10595 || put_line(fd, "if s:c > 0") == FAIL
10596 || fprintf(fd,
10597 " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'",
10598 (long)wp->w_cursor.col) < 0
10599 || put_eol(fd) == FAIL
10600 || put_line(fd, "else") == FAIL
10601 || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0
10602 || put_eol(fd) == FAIL
10603 || put_line(fd, "endif") == FAIL)
10604 return FAIL;
10605 }
10606 else
10607 {
10608 if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0
10609 || put_eol(fd) == FAIL)
10610 return FAIL;
10611 }
10612 }
10613 }
10614
10615 /*
10616 * Local directory.
10617 */
10618 if (wp->w_localdir != NULL)
10619 {
10620 if (fputs("lcd ", fd) < 0
10621 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10622 || put_eol(fd) == FAIL)
10623 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010624 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 }
10626
10627 return OK;
10628}
10629
10630/*
10631 * Write an argument list to the session file.
10632 * Returns FAIL if writing fails.
10633 */
10634 static int
10635ses_arglist(fd, cmd, gap, fullname, flagp)
10636 FILE *fd;
10637 char *cmd;
10638 garray_T *gap;
10639 int fullname; /* TRUE: use full path name */
10640 unsigned *flagp;
10641{
10642 int i;
10643 char_u buf[MAXPATHL];
10644 char_u *s;
10645
10646 if (gap->ga_len == 0)
10647 return put_line(fd, "silent! argdel *");
10648 if (fputs(cmd, fd) < 0)
10649 return FAIL;
10650 for (i = 0; i < gap->ga_len; ++i)
10651 {
10652 /* NULL file names are skipped (only happens when out of memory). */
10653 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10654 if (s != NULL)
10655 {
10656 if (fullname)
10657 {
10658 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10659 s = buf;
10660 }
10661 if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL)
10662 return FAIL;
10663 }
10664 }
10665 return put_eol(fd);
10666}
10667
10668/*
10669 * Write a buffer name to the session file.
10670 * Also ends the line.
10671 * Returns FAIL if writing fails.
10672 */
10673 static int
10674ses_fname(fd, buf, flagp)
10675 FILE *fd;
10676 buf_T *buf;
10677 unsigned *flagp;
10678{
10679 char_u *name;
10680
10681 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010682 * the session file will be sourced.
10683 * Don't do this for ":mkview", we don't know the current directory.
10684 * Don't do this after ":lcd", we don't keep track of what the current
10685 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 if (buf->b_sfname != NULL
10687 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010688 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010689#ifdef FEAT_AUTOCHDIR
10690 && !p_acd
10691#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010692 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 name = buf->b_sfname;
10694 else
10695 name = buf->b_ffname;
10696 if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL)
10697 return FAIL;
10698 return OK;
10699}
10700
10701/*
10702 * Write a file name to the session file.
10703 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10704 * characters.
10705 * Returns FAIL if writing fails.
10706 */
10707 static int
10708ses_put_fname(fd, name, flagp)
10709 FILE *fd;
10710 char_u *name;
10711 unsigned *flagp;
10712{
10713 char_u *sname;
10714 int retval = OK;
10715 int c;
10716
10717 sname = home_replace_save(NULL, name);
10718 if (sname != NULL)
10719 name = sname;
10720 while (*name != NUL)
10721 {
10722#ifdef FEAT_MBYTE
10723 {
10724 int l;
10725
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010726 if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 {
10728 /* copy a multibyte char */
10729 while (--l >= 0)
10730 {
10731 if (putc(*name, fd) != *name)
10732 retval = FAIL;
10733 ++name;
10734 }
10735 continue;
10736 }
10737 }
10738#endif
10739 c = *name++;
10740 if (c == '\\' && (*flagp & SSOP_SLASH))
10741 /* change a backslash to a forward slash */
10742 c = '/';
10743 else if ((vim_strchr(escape_chars, c) != NULL
10744#ifdef BACKSLASH_IN_FILENAME
10745 && c != '\\'
10746#endif
10747 ) || c == '#' || c == '%')
10748 {
10749 /* escape a special character with a backslash */
10750 if (putc('\\', fd) != '\\')
10751 retval = FAIL;
10752 }
10753 if (putc(c, fd) != c)
10754 retval = FAIL;
10755 }
10756 vim_free(sname);
10757 return retval;
10758}
10759
10760/*
10761 * ":loadview [nr]"
10762 */
10763 static void
10764ex_loadview(eap)
10765 exarg_T *eap;
10766{
10767 char_u *fname;
10768
10769 fname = get_view_file(*eap->arg);
10770 if (fname != NULL)
10771 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010772 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773 vim_free(fname);
10774 }
10775}
10776
10777/*
10778 * Get the name of the view file for the current buffer.
10779 */
10780 static char_u *
10781get_view_file(c)
10782 int c;
10783{
10784 int len = 0;
10785 char_u *p, *s;
10786 char_u *retval;
10787 char_u *sname;
10788
10789 if (curbuf->b_ffname == NULL)
10790 {
10791 EMSG(_(e_noname));
10792 return NULL;
10793 }
10794 sname = home_replace_save(NULL, curbuf->b_ffname);
10795 if (sname == NULL)
10796 return NULL;
10797
10798 /*
10799 * We want a file name without separators, because we're not going to make
10800 * a directory.
10801 * "normal" path separator -> "=+"
10802 * "=" -> "=="
10803 * ":" path separator -> "=-"
10804 */
10805 for (p = sname; *p; ++p)
10806 if (*p == '=' || vim_ispathsep(*p))
10807 ++len;
10808 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10809 if (retval != NULL)
10810 {
10811 STRCPY(retval, p_vdir);
10812 add_pathsep(retval);
10813 s = retval + STRLEN(retval);
10814 for (p = sname; *p; ++p)
10815 {
10816 if (*p == '=')
10817 {
10818 *s++ = '=';
10819 *s++ = '=';
10820 }
10821 else if (vim_ispathsep(*p))
10822 {
10823 *s++ = '=';
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010824#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 || defined(VMS)
10826 if (*p == ':')
10827 *s++ = '-';
10828 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010830 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831 }
10832 else
10833 *s++ = *p;
10834 }
10835 *s++ = '=';
10836 *s++ = c;
10837 STRCPY(s, ".vim");
10838 }
10839
10840 vim_free(sname);
10841 return retval;
10842}
10843
10844#endif /* FEAT_SESSION */
10845
10846/*
10847 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10848 * Return FAIL for a write error.
10849 */
10850 int
10851put_eol(fd)
10852 FILE *fd;
10853{
10854 if (
10855#ifdef USE_CRNL
10856 (
10857# ifdef MKSESSION_NL
10858 !mksession_nl &&
10859# endif
10860 (putc('\r', fd) < 0)) ||
10861#endif
10862 (putc('\n', fd) < 0))
10863 return FAIL;
10864 return OK;
10865}
10866
10867/*
10868 * Write a line to "fd".
10869 * Return FAIL for a write error.
10870 */
10871 int
10872put_line(fd, s)
10873 FILE *fd;
10874 char *s;
10875{
10876 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10877 return FAIL;
10878 return OK;
10879}
10880
10881#ifdef FEAT_VIMINFO
10882/*
10883 * ":rviminfo" and ":wviminfo".
10884 */
10885 static void
10886ex_viminfo(eap)
10887 exarg_T *eap;
10888{
10889 char_u *save_viminfo;
10890
10891 save_viminfo = p_viminfo;
10892 if (*p_viminfo == NUL)
10893 p_viminfo = (char_u *)"'100";
10894 if (eap->cmdidx == CMD_rviminfo)
10895 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010896 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10897 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 EMSG(_("E195: Cannot open viminfo file for reading"));
10899 }
10900 else
10901 write_viminfo(eap->arg, eap->forceit);
10902 p_viminfo = save_viminfo;
10903}
10904#endif
10905
10906#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010907/*
10908 * Make a dialog message in "buff[IOSIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010909 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 void
10912dialog_msg(buff, format, fname)
10913 char_u *buff;
10914 char *format;
10915 char_u *fname;
10916{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 if (fname == NULL)
10918 fname = (char_u *)_("Untitled");
Bram Moolenaarb765d632005-06-07 21:00:02 +000010919 vim_snprintf((char *)buff, IOSIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010920}
10921#endif
10922
10923/*
10924 * ":behave {mswin,xterm}"
10925 */
10926 static void
10927ex_behave(eap)
10928 exarg_T *eap;
10929{
10930 if (STRCMP(eap->arg, "mswin") == 0)
10931 {
10932 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10933 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10934 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10935 set_option_value((char_u *)"keymodel", 0L,
10936 (char_u *)"startsel,stopsel", 0);
10937 }
10938 else if (STRCMP(eap->arg, "xterm") == 0)
10939 {
10940 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10941 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10942 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10943 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10944 }
10945 else
10946 EMSG2(_(e_invarg2), eap->arg);
10947}
10948
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010949#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10950/*
10951 * Function given to ExpandGeneric() to obtain the possible arguments of the
10952 * ":behave {mswin,xterm}" command.
10953 */
10954 char_u *
10955get_behave_arg(xp, idx)
10956 expand_T *xp UNUSED;
10957 int idx;
10958{
10959 if (idx == 0)
10960 return (char_u *)"mswin";
10961 if (idx == 1)
10962 return (char_u *)"xterm";
10963 return NULL;
10964}
10965#endif
10966
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967#ifdef FEAT_AUTOCMD
10968static int filetype_detect = FALSE;
10969static int filetype_plugin = FALSE;
10970static int filetype_indent = FALSE;
10971
10972/*
10973 * ":filetype [plugin] [indent] {on,off,detect}"
10974 * on: Load the filetype.vim file to install autocommands for file types.
10975 * off: Load the ftoff.vim file to remove all autocommands for file types.
10976 * plugin on: load filetype.vim and ftplugin.vim
10977 * plugin off: load ftplugof.vim
10978 * indent on: load filetype.vim and indent.vim
10979 * indent off: load indoff.vim
10980 */
10981 static void
10982ex_filetype(eap)
10983 exarg_T *eap;
10984{
10985 char_u *arg = eap->arg;
10986 int plugin = FALSE;
10987 int indent = FALSE;
10988
10989 if (*eap->arg == NUL)
10990 {
10991 /* Print current status. */
10992 smsg((char_u *)"filetype detection:%s plugin:%s indent:%s",
10993 filetype_detect ? "ON" : "OFF",
10994 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10995 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10996 return;
10997 }
10998
10999 /* Accept "plugin" and "indent" in any order. */
11000 for (;;)
11001 {
11002 if (STRNCMP(arg, "plugin", 6) == 0)
11003 {
11004 plugin = TRUE;
11005 arg = skipwhite(arg + 6);
11006 continue;
11007 }
11008 if (STRNCMP(arg, "indent", 6) == 0)
11009 {
11010 indent = TRUE;
11011 arg = skipwhite(arg + 6);
11012 continue;
11013 }
11014 break;
11015 }
11016 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
11017 {
11018 if (*arg == 'o' || !filetype_detect)
11019 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011020 source_runtime((char_u *)FILETYPE_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 filetype_detect = TRUE;
11022 if (plugin)
11023 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011024 source_runtime((char_u *)FTPLUGIN_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025 filetype_plugin = TRUE;
11026 }
11027 if (indent)
11028 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011029 source_runtime((char_u *)INDENT_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011030 filetype_indent = TRUE;
11031 }
11032 }
11033 if (*arg == 'd')
11034 {
11035 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE);
Bram Moolenaara3227e22006-03-08 21:32:40 +000011036 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 }
11038 }
11039 else if (STRCMP(arg, "off") == 0)
11040 {
11041 if (plugin || indent)
11042 {
11043 if (plugin)
11044 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011045 source_runtime((char_u *)FTPLUGOF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 filetype_plugin = FALSE;
11047 }
11048 if (indent)
11049 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011050 source_runtime((char_u *)INDOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051 filetype_indent = FALSE;
11052 }
11053 }
11054 else
11055 {
Bram Moolenaare5b8e3d2005-08-12 19:48:49 +000011056 source_runtime((char_u *)FTOFF_FILE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 filetype_detect = FALSE;
11058 }
11059 }
11060 else
11061 EMSG2(_(e_invarg2), arg);
11062}
11063
11064/*
11065 * ":setfiletype {name}"
11066 */
11067 static void
11068ex_setfiletype(eap)
11069 exarg_T *eap;
11070{
11071 if (!did_filetype)
11072 set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL);
11073}
11074#endif
11075
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 static void
11077ex_digraphs(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011078 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079{
11080#ifdef FEAT_DIGRAPHS
11081 if (*eap->arg != NUL)
11082 putdigraph(eap->arg);
11083 else
11084 listdigraphs();
11085#else
11086 EMSG(_("E196: No digraphs in this version"));
11087#endif
11088}
11089
11090 static void
11091ex_set(eap)
11092 exarg_T *eap;
11093{
11094 int flags = 0;
11095
11096 if (eap->cmdidx == CMD_setlocal)
11097 flags = OPT_LOCAL;
11098 else if (eap->cmdidx == CMD_setglobal)
11099 flags = OPT_GLOBAL;
11100#if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE)
11101 if (cmdmod.browse && flags == 0)
11102 ex_options(eap);
11103 else
11104#endif
11105 (void)do_set(eap->arg, flags);
11106}
11107
11108#ifdef FEAT_SEARCH_EXTRA
11109/*
11110 * ":nohlsearch"
11111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112 static void
11113ex_nohlsearch(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011114 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115{
11116 no_hlsearch = TRUE;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000011117 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011118}
11119
11120/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011121 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122 * Sets nextcmd to the start of the next command, if any. Also called when
11123 * skipping commands to find the next command.
11124 */
11125 static void
11126ex_match(eap)
11127 exarg_T *eap;
11128{
11129 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000011130 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 char_u *end;
11132 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011133 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011134
11135 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011136 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000011137 else
11138 {
11139 EMSG(e_invcmd);
11140 return;
11141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
11143 /* First clear any old pattern. */
11144 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011145 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146
11147 if (ends_excmd(*eap->arg))
11148 end = eap->arg;
11149 else if ((STRNICMP(eap->arg, "none", 4) == 0
11150 && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4]))))
11151 end = eap->arg + 4;
11152 else
11153 {
11154 p = skiptowhite(eap->arg);
11155 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011156 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 p = skipwhite(p);
11158 if (*p == NUL)
11159 {
11160 /* There must be two arguments. */
11161 EMSG2(_(e_invarg2), eap->arg);
11162 return;
11163 }
11164 end = skip_regexp(p + 1, *p, TRUE, NULL);
11165 if (!eap->skip)
11166 {
11167 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11168 {
11169 eap->errmsg = e_trailing;
11170 return;
11171 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011172 if (*end != *p)
11173 {
11174 EMSG2(_(e_invarg2), p);
11175 return;
11176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177
11178 c = *end;
11179 *end = NUL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011180 match_add(curwin, g, p + 1, 10, id);
11181 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011182 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 }
11184 }
11185 eap->nextcmd = find_nextcmd(end);
11186}
11187#endif
11188
11189#ifdef FEAT_CRYPT
11190/*
11191 * ":X": Get crypt key
11192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 static void
11194ex_X(eap)
Bram Moolenaar78a15312009-05-15 19:33:18 +000011195 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196{
Bram Moolenaar49771f42010-07-20 17:32:38 +020011197 if (get_crypt_method(curbuf) == 0 || blowfish_self_test() == OK)
Bram Moolenaar40e6a712010-05-16 22:32:54 +020011198 (void)get_crypt_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199}
11200#endif
11201
11202#ifdef FEAT_FOLDING
11203 static void
11204ex_fold(eap)
11205 exarg_T *eap;
11206{
11207 if (foldManualAllowed(TRUE))
11208 foldCreate(eap->line1, eap->line2);
11209}
11210
11211 static void
11212ex_foldopen(eap)
11213 exarg_T *eap;
11214{
11215 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11216 eap->forceit, FALSE);
11217}
11218
11219 static void
11220ex_folddo(eap)
11221 exarg_T *eap;
11222{
11223 linenr_T lnum;
11224
11225 /* First set the marks for all lines closed/open. */
11226 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11227 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11228 ml_setmarked(lnum);
11229
11230 /* Execute the command on the marked lines. */
11231 global_exe(eap->arg);
11232 ml_clearmarked(); /* clear rest of the marks */
11233}
11234#endif